-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: introducing secrets_interface and encryption_interface crates
- Loading branch information
1 parent
63c383f
commit 5d7a66e
Showing
19 changed files
with
486 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "encryption_interface" | ||
version = "0.1.0" | ||
edition.workspace = true | ||
|
||
[dependencies] | ||
async-trait = "0.1.68" | ||
dyn-clone = "1.0.11" | ||
thiserror = "1.0.40" | ||
|
||
# First party crates | ||
common_utils = { version = "0.1.0", path = "../common_utils" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use common_utils::errors::CustomResult; | ||
|
||
/// Trait defining the interface for encryption management | ||
#[async_trait::async_trait] | ||
pub trait EncryptionManagementInterface: Sync + Send + dyn_clone::DynClone { | ||
/// Encrypt the given input data | ||
async fn encrypt(&self, input: String) -> CustomResult<String, EncryptionError>; | ||
|
||
/// Decrypt the given input data | ||
async fn decrypt(&self, input: String) -> CustomResult<String, EncryptionError>; | ||
} | ||
|
||
dyn_clone::clone_trait_object!(EncryptionManagementInterface); | ||
|
||
/// Errors that may occur during above encryption functionalities | ||
#[derive(Debug, thiserror::Error)] | ||
pub enum EncryptionError { | ||
/// An error occurred when encrypting input data. | ||
#[error("Failed to encrypt input data")] | ||
EncryptionFailed, | ||
|
||
/// An error occurred when decrypting input data. | ||
#[error("Failed to decrypt input data")] | ||
DecryptionFailed, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//! Encryption related interface and error types | ||
#![warn(missing_docs, missing_debug_implementations)] | ||
|
||
/// Module for encryption-related functionality | ||
pub mod encryption_management; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//! | ||
//! Encryption management util module | ||
//! | ||
use common_utils::errors::CustomResult; | ||
use encryption_interface::encryption_management::{EncryptionError, EncryptionManagementInterface}; | ||
|
||
#[cfg(feature = "aws_kms")] | ||
use crate::aws_kms; | ||
use crate::no_encryption::NoEncryption; | ||
|
||
/// Enum representing configuration options for encryption management. | ||
#[derive(Debug, Clone, Default, serde::Deserialize)] | ||
#[serde(tag = "encryption_manager")] | ||
#[serde(rename_all = "snake_case")] | ||
pub enum EncryptionManagementConfig { | ||
/// AWS KMS configuration | ||
#[cfg(feature = "aws_kms")] | ||
AwsKms { | ||
///AWS KMS config | ||
aws_kms: aws_kms::AwsKmsConfig, | ||
}, | ||
|
||
/// Varient representing no encryption | ||
#[default] | ||
NoEncryption, | ||
} | ||
|
||
impl EncryptionManagementConfig { | ||
/// Verifies that the client configuration is usable | ||
pub fn validate(&self) -> Result<(), &'static str> { | ||
match self { | ||
#[cfg(feature = "aws_kms")] | ||
Self::AwsKms { aws_kms } => aws_kms.validate(), | ||
|
||
Self::NoEncryption => Ok(()), | ||
} | ||
} | ||
|
||
/// Retrieves the appropriate encryption client based on the configuration. | ||
pub async fn get_encryption_management_client( | ||
&self, | ||
) -> CustomResult<Box<dyn EncryptionManagementInterface>, EncryptionError> { | ||
let client: Box<dyn EncryptionManagementInterface> = match self { | ||
#[cfg(feature = "aws_kms")] | ||
Self::AwsKms { aws_kms } => Box::new(aws_kms::AwsKmsClient::new(aws_kms).await), | ||
|
||
Self::NoEncryption => Box::new(NoEncryption), | ||
}; | ||
Ok(client) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//! | ||
//! No encryption functionalities | ||
//! | ||
use common_utils::errors::CustomResult; | ||
use encryption_interface::encryption_management::{EncryptionError, EncryptionManagementInterface}; | ||
use masking::{ExposeInterface, Secret}; | ||
use secrets_interface::secrets_management::{SecretManagementInterface, SecretsManagementError}; | ||
|
||
/// No encryption type | ||
#[derive(Debug, Clone)] | ||
pub struct NoEncryption; | ||
|
||
impl NoEncryption { | ||
/// Encryption functionality | ||
pub fn encrypt(&self, data: String) -> String { | ||
data | ||
} | ||
|
||
/// Decryption functionality | ||
pub fn decrypt(&self, data: String) -> String { | ||
data | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl EncryptionManagementInterface for NoEncryption { | ||
async fn encrypt(&self, input: String) -> CustomResult<String, EncryptionError> { | ||
Ok(self.encrypt(input)) | ||
} | ||
|
||
async fn decrypt(&self, input: String) -> CustomResult<String, EncryptionError> { | ||
Ok(self.decrypt(input)) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl SecretManagementInterface for NoEncryption { | ||
async fn store_secret( | ||
&self, | ||
input: Secret<String>, | ||
) -> CustomResult<String, SecretsManagementError> { | ||
Ok(self.encrypt(input.expose())) | ||
} | ||
|
||
async fn get_secret( | ||
&self, | ||
input: Secret<String>, | ||
) -> CustomResult<String, SecretsManagementError> { | ||
Ok(self.decrypt(input.expose())) | ||
} | ||
} |
Oops, something went wrong.