pub struct KeyRotator<B: SecretRotationBackend, E: KeyEncryptor + Clone, const V: usize = 256, const S: usize = 32> { /* private fields */ }Expand description
Background task that periodically generates and persists a new encryption key.
KeyRotator is the write side of the key-management system. It runs a single
perpetual loop: sleep until the current key is due for rotation, generate a new key,
encrypt it, and attempt a conditional insert via SecretRotationBackend::try_insert_key.
If another instance raced ahead the insert is skipped and the loop simply sleeps until the
new key expires.
Multiple KeyRotator instances for the same group_id can run concurrently (e.g. for
high availability); the optimistic-locking protocol in try_insert_key ensures only one
insert succeeds per rotation cycle.
§Type parameters
B— backend that implementsSecretRotationBackendE— encryptor that implementsKeyEncryptorV— ring buffer size (number of key slots, must be ≤ 256, default 256). Must match theVof anyInMemorySecretGroupconsuming the keys.S— key size in bytes (default 32)
§Standalone use
KeyRotator can be used without a SecretSyncer or
SecretManager. This is useful when you want a dedicated
rotation service that writes to shared storage while other nodes only read:
let rotator: KeyRotator<_, _, 256, 32> = KeyRotator::new(
"session-tokens",
backend,
Duration::from_secs(3600),
Duration::from_secs(30),
encryptor,
|| [0u8; 32],
);
rotator.run(CancellationToken::new()).await;Implementations§
Source§impl<B: SecretRotationBackend, E: KeyEncryptor + Clone, const V: usize, const S: usize> KeyRotator<B, E, V, S>
impl<B: SecretRotationBackend, E: KeyEncryptor + Clone, const V: usize, const S: usize> KeyRotator<B, E, V, S>
Sourcepub fn new(
group_id: impl Into<String>,
backend: B,
rotation_interval: Duration,
propagation_delay: Duration,
encryptor: E,
generate_key: impl Fn() -> [u8; S] + Send + Sync + 'static,
) -> Self
pub fn new( group_id: impl Into<String>, backend: B, rotation_interval: Duration, propagation_delay: Duration, encryptor: E, generate_key: impl Fn() -> [u8; S] + Send + Sync + 'static, ) -> Self
Create a new KeyRotator.
§Arguments
group_id— identifies the logical key group in storagebackend— implementsSecretRotationBackendrotation_interval— how long a key is valid before a new one is generatedpropagation_delay— added toSystemTime::now()to computeactivated_atfor the new key, giving syncers time to pull the key before it becomes activeencryptor— wraps key bytes before storagegenerate_key— produces fresh key material; defaults inSecretManagerto a CSPRNG fill
§Panics
Panics at compile time if V > 256 (versions are stored as u8).
Sourcepub async fn run(self, token: CancellationToken)
pub async fn run(self, token: CancellationToken)
Run the rotation loop until token is cancelled.
This method consumes self and runs forever, sleeping between rotations. Pass the
returned future to tokio::spawn or run it directly. Cancel token for a clean
shutdown; the loop exits after the current sleep or retry delay completes.
On backend or encryption errors the rotator backs off for 30 seconds before retrying, so transient failures do not cause a tight error loop.
Auto Trait Implementations§
impl<B, E, const V: usize, const S: usize> Freeze for KeyRotator<B, E, V, S>
impl<B, E, const V: usize = 256, const S: usize = 32> !RefUnwindSafe for KeyRotator<B, E, V, S>
impl<B, E, const V: usize, const S: usize> Send for KeyRotator<B, E, V, S>
impl<B, E, const V: usize, const S: usize> Sync for KeyRotator<B, E, V, S>
impl<B, E, const V: usize, const S: usize> Unpin for KeyRotator<B, E, V, S>
impl<B, E, const V: usize, const S: usize> UnsafeUnpin for KeyRotator<B, E, V, S>where
B: UnsafeUnpin,
E: UnsafeUnpin,
impl<B, E, const V: usize = 256, const S: usize = 32> !UnwindSafe for KeyRotator<B, E, V, S>
Blanket Implementations§
Source§impl<T> AggregateExpressionMethods for T
impl<T> AggregateExpressionMethods for T
Source§fn aggregate_distinct(self) -> Self::Outputwhere
Self: DistinctDsl,
fn aggregate_distinct(self) -> Self::Outputwhere
Self: DistinctDsl,
DISTINCT modifier for aggregate functions Read moreSource§fn aggregate_all(self) -> Self::Outputwhere
Self: AllDsl,
fn aggregate_all(self) -> Self::Outputwhere
Self: AllDsl,
ALL modifier for aggregate functions Read moreSource§fn aggregate_filter<P>(self, f: P) -> Self::Output
fn aggregate_filter<P>(self, f: P) -> Self::Output
Source§fn aggregate_order<O>(self, o: O) -> Self::Outputwhere
Self: OrderAggregateDsl<O>,
fn aggregate_order<O>(self, o: O) -> Self::Outputwhere
Self: OrderAggregateDsl<O>,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoSql for T
impl<T> IntoSql for T
Source§fn into_sql<T>(self) -> Self::Expression
fn into_sql<T>(self) -> Self::Expression
self to an expression for Diesel’s query builder. Read moreSource§fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
&self to an expression for Diesel’s query builder. Read moreSource§impl<T, Conn> RunQueryDsl<Conn> for T
impl<T, Conn> RunQueryDsl<Conn> for T
Source§fn execute<'conn, 'query>(
self,
conn: &'conn mut Conn,
) -> <Conn as AsyncConnectionCore>::ExecuteFuture<'conn, 'query>
fn execute<'conn, 'query>( self, conn: &'conn mut Conn, ) -> <Conn as AsyncConnectionCore>::ExecuteFuture<'conn, 'query>
Source§fn load<'query, 'conn, U>(
self,
conn: &'conn mut Conn,
) -> AndThen<Self::LoadFuture<'conn>, TryCollect<Self::Stream<'conn>, Vec<U>>>
fn load<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> AndThen<Self::LoadFuture<'conn>, TryCollect<Self::Stream<'conn>, Vec<U>>>
Source§fn load_stream<'conn, 'query, U>(
self,
conn: &'conn mut Conn,
) -> Self::LoadFuture<'conn>where
Conn: AsyncConnectionCore,
U: 'conn,
Self: LoadQuery<'query, Conn, U> + 'query,
fn load_stream<'conn, 'query, U>(
self,
conn: &'conn mut Conn,
) -> Self::LoadFuture<'conn>where
Conn: AsyncConnectionCore,
U: 'conn,
Self: LoadQuery<'query, Conn, U> + 'query,
Stream] with the returned rows. Read moreSource§fn get_result<'query, 'conn, U>(
self,
conn: &'conn mut Conn,
) -> AndThen<Self::LoadFuture<'conn>, LoadNext<Pin<Box<Self::Stream<'conn>>>>>
fn get_result<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> AndThen<Self::LoadFuture<'conn>, LoadNext<Pin<Box<Self::Stream<'conn>>>>>
Source§fn get_results<'query, 'conn, U>(
self,
conn: &'conn mut Conn,
) -> AndThen<Self::LoadFuture<'conn>, TryCollect<Self::Stream<'conn>, Vec<U>>>
fn get_results<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> AndThen<Self::LoadFuture<'conn>, TryCollect<Self::Stream<'conn>, Vec<U>>>
Vec with the affected rows. Read more