feat(crypto): zero-downtime key rotation — RotatingKeyManager (Step 6.7d) - #158
Merged
Merged
Conversation
….7d) Close Step 6.7. RotatingKeyManager (rag-core, a crypto-free KeyManager decorator) makes rotating a tenant's KEK seamless: encrypt always uses the current key; decrypt tries the current key then each non-expired retired key (RetiredKey(key_manager, expires_at)). The try-all decrypt is safe — AES-GCM authenticates the DEK, so a wrong KEK can't yield a valid key (only the KEK that wrapped a blob decrypts it). rewrap(ctx, ct) is the background-migration primitive (decrypt with whatever key still works → re-encrypt under the current key); once all blobs are migrated a retired key is dropped. An expired retired key is skipped, so old un-rewrapped data is sealed (EncryptionError) with no impact on current-key data. Injectable clock for deterministic expiry tests. Composes with every provider (local + all four cloud KMS) since it orchestrates only the KeyManager SPI. ragctl kms --rotate demos the full flow. Scope: the rotation mechanism; config-driven per-tenant rotation + the storage re-encryption job land with the EncryptingStorage ingest wiring (tiered storage). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes Step 6.7 — BYOK with zero-downtime KEK rotation.
RotatingKeyManager(rag-core, a crypto-freeKeyManagerdecorator): encrypt always uses the current key; decrypt tries the current key then each non-expired retired key (RetiredKey(key_manager, expires_at)). The try-all decrypt is safe, not a guess — AES-GCM authenticates the DEK, so a wrong KEK can't yield a valid key (only the KEK that wrapped a blob decrypts it).rewrap(ctx, ct)is the background-migration primitive (decrypt with whatever key still works → re-encrypt under the current key); once all blobs are migrated a retired key is dropped.EncryptionError) — "retain decrypt-only until expiry" — with no impact on current-key data. Injectableclockfor deterministic expiry tests.KeyManagerSPI.ragctl kms --rotatedemos the full flow.Scope / deferred: the rotation mechanism (satisfies the planning rotation acceptance — rotate → old + new both decrypt; expired key seals old data). Config-driven per-tenant multi-generation rotation + the storage-side background re-encryption job land with the
EncryptingStorageingest wiring (tiered storage).This completes Step 6.7 (6.7a library + 6.7b config/factory/AWS + 6.7c GCP/Azure/Vault + 6.7d rotation); Phase 6 → 7/10.
Tests & gates
tests/kms/test_rotation.py): rotate → old+new both decrypt;rewrapmigration; expired-key sealing; tamper; no-key sealing; multi-generation; clock-controlled expiry; health. Plusragctl kms --rotate.tests/ packages/2372 passed, 1 skipped; gateway 290 (unchanged). Green: ruff,mypy --strict(310 files), RAG001, policy-coverage, log-schema; no dist drift (rag-core-only, no config/types/endpoints).Documentation
Test plan
uv run pytest tests/ packages/ -m "not integration"— 2372 passed, 1 skippeduv run pytest apps/gateway/tests— 290 passedmypy --strict(310) + RAG001 + policy-coverage + log-schema; no dist drift🤖 Generated with Claude Code