radiantearth / stac-browser

A full-fledged UI in Vue for browsing and searching static STAC catalogs and STAC APIs
https://radiantearth.github.io/stac-browser
ISC License
286 stars 143 forks source link

Permit passing config object via env vars #461

Open alukach opened 3 months ago

alukach commented 3 months ago

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:

https://github.com/radiantearth/stac-browser/blob/108947ebe8cbd64c7e288bbaa3c5f81293706d3e/config.schema.json#L225-L236

https://github.com/radiantearth/stac-browser/blob/108947ebe8cbd64c7e288bbaa3c5f81293706d3e/docker/docker-entrypoint.sh#L58-L92

This keeps it as a string: https://github.com/radiantearth/stac-browser/blob/108947ebe8cbd64c7e288bbaa3c5f81293706d3e/docker/docker-entrypoint.sh#L1-L2

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

alukach commented 3 months ago

@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 commented 3 months ago

(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)