Secrets#

class rapidtools.jobs.Secrets(values: Mapping[str, str] | None = None, environ: Mapping[str, str] | None = None)[source]#

Bases: object

Resolves the secret references of a job.

A parameter such as an API key may be written in a job file as a literal string, but the file then has to be protected like the key itself. The two reference forms keep the value elsewhere:

  • {"$secret": "<name>"} is looked up in the secrets given to the runner (a mapping or a secrets file), then in the environment variable RAPIDTOOLS_SECRET_<NAME>.

  • {"$env": "<VAR>"} reads the environment variable VAR.

Parameters:
  • values (Mapping[str, str] | None) – Secret values by name.

  • environ (Mapping[str, str] | None) – Environment to read. Defaults to os.environ.

Example

>>> secrets = Secrets({'api_key': 'k-123'}, environ={'MLY': 'MLY|9'})
>>> secrets.resolve({'$secret': 'api_key'}), secrets.resolve({'$env': 'MLY'})
('k-123', 'MLY|9')
>>> secrets.resolve('literal')
'literal'

Methods

coerce

Accept a Secrets, a mapping, a secrets-file path or None.

document

Return the secrets-file document holding values.

load

Read a secrets file.

resolve

Return the value a parameter stands for.

classmethod load(path: str | Path) → Secrets[source]#

Read a secrets file.

The file is a JSON object {"schema": "rapidtools.secrets", "schema_version": "1.0", "secrets": {"<name>": "<value>", ...}}.

Raises:

JobError – If the file cannot be read or is malformed.

classmethod coerce(secrets: Secrets | Mapping[str, str] | str | Path | None) → Secrets[source]#

Accept a Secrets, a mapping, a secrets-file path or None.

static document(values: Mapping[str, str]) → dict[str, Any][source]#

Return the secrets-file document holding values.

resolve(value: Any, missing: str | None = None) → Any[source]#

Return the value a parameter stands for.

Parameters:
  • value (Any) – A literal, or a secret reference.

  • missing (str | None) – Returned for a reference that cannot be resolved. None (default) raises instead.

Returns:

Any – The secret for a reference; value itself otherwise.

Raises:

JobError – If a reference is malformed, or cannot be resolved and missing is None.