You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Pydantic Field object allows the specification of a default value. However, when used with this plugin the default is ignored and an exception is returned. Is this just a matter of configuration/implementation or does the plugin need to add support for this usecase?
importosfrompydanticimportBaseSettings, Fieldfrompydantic_vaultimportvault_config_settings_sourcefromfastapi.loggerimportloggerfromcore.constantsimport*classSettings(BaseSettings):
# Vault Secret Pathvault_secret_path=os.getenv("VAULT_SECRET_PATH", VAULT_SECRET_PATH)
CMR_MODEL_VERSION2: str=Field(default="test123", vault_secret_path=vault_secret_path, vault_secret_key="some_vault_key")
classConfig:
# Support .env filesenv_file=".env"env_file_encoding="utf-8"# Support Vault# This will use token and fall back to app-rolevault_url: str=os.getenv("VAULT_URL", VAULT_URL)
vault_token: str=os.getenv("VAULT_TOKEN", "")
vault_role_id: str=os.getenv("VAULT_ROLE_ID", VAULT_ROLE_ID)
vault_secret_id_id: str=os.getenv("VAULT_SECRET_ID", "")
vault_secret_mount_point: str="kv"@classmethoddefcustomise_sources(
cls,
init_settings,
env_settings,
file_secret_settings,
):
return (
init_settings,
env_settings,
vault_config_settings_source,
file_secret_settings
)
try:
settings=Settings()
print("settings": settings)
except:
logger.error("Failed to parse app settings")
So with the above example code if the vault KV pair does not exist you get a KeyError exception.
Is this just an edge case that is not supported by the plugin? If there was a way to selectively tell the plugin to use defaults if there was a vault exception that would be fine too.
The text was updated successfully, but these errors were encountered:
Hello @andysnowden!
Thank you for your this issue, it feels weird to have feedback even though the code is very much in alpha ^^'
You are right about this, I have quickly debugged your example and it is an error on my side: whether I find a key in Vault or not I return it in the values dict that Pydantic uses to populate the model, sometimes with a None value. Pydantic then validates this dict and because None is not a valid type, it gets angry ^^
I will see to fix this, or you can do a PR if you want 😉 (if you look at vault_settings.py I try to get the Vault key, log a message if it is not found, but I add it to the dict in any case; this should only happen if we found a value).
The Pydantic
Field
object allows the specification of a default value. However, when used with this plugin the default is ignored and an exception is returned. Is this just a matter of configuration/implementation or does the plugin need to add support for this usecase?So with the above example code if the vault KV pair does not exist you get a
KeyError
exception.However, if the KV pair does exist but is set or even blank it takes that value and ignores the default
Is this just an edge case that is not supported by the plugin? If there was a way to selectively tell the plugin to use defaults if there was a vault exception that would be fine too.
The text was updated successfully, but these errors were encountered: