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

envsubst: remove explicit lists of vars #818

Closed
wants to merge 1 commit into from

Conversation

alxndrsn
Copy link
Contributor

@alxndrsn alxndrsn commented Dec 6, 2024

For nginx config, a new approach is implemented with perl. This is because unrestricted use of envsubst on nginx config files will replace nginx variables like $host, $request_uri with an empty string.

Closes #473

What has been done to verify that this works as intended?

CI.

Why is this the best possible solution? Were any other approaches considered?

Discussed on #473.

How does this change affect users? Describe intentional changes to behavior and behavior that could have accidentally been affected by code changes. In other words, what are the regression risks?

No expected effect.

Does this change require updates to documentation? If so, please file an issue here and include the link below.

No.

Before submitting this PR, please make sure you have:

  • branched off and targeted the next branch OR only changed documentation/infrastructure (master is stable and used in production)
  • verified that any code or assets from external sources are properly credited in comments or that everything is internally sourced

For nginx config, a new approach is implemented with perl.  This is because unrestricted use of `envsubst` on nginx config files will replace nginx variables like $host, $request_uri with an empty string.

Closes getodk#473
@alxndrsn alxndrsn requested a review from sadiqkhoja December 6, 2024 12:03
@brontolosone brontolosone self-requested a review December 6, 2024 17:59
Copy link
Contributor

@brontolosone brontolosone left a comment

Choose a reason for hiding this comment

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

🤔

Comment on lines +3 to +8
nginx_envsubst() {
# Re-implementation of envsubst which is safe to call on nginx config files.
# This fn only substitutes variables in the form ${CAPS_AND_UNDERSCORES},
# allowing nginx variables like $host, $request_uri etc. through unmodified.
perl -pe 's/\$\{([A-Z_]*)\}/$ENV{$1}/g'
}
Copy link
Contributor

Choose a reason for hiding this comment

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

So, this silently replaces with a 0-length string any variable in the template which does not exist in the environment. Envsubst does that too, so that's the original sin: it makes for hard(er) to debug problems. Depending on the config language and construct it might well make for a syntactically valid config file with an undesirable effect at runtime, not even at initalization time, of the program using that configfile.

See

$ echo -e 'hello\n${I_WILL_BE_SILENTLY_DISCARDED}\n${SHELL}' | perl -pe 's/\$\{([A-Z_]*)\}/$ENV{$1}/g'
hello

/bin/zsh

I think it would be better if it would simply bomb out if the key is not in the env (in Python os.environ['blabla'] would raise a KeyError). At least then it'd be very obvious that something is missing, and what that thing is, right at the moment when it is generated, rather than that something mysteriously goes wrong at runtime.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think handling missing vars would be a much better aim than trying to remove the explicit list of vars 👍

@alxndrsn
Copy link
Contributor Author

alxndrsn commented Dec 9, 2024

Closing in favour of #830. As in this PR, envsubst use is simplified, but #830 handles variables more safely.

@alxndrsn alxndrsn closed this Dec 9, 2024
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.

2 participants