Skip to content

Cannot load JSON from secret files #2917

@nat212

Description

@nat212

Checks

  • I added a descriptive title to this issue
  • I have searched (google, github) for similar issues and couldn't find anything
  • I have read and followed the docs and still think this is a bug

Bug

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.8.2
            pydantic compiled: True
                 install path: /home/natasha/.local/share/virtualenvs/draperweb3-7iyfNlF4/lib/python3.8/site-packages/pydantic
               python version: 3.8.6 (default, May 27 2021, 13:28:02)  [GCC 10.2.0]
                     platform: Linux-5.11.0-7614-generic-x86_64-with-glibc2.32
     optional deps. installed: ['dotenv', 'email-validator', 'typing-extensions']

When loading settings values from a secrets file, values encoded in JSON do not get correctly parsed, leading to a ValidationError. After looking through the code, I discovered that SecretsSettingsSource simply reads the secret value as text instead of checking if it is a complex type. I'm not sure if this is intended, or if it's a bug, but either way, I think it would be beneficial if it were possible to load JSON from a secret file.

Example:

pydantic.error_wrappers.ValidationError: 1 validation error for Settings
cloud_config
  value is not a valid dict (type=type_error.dict)

Minimal example

from pydantic import BaseSettings, BaseModel


class CloudConfig(BaseModel):
    client_id: str
    client_secret: str
    token_url: str
    auth_url: str
    api_url: str


class Settings(BaseSettings):
    cloud_config: CloudConfig

    class Config:
        case_sensitive = False
        secrets_dir = "/run/secrets"

settings = Settings()
print(settings.cloud_config.client_id)

This works if I have an environment variable CLOUD_CONFIG='{"client_id": "...", ...}', but not if I have the same string in a secrets file

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.X

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions