Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

fix: make AWS KMS key_id optional for flexible encryption workflows (#10274)

Uzair Khan2a9b0d1f710f
diff --git a/crates/external_services/src/aws_kms/core.rs b/crates/external_services/src/aws_kms/core.rs
index f71a600f3f..af2bf2c12d 100644
--- a/crates/external_services/src/aws_kms/core.rs
+++ b/crates/external_services/src/aws_kms/core.rs
@@ -16,7 +16,7 @@ use crate::{consts, metrics};
#[serde(default)]
pub struct AwsKmsConfig {
/// The AWS key identifier of the KMS key used to encrypt or decrypt data.
- pub key_id: String,
+ pub key_id: Option<String>,
/// The AWS region to send KMS requests to.
pub region: String,
@@ -26,7 +26,7 @@ pub struct AwsKmsConfig {
#[derive(Debug, Clone)]
pub struct AwsKmsClient {
inner_client: Client,
- key_id: String,
+ key_id: Option<String>,
}
impl AwsKmsClient {
@@ -52,10 +52,13 @@ impl AwsKmsClient {
.change_context(AwsKmsError::Base64DecodingFailed)?;
let ciphertext_blob = Blob::new(data);
- let decrypt_output = self
- .inner_client
- .decrypt()
- .key_id(&self.key_id)
+ let mut decryption_builder = self.inner_client.decrypt();
+
+ if let Some(key_id) = &self.key_id {
+ decryption_builder = decryption_builder.key_id(key_id);
+ }
+
+ let decrypt_output = decryption_builder
.ciphertext_blob(ciphertext_blob)
.send()
.await
@@ -88,10 +91,15 @@ impl AwsKmsClient {
let start = Instant::now();
let plaintext_blob = Blob::new(data.as_ref());
- let encrypted_output = self
- .inner_client
- .encrypt()
- .key_id(&self.key_id)
+ let mut encryption_builder = self.inner_client.encrypt();
+
+ match &self.key_id {
+ Some(key_id) => encryption_builder = encryption_builder.key_id(key_id),
+ None => {
+ return Err(report!(AwsKmsError::MissingKeyId));
+ }
+ };
+ let encrypted_output = encryption_builder
.plaintext(plaintext_blob)
.send()
.await
@@ -148,6 +156,10 @@ pub enum AwsKmsError {
/// The AWS KMS client has not been initialized.
#[error("The AWS KMS client has not been initialized")]
AwsKmsClientNotInitialized,
+
+ /// AWS KMS key id not provided.
+ #[error("AWS KMS key id not provided")]
+ MissingKeyId,
}
impl AwsKmsConfig {
@@ -155,10 +167,6 @@ impl AwsKmsConfig {
pub fn validate(&self) -> Result<(), &'static str> {
use common_utils::{ext_traits::ConfigExt, fp_utils::when};
- when(self.key_id.is_default_or_empty(), || {
- Err("KMS AWS key ID must not be empty")
- })?;
-
when(self.region.is_default_or_empty(), || {
Err("KMS AWS region must not be empty")
})
@@ -173,7 +181,7 @@ mod tests {
std::env::set_var("AWS_ACCESS_KEY_ID", "YOUR AWS ACCESS KEY ID");
use super::*;
let config = AwsKmsConfig {
- key_id: "YOUR AWS KMS KEY ID".to_string(),
+ key_id: Some("YOUR AWS KMS KEY ID".to_string()),
region: "AWS REGION".to_string(),
};
@@ -194,7 +202,7 @@ mod tests {
std::env::set_var("AWS_ACCESS_KEY_ID", "YOUR AWS ACCESS KEY ID");
use super::*;
let config = AwsKmsConfig {
- key_id: "YOUR AWS KMS KEY ID".to_string(),
+ key_id: Some("YOUR AWS KMS KEY ID".to_string()),
region: "AWS REGION".to_string(),
};
diff --git a/crates/external_services/src/managers/encryption_management.rs b/crates/external_services/src/managers/encryption_management.rs
index a0add63823..e89c5947f9 100644
--- a/crates/external_services/src/managers/encryption_management.rs
+++ b/crates/external_services/src/managers/encryption_management.rs
@@ -33,7 +33,15 @@ impl EncryptionManagementConfig {
pub fn validate(&self) -> Result<(), &'static str> {
match self {
#[cfg(feature = "aws_kms")]
- Self::AwsKms { aws_kms } => aws_kms.validate(),
+ Self::AwsKms { aws_kms } => {
+ use common_utils::fp_utils::when;
+
+ aws_kms.validate()?;
+
+ when(aws_kms.key_id.is_none(), || {
+ Err("KMS AWS key ID must not be empty")
+ })
+ }
Self::NoEncryption => Ok(()),
}