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
7 changes: 7 additions & 0 deletions crates/openshell-supervisor-network/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,10 @@ 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.

The request scope pins the captured policy generation, selected endpoint and
first credential snapshot. Existing audit records gain only the selections
known at their emission; earlier records are not rewritten after a later lookup.
The provider revision fingerprints the complete credential environment, while
the installation ID distinguishes repairs at the same revision. Neither proves
that a credential was injected.
31 changes: 31 additions & 0 deletions crates/openshell-supervisor-network/src/l7/correlation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

//! Request receipts and emission-time correlation without changing audit records.

mod resources;

use std::fmt;
use std::future::Future;
use std::sync::atomic::{AtomicU64, Ordering};
Expand All @@ -15,6 +17,7 @@ use crate::l7::provider::L7Request;
use crate::l7::relay::L7EvalContext;
pub use crate::l7::request_trace::RequestStage;
use crate::l7::request_trace::RequestTrace;
use resources::RequestResources;

tokio::task_local! {
static CURRENT_REQUEST: Arc<Mutex<Option<Arc<RequestScope>>>>;
Expand Down Expand Up @@ -83,6 +86,14 @@ pub fn record_current_failure(outcome: HttpOutcome, reason: &'static str) {
}
}

pub fn record_current_provider_snapshot(revision: u64, installation_id: &str) {
if let Some(scope) = current_request().filter(|scope| scope.inspected) {
scope
.resources
.record_provider(revision, installation_id, &scope.span());
}
}

fn enrich_request(event: &mut openshell_ocsf::BaseEventData) {
let Some(scope) = current_request().filter(|scope| scope.inspected) else {
return;
Expand All @@ -97,6 +108,7 @@ fn enrich_request(event: &mut openshell_ocsf::BaseEventData) {
.or_insert_with(|| event_kind.into());
fields.insert("request_sequence".into(), sequence.into());
}
scope.resources.enrich(event);
}

fn default_event_kind(class_uid: u32) -> &'static str {
Expand Down Expand Up @@ -125,6 +137,7 @@ struct RequestScope {
request_id: String,
inspected: bool,
sequence: AtomicU64,
resources: RequestResources,
trace: Mutex<Option<RequestTrace>>,
}

Expand Down Expand Up @@ -242,6 +255,7 @@ impl RequestCorrelation {
request_id: uuid::Uuid::new_v4().to_string(),
inspected: false,
sequence: AtomicU64::new(1),
resources: RequestResources::default(),
trace: Mutex::new(None),
})
}
Expand All @@ -262,6 +276,7 @@ impl RequestCorrelation {
request_id,
inspected: true,
sequence: AtomicU64::new(1),
resources: RequestResources::default(),
trace: Mutex::new(Some(trace)),
})
}
Expand All @@ -284,6 +299,22 @@ impl RequestCorrelation {
&self.scope.request_id
}

pub(crate) fn record_policy_generation(&self, generation: u64) {
if self.scope.inspected {
self.scope
.resources
.record_generation(generation, &self.scope.span());
}
}

pub(crate) fn record_selected_endpoint(&self, config: &crate::l7::L7EndpointConfig) {
if self.scope.inspected {
self.scope
.resources
.record_endpoint(config, &self.scope.span());
}
}

