Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(keymanager): add tenant-id to keymanager requests #6968

Merged
merged 6 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/development.toml
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ sdk_eligible_payment_methods = "card"

[multitenancy]
enabled = false
global_tenant = { schema = "public", redis_key_prefix = "", clickhouse_database = "default"}
global_tenant = { tenant_id = "global" ,schema = "public", redis_key_prefix = "", clickhouse_database = "default"}
dracarys18 marked this conversation as resolved.
Show resolved Hide resolved

[multitenancy.tenants.public]
base_url = "http://localhost:8080"
Expand Down
2 changes: 2 additions & 0 deletions crates/common_utils/src/id_type/tenant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ crate::impl_try_from_cow_str_id_type!(TenantId, "tenant_id");
crate::impl_serializable_secret_id_type!(TenantId);
crate::impl_queryable_id_type!(TenantId);
crate::impl_to_sql_from_sql_id_type!(TenantId);
// This is needed in case when Multitenancy is disabled
crate::impl_default_id_type!(TenantId, "tenant_id");
dracarys18 marked this conversation as resolved.
Show resolved Hide resolved

impl TenantId {
/// Get tenant id from String
Expand Down
15 changes: 12 additions & 3 deletions crates/common_utils/src/keymanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ use once_cell::sync::OnceCell;
use router_env::{instrument, logger, tracing};

use crate::{
consts::BASE64_ENGINE,
consts::{BASE64_ENGINE, TENANT_HEADER},
errors,
types::keymanager::{
BatchDecryptDataRequest, DataKeyCreateResponse, DecryptDataRequest,
EncryptionCreateRequest, EncryptionTransferRequest, KeyManagerState,
EncryptionCreateRequest, EncryptionTransferRequest, GetKeymanagerTenant, KeyManagerState,
TransientBatchDecryptDataRequest, TransientDecryptDataRequest,
},
};
Expand Down Expand Up @@ -100,7 +100,7 @@ pub async fn call_encryption_service<T, R>(
request_body: T,
) -> errors::CustomResult<R, errors::KeyManagerClientError>
where
T: ConvertRaw + Send + Sync + 'static + Debug,
T: GetKeymanagerTenant + ConvertRaw + Send + Sync + 'static + Debug,
R: serde::de::DeserializeOwned,
{
let url = format!("{}/{endpoint}", &state.url);
Expand All @@ -122,6 +122,15 @@ where
.change_context(errors::KeyManagerClientError::FailedtoConstructHeader)?,
))
}

//Add Tenant ID
header.push((
HeaderName::from_str(TENANT_HEADER)
.change_context(errors::KeyManagerClientError::FailedtoConstructHeader)?,
HeaderValue::from_str(request_body.get_tenant_id(state).get_string_repr())
.change_context(errors::KeyManagerClientError::FailedtoConstructHeader)?,
));

let response = send_encryption_request(
state,
HeaderMap::from_iter(header.into_iter()),
Expand Down
30 changes: 30 additions & 0 deletions crates/common_utils/src/types/keymanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,23 @@ use crate::{
transformers::{ForeignFrom, ForeignTryFrom},
};

macro_rules! impl_get_tenant_for_request {
($ty:ident) => {
impl GetKeymanagerTenant for $ty {
fn get_tenant_id(&self, state: &KeyManagerState) -> id_type::TenantId {
match self.identifier {
Identifier::User(_) | Identifier::UserAuth(_) => state.global_tenant_id.clone(),
Identifier::Merchant(_) => state.tenant_id.clone(),
}
}
}
};
}

#[derive(Debug, Clone)]
pub struct KeyManagerState {
pub tenant_id: id_type::TenantId,
pub global_tenant_id: id_type::TenantId,
pub enabled: bool,
pub url: String,
pub client_idle_timeout: Option<u64>,
Expand All @@ -35,6 +50,11 @@ pub struct KeyManagerState {
#[cfg(feature = "keymanager_mtls")]
pub cert: Secret<String>,
}

pub trait GetKeymanagerTenant {
fn get_tenant_id(&self, state: &KeyManagerState) -> id_type::TenantId;
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
#[serde(tag = "data_identifier", content = "key_identifier")]
pub enum Identifier {
Expand Down Expand Up @@ -70,6 +90,10 @@ pub struct BatchEncryptDataRequest {
pub data: DecryptedDataGroup,
}

impl_get_tenant_for_request!(EncryptionCreateRequest);
impl_get_tenant_for_request!(EncryptionTransferRequest);
impl_get_tenant_for_request!(BatchEncryptDataRequest);

impl<S> From<(Secret<Vec<u8>, S>, Identifier)> for EncryptDataRequest
where
S: Strategy<Vec<u8>>,
Expand Down Expand Up @@ -219,6 +243,12 @@ pub struct DecryptDataRequest {
pub data: StrongSecret<String>,
}

impl_get_tenant_for_request!(EncryptDataRequest);
impl_get_tenant_for_request!(TransientBatchDecryptDataRequest);
impl_get_tenant_for_request!(TransientDecryptDataRequest);
impl_get_tenant_for_request!(BatchDecryptDataRequest);
impl_get_tenant_for_request!(DecryptDataRequest);

impl<T, S> ForeignFrom<(FxHashMap<String, Secret<T, S>>, BatchEncryptDataResponse)>
for FxHashMap<String, Encryptable<Secret<T, S>>>
where
Expand Down
3 changes: 2 additions & 1 deletion crates/router/src/configs/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub struct Platform {
pub enabled: bool,
}

#[derive(Debug, Deserialize, Clone, Default)]
#[derive(Debug, Clone, Default, Deserialize)]
pub struct Multitenancy {
pub tenants: TenantConfig,
pub enabled: bool,
Expand Down Expand Up @@ -197,6 +197,7 @@ impl storage_impl::config::TenantConfig for Tenant {

#[derive(Debug, Deserialize, Clone, Default)]
pub struct GlobalTenant {
pub tenant_id: id_type::TenantId,
dracarys18 marked this conversation as resolved.
Show resolved Hide resolved
pub schema: String,
pub redis_key_prefix: String,
pub clickhouse_database: String,
Expand Down
2 changes: 2 additions & 0 deletions crates/router/src/types/domain/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ impl From<&crate::SessionState> for KeyManagerState {
fn from(state: &crate::SessionState) -> Self {
let conf = state.conf.key_manager.get_inner();
Self {
global_tenant_id: state.conf.multitenancy.global_tenant.tenant_id.clone(),
tenant_id: state.tenant.tenant_id.clone(),
enabled: conf.enabled,
url: conf.url.clone(),
client_idle_timeout: state.conf.proxy.idle_pool_connection_timeout,
Expand Down
Loading