Secrets and scrubbing¶
get_secret¶
@{get_secret, /ds/vault/ds_vault/<tenant_id>/<id>/prod}
@{get_secret, owner.settings.secrets.client_key}
Secrets are resolved by the service that uses them, in memory, when it needs them (§7). When that service cannot reach the vault, its dispatcher reads them instead, with
resolve(path, strict=False)(see workers without vault access). Routers and ds-config never resolve them.The key’s tenant segment,
/ds/vault/<store>/<tenant_id>/…, must be the context’s tenant. Only the tenant is checked; the rest of the key’s format is the frontend’s concern. Any key with another tenant, or with no tenant segment (db/url), fails withsecret_scopebefore the vault is read, so a run cannot read another tenant’s secrets. The match is exact: the segment must be the run’stenant_idcharacter for character, which for a UUID means lowercase, as the vault stores it. An uppercase or otherwise respelled UUID is refused.Owner secrets are stored as vault paths under the owner’s
settings.secrets;@{get_secret, owner.settings.secrets.<key>}reads the path from the owner, then the value from the vault.A key the vault does not have fails with
secret_missing. A vault that cannot be read (noVAULT_DATABASE_URL, a connection, IAM, or KMS failure) fails withsecret_unavailable: that is the service’s configuration, not the definition.
Reading ds-vault¶
The package reads secrets itself, through ds-vault-py-sdk; services do not
wire a reader. Configure the process the same way as the dispatchers:
Setting |
Purpose |
|---|---|
|
Postgres DSN of the vault’s |
AWS credentials (role, region) |
KMS decrypt of the vault’s data keys |
One vault client is created per process, on the first get_secret. Nothing
is read at import time, and a service that never resolves a secret needs no
vault configuration. The client keeps nothing between reads: the SDK’s
plaintext, decrypted data key, record, and SSM parameter caches are disabled, so a secret
lives only in the execution that resolved it (§10) and a rotated secret is
picked up on the next read. Each read costs one vault query and one KMS
decrypt.
Only the service that uses a secret, or its dispatcher when that service
cannot reach the vault, reads it (§7). Routers and ds-config never call
resolve, so they never reach the vault.
The SDK looks a key up by path only and does not check the caller’s tenant. The tenant check above is therefore what keeps one tenant’s run away from another tenant’s secrets, and it runs before the vault is read.
The value is decoded as UTF-8, then base64-decoded if it is valid base64 (this keeps today’s behaviour). A plaintext secret that happens to be valid base64 is decoded too, so store secrets base64-encoded.
If the vault cannot be read (no VAULT_DATABASE_URL, connection or KMS
failure), get_secret fails with secret_unavailable. The message names the
exception type only, never its text.
Each read is one vault query and one KMS decrypt, serialised process-wide.
Resolve a value once and keep the result; don’t resolve the same job again
(for example inside a serialize() that runs more than once).
Tests and examples¶
ds_common_placeholder_py_lib.testing.fake_vault replaces ds-vault while a
block runs:
from ds_common_placeholder_py_lib.testing import fake_vault
ctx = Context.from_values({"tenant_id": "t1", "db": {"pw": "@{get_secret, /ds/vault/s/t1/k/prod}"}})
with fake_vault({"/ds/vault/s/t1/k/prod": b"c2VjcmV0"}) as vault:
ctx.resolve("db.pw") # "secret"
assert vault.reads == ["/ds/vault/s/t1/k/prod"]
A value that is an exception is raised, to simulate a failing read, and
default= answers every other key. The tenant check still runs first. It is
a helper for tests and offline tooling, not for code that serves runs. It
applies to the current thread or async task only.
Workers without vault access¶
The vault’s database is reachable only from inside the VPC. A worker that
runs outside it (a Lambda or task without VPC access) cannot read the vault,
so its dispatcher, which runs inside, reads the secrets for it with
resolve(path, strict=False):
# dispatcher (in the VPC): no task outputs or execution yet
ctx = Context.from_values({"tenant_id": tenant_id, "job": job}, verbatim=code_paths)
job = ctx.resolve("job", strict=False) # secrets read; what it cannot resolve yet is kept
send(job)
# worker (no vault access)
ctx = Context.from_values(
{"tenant_id": tenant_id, "run": run, "variables": declared_variables, "tasks": outputs, "job": job},
verbatim=code_paths,
)
settings = ctx.resolve(["job", "tasks", 0, "settings"]) # the rest, as each task runs
The result is a template, not final values. Each secret is written as
@{secret, '…'}(a'in it doubled). Inside quotes nothing is a placeholder, so whatever the secret holds (@,{,@{) and whatever is next to it, resolving the template gives the result oneresolveof the original would give. On bad input the error can differ.A placeholder the dispatcher’s context cannot resolve yet, because it reads a namespace or value it does not have (
unknown_namespace,missing_value), is kept as written for the worker. Any other error fails the dispatch, a failing provider too (a service fault). A key read from a namespace the dispatcher lacks (@{get_secret, execution.key}) is kept too, and the worker, without the vault, then fails on it.A call the dispatcher can resolve itself is resolved at dispatch, time functions included:
@{get_current_date, Europe/Oslo}or@{get_hour, UTC}with noatgives the date or hour of the dispatch, not of the run, and the same one on every retry. Giveata value the dispatcher lacks (run.logical_at) and the worker resolves it.A resolved value holding
@or{is written as a quoted text,@{'…'}, so the worker’s resolve reads it back exactly: nothing in it, or where it meets the text around it, starts a placeholder.A placeholder with a
get_secretorsecretcall nested in another call is not evaluated. Eachget_secretis read and replaced in place, and the rest is left for the worker:@{str_join, 'Bearer ', @{get_secret, …}}becomes@{str_join, 'Bearer ', @{secret, '…'}}, written in a normal form (every literal quoted,@{lookup, x.y}asx.y) that resolves the same way. So the worker records every secret itself, not only a text made of it (itsscrubwould not know the secret alone). The same holds for a reference to a value that holds a secret (@{str_join, 'B ', job.h},hbeing aget_secret): the reference is replaced by@{secret, '…'}. A secret insidecoalesceis read by the dispatcher even when an earlier argument has a value; if it cannot be read, the job fails there.A
get_secretwhose key is itself a nested placeholder (@{get_secret, @{lookup, owner.settings.client_key}}) is resolved whole by the dispatcher, key included, when its context has the key.The tenant check runs in the dispatcher, before the vault is read.
A
sqlpath is kept as written for the worker: a SQL literal cannot be escaped, and the worker’sresolve_sqlrefuses a secret there. The vault is not read for it.The
secretfunction returns its argument and records it as a secret in the worker’s context, asget_secretdoes. Nothing else needs to be passed: once the worker has resolved a value, itsscrubremoves it and its SQL check refuses it. Asecretcall in SQL is refused outright, and errors never show the text of a@{secret, …}placeholder.Any context’s
scrubmasks@{secret, '…'}by its form, so the dispatcher and the worker can both log the job:ctx.scrub(json.dumps(job)).A verbatim field, such as a script’s code, is passed through as written: give the worker’s context the same
verbatimpaths the dispatcher’s uses, or a@{in the code fails withsyntax.coalesceskips an argument with no value at dispatch as always: if its namespace is there but the value comes only at run time, the dispatcher already settles on a later argument.Call
resolve(path, strict=False)once per job and send that result.The trade-off: secrets are in the job the dispatcher sends, in plaintext, wherever that job travels: the invocation payload, staged S3 objects, and any log or dead-letter queue that captures it. They are read when the job is dispatched, not when the task runs. Use it only for workers that cannot reach the vault.
Checking definitions without the vault¶
To see which definitions the dispatcher would refuse (syntax, another tenant’s key, a key with no tenant segment) without reading any secret, run it with a fake vault that answers every key:
from ds_common_placeholder_py_lib import Context, PlaceholderError
from ds_common_placeholder_py_lib.testing import fake_vault
with fake_vault(default=b"x"):
for definition in definitions:
ctx = Context.from_values({"tenant_id": definition["tenant_id"], "definition": definition})
try:
ctx.resolve("definition", strict=False)
except PlaceholderError as err:
print(definition["id"], err.code.value, err.path)
The tenant check still runs, so pass each definition’s real tenant.
Scrubbing¶
A secret value must not appear in any event, log line, error message,
artifact, or checkpoint. The package’s own error messages never contain
resolved values. For everything else, call
Context.scrub before the text leaves
the process:
try:
client.connect(**linked_settings)
except Exception as exc:
logger.error(ctx.scrub(str(exc)))
raise
scrub replaces, with ***, every secret the context has read: the decoded
value, the raw (base64) value when it read it from the vault, and their
JSON-escaped forms and Python repr forms (as a message that quotes a value
with !r writes it). Longer secrets are replaced first. A secret written by
resolve(strict=False) as @{secret, '…'} is masked by its form, even by a
context that has not resolved it yet, so a worker can log the job it received. It only knows secrets read through this context, so
scrub with the same context that resolved them.
Secrets in SQL¶
Secrets are never allowed in SQL: a get_secret call is refused before the
vault is read, and a value that contains a secret this context has read (a
variable resolved from one, or one written by resolve(strict=False) as
@{secret, '…'}) is refused too; a secret call in SQL is refused before it
is resolved. Only secrets read through the
same context are known: a value from any namespace (variables, owner,
tasks, a provider) that holds a secret this context never read passes. So
resolve variables with the run’s context, and never put secret values into
a namespace yourself. See SQL.