Pydantic BaseSettings的一个简单扩展,可以从Hashicorp Vault检索机密
pydantic-vault的Python项目详细描述
Pydantic拱顶
对PydanticBaseSettings的简单扩展,可以从HashicorpVault中的KV v2 secrets engine检索机密
入门
与Pydantic BaseSettings
相同,创建一个继承pydantic_vault.VaultBaseSettings
的类,然后定义字段并使用
importosfrompydanticimportSecretStr,Fieldfrompydantic_vaultimportVaultBaseSettingsclassSettings(VaultBaseSettings):username:str=Field(...,vault_secret_path="path/to/secret",vault_secret_key="my_user")password:SecretStr=Field(...,vault_secret_path="path/to/secret",vault_secret_key="my_password")classConfig:vault_url:str="https://vault.tld"vault_token:SecretStr=os.environ["VAULT_TOKEN"]vault_namespace:str="your/namespace"# Optional, pydantic-vault supports Vault namespaces (for Vault Enterprise)vault_secret_mount_point:str="secrets"# Optional, if your KV v2 secrets engine is not available at the default "secret" mount pointsettings=Settings()# These variables will come from the Vault secret you configuredsettings.usernamesettings.password.get_secret_value()# Now let's pretend we have already set the USERNAME in an environment variable# (see the Pydantic documentation for more information and to know how to configure it)# Its value will override the Vault secretos.environ["USERNAME"]="my user"settings=Settings()settings.username# "my user", defined in the environment variablesettings.password.get_secret_value()# the value set in Vault
文件
Field
其他参数
您可能已经注意到我们直接从Pydantic导入Field
。Pydantic保险库不添加任何自定义逻辑,这意味着您仍然可以使用Pydantic中您知道和喜爱的所有内容。在
Pydantic Vault使用的其他参数包括:
Parameter name | Required | Description |
---|---|---|
^{ | Yes | The path to your secret in Vault |
^{ | Yes | The key to use in the secret |
例如,如果创建一个密钥为password
且值为a secret password
的机密database/prod
,则可以使用
身份验证
目前Pydantic Vault只支持直接令牌身份验证,即必须使用您选择的方法进行身份验证,然后将生成的保险库令牌传递给您的Settings
类。在
计划对Approle和Kubernetes身份验证方法提供支持。在
配置
在Settings.Config
类中,可以配置以下元素:
您还可以配置原始Pydantic BaseSettings
类中可用的所有内容。在
优先顺序
设定值的确定如下(按优先级降序排列):
- 传递给
Settings
类初始值设定项的参数 - 环境变量
- Vault变量
Settings
模型的默认字段值
它是same order,与原始的BaseSettings
相同,但是在默认值之前是Vault。在
许可证
Pydantic Vault在MIT license下可用。在
- 项目
标签: