Skip to content

feat: expose winning-key index via decrypt_indexed for rotation drain observability (LAB-1645) - #73

Merged
27Bslash6 merged 3 commits into
mainfrom
lab-1645-decrypt-indexed
Aug 8, 2026
Merged

feat: expose winning-key index via decrypt_indexed for rotation drain observability (LAB-1645)#73
27Bslash6 merged 3 commits into
mainfrom
lab-1645-decrypt-indexed

Conversation

@27Bslash6

@27Bslash6 27Bslash6 commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Closes LAB-1645.

Problem

During a rotation grace window an operator has no signal for when it is safe to drop a retiring master key: Keyring::decrypt and TenantKeyring::decrypt collapse the result to plaintext-or-error, discarding which entry satisfied the read. "Previous-key hit rate has reached zero" is unobservable, so dropping a key is guesswork — and guessing wrong is a hard cut-over (every pre-rotation entry becomes an error). Raised by CodeRabbit on cachekit-io/cachekit-rs#63 and deferred to core because SDKs must not re-implement keyring attempt logic (LAB-683 decision).

Change

Additive decrypt_indexed on both Keyring and TenantKeyring, returning (Vec<u8>, usize) — the plaintext plus the winning keyring entry index (0 = current key, 1.. = decrypt-only keys in list order). SDKs count non-zero-index reads; when that rate reaches zero the retiring key is drained and safe to drop.

The sequencing loop moved into decrypt_indexed and the existing decrypt on each type became a one-line delegate, so attempt semantics (current-first, identical AAD, only AuthenticationFailed advances, structural/config errors terminal, exhaustion = plain AuthenticationFailed) live in exactly one place per type and cannot drift between the two surfaces. Existing signatures unchanged — plain additive feat: (0.x minor). The new surface carries the index only: no key material, no fingerprint; ZeroizeOnDrop discipline untouched.

Tests

  • test_decrypt_indexed_reports_winning_entry — index 0 on a current-key hit, 1 on a previous-key hit, plaintext identical to decrypt.
  • test_decrypt_indexed_exhaustion_and_terminal_errors_match_decrypt — exhaustion stays plain AuthenticationFailed; InvalidCiphertext / KeyDerivation stay terminal (LAB-683 no-collapse rule).
  • test_tenant_keyring_decrypt_indexed_matches_unbound — same contract on the tenant-bound (SDK steady-state) path.
  • Doc-test on Keyring::decrypt_indexed asserting the drain signal itself (index == 1 for a retiring-key read).

cargo fmt --check, cargo clippy --all-targets --all-features -D warnings, cargo test --all-features (99 unit + 4 doc-tests + integration targets) all green locally.

Review gate

Expert panel (critical-stakes, crypto surface) run pre-PR: bug-hunter, security-specialist, and catchphrase returned no findings; code-craftsman's two findings (runnable doc-test for the drain signal; dedupe the TenantKeyring rustdoc to a cross-ref plus genuine deltas) are applied in the second commit.

Docs

Rustdoc on the new surface states the operator workflow (previous-key hit rate → zero ⇒ safe to drop) with an executable example. No other doc surfaces change: SDK exposure is explicitly out of scope (follow-up children per SDK), no wire/spec behaviour changes, README mentions the keyring only at architecture-diagram level, CHANGELOG is release-please-managed.

Summary by CodeRabbit

  • New Features

    • Added indexed decryption, allowing callers to identify which key successfully decrypted the data.
    • Supports both tenant-bound and unbound keyrings while preserving sequential key handling.
  • Bug Fixes

    • Decryption now continues only for authentication failures and stops immediately for structural or configuration errors.
  • Tests

    • Added coverage for current and previous keys, exhausted keyrings, terminal errors, and consistency across keyring types.

… observability (LAB-1645)

During a rotation grace window an operator has no signal for when it is
safe to drop a retiring master key: Keyring::decrypt and
TenantKeyring::decrypt collapse the result to plaintext-or-error,
discarding which entry satisfied the read, so "previous-key hit rate has
reached zero" is unobservable and dropping a key risks a hard cut-over.

Add decrypt_indexed to both Keyring and TenantKeyring, returning
(plaintext, winning index) with 0 = current key. The sequencing loop
moves into decrypt_indexed and decrypt delegates to it, so attempt
semantics (current-first, identical AAD, only AuthenticationFailed
advances, structural/config errors terminal, exhaustion = plain
AuthenticationFailed) live in exactly one place per type and cannot
drift between the two surfaces. Existing signatures unchanged; the new
surface carries index only — no key material.
…45 panel)

Expert-panel findings: the feature's whole point (non-zero index on a
previous-key read) had no runnable example in a crate where doc-tests
are the executable docs — add one asserting index == 1 for a retiring-
key read. TenantKeyring::decrypt_indexed restated the drain narrative
verbatim; cut to the Keyring cross-ref plus this type's genuine deltas
(no HKDF, no KeyDerivation class) so duplicated prose cannot drift.
@kodus-27b

This comment has been minimized.

@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

You’ve reached a temporary PR review limit under our Fair Usage Limits Policy.

Your recent review volume is higher than typical usage, so adaptive limits are currently applied.

Next review available in: 42 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 45f05938-72e8-47c5-8b41-02bc34e9ba60

📥 Commits

Reviewing files that changed from the base of the PR and between 6cc3975 and 0b900a3.

📒 Files selected for processing (1)
  • src/encryption/keyring.rs

Walkthrough

The change adds indexed decryption to Keyring and TenantKeyring. Existing decryption methods delegate to the indexed methods. Tests cover key selection, error handling, and bound and unbound keyring parity.

Changes

Keyring decryption

Layer / File(s) Summary
Indexed decryption API
src/encryption/keyring.rs
Keyring::decrypt_indexed and TenantKeyring::decrypt_indexed return plaintext with the successful entry index. Existing decrypt methods discard the index. Decryption continues only for authentication failures.
Indexed decryption validation
src/encryption/keyring.rs
Tests cover current and previous keys, authentication exhaustion, terminal errors, and parity between bound and unbound keyrings.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely identifies the new decrypt_indexed API and its purpose for key rotation observability.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch lab-1645-decrypt-indexed

Comment @coderabbitai help to get the list of available commands.

Comment thread src/encryption/keyring.rs

@kodus-27b kodus-27b Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found critical issues please review the requested changes

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/encryption/keyring.rs`:
- Around line 254-271: Update the Keyring doctest to import zeroize::Zeroize,
declare k1, k2, and tenant_key as mutable, and call zeroize() on each after its
final use before scope exit. Preserve the existing encryption, key rotation, and
decrypt_indexed assertions.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: d566c4b2-9499-4d87-8b85-4c54cf1e5304

📥 Commits

Reviewing files that changed from the base of the PR and between d3f0eb0 and 6cc3975.

📒 Files selected for processing (1)
  • src/encryption/keyring.rs

Comment thread src/encryption/keyring.rs
…decrypt_indexed doctest

Keyring::new copies the master keys, so ZeroizeOnDrop clears only the
keyring's copies — the example now wipes the caller-owned buffers after
their last use, modelling the full hygiene a crypto-crate example
should teach.

CodeRabbit-Resolved: src/encryption/keyring.rs:271:Zeroise the doctest key buffers
@kodus-27b

kodus-27b Bot commented Aug 8, 2026

Copy link
Copy Markdown

Kody Review Complete

Great news! 🎉
No issues were found that match your current review configurations.

Keep up the excellent work! 🚀

Kody Guide: Usage and Configuration
Interacting with Kody
  • Request a Review: Ask Kody to review your PR manually by adding a comment with the @kody start-review command at the root of your PR.

  • Validate Business Logic: Ask Kody to validate your code against business rules by adding a comment with the @kody -v business-logic command.

  • Provide Feedback: Help Kody learn and improve by reacting to its comments with a 👍 for helpful suggestions or a 👎 if improvements are needed.

Current Kody Configuration
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug
Performance
Security
Business Logic

Access your configuration settings here.

@27Bslash6

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor
⚠️ Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@27Bslash6
27Bslash6 merged commit d834f74 into main Aug 8, 2026
52 of 53 checks passed
@27Bslash6
27Bslash6 deleted the lab-1645-decrypt-indexed branch August 8, 2026 02:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant