Skip to content

Commit 720e766

Browse files
committed
fix(observability): retain resource identities on middleware observations
Signed-off-by: Alex Fournier <afournier@nvidia.com>
2 parents 4f4c0e5 + c0f634b commit 720e766

13 files changed

Lines changed: 761 additions & 120 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
@@ -826,6 +826,16 @@ impl RequestCorrelation {
826826
.state
827827
.lock()
828828
.unwrap_or_else(std::sync::PoisonError::into_inner);
829+
if is_middleware_invocation(&incoming) {
830+
if incoming.action == Some(ActionId::Denied)
831+
&& let Some(OcsfEvent::HttpActivity(primary)) = &mut state.primary
832+
{
833+
merge_middleware_denial(primary, &incoming);
834+
}
835+
drop(state);
836+
self.emit_correlated(OcsfEvent::HttpActivity(incoming));
837+
return;
838+
}
829839
if !state.seeded && state.primary.is_some() {
830840
state.primary = Some(OcsfEvent::HttpActivity(incoming));
831841
state.seeded = true;
@@ -922,13 +932,16 @@ impl RequestCorrelation {
922932
if let Some(code) = status {
923933
http.http_response = Some(HttpResponse { code });
924934
}
925-
if let Some(upstream) = upstream_status.filter(|upstream| Some(*upstream) != status)
926-
&& let serde_json::Value::Object(unmapped) = http
927-
.base
928-
.unmapped
929-
.get_or_insert_with(|| serde_json::json!({}))
935+
if let serde_json::Value::Object(unmapped) = http
936+
.base
937+
.unmapped
938+
.get_or_insert_with(|| serde_json::json!({}))
930939
{
931-
unmapped.insert("upstream_response_code".into(), serde_json::json!(upstream));
940+
unmapped.insert("http_event_kind".into(), "request".into());
941+
if let Some(upstream) = upstream_status.filter(|upstream| Some(*upstream) != status)
942+
{
943+
unmapped.insert("upstream_response_code".into(), serde_json::json!(upstream));
944+
}
932945
}
933946
if blocked || outcome == Some(HttpOutcome::Denied) {
934947
if http.action != Some(ActionId::Denied) {
@@ -978,7 +991,9 @@ impl RequestCorrelation {
978991
.unwrap_or_else(std::sync::PoisonError::into_inner);
979992
state.resources.enrich(&mut event);
980993
state.trace.as_mut().map(|trace| {
981-
if let OcsfEvent::HttpActivity(http) = &event {
994+
if let OcsfEvent::HttpActivity(http) = &event
995+
&& !is_middleware_invocation(http)
996+
{
982997
trace.observe_http(http);
983998
}
984999
trace.span()
@@ -1005,6 +1020,32 @@ fn is_failure(event: &HttpActivityEvent) -> bool {
10051020
event.action == Some(ActionId::Denied) || event.base.status == Some(StatusId::Failure)
10061021
}
10071022

1023+
fn is_middleware_invocation(event: &HttpActivityEvent) -> bool {
1024+
event
1025+
.base
1026+
.unmapped
1027+
.as_ref()
1028+
.and_then(|fields| fields.get("http_event_kind"))
1029+
.and_then(serde_json::Value::as_str)
1030+
== Some("middleware_invocation")
1031+
}
1032+
1033+
fn merge_middleware_denial(primary: &mut HttpActivityEvent, invocation: &HttpActivityEvent) {
1034+
primary.action = invocation.action;
1035+
primary.disposition = invocation.disposition;
1036+
primary.base.status = Some(StatusId::Failure);
1037+
raise_severity(primary, invocation.base.severity);
1038+
if invocation.base.message.is_some() {
1039+
primary.base.message.clone_from(&invocation.base.message);
1040+
}
1041+
if invocation.base.status_detail.is_some() {
1042+
primary
1043+
.base
1044+
.status_detail
1045+
.clone_from(&invocation.base.status_detail);
1046+
}
1047+
}
1048+
10081049
fn raise_severity(event: &mut HttpActivityEvent, severity: SeverityId) {
10091050
if event.base.severity.as_u8() < severity.as_u8() {
10101051
event.base.severity = severity;
@@ -1169,17 +1210,19 @@ pub fn emit_phase_events(
11691210
};
11701211
for mut event in events {
11711212
if let OcsfEvent::HttpActivity(http) = &event {
1172-
// Routine invocation records belong to the phase span. A late
1173-
// failure is separate from the already-written primary record.
1174-
let emitted = correlation
1175-
.state
1176-
.lock()
1177-
.unwrap_or_else(std::sync::PoisonError::into_inner)
1178-
.primary
1179-
.is_none();
1180-
if !emitted || http.base.status != Some(StatusId::Failure) {
1213+
if !is_middleware_invocation(http) {
1214+
correlation.emit(event);
11811215
continue;
11821216
}
1217+
if http.action == Some(ActionId::Denied)
1218+
&& let Some(OcsfEvent::HttpActivity(primary)) = &mut correlation
1219+
.state
1220+
.lock()
1221+
.unwrap_or_else(std::sync::PoisonError::into_inner)
1222+
.primary
1223+
{
1224+
merge_middleware_denial(primary, http);
1225+
}
11831226
}
11841227
correlation
11851228
.state

0 commit comments

Comments
 (0)