From 3ae7a9c87650cd11812d57711bc825dc02ec438b Mon Sep 17 00:00:00 2001 From: Chethan Date: Tue, 31 Oct 2023 16:55:05 +0530 Subject: [PATCH] refactor: address requested changes --- src/app.rs | 16 ++++++---------- src/config.rs | 6 +++++- src/crypto/kms.rs | 7 +++++-- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/app.rs b/src/app.rs index 36fbc2c..bba73bd 100644 --- a/src/app.rs +++ b/src/app.rs @@ -19,8 +19,6 @@ use crate::crypto::{ kms::{self, Base64Encoded, KmsData, Raw}, Encryption, }; -#[cfg(feature = "kms")] -use std::marker::PhantomData; /// /// AppState: @@ -125,7 +123,6 @@ impl AppState { let master_key_kms_input: KmsData = KmsData::new( String::from_utf8(config.secrets.master_key.clone()) .expect("Failed while converting bytes to String"), - PhantomData, ); let kms_decrypted_master_key: KmsData = kms_client .decrypt(master_key_kms_input) @@ -134,7 +131,7 @@ impl AppState { config.secrets.master_key = kms_decrypted_master_key.data; let tenant_public_key_kms_input: KmsData = - 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 = kms_client .decrypt(tenant_public_key_kms_input) .await @@ -146,10 +143,8 @@ impl AppState { .expect("Failed while converting bytes to String") .into(); - let locker_private_key_kms_input: KmsData = KmsData::new( - config.secrets.locker_private_key.peek().clone(), - PhantomData, - ); + let locker_private_key_kms_input: KmsData = + KmsData::new(config.secrets.locker_private_key.peek().clone()); let kms_decrypted_locker_private_key: KmsData = kms_client .decrypt(locker_private_key_kms_input) .await @@ -162,7 +157,7 @@ impl AppState { .into(); let db_password_kms_input: KmsData = - KmsData::new(config.database.password.clone(), PhantomData); + KmsData::new(config.database.password.peek().clone()); let kms_decrypted_db_password: KmsData = kms_client .decrypt(db_password_kms_input) .await @@ -170,7 +165,8 @@ impl AppState { "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 { diff --git a/src/config.rs b/src/config.rs index 0777d8f..38ec244 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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, pub host: String, pub port: u16, pub dbname: String, @@ -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, + // KMS encrypted #[cfg(feature = "middleware")] pub locker_private_key: masking::Secret, + // KMS encrypted #[cfg(feature = "middleware")] pub tenant_public_key: masking::Secret, } diff --git a/src/crypto/kms.rs b/src/crypto/kms.rs index ef5576e..f6aa51c 100644 --- a/src/crypto/kms.rs +++ b/src/crypto/kms.rs @@ -100,8 +100,11 @@ pub struct KmsData { } impl KmsData { - pub fn new(data: T::Data, decode_op: PhantomData) -> Self { - Self { data, decode_op } + pub fn new(data: T::Data) -> Self { + Self { + data, + decode_op: PhantomData, + } } pub fn into_decoded(self) -> Result, T::Error> { T::decode(self.data)