pub struct SecretManager { /* private fields */ }Expand description
Secret manager combining cached and per-request encryption.
Implementations§
Source§impl SecretManager
impl SecretManager
Sourcepub fn new(provider: Arc<dyn BaseKmsProvider>, default_key_id: String) -> Self
pub fn new(provider: Arc<dyn BaseKmsProvider>, default_key_id: String) -> Self
Create a new secret manager.
Sourcepub fn with_context_prefix(self, prefix: String) -> Self
pub fn with_context_prefix(self, prefix: String) -> Self
Set a context prefix (e.g., “fraiseql-prod”).
This prefix is added to all encryption contexts for additional isolation between environments.
Sourcepub async fn initialize(&self) -> KmsResult<()>
pub async fn initialize(&self) -> KmsResult<()>
Initialize by generating and caching a data key.
Call this at application startup. The data key is cached in memory for fast local encryption during the application’s lifetime.
§Errors
Returns KmsError if data key generation fails
Sourcepub async fn is_initialized(&self) -> bool
pub async fn is_initialized(&self) -> bool
Check if a data key is cached.
Sourcepub async fn rotate_cached_key(&self) -> KmsResult<()>
pub async fn rotate_cached_key(&self) -> KmsResult<()>
Rotate the cached data key.
Call this periodically to rotate keys. This regenerates the cached data key via KMS while maintaining application uptime.
§Errors
Returns KmsError if rotation fails
Sourcepub async fn local_encrypt(&self, plaintext: &[u8]) -> KmsResult<Vec<u8>>
pub async fn local_encrypt(&self, plaintext: &[u8]) -> KmsResult<Vec<u8>>
Encrypt data using the cached data key (NO KMS call).
This is fast (~microseconds) and safe for use in hot paths.
Requires initialize() to be called first.
§Errors
Returns KmsError::EncryptionFailed if not initialized or encryption fails
Sourcepub async fn local_decrypt(&self, encrypted: &[u8]) -> KmsResult<Vec<u8>>
pub async fn local_decrypt(&self, encrypted: &[u8]) -> KmsResult<Vec<u8>>
Decrypt data using the cached data key (NO KMS call).
§Errors
Returns KmsError::DecryptionFailed if not initialized or decryption fails
Sourcepub async fn encrypt(
&self,
plaintext: &[u8],
key_id: Option<&str>,
) -> KmsResult<EncryptedData>
pub async fn encrypt( &self, plaintext: &[u8], key_id: Option<&str>, ) -> KmsResult<EncryptedData>
Encrypt data using KMS (per-request operation).
This contacts the KMS provider for each encryption, providing per-request key isolation but with higher latency (50-200ms). Use for secrets management, not response data.
§Arguments
plaintext- Data to encryptkey_id- KMS key identifier (or None for default)
§Errors
Returns KmsError if encryption fails
Sourcepub async fn decrypt(&self, encrypted: &EncryptedData) -> KmsResult<Vec<u8>>
pub async fn decrypt(&self, encrypted: &EncryptedData) -> KmsResult<Vec<u8>>
Decrypt data using KMS (per-request operation).
Auto-detects the correct provider from EncryptedData metadata.
§Errors
Returns KmsError if decryption fails
Sourcepub async fn encrypt_string(
&self,
plaintext: &str,
key_id: Option<&str>,
) -> KmsResult<EncryptedData>
pub async fn encrypt_string( &self, plaintext: &str, key_id: Option<&str>, ) -> KmsResult<EncryptedData>
Encrypt a string field (convenience method).
Handles UTF-8 encoding/decoding automatically.
Sourcepub async fn decrypt_string(
&self,
encrypted: &EncryptedData,
) -> KmsResult<String>
pub async fn decrypt_string( &self, encrypted: &EncryptedData, ) -> KmsResult<String>
Decrypt a string field.
Auto Trait Implementations§
impl Freeze for SecretManager
impl !RefUnwindSafe for SecretManager
impl Send for SecretManager
impl Sync for SecretManager
impl Unpin for SecretManager
impl !UnwindSafe for SecretManager
Blanket Implementations§
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> 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> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request