Open lmmx opened 1 year ago
Since @hramezani already replied on the PR I'll assign him here, but in general I think it makes sense for us to support/maintain this if you are willing to provide an initial implementation. (And it seems like you are — thank you!)
Are there any updates on this feature?
Are there any updates on this feature?
Happy annual kanban refresh to all who celebrate: I've put it back onto the TODO list :face_with_peeking_eye: :hourglass_flowing_sand:
Outline
Setting env. variables for auth config is awkward, so
python-dotenv
can be used to load them from a.env
text file insteadKeyrings can be preferable to plain text secret storage for security, and Python has a
keyring
module [source]This practice is used by other well known tools such as the GitHub CLI tool
gh
andtwine
[source]This could be made an optional dependency if desired (perhaps to keep package size minimal/consistent).
Impact
When I
pip install keyring
on Linux after first installingpydantic
andpydantic-settings
the additional dependencies are:Usage
Once installed, secret access is achieved like so:
The
gh
tool sets the username to an empty string, indicating that it's used as a simple key-value secret store.You can also access specific keyrings, also known as 'collections' (for instance if you wanted to have different applications using different keys with the same name, say a different API key for different services). For reference
Proposed implementation
Essentially we are replacing
os.environ.get(validation_alias)
forkeyring.get_password(validation_alias)
In this library, both environment variables and
.env
configured variables are loaded into theenv_vars
attribute.EnvSettingsSource
calls_load_env_vars()
at initialisation:https://github.com/pydantic/pydantic-settings/blob/5933ea6ca51d58342378eed5db12d3f6f6dee8df/pydantic_settings/sources.py#L376-L381
DotEnvSettingsSource
subclassesEnvSettingsSource
and overrides the_load_env_vars()
methodhttps://github.com/pydantic/pydantic-settings/blob/5933ea6ca51d58342378eed5db12d3f6f6dee8df/pydantic_settings/sources.py#L571-L590
I would have this work similarly to
.env
handling with a subclass exposing a custom way to load env vars.We can enumerate all keys (as bytes) via:
(In real code you'd have to have some error handling in case the 3 chained methods error!)
I think default conversion of bytes to str type would be reasonable here?
Proof of concept
The attached PR supplies a working implementation of this feature on Linux, using the
SecretStorage
backend.This is overridden by setting an environment variable.
Selected Assignee: @dmontagu