-
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
feat: add mtls for keymanager api requests #5048
Open
dracarys18
wants to merge
5
commits into
add_encryption_service
Choose a base branch
from
add_mtls_keymanager
base: add_encryption_service
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+106
−19
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
409d9df
eat: add mtls to keymanager
dracarys18 2b516f8
chore: run formatter
hyperswitch-bot[bot] 5591785
Merge branch 'add_encryption_service' of github.com:juspay/hyperswitc…
dracarys18 551cae0
Merge branch 'add_mtls_keymanager' of github.com:juspay/hyperswitch i…
dracarys18 42b0dc8
Merge branch 'add_encryption_service' of github.com:juspay/hyperswitc…
dracarys18 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,116 @@ | ||
use crate::{ | ||
errors, headers, services, | ||
types::{domain::EncryptionCreateRequest, RequestContent, Response}, | ||
SessionState, | ||
}; | ||
use std::str::FromStr; | ||
|
||
use error_stack::ResultExt; | ||
use http::{HeaderMap, HeaderName, HeaderValue}; | ||
#[cfg(feature = "keymanager_mtls")] | ||
use masking::PeekInterface; | ||
use once_cell::sync::OnceCell; | ||
|
||
use crate::{errors, headers, types::domain::EncryptionCreateRequest, SessionState}; | ||
|
||
static ENCRYPTION_API_CLIENT: OnceCell<reqwest::Client> = OnceCell::new(); | ||
|
||
#[allow(unused_mut)] | ||
pub fn get_api_encryption_client( | ||
state: &SessionState, | ||
) -> errors::CustomResult<reqwest::Client, errors::ApiClientError> { | ||
let proxy = &state.conf.proxy; | ||
|
||
let get_client = || { | ||
let mut client = reqwest::Client::builder() | ||
.redirect(reqwest::redirect::Policy::none()) | ||
.pool_idle_timeout(std::time::Duration::from_secs( | ||
proxy.idle_pool_connection_timeout.unwrap_or_default(), | ||
)); | ||
|
||
#[cfg(feature = "keymanager_mtls")] | ||
{ | ||
let ca = state.conf.key_manager.ca.clone(); | ||
let cert = state.conf.key_manager.cert.clone(); | ||
|
||
let identity = reqwest::Identity::from_pem(cert.peek().as_ref()) | ||
.change_context(errors::ApiClientError::ClientConstructionFailed)?; | ||
let ca_cert = reqwest::Certificate::from_pem(ca.peek().as_ref()) | ||
.change_context(errors::ApiClientError::ClientConstructionFailed)?; | ||
|
||
client = client | ||
.use_rustls_tls() | ||
.identity(identity) | ||
.add_root_certificate(ca_cert) | ||
.https_only(true); | ||
} | ||
|
||
client | ||
.build() | ||
.change_context(errors::ApiClientError::ClientConstructionFailed) | ||
}; | ||
|
||
Ok(ENCRYPTION_API_CLIENT.get_or_try_init(get_client)?.clone()) | ||
} | ||
|
||
pub async fn send_encryption_request<T>( | ||
state: &SessionState, | ||
headers: Vec<(String, String)>, | ||
url: String, | ||
request_body: T, | ||
) -> errors::CustomResult<reqwest::Response, errors::ApiClientError> | ||
where | ||
T: serde::Serialize, | ||
{ | ||
let client = get_api_encryption_client(state)?; | ||
let url = | ||
reqwest::Url::parse(&url).change_context(errors::ApiClientError::UrlEncodingFailed)?; | ||
|
||
let headers = headers.into_iter().try_fold( | ||
HeaderMap::new(), | ||
|mut header_map, (header_name, header_value)| { | ||
let header_name = HeaderName::from_str(&header_name) | ||
.change_context(errors::ApiClientError::HeaderMapConstructionFailed)?; | ||
let header_value = HeaderValue::from_str(&header_value) | ||
.change_context(errors::ApiClientError::HeaderMapConstructionFailed)?; | ||
header_map.append(header_name, header_value); | ||
Ok::<_, error_stack::Report<errors::ApiClientError>>(header_map) | ||
}, | ||
)?; | ||
|
||
client | ||
.post(url) | ||
.json(&request_body) | ||
.headers(headers) | ||
.send() | ||
.await | ||
.change_context(errors::ApiClientError::RequestNotSent( | ||
"Unable to send request to encryption service".to_string(), | ||
)) | ||
} | ||
|
||
pub async fn call_encryption_service<T>( | ||
state: &SessionState, | ||
endpoint: &str, | ||
request_body: T, | ||
) -> errors::CustomResult<Result<Response, Response>, errors::ApiClientError> | ||
) -> errors::CustomResult<reqwest::Response, errors::ApiClientError> | ||
where | ||
T: masking::ErasedMaskSerialize + Send + Sync + 'static, | ||
T: serde::Serialize + Send + Sync + 'static, | ||
{ | ||
let url = format!("{}/{}", &state.conf.key_manager.url, endpoint); | ||
|
||
let encryption_req = services::RequestBuilder::new() | ||
.method(services::Method::Post) | ||
.url(&url) | ||
.attach_default_headers() | ||
.headers(vec![( | ||
send_encryption_request( | ||
state, | ||
vec![( | ||
headers::CONTENT_TYPE.to_string(), | ||
"application/json".to_string().into(), | ||
)]) | ||
.set_body(RequestContent::Json(Box::new(request_body))) | ||
.build(); | ||
|
||
services::call_connector_api(state, encryption_req, "EncryptionServiceRequest").await | ||
"application/json".to_string(), | ||
)], | ||
url, | ||
request_body, | ||
) | ||
.await | ||
} | ||
|
||
pub async fn create_key_in_key_manager( | ||
state: &SessionState, | ||
request_body: EncryptionCreateRequest, | ||
) -> errors::CustomResult<(), errors::ApiClientError> { | ||
let _ = call_encryption_service(state, "/key/create", request_body).await?; | ||
let _ = call_encryption_service(state, "key/create", request_body).await?; | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
you can use
you would not need #[allow(unused_mut)] in that case
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.
We cannot do this since ca and cert have compile time feature constraints