pub trait KeyManager:
Send
+ Sync
+ 'static {
// Required methods
fn encrypt(
&self,
key_id: &String,
data: Vec<u8>,
) -> Pin<Box<dyn Future<Output = Result<Vec<u8>>>>>;
fn decrypt(
&self,
key_id: &String,
data: Vec<u8>,
) -> Pin<Box<dyn Future<Output = Result<Vec<u8>>>>>;
fn create_key(&self) -> Pin<Box<dyn Future<Output = Result<String>>>>;
fn delete_key(
&self,
key_id: &String,
) -> Pin<Box<dyn Future<Output = Result<()>>>>;
// Provided methods
fn begin_rotation<'a>(
&'a self,
key_id: &String,
) -> Pin<Box<dyn Future<Output = Result<Rotation>> + 'a>> { ... }
fn complete_rotation<'a>(
&'a self,
handle: Rotation,
) -> Pin<Box<dyn Future<Output = Result<()>> + 'a>> { ... }
}Expand description
Core trait for key management operations.
This trait defines the interface for a key management service, providing methods for:
- Encrypting and decrypting data
- Creating and deleting encryption keys
- Rotating keys safely
Implementations of this trait should handle the underlying cryptographic operations and key management details for specific KMS providers.
Required Methods§
Sourcefn encrypt(
&self,
key_id: &String,
data: Vec<u8>,
) -> Pin<Box<dyn Future<Output = Result<Vec<u8>>>>>
fn encrypt( &self, key_id: &String, data: Vec<u8>, ) -> Pin<Box<dyn Future<Output = Result<Vec<u8>>>>>
Sourcefn decrypt(
&self,
key_id: &String,
data: Vec<u8>,
) -> Pin<Box<dyn Future<Output = Result<Vec<u8>>>>>
fn decrypt( &self, key_id: &String, data: Vec<u8>, ) -> Pin<Box<dyn Future<Output = Result<Vec<u8>>>>>
Provided Methods§
Sourcefn begin_rotation<'a>(
&'a self,
key_id: &String,
) -> Pin<Box<dyn Future<Output = Result<Rotation>> + 'a>>
fn begin_rotation<'a>( &'a self, key_id: &String, ) -> Pin<Box<dyn Future<Output = Result<Rotation>> + 'a>>
Begin a key rotation operation.
This will generate a new key and return a handle to the rotation operation. The handle should be stored securely and used to complete the rotation operation. The new key will not be used until the rotation is completed.
During the rotation operation, you should decrypt data using the old key and re-encrypt it
using the new key. Then, call KeyManager::complete_rotation with the handle to complete the
rotation and activate the new key.
§Important
It is recommended to perform the rotation operations in a database transaction to avoid attempting to decrypt data requiring the new key before it is activated.
Sourcefn complete_rotation<'a>(
&'a self,
handle: Rotation,
) -> Pin<Box<dyn Future<Output = Result<()>> + 'a>>
fn complete_rotation<'a>( &'a self, handle: Rotation, ) -> Pin<Box<dyn Future<Output = Result<()>> + 'a>>
Complete a key rotation operation.
This method finalizes a key rotation operation that was started with KeyManager::begin_rotation.
It validates the rotation handle and secret, then activates the new key for use.
§Important
Before calling this method, ensure that:
- All necessary data has been re-encrypted with the new key
- The rotation handle and secret have been kept secure
- You are ready to permanently switch to using the new key
After successful completion:
- The old key will be deactivated
- All future encryption operations will use the new key
- The rotation handle will no longer be valid