Skip to content

secrets_provider

secrets_provider

Read a secret by name through a secrets provider.

A secrets provider is any function that takes a secret name and returns its value or None. get_secret is the one call scripts make; which provider answers is chosen by an explicit argument, else the CREW_DCS_SECRETS_PROVIDER setting, else env_provider. Secret values are never logged.

PROVIDER_SETTING module-attribute

PROVIDER_SETTING = 'CREW_DCS_SECRETS_PROVIDER'

Setting naming the configured provider as package.module:function.

SecretsProvider module-attribute

SecretsProvider = Callable[[str], str | None]

Takes a secret name, returns its value or None when it has none.

RecursiveSecretsProviderError

RecursiveSecretsProviderError(provider_name: str)

Bases: AuthError

The configured secrets provider called get_secret without a provider.

That call would resolve the configured provider again and never return. A provider that needs a secret of its own passes an explicit provider=.

SecretNotFoundError

SecretNotFoundError(secret_name: str, provider_name: str)

Bases: AuthError

The secrets provider has no value for the requested secret name.

env_provider

env_provider(name: str) -> str | None

The default secrets provider: process environment, then the repo-root .env.

The repo root is the nearest ancestor of the working directory that holds .git (the working directory itself when there is none). The .env is read, not loaded: os.environ is never modified, and a non-empty value in the environment wins; an empty one counts as missing and falls through to .env.

get_secret

get_secret(
    name: str, provider: SecretsProvider | None = None
) -> str

Return the secret name from a secrets provider.

The provider is, in order: provider when passed; else the function named by the CREW_DCS_SECRETS_PROVIDER setting (package.module:function, read from the environment or the repo-root .env); else env_provider.

Raises:

Type Description
SecretNotFoundError

the provider returned None or an empty string.

ValueError

CREW_DCS_SECRETS_PROVIDER is not a package.module:function path.

TypeError

CREW_DCS_SECRETS_PROVIDER names something that is not callable.

RecursiveSecretsProviderError

the configured provider called get_secret without provider=.