Skip to content

Commit 39a24fe

Browse files
committed
refactor(supervisor-network): reduce deny spans to one entry point
The deny-span API still carried paths for nested and non-HTTP deny scopes that no caller reaches now that only HTTP decisions get spans, and a decision whose first HTTP record was allowed opened its deny span with that record's destination and no reason. emit_decision is now the single entry point, behind a one-arm egress_emit!. It anchors the span on the denied HTTP record and sets optional attributes directly. Records that never share a span are emitted inline again, response diagnostics return their events so each error path adds its own terminal failure, and the trace tests share deny-span helpers and fixtures. Signed-off-by: Kris Hicks <khicks@nvidia.com>
1 parent 21ccf1c commit 39a24fe

9 files changed

Lines changed: 382 additions & 538 deletions

File tree

‎crates/openshell-supervisor-network/src/l7/middleware.rs‎

Lines changed: 58 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,10 +1008,11 @@ fn emit_middleware_events(
10081008
#[cfg(test)]
10091009
mod trace_tests {
10101010
use super::*;
1011-
use crate::telemetry::tests::{assert_deny_resolves, exported};
1011+
use crate::telemetry::tests::{assert_deny_resolves, assert_no_deny, deny_spans, exported};
1012+
use openshell_supervisor_middleware::{ChainOutcome, MiddlewareInvocation, NamespacedFinding};
10121013

1013-
fn finding() -> openshell_supervisor_middleware::NamespacedFinding {
1014-
openshell_supervisor_middleware::NamespacedFinding {
1014+
fn finding() -> NamespacedFinding {
1015+
NamespacedFinding {
10151016
middleware: "guard".to_string(),
10161017
finding: openshell_core::proto::Finding {
10171018
r#type: "test.finding".to_string(),
@@ -1023,22 +1024,31 @@ mod trace_tests {
10231024
}
10241025
}
10251026

1026-
fn assert_decision(
1027-
events: &[serde_json::Value],
1028-
received: &openshell_otel_test_support::ReceivedTraces,
1029-
denied: bool,
1030-
) {
1031-
assert!(!events.is_empty());
1032-
let deny_count = received
1033-
.spans
1034-
.iter()
1035-
.filter(|span| span.name == "supervisor.egress.deny")
1036-
.count();
1037-
assert_eq!(deny_count, usize::from(denied));
1038-
if denied {
1039-
assert_deny_resolves(events, received, true);
1040-
} else {
1041-
assert!(events.iter().all(|event| event.get("trace").is_none()));
1027+
fn outcome(
1028+
allowed: bool,
1029+
reason: &str,
1030+
applied: Vec<MiddlewareInvocation>,
1031+
findings: Vec<NamespacedFinding>,
1032+
) -> ChainOutcome {
1033+
ChainOutcome {
1034+
allowed,
1035+
reason: reason.to_string(),
1036+
body: vec![],
1037+
header_mutations: vec![],
1038+
findings,
1039+
metadata: std::collections::BTreeMap::new(),
1040+
applied,
1041+
denial: None,
1042+
}
1043+
}
1044+
1045+
fn request() -> crate::l7::provider::L7Request {
1046+
crate::l7::provider::L7Request {
1047+
action: "POST".to_string(),
1048+
target: "/".to_string(),
1049+
query_params: std::collections::HashMap::new(),
1050+
raw_header: vec![],
1051+
body_length: crate::l7::provider::BodyLength::None,
10421052
}
10431053
}
10441054

@@ -1048,24 +1058,11 @@ mod trace_tests {
10481058
"middleware_failed: guard",
10491059
"transformed_body_policy_evaluation_failed: unavailable",
10501060
] {
1051-
let outcome = openshell_supervisor_middleware::ChainOutcome {
1052-
allowed: false,
1053-
reason: reason.to_string(),
1054-
body: vec![],
1055-
header_mutations: vec![],
1056-
findings: vec![],
1057-
metadata: std::collections::BTreeMap::new(),
1058-
applied: vec![],
1059-
denial: None,
1060-
};
1061-
let request = crate::l7::provider::L7Request {
1062-
action: "POST".to_string(),
1063-
target: "/".to_string(),
1064-
query_params: std::collections::HashMap::new(),
1065-
raw_header: vec![],
1066-
body_length: crate::l7::provider::BodyLength::None,
1067-
};
1068-
let events = middleware_events(&L7EvalContext::default(), &request, &outcome);
1061+
let events = middleware_events(
1062+
&L7EvalContext::default(),
1063+
&request(),
1064+
&outcome(false, reason, vec![], vec![]),
1065+
);
10691066
assert_eq!(events.len(), 1, "{reason}");
10701067
let finding = events[0].to_json().unwrap();
10711068
assert_eq!(finding["class_uid"], 2004);
@@ -1077,44 +1074,36 @@ mod trace_tests {
10771074
async fn http_chain_outcomes_group_findings_and_preserve_allowed_controls() {
10781075
for allowed in [true, false] {
10791076
let (events, received) = exported("warn", || {
1080-
let outcome = openshell_supervisor_middleware::ChainOutcome {
1081-
allowed,
1082-
reason: if allowed {
1083-
"allowed"
1077+
let invocation = MiddlewareInvocation {
1078+
name: "guard".to_string(),
1079+
implementation: "test/guard".to_string(),
1080+
decision: if allowed {
1081+
openshell_core::proto::Decision::Allow
10841082
} else {
1085-
"middleware_failed: guard"
1086-
}
1087-
.to_string(),
1088-
body: vec![],
1089-
header_mutations: vec![],
1090-
findings: vec![finding()],
1091-
metadata: std::collections::BTreeMap::new(),
1092-
applied: vec![openshell_supervisor_middleware::MiddlewareInvocation {
1093-
name: "guard".to_string(),
1094-
implementation: "test/guard".to_string(),
1095-
decision: if allowed {
1096-
openshell_core::proto::Decision::Allow
1097-
} else {
1098-
openshell_core::proto::Decision::Deny
1099-
},
1100-
transformed: false,
1101-
failed: false,
1102-
}],
1103-
denial: None,
1083+
openshell_core::proto::Decision::Deny
1084+
},
1085+
transformed: false,
1086+
failed: false,
11041087
};
1105-
let request = crate::l7::provider::L7Request {
1106-
action: "POST".to_string(),
1107-
target: "/".to_string(),
1108-
query_params: std::collections::HashMap::new(),
1109-
raw_header: vec![],
1110-
body_length: crate::l7::provider::BodyLength::None,
1088+
let reason = if allowed {
1089+
"allowed"
1090+
} else {
1091+
"middleware_failed: guard"
11111092
};
1112-
emit_middleware_events(&L7EvalContext::default(), &request, &outcome);
1113-
tracing::info_span!("test.export-barrier").in_scope(|| {});
1093+
emit_middleware_events(
1094+
&L7EvalContext::default(),
1095+
&request(),
1096+
&outcome(allowed, reason, vec![invocation], vec![finding()]),
1097+
);
11141098
})
11151099
.await;
11161100
assert!(events.len() >= 2);
1117-
assert_decision(&events, &received, !allowed);
1101+
if allowed {
1102+
assert_no_deny(&events, &received);
1103+
} else {
1104+
assert_eq!(deny_spans(&received).len(), 1);
1105+
assert_deny_resolves(&events, &received, true);
1106+
}
11181107
}
11191108
}
11201109
}

‎crates/openshell-supervisor-network/src/l7/mod.rs‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -429,12 +429,8 @@ pub fn parse_l7_config(val: &regorus::Value) -> Option<L7EndpointConfig> {
429429
})
430430
}
431431

432-
pub(crate) fn build_uninspected_credential_finding(
433-
host: &str,
434-
policy_name: &str,
435-
surface: &str,
436-
) -> openshell_ocsf::OcsfEvent {
437-
openshell_ocsf::DetectionFindingBuilder::new(openshell_ocsf::ctx::ctx())
432+
pub(crate) fn emit_uninspected_credential_finding(host: &str, policy_name: &str, surface: &str) {
433+
let event = openshell_ocsf::DetectionFindingBuilder::new(openshell_ocsf::ctx::ctx())
438434
.severity(openshell_ocsf::SeverityId::High)
439435
.finding_info(openshell_ocsf::FindingInfo::new(
440436
"openshell.credentials.traffic_uninspectable",
@@ -447,7 +443,8 @@ pub(crate) fn build_uninspected_credential_finding(
447443
("disposition", "denied"),
448444
])
449445
.message("Uninspected credential-bearing traffic denied")
450-
.build()
446+
.build();
447+
crate::telemetry::egress_emit!(event);
451448
}
452449

453450
impl L7EndpointConfig {

‎crates/openshell-supervisor-network/src/l7/rest.rs‎

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,6 +1534,7 @@ fn emit_uninspected_body_credential_denial(
15341534
req.action, options.host, options.port
15351535
))
15361536
.build();
1537+
crate::telemetry::egress_emit!(event);
15371538
let finding = openshell_ocsf::DetectionFindingBuilder::new(openshell_ocsf::ctx::ctx())
15381539
.severity(openshell_ocsf::SeverityId::High)
15391540
.finding_info(openshell_ocsf::FindingInfo::new(
@@ -1547,7 +1548,7 @@ fn emit_uninspected_body_credential_denial(
15471548
])
15481549
.message("Request body credential placeholder denied")
15491550
.build();
1550-
crate::telemetry::emit_decision([event, finding], None);
1551+
crate::telemetry::egress_emit!(finding);
15511552
}
15521553

15531554
struct PreparedRequestBody {
@@ -6917,7 +6918,7 @@ mod tests {
69176918

69186919
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
69196920
async fn response_trace_failure_records_share_the_terminal_decision() {
6920-
use crate::telemetry::tests::{assert_deny_resolves, exported};
6921+
use crate::telemetry::tests::{assert_deny_resolves, deny_spans, exported};
69216922

69226923
// This relay fixture has no CONNECT span, so denies are roots at both
69236924
// levels. The thread-local export harness observes the relay task;
@@ -6966,11 +6967,7 @@ mod tests {
69666967
.collect();
69676968
assert!(companions.len() >= 2, "failure includes diagnostic records");
69686969
assert_eq!(
6969-
received
6970-
.spans
6971-
.iter()
6972-
.filter(|span| span.name == "supervisor.egress.deny")
6973-
.count(),
6970+
deny_spans(&received).len(),
69746971
1,
69756972
"one response stop is one exported decision"
69766973
);
@@ -7025,18 +7022,11 @@ mod tests {
70257022
}
70267023
});
70277024
});
7028-
tracing::info_span!("test.export-barrier").in_scope(|| {});
70297025
})
70307026
.await;
70317027
assert!(events.iter().any(|event| event["finding_info"]["uid"]
70327028
== "openshell.middleware.http_response_failure"));
7033-
assert!(events.iter().all(|event| event.get("trace").is_none()));
7034-
assert!(
7035-
!received
7036-
.spans
7037-
.iter()
7038-
.any(|span| span.name == "supervisor.egress.deny")
7039-
);
7029+
crate::telemetry::tests::assert_no_deny(&events, &received);
70407030
}
70417031
}
70427032

@@ -7093,20 +7083,13 @@ mod tests {
70937083
assert!(!String::from_utf8_lossy(&delivered.0).contains("502 Bad Gateway"));
70947084
});
70957085
});
7096-
tracing::info_span!("test.export-barrier").in_scope(|| {});
70977086
})
70987087
.await;
70997088
assert!(
71007089
events.iter().any(|event| event["finding_info"]["uid"]
71017090
== "openshell.middleware.http_response_failure")
71027091
);
7103-
assert!(events.iter().all(|event| event.get("trace").is_none()));
7104-
assert!(
7105-
!received
7106-
.spans
7107-
.iter()
7108-
.any(|span| span.name == "supervisor.egress.deny")
7109-
);
7092+
crate::telemetry::tests::assert_no_deny(&events, &received);
71107093
}
71117094

71127095
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
@@ -7129,17 +7112,10 @@ mod tests {
71297112
});
71307113
assert!(outcome.is_ok());
71317114
assert!(delivered.starts_with(b"HTTP/1.1 200 OK\r\n"));
7132-
tracing::info_span!("test.export-barrier").in_scope(|| {});
71337115
})
71347116
.await;
71357117
assert!(!events.is_empty());
7136-
assert!(events.iter().all(|event| event.get("trace").is_none()));
7137-
assert!(
7138-
!received
7139-
.spans
7140-
.iter()
7141-
.any(|span| span.name == "supervisor.egress.deny")
7142-
);
7118+
crate::telemetry::tests::assert_no_deny(&events, &received);
71437119
}
71447120
}
71457121

0 commit comments

Comments
 (0)