Skip to content

Commit 967d7d0

Browse files
committed
fix(observability): carry preserved audit records through terminal delivery
Signed-off-by: Alex Fournier <afournier@nvidia.com>
2 parents d8ef178 + d0f6e48 commit 967d7d0

14 files changed

Lines changed: 823 additions & 128 deletions

File tree

‎crates/openshell-supervisor-middleware/src/lib.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
//! Supervisor middleware registration and chain execution.
5+
//!
6+
//! # Rust API compatibility
7+
//!
8+
//! Typed outcome diagnostics add fields to [`ChainOutcome`], [`HttpResponseInvocation`],
9+
//! [`HttpResponseMiddlewareFailure`], [`HttpResponseFinish`] and [`HttpResponseDiagnostics`].
10+
//! Direct Rust consumers must update struct literals and exhaustive destructuring.
11+
//! `HttpResponseMiddlewareFailure` now has private classification state; obtain it from
12+
//! runner or session operations. SDK/protobuf contracts and middleware implementation
13+
//! trait signatures are unchanged.
514
615
mod binding_policy;
716
mod diagnostics;

‎crates/openshell-supervisor-network/README.md‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,12 @@ L4 and passthrough traffic do not provide this header guarantee.
6666

6767
The audit state commits one primary HTTP Activity record when the final response
6868
head is written, with an early-termination fallback. It retains the original
69-
request and process context while consolidating middleware and credential
70-
outcomes. Routine outcomes do not add records; existing security findings and
71-
post-header failure records remain distinct. A process crash or indefinitely
72-
hung request cannot be finalized by the fallback.
69+
request and process context. Middleware invocation records remain separate,
70+
request-correlated observations, including after the primary is committed. They
71+
retain attribution and transformation details without changing the final
72+
response status or consuming the late-failure budget. Security findings remain
73+
separate. A process crash or indefinitely hung request cannot be finalized by
74+
the fallback.
7375

7476
Request IDs and audit outcomes do not depend on OpenTelemetry, sampling or
7577
collector availability. Inspected requests have a supervisor-owned request span

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

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,16 @@ impl RequestCorrelation {
832832
.state
833833
.lock()
834834
.unwrap_or_else(std::sync::PoisonError::into_inner);
835+
if is_middleware_invocation(&incoming) {
836+
if incoming.action == Some(ActionId::Denied)
837+
&& let Some(OcsfEvent::HttpActivity(primary)) = &mut state.primary
838+
{
839+
merge_middleware_denial(primary, &incoming);
840+
}
841+
drop(state);
842+
self.emit_correlated(OcsfEvent::HttpActivity(incoming));
843+
return;
844+
}
835845
if !state.seeded && state.primary.is_some() {
836846
state.primary = Some(OcsfEvent::HttpActivity(incoming));
837847
state.seeded = true;
@@ -929,13 +939,16 @@ impl RequestCorrelation {
929939
if let Some(code) = status {
930940
http.http_response = Some(HttpResponse { code });
931941
}
932-
if let Some(upstream) = upstream_status.filter(|upstream| Some(*upstream) != status)
933-
&& let serde_json::Value::Object(unmapped) = http
934-
.base
935-
.unmapped
936-
.get_or_insert_with(|| serde_json::json!({}))
942+
if let serde_json::Value::Object(unmapped) = http
943+
.base
944+
.unmapped
945+
.get_or_insert_with(|| serde_json::json!({}))
937946
{
938-
unmapped.insert("upstream_response_code".into(), serde_json::json!(upstream));
947+
unmapped.insert("http_event_kind".into(), "request".into());
948+
if let Some(upstream) = upstream_status.filter(|upstream| Some(*upstream) != status)
949+
{
950+
unmapped.insert("upstream_response_code".into(), serde_json::json!(upstream));
951+
}
939952
}
940953
if blocked || outcome == Some(HttpOutcome::Denied) {
941954
if http.action != Some(ActionId::Denied) {
@@ -985,7 +998,9 @@ impl RequestCorrelation {
985998
.unwrap_or_else(std::sync::PoisonError::into_inner);
986999
state.resources.enrich(&mut event);
9871000
state.trace.as_mut().map(|trace| {
988-
if let OcsfEvent::HttpActivity(http) = &event {
1001+
if let OcsfEvent::HttpActivity(http) = &event
1002+
&& !is_middleware_invocation(http)
1003+
{
9891004
trace.observe_http(http);
9901005
}
9911006
trace.span()
@@ -1012,6 +1027,32 @@ fn is_failure(event: &HttpActivityEvent) -> bool {
10121027
event.action == Some(ActionId::Denied) || event.base.status == Some(StatusId::Failure)
10131028
}
10141029

1030+
fn is_middleware_invocation(event: &HttpActivityEvent) -> bool {
1031+
event
1032+
.base
1033+
.unmapped
1034+
.as_ref()
1035+
.and_then(|fields| fields.get("http_event_kind"))
1036+
.and_then(serde_json::Value::as_str)
1037+
== Some("middleware_invocation")
1038+
}
1039+
1040+
fn merge_middleware_denial(primary: &mut HttpActivityEvent, invocation: &HttpActivityEvent) {
1041+
primary.action = invocation.action;
1042+
primary.disposition = invocation.disposition;
1043+
primary.base.status = Some(StatusId::Failure);
1044+
raise_severity(primary, invocation.base.severity);
1045+
if invocation.base.message.is_some() {
1046+
primary.base.message.clone_from(&invocation.base.message);
1047+
}
1048+
if invocation.base.status_detail.is_some() {
1049+
primary
1050+
.base
1051+
.status_detail
1052+
.clone_from(&invocation.base.status_detail);
1053+
}
1054+
}
1055+
10151056
fn raise_severity(event: &mut HttpActivityEvent, severity: SeverityId) {
10161057
if event.base.severity.as_u8() < severity.as_u8() {
10171058
event.base.severity = severity;
@@ -1176,17 +1217,19 @@ pub fn emit_phase_events(
11761217
};
11771218
for mut event in events {
11781219
if let OcsfEvent::HttpActivity(http) = &event {
1179-
// Routine invocation records belong to the phase span. A late
1180-
// failure is separate from the already-written primary record.
1181-
let emitted = correlation
1182-
.state
1183-
.lock()
1184-
.unwrap_or_else(std::sync::PoisonError::into_inner)
1185-
.primary
1186-
.is_none();
1187-
if !emitted || http.base.status != Some(StatusId::Failure) {
1220+
if !is_middleware_invocation(http) {
1221+
correlation.emit(event);
11881222
continue;
11891223
}
1224+
if http.action == Some(ActionId::Denied)
1225+
&& let Some(OcsfEvent::HttpActivity(primary)) = &mut correlation
1226+
.state
1227+
.lock()
1228+
.unwrap_or_else(std::sync::PoisonError::into_inner)
1229+
.primary
1230+
{
1231+
merge_middleware_denial(primary, http);
1232+
}
11901233
}
11911234
correlation
11921235
.state

0 commit comments

Comments
 (0)