Secrets#
- class rapidtools.jobs.Secrets(values: Mapping[str, str] | None = None, environ: Mapping[str, str] | None = None)[source]#
Bases:
objectResolves 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 variableRAPIDTOOLS_SECRET_<NAME>.{"$env": "<VAR>"}reads the environment variableVAR.
- Parameters:
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
Accept a
Secrets, a mapping, a secrets-file path orNone.Return the secrets-file document holding
values.Read a secrets file.
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 orNone.
- 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;
valueitself otherwise.- Raises:
JobError – If a reference is malformed, or cannot be resolved and
missingisNone.