Secrets#

A Secret describes sensitive configuration that a remote Task can receive as environment-style input. The Task reads the value with os.environ or another normal configuration interface; it does not receive a Secret object as an argument. Inline descriptors work when attached at App or Task level for ordinary remote Tasks.

There are two different Secret shapes in the Python SDK:

  • An inline Secret carries key-value data in the invocation request. Use Secret.from_dict, Secret.from_dotenv or Secret.from_local_environ for this path.
  • A named Secret carries only a backend name, created with Secret.from_name. The SDK sends that name for server-side resolution. Provision the name with the released CLI workflow before referencing it from code.

The generated Secret reference is the authority for the released constructor and helper signatures. This page explains which paths are useful in the current Preview and how they behave when attached to a Task.

A working inline example#

The canonical example uses Secret.from_dict with a harmless placeholder. It checks only whether the key is present in the remote process and never prints the value. The descriptor is attached at App level, and the task omits secrets=, so the example verifies App-level inheritance as well as inline injection.

Run it as a normal remote Task:

bash
yak run secrets.py

The example contains only preview-demo-value, which is not a credential. For a real credential, keep the value out of source control and use a locally managed configuration source.

Inside the remote task, os.environ.get("YAK_DEMO_TOKEN") returns a non-empty value and the entrypoint prints secret configured: True. The example proves presence only; it is not a pattern for logging, returning or otherwise exposing a secret.

Choose the Secret shape#

Use Secret.from_local_environ(["KEY"]) when a value exists in the process that constructs the descriptor and you want to select exactly which keys become inline Secret data. This avoids writing the value into the Python file. With yak run, do not assume that a shell export is uploaded automatically; an explicit --env is required to make that key available to the bootstrap process, and the same explicit value is also ordinary run-scoped environment. Use from_dict for a Secret-only smoke test like the canonical example.

Use Secret.from_dict({"KEY": value}) when the value is being supplied by an already controlled configuration layer:

python
secret = yak.Secret.from_dict({"SERVICE_TOKEN": configured_token})
app = yak.App(name="inline-config", secrets=[secret])

Use Secret.from_dotenv() only when the .env file is deliberately part of the local execution setup. It reads the file into an inline descriptor; it is not a remote secret store, and the file must not be committed or included in a source archive. Prefer an explicit path or selected local environment keys when the file location or contents need to be unambiguous.

Use Secret.from_name("team-api") when a named backend Secret has been provisioned through the CLI workflow. Yak resolves the name server-side for the Invocation; the client sends the reference, not the value.

Attach a Secret to an App or task#

The SDK exposes an App-level list:

python
app = yak.App(
    name="shared-config",
    secrets=[yak.Secret.from_local_environ(["SERVICE_TOKEN"])],
)

A Task with no secrets= argument inherits this App-level list. The SDK distinguishes omission from an explicit empty list and validates the App declaration before registration.

Pass secrets= explicitly to @app.task() for the current working path:

python
task_secret = yak.Secret.from_local_environ(["BATCH_TOKEN"])


@app.task(secrets=[task_secret])
def batch() -> str:
    return "ready" if os.environ.get("BATCH_TOKEN") else "missing"

An explicit Task list replaces any App-level declaration rather than extending it. secrets=[...] uses only the Task list, while secrets=[] deliberately clears the inherited list. A Task with no secrets= argument inherits the App list.

See App and App.task for the complete declaration surface. Keep the actual Secret values in the descriptor boundary; do not pass them as ordinary Task arguments.

Read a value inside a Task#

Secrets are environment-style configuration. Read only the key your Task needs and keep the result in memory for the shortest useful scope:

python
@app.task()
def call_service() -> str:
    token = os.environ["SERVICE_TOKEN"]
    return "configured" if token else "missing"

Do not print token, return it from a Task, include it in an exception, or put it into a metric, trace attribute, response body or serialized argument. A boolean presence check, as used by the canonical example, is safer for a smoke test.

If a key may be absent, os.environ.get("SERVICE_TOKEN") lets your code choose a controlled fallback. os.environ["SERVICE_TOKEN"] raises KeyError when the key is absent; that failure is reported as a user Task failure, not as proof that a named Secret was successfully provisioned.

Multiple descriptors and precedence#

For inline descriptors attached to an ordinary remote Task, the SDK validates and composes the Secret set before sending the request. Inline values overlay the run-scoped environment values supplied to the App. At runtime, the Invocation environment overlays non-secret environment defaults from the selected Image.

Duplicate keys are rejected rather than resolved by position:

