-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
fix(connector_token): Move config redis #4540
Changes from 3 commits
6998f4f
5ff6038
53bbba5
eadfc38
58ac90f
f1f4fca
ebeb323
b9423a7
078a44f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ use data_models::{ | |
use diesel_models::enums; | ||
// TODO : Evaluate all the helper functions () | ||
use error_stack::{report, ResultExt}; | ||
use futures::future::Either; | ||
use josekit::jwe; | ||
use masking::{ExposeInterface, PeekInterface}; | ||
use openssl::{ | ||
|
@@ -3060,27 +3061,27 @@ pub async fn insert_merchant_connector_creds_to_config( | |
merchant_connector_details: admin::MerchantConnectorDetailsWrap, | ||
) -> RouterResult<()> { | ||
if let Some(encoded_data) = merchant_connector_details.encoded_data { | ||
match db | ||
.insert_config(storage::ConfigNew { | ||
key: format!( | ||
"mcd_{merchant_id}_{}", | ||
merchant_connector_details.creds_identifier | ||
), | ||
config: encoded_data.peek().to_owned(), | ||
}) | ||
let redis = &db | ||
.get_redis_conn() | ||
.change_context(errors::ApiErrorResponse::InternalServerError) | ||
.attach_printable("Failed to get redis connection")?; | ||
|
||
let key = format!( | ||
"mcd_{merchant_id}_{}", | ||
merchant_connector_details.creds_identifier | ||
); | ||
|
||
redis | ||
.serialize_and_set_key_with_expiry(key.as_str(), &encoded_data.peek(), i64::from(900)) | ||
.await | ||
{ | ||
Ok(_) => Ok(()), | ||
Err(err) => { | ||
if err.current_context().is_db_unique_violation() { | ||
Ok(()) | ||
} else { | ||
Err(err | ||
.map_or_else( | ||
|e| { | ||
Err(e | ||
.change_context(errors::ApiErrorResponse::InternalServerError) | ||
.attach_printable("Failed to insert connector_creds to config")) | ||
} | ||
} | ||
} | ||
}, | ||
|_| Ok(()), | ||
) | ||
} else { | ||
Ok(()) | ||
} | ||
|
@@ -3153,14 +3154,49 @@ pub async fn get_merchant_connector_account( | |
let db = &*state.store; | ||
match creds_identifier { | ||
Some(creds_identifier) => { | ||
let mca_config = db | ||
.find_config_by_key(format!("mcd_{merchant_id}_{creds_identifier}").as_str()) | ||
let key = format!("mcd_{merchant_id}_{creds_identifier}"); | ||
let redis_fetch = || async { | ||
db.get_redis_conn() | ||
.change_context(errors::ApiErrorResponse::InternalServerError) | ||
.attach_printable("Failed to get redis connection") | ||
.async_and_then(|redis| async move { | ||
redis | ||
.get_and_deserialize_key(key.as_str(), "String") | ||
.await | ||
.change_context( | ||
errors::ApiErrorResponse::MerchantConnectorAccountNotFound { | ||
id: key, | ||
}, | ||
) | ||
.attach_printable("Failed to get redis Value") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you also specify which data we failed to retrieve from redis, just for better tracking since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does the result from this function logged or returned somewhere? |
||
}) | ||
.await | ||
}; | ||
|
||
let db_fetch = || async { | ||
db.find_config_by_key(format!("mcd_{merchant_id}_{creds_identifier}").as_str()) | ||
.await | ||
.to_not_found_response( | ||
errors::ApiErrorResponse::MerchantConnectorAccountNotFound { | ||
id: format!("mcd_{merchant_id}_{creds_identifier}"), | ||
}, | ||
) | ||
}; | ||
|
||
let mca_config: String = redis_fetch() | ||
.await | ||
.to_not_found_response( | ||
errors::ApiErrorResponse::MerchantConnectorAccountNotFound { | ||
id: format!("mcd_{merchant_id}_{creds_identifier}"), | ||
.map_or_else( | ||
|_| { | ||
Either::Left(async { | ||
match db_fetch().await { | ||
Ok(config_entry) => Ok(config_entry.config), | ||
Err(e) => Err(e), | ||
} | ||
}) | ||
}, | ||
)?; | ||
|result| Either::Right(async { Ok(result) }), | ||
) | ||
.await?; | ||
|
||
let private_key = state | ||
.conf | ||
|
@@ -3170,7 +3206,7 @@ pub async fn get_merchant_connector_account( | |
.peek() | ||
.as_bytes(); | ||
|
||
let decrypted_mca = services::decrypt_jwe(mca_config.config.as_str(), services::KeyIdCheck::SkipKeyIdCheck, private_key, jwe::RSA_OAEP_256) | ||
let decrypted_mca = services::decrypt_jwe(mca_config.as_str(), services::KeyIdCheck::SkipKeyIdCheck, private_key, jwe::RSA_OAEP_256) | ||
.await | ||
.change_context(errors::ApiErrorResponse::UnprocessableEntity{ | ||
message: "decoding merchant_connector_details failed due to invalid data format!".into()}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we not use hardcoded 900 value instead use const something like
TOKEN_TTL
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure