Skip to content
Draft
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
5 changes: 5 additions & 0 deletions crates/openshell-supervisor-network/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,8 @@ filtering preserves raw HTTP bytes
and borrows unchanged header blocks. It removes only protected receipt fields
and their `Connection` or `Trailer` nominations, including folded continuations,
without adding general response-header validation.

Credential preparation stages record bounded mechanism, placement and preparation
outcomes, never credential values. A configured resolver without placeholders is
`not_needed`; `prepared` means resolution or signing succeeded, not that the
upstream received the request. Transport failures belong to their own stages.
11 changes: 11 additions & 0 deletions crates/openshell-supervisor-network/src/l7/correlation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ pub fn record_current_failure(outcome: HttpOutcome, reason: &'static str) {
}
}

pub fn record_credential_preparation(
span: &tracing::Span,
mechanism: &'static str,
placement: &'static str,
preparation: &'static str,
) {
span.record("openshell.credential.mechanism", mechanism);
span.record("openshell.credential.placement", placement);
span.record("openshell.credential.preparation", preparation);
}

fn enrich_request(event: &mut openshell_ocsf::BaseEventData) {
let Some(scope) = current_request().filter(|scope| scope.inspected) else {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,12 +514,22 @@ network_policies:

#[tokio::test]
async fn inspected_allow_creates_only_the_stages_that_run() {
for sampled in [true, false] {
for (sampled, credential) in [(true, false), (false, false), (true, true)] {
let mut fixture = rest_fixture("POST", false);
let authorization = if credential {
let (_, resolver) = openshell_core::secrets::SecretResolver::from_provider_env(
std::iter::once(("PRESENT".into(), "SENTINEL-CREDENTIAL".into())).collect(),
);
fixture.ctx.secret_resolver = resolver.map(Arc::new);
"Authorization: Basic dXNlcjpvcGVuc2hlbGw6cmVzb2x2ZTplbnY6UFJFU0VOVA==\r\n"
} else {
""
};
let evidence = capture_request_spans(
sampled,
Box::pin(run_exchange(
rest_fixture("POST", false),
request("rest.example.test", "/item", "", "{}"),
fixture,
request("rest.example.test", "/item", authorization, "{}"),
Some(b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"),
)),
)
Expand All @@ -543,10 +553,30 @@ async fn inspected_allow_creates_only_the_stages_that_run() {
("openshell.http.response", true),
("openshell.middleware.request", false),
("openshell.middleware.response", false),
("openshell.credential.prepare", false),
("openshell.credential.prepare", credential),
] {
assert_eq!(names.contains(&name), expected, "{name}: {names:?}");
}
if credential {
let preparation = evidence
.spans
.iter()
.find(|span| span.name == "openshell.credential.prepare")
.unwrap();
assert_eq!(
span_attribute(preparation, "openshell.credential.preparation")
.unwrap()
.as_str(),
"prepared"
);
assert!(
String::from_utf8_lossy(&evidence.output.forwarded)
.contains("Basic dXNlcjpTRU5USU5FTC1DUkVERU5USUFM")
);
let metadata = format!("{:?}", evidence.spans);
assert!(!metadata.contains("SENTINEL-CREDENTIAL"));
assert!(!metadata.contains("dXNlcjpTRU5USU5FTC1DUkVERU5USUFM"));
}
}
let records = assert_control_records(&evidence, &[("l7", "Allowed")], &[], "/item");
assert!(records[0].get("status").is_none());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -998,23 +998,35 @@ async fn failure_then_reconnect(route_selection: bool) {
}

#[tokio::test]
async fn token_grant_refusal_never_creates_a_denial_only_span() {
async fn token_grant_preparation_reports_success_and_refusal_without_denial_only_spans() {
use super::correlation_dispatch_tests::capture_request_spans;
use crate::test_support::request_tracing::span_attribute;

for sampled in [true, false] {
for (sampled, refuses) in [(true, true), (false, true), (true, false)] {
let evidence = capture_request_spans(sampled, async {
let fixture = Fixture::new(&[route("a", "/a/**")], &[("/a/**", "a", "aud-a")], 1);
fixture.resolver.fail_once.store(true, Ordering::SeqCst);
fixture.resolver.fail_once.store(refuses, Ordering::SeqCst);
let mut connection = fixture.connect();
let response = connection.exchange("GET", "/a/item", true).await;
assert!(connection.observes_eof().await);
if refuses {
assert!(connection.observes_eof().await);
}
let capture = connection.finish().await;
assert_eq!(capture.bytes_seen, 0);
if refuses {
assert_eq!(capture.bytes_seen, 0);
} else {
assert_eq!(capture.requests.len(), 1);
assert_authorization(&capture.requests[0], "aud-a");
}
response
})
.await;
evidence.assert_no_deny_span(sampled);
assert!(evidence.output.starts_with("HTTP/1.1 502"));
assert!(evidence.output.starts_with(if refuses {
"HTTP/1.1 502"
} else {
"HTTP/1.1 204"
}));
if sampled {
assert_eq!(
evidence
Expand All @@ -1024,12 +1036,42 @@ async fn token_grant_refusal_never_creates_a_denial_only_span() {
.count(),
1
);
assert!(
let preparation = evidence
.spans
.iter()
.find(|span| span.name == "openshell.credential.prepare")
.unwrap();
for (field, expected) in [
("openshell.credential.mechanism", "token_grant"),
("openshell.credential.placement", "header"),
(
"openshell.credential.preparation",
if refuses { "refused" } else { "prepared" },
),
] {
assert_eq!(
span_attribute(preparation, field).unwrap().as_str(),
expected
);
}
assert_eq!(
span_attribute(preparation, "openshell.credential.failure_code")
.map(|value| value.as_str()),
refuses.then_some(std::borrow::Cow::Borrowed("unavailable")),
);
assert_eq!(
span_attribute(preparation, "openshell.credential.failure_stage")
.map(|value| value.as_str()),
refuses.then_some(std::borrow::Cow::Borrowed("token_endpoint")),
);
assert_eq!(
evidence
.spans
.iter()
.all(|span| span.name != "openshell.upstream.send")
.any(|span| span.name == "openshell.upstream.send"),
!refuses,
);
assert!(!format!("{:?}", evidence.spans).contains("inert-aud-a"));
}
let records: Vec<_> = evidence
.events
Expand All @@ -1044,12 +1086,19 @@ async fn token_grant_refusal_never_creates_a_denial_only_span() {
.unwrap()
.starts_with("L7_REQUEST allow ")
);
assert_eq!(records[1]["action"], "Denied");
assert_eq!(
records[1]["action"],
if refuses { "Denied" } else { "Allowed" }
);
assert!(
records[1]["message"]
.as_str()
.unwrap()
.starts_with("Token grant failed ")
.starts_with(if refuses {
"Token grant failed "
} else {
"Token grant successful "
})
);
for record in records {
assert!(record.get("http_response").is_none());
Expand Down
14 changes: 14 additions & 0 deletions crates/openshell-supervisor-network/src/l7/request_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ impl RequestTrace {
if self.span.is_disabled() || !self.span.context().span().span_context().is_valid() {
return Span::none();
}
if matches!(stage, RequestStage::CredentialPrepare) {
return tracing::info_span!(
parent: &self.span,
"openshell.http.stage",
otel.name = stage.name(),
otel.status_code = tracing::field::Empty,
openshell.request.failure_reason = tracing::field::Empty,
openshell.credential.mechanism = tracing::field::Empty,
openshell.credential.placement = tracing::field::Empty,
openshell.credential.preparation = tracing::field::Empty,
openshell.credential.failure_code = tracing::field::Empty,
openshell.credential.failure_stage = tracing::field::Empty,
);
}
tracing::info_span!(
parent: &self.span,
"openshell.http.stage",
Expand Down
Loading