python
secrets = [
    yak.Secret.from_dict({"SHARED": "first", "FIRST_ONLY": "one"}),
    yak.Secret.from_dict({"SHARED": "second", "SECOND_ONLY": "two"}),
]

This raises ValueError before registration or invocation. Duplicate named references are rejected the same way, and named references are canonicalized into deterministic sorted request names. Named-versus-inline key overlap is checked after the named Secret is resolved server-side. A collision fails invocation admission before user code runs, and the diagnostic identifies only the conflicting key name—not either Secret value.

Platform-owned runtime channels may be removed or replaced after application configuration is merged. Treat names reserved by the Yak runtime as unavailable for application Secrets, and never use a platform channel as a place to store application credentials.

Named provisioning and missing values#

Secret.from_name("team-api") references a named backend Secret. The released CLI provides the provisioning workflow:

text
secret.json
{"SERVICE_TOKEN": "replace-with-a-locally-managed-value"}
bash
yak secret create team-api --file secret.json --environment development
yak secret list --environment development
yak run secrets.py --environment development
yak secret update team-api --file secret.json --environment development
yak secret get team-api --environment development
yak secret delete team-api --force --environment development

Keep the JSON file local and out of source control. Use --environment consistently when provisioning and running, or set YAK_ENVIRONMENT. yak secret get returns Secret metadata and key names only; Secret values are not returned by management commands, including JSON output. Check the installed CLI’s command help for exact options.

After provisioning, attach the reference to the App or task and read the resulting key from os.environ. Named values remain server-owned; the client sends the reference, not the value. Updates are observed on a subsequent invocation in the current Preview path, so changing a named Secret does not require rebuilding the Image.

If a named Secret is missing, current named Secret resolution fails before the user Task runs rather than silently producing an empty environment key. Treat a missing or inaccessible name as a provisioning or scope error and verify the name and --environment selection.

The generated reference exposes from_name(name: str) only. The older environment_name and required_keys parameters were removed; do not use examples written for the older reference.

Secret() with no name or data is an empty inline descriptor. A Secret cannot be both named and inline: Secret(name="team-api", data={"KEY": "value"}) raises ValueError, and an empty or whitespace-only name also raises ValueError. Use the named constructor or an inline helper for an unambiguous declaration; the generated reference owns the exact constructor signature.

Secret lifecycle and image lifecycle#

An Image and a Secret have separate lifecycles:

text
Image.env("MODE", "preview")
    image definition and non-sensitive runtime default

Secret.from_dict({"SERVICE_TOKEN": value})
    sensitive value supplied separately when a Task runs

Changing an inline Secret value does not require rebuilding an Image. Conversely, changing image packages or non-secret Image.env configuration is an image change, not a Secret update. Named Secret updates are observed on subsequent invocations, while rotation policy and automatic rotation remain outside the documented contract.

Choose an approach#

Use these current boundaries when choosing a declaration:

  • Pre-provisioned reusable credential: provision with yak secret create, then reference it with Secret.from_name(...).
  • Temporary or local Preview value: Secret.from_dict(...), attached at App or Task level, is verified.
  • One Task needs a different Secret set: use @app.task(secrets=[...]); explicit task replacement is verified.
  • One Task must receive no Secret descriptors: use @app.task(secrets=[]); the empty override is verified.
  • The same Secret set should apply to every Task: use App(secrets=[...]); a Task without an override inherits the App-level list.
  • Fixed non-sensitive image configuration: use Image.env(...), not a Secret. The Images guide covers that surface.

Security guidance#

Keep secrets out of source files, Git history, Dockerfiles, image environment declarations, build commands, logs and responses. The Image.env surface is for non-sensitive configuration; see the Images guide for that boundary.

Select only the keys a task needs. Prefer short-lived or least-privilege credentials, rotate them through the system that owns them, and avoid placing one broad credential on every task in an App. Do not assume that an inline descriptor provides automatic redaction, rotation, external vault storage or a key-management policy; those guarantees are not part of this Preview guide.

When testing, use a harmless placeholder and assert presence rather than displaying the value. Before sharing output, inspect logs, exception text, serialized arguments and HTTP responses for accidental disclosure.

Preview limitations#

This guide documents App-level and Task-level Secret injection for ordinary remote Python Tasks. Persistent Live Apps created with yak deploy accept named Secrets declared at App level. Inline descriptors and Task-level secrets= overrides are run-scoped and are rejected for persistent use.

The Live Apps guide covers persistent use separately. Provision the named Secret first and attach only Secret.from_name(...) values to App(secrets=[...]); do not attach inline values or Task-level overrides.