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

Fix configuration with env vars #280

Closed
wants to merge 1 commit into from

Conversation

hypnoglow
Copy link
Contributor

Related issue

Proposed changes

Currently env vars do not work for any configuration property. One reason is because BindEnv is never called. The other thing that actually the usage of BindEnv is incorrect: this function DOES NOT accept multiple keys (see source for details). Even if BindEnv would be used correctly, it will not resolve env vars like SERVE_PROXY_PORT because it does not replaces dots with underscores.

This MR provides the same way of loading env vars as in hydra.

Checklist

  • I have read the contributing guidelines
  • I have read the security policy
  • I confirm that this pull request does not address a security
    vulnerability. If this pull request addresses a security vulnerability, I
    confirm that I got green light (please contact
    security@ory.sh) from the maintainers to push
    the changes.
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation within the code base (if appropriate)
  • I have documented my changes in the
    developer guide (if appropriate)

driver/configuration/provider_viper.go Outdated Show resolved Hide resolved
@@ -86,35 +86,9 @@ const (
ViperKeyAuthenticatorUnauthorizedIsEnabled = "authenticators.unauthorized.enabled"
)

func BindEnvs() {
if err := viper.BindEnv(
ViperKeyProxyReadTimeout,
Copy link
Member

Choose a reason for hiding this comment

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

Removing these will have a detrimental effect. Viper does not automatically check environment variables. You have to bind them explicitly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Viper does not automatically check environment variables. You have to bind them explicitly.

Why not? It indeed loads envs automatically: https://github.com/spf13/viper/blob/master/viper.go#L1039

In hydra, everything works fine without any explicit bindings.

Copy link
Member

Choose a reason for hiding this comment

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

Your analysis is unfortunately incorrect. In Hydra, we do not (yet) use JSON Schema to validate the config. However, validating against the Config JSON Schema requires all keys to be loaded. That's just the way Viper works (it is quirky).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I do not understand how we suppose to explicitly bind all keys, since most of them were recently removed for example in #258 . How do envs like AUTHENTICATORS_OAUTH2_INTROSPECTION_CONFIG_PRE_AUTHORIZATION_CLIENT_ID suppose to work?

(also related to the comment below about tests)

Copy link
Member

@aeneasr aeneasr Oct 22, 2019

Choose a reason for hiding this comment

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

While the keys have been removed as constants (because we don't explicitly need them any more - except for BindEnvs()) they are still being loaded using viper.Get(). However, I think it would be better to add a patch to our fork to automatically bind all envs when doing AutomaticEnv() instead of doing both AutomaticEnv() as well as BindEnv(). This is a known (and unsolved issue) in viper that has lead to some frustration (me included): spf13/viper#761 (and all linked issues).

Then, we can throw away BindEnvs() which - to be honest - sucks anyways.

Copy link
Member

Choose a reason for hiding this comment

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

This however would need some thinking as _ can both be . as well as _. So we'd end up with a lot of keys when "autom-mounting" all environment keys. For example env var FOO_BAR could both be foo_bar as well as foo.bar. That would in turn cause issues when we set additionalProperties: false in the JSON Config Schema - which is not great because with that property set people would immediately find any typos they're doing. Maybe by defining some "categories" that always replace _ with . would be the way to go. Not sure yet, needs some thinking.

Copy link
Member

Choose a reason for hiding this comment

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

Actually, I just found my original issue which touched this topic in 2016 :D spf13/viper#212

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I see, things getting complicated, combining viper behaviour and the introduction of json schemas.

If you come up with the idea how should we solve it, let me know.

I've pushed changes that make env vars work for defined keys (e.g. SERVE_PROXY_PORT), but envs like AUTHENTICATORS_OAUTH2_INTROSPECTION_CONFIG_PRE_AUTHORIZATION_CLIENT_ID still do not work. If you feel like we don't need this intermediary improvement, feel free to close this MR.

driver/configuration/provider_viper_public_test.go Outdated Show resolved Hide resolved
@aeneasr
Copy link
Member

aeneasr commented Oct 26, 2019

I wrote a small helper that is capable of extracting all configuration keys from the JSON Schema and that binds those keys to viper. This will resolve all of #280 #279 #276 #270 in a way that's future proof and very little effort code-wise and further homogenizes the configuration setup: ory/x#85

@hypnoglow
Copy link
Contributor Author

@aeneasr Looks like a nice solution to the problem! Thanks

aeneasr added a commit that referenced this pull request Oct 26, 2019
This patch automatically binds environment variables to configuration keys. This patch resolves several issues:

- closes #276
- closes #270
- closes #279
- closes #280
@aeneasr aeneasr closed this in #285 Oct 27, 2019
aeneasr added a commit that referenced this pull request Oct 27, 2019
This patch automatically binds environment variables to configuration keys. This patch resolves several issues:

- closes #276
- closes #270
- closes #279
- closes #280

Aösp resolves fsnotify permission test issues on macOS
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.

None yet

2 participants