Skip to content

Commit

Permalink
Log auth failures and failing requests
Browse files Browse the repository at this point in the history
  • Loading branch information
plcplc committed Nov 6, 2023
1 parent 5b8761b commit e7fc40f
Showing 1 changed file with 64 additions and 5 deletions.
69 changes: 64 additions & 5 deletions rust-connector-sdk/src/default_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,21 @@ where
.route("/query", post(post_query::<C>))
.route("/explain", post(post_explain::<C>))
.route("/mutation", post(post_mutation::<C>))
.layer(
TraceLayer::new_for_http()
.make_span_with(make_span)
.on_response(on_response)
.on_failure(|err, _dur, _span: &tracing::Span| {
tracing::error!(
meta.signal_type = "log",
event.domain = "ndc",
event.name = "Request failure",
name = "Request failure",
body = %err,
error = true,
)
}),
)
.with_state(state);

let expected_auth_header: Option<HeaderValue> =
Expand All @@ -310,13 +325,24 @@ where
if auth_header == expected_auth_header {
return Ok(());
}

let message = "Bearer token does not match.".to_string();

tracing::error!(
meta.signal_type = "log",
event.domain = "ndc",
event.name = "Authorization error",
name = "Authorization error",
body = message,
error = true,
);
Err((
StatusCode::UNAUTHORIZED,
Json(ErrorResponse {
message: "Internal error".into(),
details: serde_json::Value::Object(serde_json::Map::from_iter([(
"cause".into(),
serde_json::Value::String("Bearer token does not match.".to_string()),
serde_json::Value::String(message),
)])),
}),
)
Expand Down Expand Up @@ -362,15 +388,23 @@ where
Ok(())
} else {
// all other cases, block request
let message = "Service Token Secret does not match.".to_string();

tracing::error!(
meta.signal_type = "log",
event.domain = "ndc",
event.name = "Authorization error",
name = "Authorization error",
body = message,
error = true,
);
Err((
StatusCode::UNAUTHORIZED,
Json(ErrorResponse {
message: "Internal error".into(),
details: serde_json::Value::Object(serde_json::Map::from_iter([(
"cause".into(),
serde_json::Value::String(
"Service Token Secret does not match.".to_string(),
),
serde_json::Value::String(message),
)])),
}),
)
Expand All @@ -381,6 +415,21 @@ where
// capabilities and health endpoints are exempt from auth requirements
.route("/capabilities", get(v2_compat::get_capabilities::<C>))
.route("/health", get(v2_compat::get_health))
.layer(
TraceLayer::new_for_http()
.make_span_with(make_span)
.on_response(on_response)
.on_failure(|err, _dur, _span: &_| {
tracing::error!(
meta.signal_type = "log",
event.domain = "ndc",
event.name = "Request failure",
name = "Request failure",
body = %err,
error = true,
);
}),
)
.with_state(state)
}

Expand Down Expand Up @@ -476,7 +525,17 @@ where
.layer(
TraceLayer::new_for_http()
.make_span_with(make_span)
.on_response(on_response),
.on_response(on_response)
.on_failure(|err, _dur, _span: &_| {
tracing::error!(
meta.signal_type = "log",
event.domain = "ndc",
event.name = "Request failure",
name = "Request failure",
body = %err,
error = true,
);
}),
)
.layer(cors);

Expand Down

0 comments on commit e7fc40f

Please sign in to comment.