Skip to content

fix(app): Fix status codes of IDS responses #134

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

Merged
merged 1 commit into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion clearing-house-app/src/model/ids/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ where

#[derive(Debug, Clone, serde::Serialize)]
pub struct RejectionMessage {
#[serde(skip)]
code: axum::http::StatusCode,
#[serde(flatten)]
inner: IdsHeader,
#[serde(rename = "ids:rejectionReason")]
Expand All @@ -275,6 +277,7 @@ impl RejectionMessage {
#[must_use]
pub fn new(
clearinghouse_uri: &str,
code: axum::http::StatusCode,
rejection_message: String,
correlation_msg_id: Option<String>,
) -> Self {
Expand All @@ -288,6 +291,7 @@ impl RejectionMessage {
};

Self {
code,
inner: header,
rejection_reason: rejection_message,
}
Expand All @@ -308,7 +312,7 @@ impl axum::response::IntoResponse for RejectionMessage {
.expect("application/json is a valid mime type"),
]);

form.into_response()
(self.code, form).into_response()
}
}

Expand Down
75 changes: 47 additions & 28 deletions clearing-house-app/src/ports/logging_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::model::process::OwnerList;
use crate::{AppState, model::SortingOrder, model::claims::get_jwks};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use ids_daps_client::DapsError;

async fn log(
axum::extract::State(state): axum::extract::State<AppState>,
Expand All @@ -15,13 +16,10 @@ async fn log(
}: ExtractIdsMessage<serde_json::Value>,
) -> super::ApiResult {
let correlation_id = ids_message.header.id.clone();
let daps_token = state.daps_client.request_dat().await.map_err(|e| {
RejectionMessage::new(
state.logging_service.issuer(),
format!("DAPS error: {e:?}"),
correlation_id.clone(),
)
})?;
let daps_token =
state.daps_client.request_dat().await.map_err(|e| {
map_daps_error(e, state.logging_service.issuer(), correlation_id.clone())
})?;

let cloned_ids_message: IdsMessage<String> = IdsMessage {
header: ids_message.header.clone(),
Expand All @@ -48,6 +46,7 @@ async fn log(
error!("Error while logging: {:?}", e);
Err(RejectionMessage::new(
state.logging_service.issuer(),
axum::http::StatusCode::BAD_REQUEST,
format!("Error while logging: {e:?}"),
correlation_id,
))
Expand All @@ -69,13 +68,10 @@ async fn create_process(
}: ExtractIdsMessage<OwnerList>,
) -> super::ApiResult {
let correlation_id = ids_message.header.id.clone();
let daps_token = state.daps_client.request_dat().await.map_err(|e| {
RejectionMessage::new(
state.logging_service.issuer(),
format!("DAPS error: {e:?}"),
correlation_id.clone(),
)
})?;
let daps_token =
state.daps_client.request_dat().await.map_err(|e| {
map_daps_error(e, state.logging_service.issuer(), correlation_id.clone())
})?;

match state
.logging_service
Expand All @@ -96,6 +92,7 @@ async fn create_process(
error!("Error while creating process: {e:?}");
Err(RejectionMessage::new(
state.logging_service.issuer(),
axum::http::StatusCode::BAD_REQUEST,
format!("Error while creating process: {e:?}"),
correlation_id,
))
Expand All @@ -122,13 +119,10 @@ async fn query_pid(
}: ExtractIdsMessage<()>,
) -> super::ApiResult {
let correlation_id = ids_message.header.id.clone();
let daps_token = state.daps_client.request_dat().await.map_err(|e| {
RejectionMessage::new(
state.logging_service.issuer(),
format!("DAPS error: {e:?}"),
correlation_id.clone(),
)
})?;
let daps_token =
state.daps_client.request_dat().await.map_err(|e| {
map_daps_error(e, state.logging_service.issuer(), correlation_id.clone())
})?;

match state
.logging_service
Expand Down Expand Up @@ -156,6 +150,7 @@ async fn query_pid(
error!("Error while querying: {e:?}");
Err(RejectionMessage::new(
state.logging_service.issuer(),
axum::http::StatusCode::BAD_REQUEST,
format!("Error while querying: {e:?}"),
correlation_id,
))
Expand All @@ -173,13 +168,10 @@ async fn query_id(
}: ExtractIdsMessage<()>,
) -> super::ApiResult {
let correlation_id = ids_message.header.id.clone();
let daps_token = state.daps_client.request_dat().await.map_err(|e| {
RejectionMessage::new(
state.logging_service.issuer(),
format!("DAPS error: {e:?}"),
correlation_id.clone(),
)
})?;
let daps_token =
state.daps_client.request_dat().await.map_err(|e| {
map_daps_error(e, state.logging_service.issuer(), correlation_id.clone())
})?;

match state
.logging_service
Expand All @@ -200,6 +192,7 @@ async fn query_id(
error!("Error while querying: {:?}", e);
Err(RejectionMessage::new(
state.logging_service.issuer(),
axum::http::StatusCode::BAD_REQUEST,
format!("Error while querying: {e:?}"),
correlation_id,
))
Expand All @@ -214,6 +207,7 @@ async fn get_public_sign_key(
Some(jwks) => Ok((StatusCode::OK, axum::Json(jwks)).into_response()),
None => Err(RejectionMessage::new(
state.logging_service.issuer(),
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
"Error reading signing key".to_string(),
None,
)),
Expand All @@ -231,3 +225,28 @@ pub(crate) fn router() -> axum::routing::Router<AppState> {
axum::routing::get(get_public_sign_key),
)
}

fn map_daps_error(e: DapsError, issuer: &str, correlation_id: Option<String>) -> RejectionMessage {
let (code, msg) = match e {
DapsError::DapsHttpClient(c) => {
tracing::warn!("DAPS HTTP Client issue: {c:?}");
(
axum::http::StatusCode::BAD_REQUEST,
"DAPS Client HTTP issue".to_string(),
)
}
DapsError::InvalidToken => (
axum::http::StatusCode::UNAUTHORIZED,
"Invalid Token".to_string(),
),
DapsError::CacheError(c) => {
tracing::warn!("DAPS Client certificate cache issue: {c:?}");
(
axum::http::StatusCode::EXPECTATION_FAILED,
"DAPS Client certificate cache issue".to_string(),
)
}
};

RejectionMessage::new(issuer, code, msg, correlation_id)
}