#122 Overlapping dual-key secret rotation with grace period
Originally created by: bosd
Add [#120] rotation: a secret keeps its replaced value readable for a configurable grace period so consumers can pick up the new credential without downtime.
- Migration 0019 adds six nullable previous_* columns; each slot keeps its own version and AAD, so rotation and promotion never re-encrypt.
- POST .../secrets/{key}/rotate stores the new value in the free slot and sets the retire deadline (graceSeconds, default 24h, 0..30d, 422 SECRET_GRACE_INVALID outside the range).
- POST .../secrets/{key}/promote swaps the two slots back; the demoted value retires at the original deadline. A secret without a live previous value returns 404 SECRET_PREVIOUS_NOT_FOUND.
- Retirement stays lazy on access and writes are committed, so the secret.retired audit event survives failed operations.
- Secret metadata gains previousVersion/previousExpiresAt, and reveal accepts includePrevious for verification tooling.
- CLI: dopbase secret rotate (grace, stdin), dopbase secret promote (--yes), and dopbase secret get --reveal --include-previous.
- Overwriting a secret (set or import) clears the previous slot.
Docs, changelog, and tests for grace parsing, rotation, promotion, lazy retirement, and audit events.
Summary
Implements [#120]: overlapping (dual-key) secret rotation.
A secret now keeps the value it replaced readable for a grace period, so consumers adopt the new credential at their own pace instead of at a coordinated cutover. Runtime reads keep returning the current value only.
- Storage — migration 0019_secret_rotation adds six nullable previous_* columns to secrets. Each slot keeps the version and AAD it was encrypted with, so rotation and promotion never re-encrypt anything, and existing secrets keep no previous value.
- Rotate — POST /api/v1/environments/{id}/secrets/{key}/rotate stores the new value in the free slot and records the retire deadline. graceSeconds defaults to 24h, accepts 0 up to 30d, and returns 422 SECRET_GRACE_INVALID outside that range; 404 SECRET_NOT_FOUND when the secret does not exist. --grace 0 retires the previous value immediately.
- Promote — POST /api/v1/environments/{id}/secrets/{key}/promote swaps the current and previous slots back. The demoted value retires at the original deadline recorded by the rotation. A secret without a live previous value returns 404 SECRET_PREVIOUS_NOT_FOUND.
- Retirement — lazy on access, no background sweeper. Retirement writes are committed so the secret.retired audit event survives a later failure (export commits its transaction, promote commits before returning its 404).
- Metadata and reveal — secret metadata and listings gain previousVersion / previousExpiresAt (values never included). GET .../reveal?includePrevious=true returns the previous value with its retire deadline for verification tooling; without the parameter the response keeps its original shape.
- Overwrite ends a rotation — secret set and imports clear the previous slot, so only the stored value stays valid.
- CLI — dopbase secret rotate <env> <key> [--grace 24h] [--stdin], dopbase secret promote <env> <key> [--yes], and dopbase secret get <env> <key> --reveal --include-previous.</key></env></key></env></key></env>
- Audit — new secret.rotated, secret.promoted, secret.retired events naming versions and deadlines, never a value.
- Docs — CLI reference and cheat sheet, secrets guide, API reference, audit-event reference, and CHANGELOG.md under 0.1.10 - Unreleased.
Out of scope per the issue: provider-specific rotators and rotation hooks.
Compatibility
No breaking changes. New fields and endpoints are additive: older clients ignore previousVersion / previousExpiresAt / previous on reveal, and graceSeconds defaults server-side.
- Running the 0019_secret_rotation migration is required on upgrade; down reverts it and drops the previous values.
- Existing secrets start with no previous value; nothing is rotated until an operator runs secret rotate.
- Runtime reads, runner tokens, and exports keep returning current values only.
Verification
- cargo fmt --check
- cargo clippy --locked --all-targets --all-features -- -D warnings
- cargo test --locked --test main --test constants --test services (26 / 4 / 13 passing, includes new rotation, promotion, lazy-retirement, overwrite, grace-parsing, and route tests)
- cargo test --locked --test cli (205 passing; the 3 commands::export::* failures are pre-existing in this environment — the util-linux script binary is missing, and they fail the same way on a clean main)
- bun run test:docs, bun run test:github
- bunx prettier --check on the changed Markdown files
Not run locally: bun run build:ui fails before this change too (vue-tsc "Failed to locate tsc module path from shim" with the local bun/lockfile combination). The Admin UI is out of scope and its TypeScript types treat the new response fields as optional.
Checklist
- [x] I kept this pull request focused on one problem.
- [x] I added or updated tests for behavior changes, or explained why tests are not needed.
- [x] I updated the documentation and
CHANGELOG.mdwhen public behavior changed, or explained why they are not needed. - [x] I updated relevan documentation inside
/docs
Closes [#120]
Related
Tickets: #120
Tickets: #123
Tickets: #128
Tickets: #131
Tickets: #33
Originally posted by: sonarqubecloud[bot]
Failed conditions
3.9% Duplication on New Code (required ≤ 3%)
See analysis details on SonarQube Cloud
Ticket changed by: dopbase