pub struct KeyManager { /* private fields */ }Expand description
Manages encryption keys for secret storage.
The KeyManager handles key discovery, generation, and persistence
with automatic fallback through multiple sources.
§Example
use zlayer_secrets::KeyManager;
let manager = KeyManager::new();
let key = manager.get_or_create_key("production").unwrap();Implementations§
Source§impl KeyManager
impl KeyManager
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new KeyManager with the default secrets directory.
The default directory is determined by zlayer_paths::ZLayerDirs::system_default().
Sourcepub fn with_base_dir(base_dir: impl AsRef<Path>) -> Self
pub fn with_base_dir(base_dir: impl AsRef<Path>) -> Self
Creates a new KeyManager with a custom base directory.
§Arguments
base_dir- Path to the directory for storing key files
Sourcepub fn get_or_create_key(&self, deployment: &str) -> Result<EncryptionKey>
pub fn get_or_create_key(&self, deployment: &str) -> Result<EncryptionKey>
Gets or creates an encryption key for the specified deployment.
Attempts to obtain a key through the following priority chain:
-
Environment key: If
ZLAYER_SECRETS_KEYis set, decodes the hex-encoded 32-byte key directly. -
Password derivation: If
ZLAYER_SECRETS_PASSWORDis set, derives a key using Argon2id with the deployment name as salt. -
File-based key: Loads the key from the deployment’s key file if it exists at
{base_dir}/secrets_{deployment}.key. -
Auto-generation: Generates a new random key and saves it to the key file with restricted permissions (0600 on Unix).
§Arguments
deployment- The deployment name used for key file naming and password salt derivation
§Errors
Returns SecretsError::Encryption if:
- The hex-encoded key in
ZLAYER_SECRETS_KEYis invalid - Key file I/O operations fail
- Password derivation fails
§Example
use zlayer_secrets::KeyManager;
let manager = KeyManager::new();
// First call: generates and saves key
let key = manager.get_or_create_key("production").unwrap();
// Subsequent calls: loads from file
let same_key = manager.get_or_create_key("production").unwrap();
assert_eq!(key.as_bytes(), same_key.as_bytes());Trait Implementations§
Source§impl Clone for KeyManager
impl Clone for KeyManager
Source§fn clone(&self) -> KeyManager
fn clone(&self) -> KeyManager
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for KeyManager
impl Debug for KeyManager
Auto Trait Implementations§
impl Freeze for KeyManager
impl RefUnwindSafe for KeyManager
impl Send for KeyManager
impl Sync for KeyManager
impl Unpin for KeyManager
impl UnsafeUnpin for KeyManager
impl UnwindSafe for KeyManager
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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 more