-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: migrate tenant and provider_config to diesel (#183)
- Loading branch information
Showing
45 changed files
with
693 additions
and
561 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
modules/meteroid/crates/meteroid-store/src/domain/configs.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
use crate::domain::enums::InvoicingProviderEnum; | ||
use crate::errors::StoreError; | ||
use crate::StoreResult; | ||
use chrono::NaiveDateTime; | ||
use error_stack::ResultExt; | ||
use secrecy::{ExposeSecret, SecretString}; | ||
use serde::{Deserialize, Serialize}; | ||
use uuid::Uuid; | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
pub struct WebhookSecurity { | ||
pub secret: String, | ||
} | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
pub struct ApiSecurity { | ||
pub api_key: String, | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct ProviderConfig { | ||
pub id: Uuid, | ||
pub created_at: NaiveDateTime, | ||
pub tenant_id: Uuid, | ||
pub invoicing_provider: InvoicingProviderEnum, | ||
pub enabled: bool, | ||
pub webhook_security: WebhookSecurity, | ||
pub api_security: ApiSecurity, | ||
} | ||
|
||
impl ProviderConfig { | ||
pub fn from_row( | ||
key: &SecretString, | ||
row: diesel_models::configs::ProviderConfig, | ||
) -> StoreResult<ProviderConfig> { | ||
let enc_wh_sec: WebhookSecurity = | ||
serde_json::from_value(row.webhook_security).map_err(|e| { | ||
StoreError::SerdeError("Failed to deserialize webhook_security".to_string(), e) | ||
})?; | ||
|
||
let enc_api_sec: ApiSecurity = serde_json::from_value(row.api_security).map_err(|e| { | ||
StoreError::SerdeError("Failed to deserialize api_security".to_string(), e) | ||
})?; | ||
|
||
let wh_sec = WebhookSecurity { | ||
secret: crate::crypt::decrypt(key, enc_wh_sec.secret.as_str()) | ||
.change_context(StoreError::CryptError( | ||
"webhook_security decryption error".into(), | ||
))? | ||
.expose_secret() | ||
.clone(), | ||
}; | ||
|
||
let api_sec = ApiSecurity { | ||
api_key: crate::crypt::decrypt(key, enc_api_sec.api_key.as_str()) | ||
.change_context(StoreError::CryptError( | ||
"api_security decryption error".into(), | ||
))? | ||
.expose_secret() | ||
.clone(), | ||
}; | ||
|
||
Ok(ProviderConfig { | ||
id: row.id, | ||
created_at: row.created_at, | ||
tenant_id: row.tenant_id, | ||
invoicing_provider: row.invoicing_provider.into(), | ||
enabled: row.enabled, | ||
webhook_security: wh_sec, | ||
api_security: api_sec, | ||
}) | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct ProviderConfigNew { | ||
pub tenant_id: Uuid, | ||
pub invoicing_provider: InvoicingProviderEnum, | ||
pub enabled: bool, | ||
pub webhook_security: WebhookSecurity, | ||
pub api_security: ApiSecurity, | ||
} | ||
|
||
impl ProviderConfigNew { | ||
pub fn domain_to_row( | ||
key: &SecretString, | ||
domain: &ProviderConfigNew, | ||
) -> StoreResult<diesel_models::configs::ProviderConfigNew> { | ||
let wh_sec_enc = WebhookSecurity { | ||
secret: crate::crypt::encrypt(key, domain.webhook_security.secret.as_str()) | ||
.change_context(StoreError::CryptError( | ||
"webhook_security encryption error".into(), | ||
))?, | ||
}; | ||
|
||
let api_sec_enc = ApiSecurity { | ||
api_key: crate::crypt::encrypt(key, domain.api_security.api_key.as_str()) | ||
.change_context(StoreError::CryptError( | ||
"api_security encryption error".into(), | ||
))?, | ||
}; | ||
|
||
let wh_sec = serde_json::to_value(&wh_sec_enc).map_err(|e| { | ||
StoreError::SerdeError("Failed to serialize webhook_security".to_string(), e) | ||
})?; | ||
|
||
let api_sec = serde_json::to_value(&api_sec_enc).map_err(|e| { | ||
StoreError::SerdeError("Failed to serialize api_security".to_string(), e) | ||
})?; | ||
|
||
Ok(diesel_models::configs::ProviderConfigNew { | ||
id: Uuid::now_v7(), | ||
tenant_id: domain.tenant_id, | ||
invoicing_provider: domain.invoicing_provider.clone().into(), | ||
enabled: domain.enabled, | ||
webhook_security: wh_sec, | ||
api_security: api_sec, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.