Skip to content

Commit

Permalink
refactor: address requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Chethan-rao committed Oct 31, 2023
1 parent 18fb1fa commit 3ae7a9c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
16 changes: 6 additions & 10 deletions src/app.rs
Expand Up @@ -19,8 +19,6 @@ use crate::crypto::{
kms::{self, Base64Encoded, KmsData, Raw},
Encryption,
};
#[cfg(feature = "kms")]
use std::marker::PhantomData;

///
/// AppState:
Expand Down Expand Up @@ -125,7 +123,6 @@ impl AppState {
let master_key_kms_input: KmsData<Base64Encoded> = KmsData::new(
String::from_utf8(config.secrets.master_key.clone())
.expect("Failed while converting bytes to String"),
PhantomData,
);
let kms_decrypted_master_key: KmsData<Raw> = kms_client
.decrypt(master_key_kms_input)
Expand All @@ -134,7 +131,7 @@ impl AppState {
config.secrets.master_key = kms_decrypted_master_key.data;

let tenant_public_key_kms_input: KmsData<Base64Encoded> =
KmsData::new(config.secrets.tenant_public_key.peek().clone(), PhantomData);
KmsData::new(config.secrets.tenant_public_key.peek().clone());
let kms_decrypted_tenant_public_key: KmsData<Raw> = kms_client
.decrypt(tenant_public_key_kms_input)
.await
Expand All @@ -146,10 +143,8 @@ impl AppState {
.expect("Failed while converting bytes to String")
.into();

let locker_private_key_kms_input: KmsData<Base64Encoded> = KmsData::new(
config.secrets.locker_private_key.peek().clone(),
PhantomData,
);
let locker_private_key_kms_input: KmsData<Base64Encoded> =
KmsData::new(config.secrets.locker_private_key.peek().clone());
let kms_decrypted_locker_private_key: KmsData<Raw> = kms_client
.decrypt(locker_private_key_kms_input)
.await
Expand All @@ -162,15 +157,16 @@ impl AppState {
.into();

let db_password_kms_input: KmsData<Base64Encoded> =
KmsData::new(config.database.password.clone(), PhantomData);
KmsData::new(config.database.password.peek().clone());
let kms_decrypted_db_password: KmsData<Raw> = kms_client
.decrypt(db_password_kms_input)
.await
.change_context(error::ConfigurationError::KmsDecryptError(
"locker_private_key",
))?;
config.database.password = String::from_utf8(kms_decrypted_db_password.data)
.expect("Failed while converting bytes to String");
.expect("Failed while converting bytes to String")
.into();
}

Ok(Self {
Expand Down
6 changes: 5 additions & 1 deletion src/config.rs
Expand Up @@ -22,7 +22,8 @@ pub struct Server {
#[derive(Clone, serde::Deserialize, Debug)]
pub struct Database {
pub username: String,
pub password: String,
// KMS encrypted
pub password: masking::Secret<String>,
pub host: String,
pub port: u16,
pub dbname: String,
Expand All @@ -31,10 +32,13 @@ pub struct Database {
#[derive(Clone, serde::Deserialize, Debug)]
pub struct Secrets {
pub tenant: String,
// KMS encrypted
#[serde(deserialize_with = "deserialize_hex")]
pub master_key: Vec<u8>,
// KMS encrypted
#[cfg(feature = "middleware")]
pub locker_private_key: masking::Secret<String>,
// KMS encrypted
#[cfg(feature = "middleware")]
pub tenant_public_key: masking::Secret<String>,
}
Expand Down
7 changes: 5 additions & 2 deletions src/crypto/kms.rs
Expand Up @@ -100,8 +100,11 @@ pub struct KmsData<T: Decoder> {
}

impl<T: Decoder> KmsData<T> {
pub fn new(data: T::Data, decode_op: PhantomData<T>) -> Self {
Self { data, decode_op }
pub fn new(data: T::Data) -> Self {
Self {
data,
decode_op: PhantomData,
}
}
pub fn into_decoded(self) -> Result<Vec<u8>, T::Error> {
T::decode(self.data)
Expand Down

0 comments on commit 3ae7a9c

Please sign in to comment.