Chapter DSecrets rotation labPage 7 of 8

Secrets rotation lab

Constrain access and rehearse rollback

Build an answer API and background indexer sharing a provider credential through a versioned secrets manager as an operable release, not a slide-deck example.

~14 minSecurity and rollback

1Try it yourself

Playground

Secrets rotation lab

Revoke leaked keys, rotate on schedule, audit access.

API key found in a public gist

Before you start

Why this matters

Before changing code, write the single production outcome this chapter must prove and the signal that would stop you. For this lab, the service boundary is SecretRef(name, version) is injected at runtime; health telemetry reports only credential_version and authentication result, never secret bytes. Record one request identifier you can follow from ingress through the final decision. If you cannot name the owner of the stop decision, the rollout is not yet controlled.

The source lesson requires a dual-key window: issue v2, deploy readers, confirm traffic, then revoke v1 and audit every read. This chapter turns that compact lesson into implementation evidence. The running scenario is an answer API and background indexer sharing a provider credential through a versioned secrets manager. You will keep the same scenario across all eight chapters so setup decisions, tests, telemetry, and rollback controls accumulate into one coherent system rather than eight disconnected exercises.

2Learn the idea

Read

Draw the trust boundaries

Mark user input, application runtime, configuration service, external provider, durable store, and operator console. At each crossing, specify authentication, authorization, validation, encryption, and audit evidence. The system’s concrete policy is: never print secret values, use workload identity to read one named secret, separate issuer and deployer roles, and retain immutable audit events. Test that policy with a negative request, not only a configuration screenshot.

Secrets enter at runtime through a secrets manager or workload identity. They never appear in source, fixtures, process arguments, telemetry, or exception bodies. Scope credentials to the smallest resource and operation. Production configuration writes require role-based access, review for high-risk changes, and an immutable record with actor and reason.

Read

Defend the data path

Set body and field-size limits before parsing expensive input. Normalize only after signature or identity checks where raw bytes are security-relevant. Treat retrieved documents, webhook payloads, and tool responses as untrusted data. Apply output schemas and explicit allowlists before any side effect. For customer data, minimize collection, define retention, and verify deletion reaches logs or indexes where promised.

Build abuse tests for replay, cross-tenant identifiers, prompt injection, forged configuration, stale credentials, and denial-of-service through expensive requests. Assert both response and absence of side effects. A 403 that still performed a provider call is a security and cost bug.

Read

Rehearse rollback safely

Execute: during the grace window restore v1 as current; after compromise, do not reactivate v1—repair the failed consumer with a newly issued v3. Use a two-person checklist for production-like drills: announce start, snapshot current revision and metrics, apply the control, verify safe behavior, reconcile in-flight work, and announce completion. Ensure rollback permissions are available to on-call staff without granting broad routine admin access.

Distinguish rollback from evidence destruction. Keep the failed artifact, configuration digest, audit events, and redacted traces under retention policy. If compromise is possible, rotate credentials and isolate the revision before analysis. Never reactivate a known-compromised secret merely because it restores service.

Read

Operational handoff

The runbook must name owner, escalation path, safe-mode behavior, exact control, verification query, and conditions for re-enabling. Run it with someone who did not write the code. Measure completion time and note any undocumented access. The chapter passes only when least privilege, negative tests, audited changes, and a timed rollback drill all produce evidence.

Read

Test least privilege and denial

Separate runtime and operator permissions. The application receives one capability, while the operator role owns the audited recovery action. Security invariant: Runtime can read one secret but cannot list, issue, or revoke versions.

roles:
  runtime:
    principal: answer-api-workload
    allow:
      - secrets:read/provider-api-key
    deny:
      - admin:*
  break_glass_operator:
    allow:
      - secrets:read/provider-api-key
    conditions:
      reason_required: true
      approval_count: 2
      ttl_minutes: 30

Run a negative probe before the positive path. The exact status may be 401 or 403 by contract, but no downstream call or durable mutation may occur. Audit output names the decision, never credentials or payload contents.

$ curl -si -H 'Authorization: Bearer invalid-fixture' http://localhost:8000/ready
HTTP/1.1 401 Unauthorized
$ printf '%s\n' '{"decision":"deny","principal":"unknown","side_effects":0}'
{"decision":"deny","principal":"unknown","side_effects":0}

If side_effects is nonzero, move authorization ahead of retrieval, queue publication, or provider calls; changing only the status code is not a fix. If runtime unexpectedly has admin rights, inspect role inheritance and workload identity binding. Record actor, reason, approvals, prior provider-key-v2, and expiry during rollback. A break-glass token still valid after the drill is a failed security test.

Checking tutor…

Continue learning · glossary & guides