Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permit passing config object via env vars #461

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

alukach
Copy link

@alukach alukach commented Aug 7, 2024

What I'm changing

If a config value is an object, the tooling to convert it from an environment var to JS will keep it as a string (see #460).

This PR will instead retain its state as a JSON object.

This will allow the following configurations to be correctly set via environment variables:

▶ cat config.schema.json | jq '.properties | to_entries | map(select(.value.type[0] == "object")) | map(.key)'
[
  "requestHeaders",
  "requestQueryParameters",
  "authConfig"
]

How I did it

The inability to pass in objects via ENV vars was caused by the fact that when we parse the SB_* environment values based on their type as specified in the config.schema.json; however, we don't specifically handle the object type so it is run through the catch-all safe_echo code:

"authConfig": {
"type": [
"object"
],
"allOf": [
{
"$ref": "https://stac-extensions.github.io/authentication/v1.1.0/schema.json"
}
],
"noCLI": true,
"noEnv": true
}

env -0 | cut -f1 -d= | tr '\0' '\n' | grep "^SB_" | {
echo "window.STAC_BROWSER_CONFIG = {"
while IFS='=' read -r name; do
# Strip the prefix
argname="${name#SB_}"
# Read the variable's value
value="$(eval "echo \"\$$name\"")"
# Get the argument type from the schema
argtype="$(echo "$config_schema" | jq -r ".properties.$argname.type[0]")"
arraytype="$(echo "$config_schema" | jq -r ".properties.$argname.items.type[0]")"
# Encode key/value
echo -n " $argname: "
case "$argtype" in
string)
safe_echo "$value"
;;
boolean)
bool "$value"
;;
integer | number)
object "$value"
;;
array)
array "$value" "$arraytype"
;;
*)
safe_echo "$value"
;;
esac
echo ","
done
echo "}"
} > /usr/share/nginx/html/config.js

This keeps it as a string:

# echo a string, handling different types
safe_echo() {

However, if we permit configurations of type "object" to encoded directly into the JSON via the object() function in the bash script, we can use them as standard configuration.

How you can test this

Before

On the main branch:

  1. Build docker image: docker build -t stac-browser
  2. Run docker image with auth config set as environment variable: docker run -it -e SB_authConfig='{"foo": "bar"}' --name stac-browser stac-browser
  3. In another terminal, review the generated config: docker exec stac-browser cat /usr/share/nginx/html/config.js

You should see an object where the authConfig property is a string rather than an object:

window.STAC_BROWSER_CONFIG = {
  authConfig: '{"foo": "bar"}',
}

After

Following the above steps on this branch, you should see now see an object where the authConfig property is an object:

window.STAC_BROWSER_CONFIG = {
  authConfig: {"foo": "bar"},
}

closes #460

Copy link
Collaborator

@m-mohr m-mohr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the suggestion.
Indeed, it is not supported to pass objects via ENV or CLI and it is reflected as such in the docs and config.schema.js.
If this works reliably, this should be reflected in the docs and in the config.schema.js.

@alukach
Copy link
Author

alukach commented Aug 9, 2024

@m-mohr Ah, I had missed those details. Thanks for pointing out that this is documented and intended behavior. In 408ed94 and 88212c8 I've removed such documentation in hopes that we can indeed adjust this beavior.

@alukach
Copy link
Author

alukach commented Aug 9, 2024

(Seeing that this PR is not a trivial one-liner as I had initially hoped, I've updated the description in hopes of adding more clarity to what is being changed and why)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot pass in authConfig via environment variable
2 participants