-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(router): add support v2 /add /retrieve /delete api handler
- Loading branch information
1 parent
98d6b20
commit 5c3dd38
Showing
10 changed files
with
205 additions
and
132 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,92 @@ | ||
use axum::Json; | ||
use error_stack::ResultExt; | ||
use masking::PeekInterface; | ||
pub mod types; | ||
|
||
use crate::{ | ||
crypto::keymanager::{self, KeyProvider}, | ||
custom_extractors::TenantStateResolver, | ||
error::{self, ContainerError}, | ||
error::{self, ContainerError, ResultContainerExt}, | ||
routes::data::crypto_operation, | ||
storage::storage_v2::VaultInterface, | ||
utils, | ||
}; | ||
|
||
pub async fn delete_data( | ||
TenantStateResolver(_tenant_app_state): TenantStateResolver, | ||
Json(_request): Json<types::DeleteDataRequest>, | ||
TenantStateResolver(tenant_app_state): TenantStateResolver, | ||
Json(request): Json<types::DeleteDataRequest>, | ||
) -> Result<Json<types::DeleteDataResponse>, ContainerError<error::ApiError>> { | ||
// need handle this once the key manger service is ready | ||
todo!() | ||
let _entity = keymanager::get_dek_manager() | ||
.find_by_entity_id(&tenant_app_state, request.entity_id.clone()) | ||
.await?; | ||
|
||
let _delete_status = tenant_app_state | ||
.db | ||
.delete_from_vault(request.vault_id.clone().into(), &request.entity_id) | ||
.await?; | ||
Ok(Json(types::DeleteDataResponse { | ||
entity_id: request.entity_id, | ||
vault_id: request.vault_id, | ||
})) | ||
} | ||
|
||
pub async fn retrieve_data( | ||
TenantStateResolver(_tenant_app_state): TenantStateResolver, | ||
Json(_request): Json<types::RetrieveDataRequest>, | ||
TenantStateResolver(tenant_app_state): TenantStateResolver, | ||
Json(request): Json<types::RetrieveDataRequest>, | ||
) -> Result<Json<types::RetrieveDataResponse>, ContainerError<error::ApiError>> { | ||
// need handle this once the key manger service is ready | ||
todo!() | ||
let crypto_manager = keymanager::get_dek_manager() | ||
.find_by_entity_id(&tenant_app_state, request.entity_id.clone()) | ||
.await?; | ||
|
||
let mut vault = tenant_app_state | ||
.db | ||
.find_by_vault_id_entity_id(request.vault_id.clone().into(), &request.entity_id) | ||
.await?; | ||
|
||
crypto_operation::decrypt_data(&tenant_app_state, crypto_manager, &mut vault).await?; | ||
|
||
vault | ||
.expires_at | ||
.map(|ttl| -> Result<(), error::ApiError> { | ||
if utils::date_time::now() > ttl { | ||
tokio::spawn(async move { | ||
tenant_app_state | ||
.db | ||
.delete_from_vault(request.vault_id.into(), &request.entity_id) | ||
.await | ||
}); | ||
|
||
Err(error::ApiError::NotFoundError) | ||
} else { | ||
Ok(()) | ||
} | ||
}) | ||
.transpose()?; | ||
let decrypted_data = vault | ||
.data | ||
.get_decrypted_inner_value() | ||
.ok_or(error::ApiError::UnknownError) | ||
.attach_printable("Failed to decrypt the stored data")?; | ||
let og = serde_json::from_slice(decrypted_data.peek().as_ref()) | ||
.change_error(error::ApiError::DecodingError)?; | ||
|
||
Ok(Json(types::RetrieveDataResponse { data: og })) | ||
} | ||
|
||
pub async fn add_data( | ||
TenantStateResolver(_tenant_app_state): TenantStateResolver, | ||
Json(_request): Json<types::StoreDataRequest>, | ||
TenantStateResolver(tenant_app_state): TenantStateResolver, | ||
Json(request): Json<types::StoreDataRequest>, | ||
) -> Result<Json<types::StoreDataResponse>, ContainerError<error::ApiError>> { | ||
// need handle this once the key manger service is ready | ||
todo!() | ||
let crypto_manager = keymanager::get_dek_manager() | ||
.find_or_create_entity(&tenant_app_state, request.entity_id.clone()) | ||
.await?; | ||
|
||
let insert_data = crypto_operation::encrypt_data_and_insert_into_db_v2( | ||
&tenant_app_state, | ||
crypto_manager, | ||
request, | ||
) | ||
.await?; | ||
|
||
Ok(Json(types::StoreDataResponse::from(insert_data))) | ||
} |
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
Oops, something went wrong.