Skip to content
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

Log auth failures and failing requests #74

Merged
merged 1 commit into from
Nov 7, 2023
Merged
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
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 @@ -286,6 +286,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 @@ -309,13 +324,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 @@ -468,7 +517,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