# Secrets (Third-Party Keys)

A secret is a real third-party credential your app needs at run time: a
Stripe or OpenAI key, an email provider's API key, an outside service's
token. Cofactor stores it encrypted per application and hands it to the
sandbox's processes as an environment variable. It is never written into
the definition or your image, and no command prints it back.

## Which kind of value is it?

- A shared constant (a database URL, a feature name): `env:` in the
  definition. See the `env` topic.
- A URL that differs per sandbox (the app's own public origin): a
  `{port: <name>}` param. See the `definition` topic.
- A value generated for you (a session-signing key): a `{secret: true}`
  param. One random value is made when the ready snapshot is built and is
  shared by every copy of that snapshot.
- A real third-party secret: the secret store, described below.

Short non-secret values belong in `env:`: a value under 8 bytes is not a
credential, and the store refuses it.

## Use the provider's test keys

Sandbox egress is open, except for the domains the definition's
`emulation` intercepts (see the `emulation` topic): a call your app makes
in a sandbox goes out to the real provider. Set the provider's test or
sandbox key (Stripe's `sk_test_...`, a sandbox API key), never a live
one. Never put a key in the image, a `.env` file or
`cofactor.sandbox.yaml`: those end up in layers, snapshots and the
repository.

## Setting one

The value comes from stdin only, so it never lands in shell history or
`ps`. The CLI refuses to read it from a terminal.

```bash
printf %s "$STRIPE_TEST_KEY" | cofactor sandbox secret set STRIPE_API_KEY --application shop
```

That is one secret for all of the application's environments. To give
one environment a different value, name it:

```bash
printf %s "$STRIPE_STAGING_KEY" | cofactor sandbox secret set STRIPE_API_KEY --application shop --environment staging
```

See what is set (names and scopes, never values):

```bash
cofactor sandbox secret list --application shop
```

```text
NAME            SCOPE                UPDATED
STRIPE_API_KEY  environment staging  2026-10-08T09:13:41.000Z
STRIPE_API_KEY  all environments     2026-10-08T09:12:00.000Z
```

Names are capital letters, digits and `_`, not starting with a digit,
and must not be PATH, HOME, USER, SHELL, NODE_OPTIONS,
NODE_EXTRA_CA_CERTS, `LD_*` or `COFACTOR_*`. A value is 8 to 8192
bytes, with no NUL. The environment, when given, must be one the
application's synced definition has.

## Override and inheritance

A secret with no `--environment` applies to every environment. One with
`--environment` is an override that wins for that environment. Resolution
follows the same chain as `env:` and seeds (see `environments`): the
application-wide value, then each ancestor's override, then the claimed
environment's own, last one wins. An override on a parent applies to
every environment that extends it; a child's own override beats it.

A secret also overrides an `env:` entry of the same name.

## Reaching the app

Secrets are injected into:

- services that declare `reload_on_prepare: true`;
- `lifecycle.prepare` commands;
- seeds and actions;
- `authentication.run`.

They are NOT injected into `lifecycle.build` steps or `setup`:
build-time secrets are unsupported (a build bakes its output into a
layer that is shared). And a service WITHOUT `reload_on_prepare: true`
never sees a secret, not even once: set it on every service that reads
one.

```yaml
# cofactor.sandbox.yaml: the key itself is not here, only the service
# that reads it.
services:
  api:
    run: node server.js
    health: { http: 3001, path: /healthz }
    reload_on_prepare: true   # reads STRIPE_API_KEY; without this it never sees it

environments:
  staging:
    extends: default
    description: Same as default, with the staging Stripe account.
```

The app reads `process.env.STRIPE_API_KEY`; nothing in the definition
names the secret.

A secret can also be served through the `aws` emulator, for an app that
fetches its real third-party key from AWS Secrets Manager at run time
instead of reading an env var: `app/prod/ai-key: "${secrets.AI_API_KEY}"`
under `emulation.aws.secrets_manager.secrets`. Sandbox-internal values (a
local database password) are `{secret: true}` params, not store secrets.
See the `emulation` topic.

## Rotation

Set the new value with `secret set`, or remove it with `secret rm`. The
change takes effect for new claims and runs:

- The next claim, run or refresh of an environment whose ready snapshot
  was built with different secret values rebuilds that snapshot first,
  the same as after a new image push or commit. The claim or run waits
  for the rebuild. An application-wide secret reaches every
  environment; an override reaches its environment and those that
  extend it. Other environments keep their snapshots.
- Copies already running, and runs already under way, keep the values
  they were made with and are not interrupted. That includes a workflow
  reset, and an external test run: each restores the snapshot it
  started on, not the rebuilt one.
- Pull request preview captures and tool layers are not rebuilt by a
  secret change; they keep their baked values until their own next
  rebuild.
- A snapshot is rebuilt when the secret names or values it baked
  differ from the current ones, so renaming a secret rebuilds it. A
  snapshot built before names were recorded rebuilds once, on its
  first use, if its environment has secrets.
- Seeds run again in the rebuild, with the current values. Anything a
  seed created in a third-party system with the old key stays as
  created.
- Claims on the legacy prepare path pick up changes immediately:
  prepare reads the current store, omits removed secrets, and restarts
  `reload_on_prepare` services when values change. Prepare commands,
  actions and `authentication.run` then see those current values.
- A sandbox that is already claimed keeps the values it was prepared
  with until it is destroyed. A pull request preview keeps the values
  of its first prepare (later pushes move it in place without
  preparing again), and is scrubbed of the application's current
  values; a value rotated away since that first prepare is not
  scrubbed.

So set secrets BEFORE the first claim: the first snapshot's seeds run
with whatever is set at that moment. A later change does rebuild the
snapshot and run the seeds again, but the first claim's seeds have
already used the values set then.

The ready snapshot is private to your organization, but the processes
running when it was captured hold the values set when the snapshot was
built, so the snapshot contains them: the snapshot's `prepare.json`
holds the values. Layers built before prepare (platform, toolchain,
repo base) never do.

## What is scrubbed

Every injected value is replaced with `***` in action output and tails,
seed outputs and failed-build messages. Object keys and numeric values
equal to a secret are scrubbed too. The match is exact only:

- a value your app prints transformed (base64, URL-encoded,
  JSON-escaped, split across lines) is not scrubbed;
- a multi-line value (a private key) is scrubbed only when printed
  whole;
- an action's `output` keeps the action's own `sensitive` inputs: only
  injected secrets are scrubbed from it;
- values your app renders into a page are not scrubbed from
  screenshots, traces or page text.

Don't print a secret, and don't render one into a page.