pub(crate) fn capture_error(&self, error: miette::Report) -> miette::Report {
if !self.scope.inspected || error.downcast_ref::<CapturedRequestError>().is_some() {
return error;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

use std::sync::OnceLock;

use openshell_ocsf::BaseEventData;
use tracing::Span;

use crate::l7::L7EndpointConfig;

#[derive(Debug, Default)]
pub(super) struct RequestResources {
generation: OnceLock<String>,
endpoint: OnceLock<SelectedEndpoint>,
provider: OnceLock<SelectedProvider>,
}

#[derive(Debug)]
struct SelectedEndpoint {
policy_hash: String,
endpoint_id: String,
}

#[derive(Debug)]
struct SelectedProvider {
revision: String,
installation_id: String,
}

impl RequestResources {
pub(super) fn record_generation(&self, generation: u64, span: &Span) {
let generation = self.generation.get_or_init(|| generation.to_string());
span.record("openshell.policy.runtime_generation", generation.as_str());
}

pub(super) fn record_endpoint(&self, config: &L7EndpointConfig, span: &Span) {
if config.policy_hash.is_empty() || config.endpoint_id.is_empty() {
return;
}
let selected = self.endpoint.get_or_init(|| SelectedEndpoint {
policy_hash: config.policy_hash.clone(),
endpoint_id: config.endpoint_id.clone(),
});
span.record("openshell.policy.hash", selected.policy_hash.as_str());
span.record("openshell.endpoint.id", selected.endpoint_id.as_str());
}

pub(super) fn record_provider(&self, revision: u64, installation_id: &str, span: &Span) {
let selected = self.provider.get_or_init(|| SelectedProvider {
revision: revision.to_string(),
installation_id: installation_id.to_owned(),
});
span.record(
"openshell.provider.environment_revision",
selected.revision.as_str(),
);
span.record(
"openshell.provider.credential_installation_id",
selected.installation_id.as_str(),
);
}

pub(super) fn enrich(&self, event: &mut BaseEventData) {
if let Some(generation) = self.generation.get() {
event.add_unmapped("policy_runtime_generation", generation.as_str());
}
if let Some(selected) = self.endpoint.get() {
event.add_unmapped("policy_hash", selected.policy_hash.as_str());
event.add_unmapped("endpoint_id", selected.endpoint_id.as_str());
}
// Per-control records contain only selections already made when the
// record is emitted; a later credential lookup cannot rewrite them.
if let Some(selected) = self.provider.get() {
event.add_unmapped("provider_environment_revision", selected.revision.as_str());
event.add_unmapped(
"provider_credential_installation_id",
selected.installation_id.as_str(),
);
}
}
}
158 changes: 158 additions & 0 deletions crates/openshell-supervisor-network/src/l7/correlation/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,164 @@ fn unused_request_scope_does_not_create_an_audit_record() {
assert!(capture.json_events().is_empty());
}

fn resource_endpoint(policy_hash: &str, endpoint_id: &str) -> crate::l7::L7EndpointConfig {
crate::l7::parse_l7_config(
&regorus::Value::from_json_str(
&serde_json::json!({
"protocol": "rest", "policy_hash": policy_hash, "endpoint_id": endpoint_id,
})
.to_string(),
)
.unwrap(),
)
.unwrap()
}

#[tokio::test]
async fn resource_snapshots_enrich_only_subsequent_records_without_trace_dependency() {
use openshell_core::provider_credentials::ProviderCredentialState;
use opentelemetry_sdk::trace::Sampler;

let _lock = openshell_otel_test_support::tracing_test_lock().await;
for sampler in [None, Some(Sampler::AlwaysOn)] {
let sampled = sampler.is_some();
let capture = RequestTracing::new(sampler);
let state = ProviderCredentialState::from_child_env_snapshot(
u64::MAX,
[("SECRET".into(), "RESOURCE-SECRET-SENTINEL".into())].into(),
);
let before = state.snapshot();
let after = scope_connection(async {
let mut ctx = L7EvalContext {
provider_credentials: Some(state.clone()),
..context()
};
let correlation = RequestCorrelation::new(&request(), &ctx);
correlation.record_policy_generation(u64::MAX);
correlation.record_selected_endpoint(&resource_endpoint("hash-a", "endpoint-a"));
correlation.emit(allowed());
crate::l7::token_grant_injection::pin_live_credentials(&mut ctx).unwrap();
correlation.emit(deny());
state.install_child_env_snapshot(u64::MAX - 1, std::collections::HashMap::new());
let after = state.snapshot();
crate::l7::token_grant_injection::pin_live_credentials(&mut ctx).unwrap();
correlation.record_policy_generation(2);
correlation.record_selected_endpoint(&resource_endpoint("hash-b", "endpoint-b"));
correlation.emit(allowed());
correlation.record_outcome(HttpOutcome::Allowed);
drop(correlation);

let next = RequestCorrelation::new(&request(), &ctx);
next.record_policy_generation(2);
next.record_selected_endpoint(&resource_endpoint("hash-b", "endpoint-b"));
crate::l7::token_grant_injection::pin_live_credentials(&mut ctx).unwrap();
next.emit(allowed());
next.record_outcome(HttpOutcome::Allowed);
after
})
.with_subscriber(capture.subscriber())
.await;
let events = capture.events.json_events();
assert_eq!(events.len(), 4);
assert!(events[0]["unmapped"]["provider_environment_revision"].is_null());
assert!(events[0]["unmapped"]["provider_credential_installation_id"].is_null());
for (index, event) in events[..3].iter().enumerate() {
assert_eq!(
event["unmapped"]["policy_runtime_generation"],
u64::MAX.to_string()
);
assert_eq!(event["unmapped"]["policy_hash"], "hash-a");
assert_eq!(event["unmapped"]["endpoint_id"], "endpoint-a");
assert_eq!(event["unmapped"]["request_sequence"], index + 1);
assert_eq!(
event["metadata"]["correlation_uid"],
events[0]["metadata"]["correlation_uid"]
);
}
for event in &events[1..3] {
assert_eq!(
event["unmapped"]["provider_environment_revision"],
u64::MAX.to_string()
);
assert_eq!(
event["unmapped"]["provider_credential_installation_id"],
before.installation_id
);
}
assert_eq!(events[1]["action"], "Denied");
assert_eq!(events[1]["unmapped"]["middleware_phase"], "response");
assert_eq!(events[1]["unmapped"]["transformed"], true);
assert_eq!(events[3]["unmapped"]["policy_runtime_generation"], "2");
assert_eq!(events[3]["unmapped"]["policy_hash"], "hash-b");
assert_eq!(events[3]["unmapped"]["endpoint_id"], "endpoint-b");
assert_eq!(
events[3]["unmapped"]["provider_environment_revision"],
(u64::MAX - 1).to_string()
);
assert_eq!(
events[3]["unmapped"]["provider_credential_installation_id"],
after.installation_id
);
assert_eq!(events[3]["unmapped"]["request_sequence"], 1);
assert_ne!(
events[0]["metadata"]["correlation_uid"],
events[3]["metadata"]["correlation_uid"]
);
let spans = capture.spans();
assert_eq!(spans.len(), usize::from(sampled) * 2);
if sampled {
let root = spans
.iter()
.find(|span| {
span_attribute(span, "openshell.request.id")
.unwrap()
.as_str()
== events[0]["metadata"]["correlation_uid"].as_str().unwrap()
})
.unwrap();
for (key, expected) in [
("openshell.policy.runtime_generation", u64::MAX.to_string()),
("openshell.policy.hash", "hash-a".into()),
("openshell.endpoint.id", "endpoint-a".into()),
(
"openshell.provider.environment_revision",
u64::MAX.to_string(),
),
(
"openshell.provider.credential_installation_id",
before.installation_id.clone(),
),
] {
assert_eq!(
span_attribute(root, key).unwrap().as_str(),
expected,
"{key}"
);
}
}
assert!(!format!("{events:?}{spans:?}").contains("RESOURCE-SECRET-SENTINEL"));
capture.shutdown();
}
}

#[tokio::test]
async fn uninspected_traffic_does_not_claim_resource_selections() {
let capture = OcsfCapture::default();
let _subscriber = capture.install();
scope_connection(async {
let correlation = RequestCorrelation::uninspected();
correlation.record_policy_generation(4);
correlation.record_selected_endpoint(&resource_endpoint("hash", "endpoint"));
record_current_provider_snapshot(5, "installation");
correlation.emit(allowed());
})
.await;
let events = capture.json_events();
assert_eq!(events.len(), 1);
assert!(events[0]["metadata"]["correlation_uid"].is_null());
assert!(events[0]["unmapped"].is_null());
}

#[test]
fn request_join_does_not_depend_on_export_and_never_creates_denial_spans() {
use opentelemetry_sdk::trace::Sampler;
Expand Down
11 changes: 8 additions & 3 deletions crates/openshell-supervisor-network/src/l7/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2259,7 +2259,9 @@ mod tests {

#[tokio::test]
async fn non_mcp_endpoint_never_begins_an_observation() {
use openshell_core::endpoint_status::{EndpointConfigVersion, endpoint_status_channel};
use openshell_core::endpoint_status::{
EndpointConfigVersion, EndpointInventoryEntry, endpoint_status_channel,
};

let (sender, _receiver) = endpoint_status_channel();
sender
Expand All @@ -2268,12 +2270,15 @@ mod tests {
policy_hash: "policy".to_string(),
provider_env_revision: 1,
},
Vec::new(),
vec![EndpointInventoryEntry {
endpoint_id: "endpoint:v1:not-mcp".to_string(),
uses_provider_credentials: false,
}],
)
.await
.expect("install MCP test configuration");
let value = regorus::Value::from_json_str(
r#"{"protocol":"rest","endpoint_id":"endpoint:v1:not-mcp"}"#,
r#"{"protocol":"rest","endpoint_id":"endpoint:v1:not-mcp","policy_hash":"policy"}"#,
)
.expect("parse REST config JSON");
let config = parse_l7_config(&value).expect("parse REST config");
Expand Down
Loading