-
-
Notifications
You must be signed in to change notification settings - Fork 76
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
BaseSettings env_prefix also apply for secrets #30
Comments
Hi @JerPk and thanks a lot for reporting! |
Hi @PrettyWood, I didn't expect such a fast response and implementation. |
Similar issue exists, if "env" is set for a field. class Settings(BaseSettings):
auth_key: str
api_key: str = Field(..., env="my_api_key")
redis_dsn: RedisDsn = "redis://user:pass@localhost:6379/1"
pg_dsn: PostgresDsn = "postgres://postgres:password@localhost:5432/postgres"
# to override domains:
# export my_prefix_domains='["foo.com", "bar.com"]'
domains: Set[str] = set()
# to override more_settings:
# export my_prefix_more_settings='{"foo": "x", "apple": 1}'
more_settings: SubModel = SubModel()
class Config:
# env_prefix = "my_prefix_" # defaults to no prefix, i.e. ""
fields = {
"auth_key": {"env": "my_auth_key",},
"redis_dsn": {"env": ["service_redis_dsn", "redis_url"]},
}
secrets_dir = "/var/run" My secret had to be renamed to my_auth_key to succeed. |
ProposalKeep the solution as is now - thus use the environment variable name in all situations - not only for trying to read values from env var (and from dotenv file), but also from secrets. Action needed: update the docs to clarify this rule. ReasoningKeep things simple. With unique naming rule for named configuration sources it is much simpler to document the applicaiton - simply give single set of names and add, that they can be provided via env vars, dotenv file or via secrets in a directory. Another advantage: all existing deployment will work. |
To quote @JerPk we have two solutions
I thought option 2 made more sense but reading @vlcinsky opinion, I understand option 1 can be desired.
This would hence break nothing and allow both behaviours but could have side effects and is definitely less clear... I add "feedback wanted" label and hope others will share their point of view :) |
Thanks @PrettyWood for reaction. Here is my feedback: Following the KISS I would invite not to add any extra option (such as "for secrets use env_prefix yes/no") unless it is really needed.
|
But, if pydantic/pydantic#2190 get merged, we won't be able to transparently prefix secrets? My stack has many services, and I would like to store secrets as |
Agree. But if we go this way, then aliases passed via |
The workaround to ignore the @classmethod
def settings_customise_sources(
cls,
settings_cls: Type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> Tuple[PydanticBaseSettingsSource, ...]:
return init_settings, env_settings, dotenv_settings, SecretsSettingsSource(settings_cls, env_prefix=None) |
Released a small package that solves this problem (available on PyPi): Some features:
|
Checks
Bug
The
env_prefix
is used for both env vars and secrets, which makes it impossible to combine env vars (with prefix) and secrets (without prefix) into a single settings model.Output of
python -c "import pydantic.utils; print(pydantic.utils.version_info())"
:Environment vars:
Secrets in container:
Tested without the
env_prefix = 'RESTAPI_'
line, which filled the settings from the secrets correctly, env vars were not read as expected.Possible solutions
env_prefix
is also used for secretsenv_prefix
for the secretssecrets_prefix
to be used as secrets prefixThe text was updated successfully, but these errors were encountered: