Add collision-safe KeyRing rename API - #144
Conversation
|
Thank you for the contribution! Appreciate it. I think the semantics mostly make sense, but the type output should really be a Result here. A boolean made sense when we expected the user to hold the interface right - if there's multiple ways to fail, however, then a Result communicates the full range of behaviours. Here is my request for how to do this:
If new key is same as old key, we return an Let me know what you think! |
|
Makes sense, thanks! I’ll keep Quick question on an edge case: if |
|
Apply an order of precedence:
|
|
and I don't think we need to have rename_key as a wrapper around try_rename_key, especially since we're marking it deprecated. Just leave a comment explaining it doesn't guarantee renaming conflicts. I'll cut a new version later. |
|
Just pushed the updates in
|
|
🚀 @AkshatM |
1. Purpose
KeyRing::rename_keyreturns a boolean, so callers cannot distinguish a missing source key from an occupied destination. Its legacy remove-then-insert behavior can also replace the destination before reporting failure.This PR adds a fallible rename API that reports each failure explicitly while retaining the existing method for compatibility.
2. Implementation
crates/web-bot-auth/src/keyring.rsadds:OperationError::KeyNotPresentwhen the source identifier is absent.OperationError::KeyOccupiedwhen the destination identifier is already present.try_rename_key, returningResult<(), OperationError>.try_rename_keychecks source absence first, then destination occupancy, then identifier equality. Otherwise it removes the existingKeyEntryand assigns it to the new identifier.The existing
rename_keyimplementation remains independent and is deprecated since0.7.1, with a warning that it does not safely handle destination conflicts.3. Behavior
The new API behaves as follows:
Err(OperationError::KeyNotPresent).Err(OperationError::KeyOccupied)and preserves both keys.KeyEntry, including its prepared verification state.The legacy
rename_keysignature and behavior remain available during the deprecation period.4. Tests
The Rust tests cover:
KeyOccupiedresult.KeyNotPresenttaking precedence when the source is absent.Internal test call sites use
try_rename_key, avoiding deprecation warnings.5. Review Guide
Review
crates/web-bot-auth/src/keyring.rs:try_rename_key.rename_keyretains its legacy implementation and records0.7.1.Review
crates/web-bot-auth/src/message_signatures.rs:try_rename_key.6. Risk
The new API is additive. The existing method remains callable with unchanged behavior, but now emits a deprecation warning.
This PR does not change key import, lookup, JWK handling, prepared-key construction, or signature verification.
7. Validation
The following commands passed:
cargo test -p web-bot-auth --all-features(41 tests plus doc-tests)cargo clippy -p web-bot-auth --all-features --all-targets -- -D warningscargo fmt --all -- --checkgit diff --check