Skip to content

Commit

Permalink
chore: use x-tenant-id sent by router rather than maintaining a seper…
Browse files Browse the repository at this point in the history
…ate global tenant
  • Loading branch information
dracarys18 committed Jan 3, 2025
1 parent 44dc73f commit 4ede441
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 29 deletions.
8 changes: 4 additions & 4 deletions config/development.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ pool_size = 5
min_idle = 2
enable_ssl = false

[multitenancy.global_tenant]
cache_prefix = "global"
schema = "global"

[multitenancy.tenants.public]
cache_prefix = "public"
schema = "public"

[multitenancy.tenants.global]
cache_prefix = "global"
schema = "global"

[log]
log_level = "debug"
log_format = "console"
Expand Down
9 changes: 0 additions & 9 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ pub struct SessionState {
pub thread_pool: ThreadPool,
pub keymanager_client: KeyManagerClient,
db_pool: StorageState,
global_db_pool: StorageState,
pub hash_client: Blake3,
}

Expand All @@ -65,17 +64,13 @@ impl SessionState {
pub async fn from_config(config: &Config, tenant_config: &TenantConfig) -> Self {
let secrets = config.secrets.clone();
let db_pool = StorageState::from_config(config, &tenant_config.schema).await;
let global_db_pool =
StorageState::from_config(config, &config.multitenancy.global_tenant.0.schema).await;

let num_threads = config.pool_config.pool;
let hash_client = Blake3::from_config(config).await;

Self {
cache_prefix: tenant_config.cache_prefix.clone(),
keymanager_client: secrets.create_keymanager_client().await,
db_pool,
global_db_pool,
hash_client,
thread_pool: ThreadPoolBuilder::new()
.num_threads(num_threads)
Expand All @@ -84,10 +79,6 @@ impl SessionState {
}
}

pub fn global_db_pool(&self) -> &StorageState {
&self.global_db_pool
}

pub fn db_pool(&self) -> &StorageState {
&self.db_pool
}
Expand Down
19 changes: 15 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,8 @@ pub struct Config {
#[derive(Deserialize, Debug)]
pub struct MultiTenancy {
pub tenants: TenantsConfig,
pub global_tenant: GlobalTenant,
}

#[derive(Deserialize, Debug)]
pub struct GlobalTenant(pub TenantConfig);

#[derive(Deserialize, Debug)]
pub struct TenantsConfig(pub FxHashMap<String, TenantConfig>);

Expand Down Expand Up @@ -258,6 +254,17 @@ impl Cassandra {
}
}

impl MultiTenancy {
fn validate(&self) -> CustomResult<(), errors::ParsingError> {
error_stack::ensure!(
!self.tenants.0.is_empty(),
errors::ParsingError::DecodingFailed("Failed to validate multitenancy configuration. You need to configure atleast one tenant".to_string()
)
);
Ok(())
}
}

impl Config {
pub fn config_path(environment: Environment, explicit_config_path: Option<PathBuf>) -> PathBuf {
let mut config_path = PathBuf::new();
Expand Down Expand Up @@ -305,6 +312,10 @@ impl Config {
self.cassandra
.validate()
.expect("Failed to valdiate cassandra some missing configuration found");

self.multitenancy
.validate()
.expect("Failed to validate multitenancy, some missing configuration found");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/datakey/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub async fn generate_and_create_data_key(
custodian: Custodian,
req: CreateDataKeyRequest,
) -> errors::CustomResult<DataKeyCreateResponse, errors::ApplicationErrorResponse> {
let db = state.get_db_pool(&req.identifier);
let db = state.get_db_pool();
let version = Version::get_latest(&req.identifier, &state).await;

let (source, aes_key) = state.keymanager_client.generate_key().await.switch()?;
Expand Down
2 changes: 1 addition & 1 deletion src/core/datakey/rotate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub async fn generate_and_rotate_data_key(
custodian: Custodian,
req: RotateDataKeyRequest,
) -> errors::CustomResult<DataKeyCreateResponse, errors::ApplicationErrorResponse> {
let db = state.get_db_pool(&req.identifier);
let db = state.get_db_pool();
let version = db
.get_latest_version(&req.identifier)
.await
Expand Down
2 changes: 1 addition & 1 deletion src/core/datakey/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub async fn transfer_data_key(
custodian: Custodian,
req: TransferKeyRequest,
) -> errors::CustomResult<DataKeyCreateResponse, errors::ApplicationErrorResponse> {
let db = &state.get_db_pool(&req.identifier);
let db = &state.get_db_pool();
let key = BASE64_ENGINE.decode(req.key).change_context(
errors::ApplicationErrorResponse::InternalServerError("Failed to decode the base64 key"),
)?;
Expand Down
8 changes: 2 additions & 6 deletions src/multitenancy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{
app::{AppState, SessionState, StorageState},
errors::{self, ApiErrorContainer, ToContainerError},
types::Identifier,
};
use error_stack::ResultExt;
use hyper::header;
Expand All @@ -28,11 +27,8 @@ impl TenantState {
Self(session)
}

pub(crate) fn get_db_pool(&self, identifier: &Identifier) -> &StorageState {
match identifier {
Identifier::UserAuth(_) | Identifier::User(_) => self.global_db_pool(),
Identifier::Merchant(_) | Identifier::Entity(_) => self.db_pool(),
}
pub(crate) fn get_db_pool(&self) -> &StorageState {
self.db_pool()
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/types/core/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Key {
identifier: &Identifier,
version: Version,
) -> errors::CustomResult<Self, errors::DatabaseError> {
let db = state.get_db_pool(identifier);
let db = state.get_db_pool();
let get_and_decrypt_key = || async {
let key = db.get_key(version, identifier).await?;
key.decrypt(state).await.switch()
Expand All @@ -63,7 +63,7 @@ impl Key {
identifier: &Identifier,
version: FxHashSet<Version>,
) -> errors::CustomResult<FxHashMap<Version, Self>, errors::DatabaseError> {
let db = &state.get_db_pool(identifier);
let db = &state.get_db_pool();
let get_and_decrypt_key = |v: Version| async move {
let key = db.get_key(v, identifier).await?;
key.decrypt(state).await.switch()
Expand Down Expand Up @@ -168,7 +168,7 @@ impl Serialize for Version {

impl Version {
pub async fn get_latest(identifier: &Identifier, state: &TenantState) -> Self {
let db = state.get_db_pool(identifier);
let db = state.get_db_pool();
let latest_version = db.get_latest_version(identifier);
let v = cache::get_or_populate_cache(
state,
Expand Down

0 comments on commit 4ede441

Please sign in to comment.