Skip to content

Environments and grants

An environment is a set of secrets for one stage of an app, named app/environment, such as webapp/staging. A grant lets one workload fetch from one environment: all of its secrets, or only some names. Both live in your deployment repository, typed against the engine’s schema in portona/config, and reach the Worker in the PORTONA_ACCESS binding.

import type { Access } from "portona/config"
const deploy = {
issuer: "github-actions",
claims: {
repository_owner_id: "1234567",
repository_id: "89012345",
ref: "refs/heads/main",
environment: "production",
workflow_ref: "outlearn/webapp/.github/workflows/deploy.yml@refs/heads/main",
event_name: "push",
},
} as const
export const access = {
environments: {
// Generate each ID once and never reuse it. Never reuse a name either:
// stored secrets are still keyed by name (see below).
"webapp/staging": { id: "env_3k9q2m7x1c" },
"webapp/production": { id: "env_8f2w6n4z0d", protected: true },
},
grants: {
"gh-webapp-deploy": {
environment: "webapp/production",
principal: deploy,
permission: "fetch",
names: ["TWILIO_ACCOUNT_SID", "TWILIO_AUTH_TOKEN"],
note: "Pushes Twilio credentials into the production Worker on deploy",
},
"amp-webapp-graph": {
environment: "webapp/staging",
principal: {
issuer: "amp",
claims: { user_id: "user_…", project_id: "project-…", token_use: "exchanged" },
},
permission: "fetch",
names: ["GRAPH_CLIENT_SECRET"],
expires: "2027-01-31T00:00:00Z",
note: "Until the Entra federation covers staging",
},
},
} satisfies Access

Pass it with PORTONA_ACCESS: bindings.json(access) in cloudflare.config.ts.

  • Names are app/environment: lowercase letters, digits and hyphens on each side.

  • IDs are env_ and 8 to 32 lowercase letters or digits, unique in the deployment. Generate one when you create the environment, and never reuse it, even after retiring it. The audit log records it with each exchange.

  • The ledger pins each name to its ID. Stored secrets and federations still belong to the environment’s name. So that a renamed or recreated environment can’t pick up another’s secrets, the Worker records the first ID it sees for each name, at its first exchange, admin change or daily cron, and refuses exchanges and admin changes (environment_mismatch) for an environment that disagrees with that record:

    • Renamed: the configuration gives an ID another name. An ID can’t move: rename it back, or give the environment a new ID.
    • Recreated: the configuration gives a name a new ID while the ledger holds it under the old one. Retire the name to let the old secrets go.
    • Retired: the ID was retired. Give the environment a new one.

    The daily cron fails, naming each conflict, so a mistaken deploy shows within a day even if nothing uses the environment. PORTONA_POLICIES environments have no ID and aren’t in the ledger.

  • Production is marked protected. A name ending in /production must set protected: true. In 0.4 the flag changes nothing else, since every grant lives in git; later releases will keep protected environments’ grants in git only when grants can be made elsewhere.

Retiring deletes a name’s secrets and federations and frees the name for a new ID. Check on the admin page which secrets it holds, then:

Terminal window
bunx --no-install portona-ledger retire webapp/staging --by you@example.com > retire.json
bunx --no-install cf d1 query <database-id> --batch @retire.json

The batch runs as one transaction: it adds a retire audit row naming the secrets let go, deletes them and the name’s federations, and marks the ledger row retired. It fails and changes nothing if the ledger holds no live row for the name. Restores skip secrets and federations written before their name was retired, so a backup can’t bring them back; set any value the new environment needs again.

Field Meaning
key The grant’s ID: lowercase letters, digits and hyphens. Recorded in the audit log; never reuse.
environment A configured environment.
principal Who may fetch: an issuer and the claims its tokens must match (see Issuers).
permission fetch, the only permission a workload can have.
names Optional. Only these secrets; without it, the whole environment.
expires Optional. A full UTC instant, such as 2027-01-31T00:00:00Z. From then the grant admits nothing.
note Required. Why the grant exists, for access reviews.

A request names an environment and, optionally, up to 64 secret names.

  1. Portona collects every grant to that environment whose principal the token matches and which hasn’t expired. Grants add up: a narrower or expired grant never takes away what another allows.
  2. With names, each name must be allowed by one of those grants and must exist. If any one isn’t, the whole request is refused, before anything is decrypted.
  3. Without names, one of them must cover the whole environment. A narrowed grant never falls back to everything.
  4. Only the allowed names are decrypted and returned. The audit row records them, the environment’s ID, the issuer and the grants used.

Every refusal gets the same 403. The audit log records why:

Outcome Meaning
unknown_bundle No such environment.
no_grant The environment has no grants.
policy_mismatch[:claim] The token matches no grant. With one grant, names the failing claim.
grant_expired:<id> Every grant the token matches has expired.
names_required No names were asked for, and no matching grant covers everything.
name_not_granted:<name> A name no matching grant allows.
name_missing:<name> A granted name with no secret.

The original form still works, alone or beside PORTONA_ACCESS. Each entry is an environment with no ID and one environment-wide grant, recorded as policy:<name>:

import type { Policies } from "portona/config"
export const policies = {
"outlearn/sync": {
issuer: "amp",
claims: { user_id: "user_…", project_id: "project-…", token_use: "exchanged" },
},
} satisfies Policies

A name can’t be in both bindings. Move an entry to PORTONA_ACCESS by removing it from PORTONA_POLICIES in the same commit.

amp github-actions
Tokens from amp orb id-token in an Amp orb A GitHub Actions job with permissions: id-token: write
A grant pins user_id, project_id, token_use: "exchanged" repository_owner_id, repository_id, ref, environment, workflow_ref, event_name

Both issuers’ tokens are checked the same way: RS256 only, the issuer’s own keys, the deployment’s audience, at most a one-hour lifetime with 60 seconds of skew, and single use. A token from any other issuer is refused.

  • Every claim is required. Repository and owner are pinned by immutable ID, so a renamed, transferred or re-registered repository doesn’t match. Find them with gh api repos/OWNER/REPO --jq '.id, .owner.id'.
  • event_name is push or workflow_dispatch. Pull request events, pull_request_target included, can never match.
  • workflow_ref pins the workflow file at the branch, so another workflow in the same repository can’t use the grant.
  • Reusable workflows are refused. A job whose job_workflow_ref differs from its workflow_ref runs code from another workflow file, so Portona refuses it before any grant runs.
  • Portona can’t see your GitHub settings. Protect the pinned branch, add required reviewers to production environments where your plan allows, and give id-token: write only to the job that needs the secrets. Keep untrusted code, inputs and artifacts out of that job.
  • Every claim must match exactly. For Amp, user_id and project_id are required, so a grant can never admit every Amp user or every project, and token_use must be exchanged. A token from another issuer than the principal’s never matches.
  • Immutable ids only. Grants never match on email or workspace_id, which can change or be shared.
  • Strict decoding. An unknown key is an error, not ignored, so a misspelt claim or field fails the build instead of silently dropping a check. bun check catches it in the editor, portona-assemble refuses it at build time, and the Worker refuses every exchange if its access configuration doesn’t decode.

Environments and grants change only through a reviewed commit and a deploy, never from the admin page.