Skip to content

Commit 2cbf1d5

Browse files
committed
refactor(ocsf): attribute events to session-bound contexts
A supervisor process that hosts several sandboxes cannot use one process-wide OCSF context: every event would carry the wrong sandbox identity, and middleware requests read their sandbox identity from the same context. Builders now accept a ContextRef, either a borrowed context or a context shared by a hosted session, so per-session contexts need not be leaked. ctx() returns the context bound to the calling thread when present and falls back to the process-wide context otherwise. OCSF JSON output settings can likewise be bound per thread, and OpenTelemetry can parent a span from an explicit W3C trace context instead of process environment. A dedicated supervisor keeps using the process-wide context. Signed-off-by: Drew Newberry <anewberry@nvidia.com>
1 parent 856ad8a commit 2cbf1d5

22 files changed

Lines changed: 181 additions & 83 deletions

File tree

‎crates/openshell-ocsf/src/builders/api_activity.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
//! Builder for API Activity [6003] events.
55
6-
use crate::builders::EventContext;
6+
use crate::builders::ContextRef;
77
use crate::enums::{SeverityId, StatusId};
88
use crate::events::base_event::BaseEventData;
99
use crate::events::{ApiActivityEvent, OcsfEvent};
@@ -26,7 +26,7 @@ fn sanitize_model_name(name: &str) -> String {
2626

2727
/// Builder for API Activity [6003] events.
2828
pub struct ApiActivityBuilder<'a> {
29-
ctx: &'a EventContext,
29+
ctx: ContextRef<'a>,
3030
severity: SeverityId,
3131
status: Option<StatusId>,
3232
message: Option<String>,
@@ -39,9 +39,9 @@ pub struct ApiActivityBuilder<'a> {
3939

4040
impl<'a> ApiActivityBuilder<'a> {
4141
#[must_use]
42-
pub fn new(ctx: &'a EventContext, operation: impl Into<String>) -> Self {
42+
pub fn new(ctx: impl Into<ContextRef<'a>>, operation: impl Into<String>) -> Self {
4343
Self {
44-
ctx,
44+
ctx: ctx.into(),
4545
severity: SeverityId::Informational,
4646
status: None,
4747
message: None,

‎crates/openshell-ocsf/src/builders/authentication.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
//! Builder for Authentication [3002] events.
55
6-
use crate::builders::EventContext;
6+
use crate::builders::ContextRef;
77
use crate::enums::{ActionId, AuthProtocolId, DispositionId, SeverityId, StatusId};
88
use crate::events::base_event::BaseEventData;
99
use crate::events::{AuthenticationEvent, OcsfEvent};
1010
use crate::objects::{Endpoint, HttpRequest, Service, User};
1111

1212
/// Builder for Authentication [3002] Logon events.
1313
pub struct AuthenticationBuilder<'a> {
14-
ctx: &'a EventContext,
14+
ctx: ContextRef<'a>,
1515
user: User,
1616
service: Service,
1717
auth_protocol: Option<AuthProtocolId>,
@@ -28,9 +28,9 @@ pub struct AuthenticationBuilder<'a> {
2828

2929
impl<'a> AuthenticationBuilder<'a> {
3030
#[must_use]
31-
pub fn new(ctx: &'a EventContext, user: User, service: Service) -> Self {
31+
pub fn new(ctx: impl Into<ContextRef<'a>>, user: User, service: Service) -> Self {
3232
Self {
33-
ctx,
33+
ctx: ctx.into(),
3434
user,
3535
service,
3636
auth_protocol: None,

‎crates/openshell-ocsf/src/builders/base.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
//! Builder for Base Event [0].
55
6-
use crate::builders::EventContext;
6+
use crate::builders::ContextRef;
77
use crate::enums::{SeverityId, StatusId};
88
use crate::events::base_event::BaseEventData;
99
use crate::events::{BaseEvent, OcsfEvent};
1010

1111
/// Builder for Base Event [0] — events without a specific OCSF class.
1212
pub struct BaseEventBuilder<'a> {
13-
ctx: &'a EventContext,
13+
ctx: ContextRef<'a>,
1414
severity: SeverityId,
1515
status: Option<StatusId>,
1616
message: Option<String>,
@@ -20,9 +20,9 @@ pub struct BaseEventBuilder<'a> {
2020

2121
impl<'a> BaseEventBuilder<'a> {
2222
#[must_use]
23-
pub fn new(ctx: &'a EventContext) -> Self {
23+
pub fn new(ctx: impl Into<ContextRef<'a>>) -> Self {
2424
Self {
25-
ctx,
25+
ctx: ctx.into(),
2626
severity: SeverityId::Informational,
2727
status: None,
2828
message: None,

‎crates/openshell-ocsf/src/builders/config.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
//! Builder for Device Config State Change [5019] events.
55
6-
use crate::builders::EventContext;
6+
use crate::builders::ContextRef;
77
use crate::enums::{SecurityLevelId, SeverityId, StateId, StatusId};
88
use crate::events::base_event::BaseEventData;
99
use crate::events::{DeviceConfigStateChangeEvent, OcsfEvent};
1010

1111
/// Builder for Device Config State Change [5019] events.
1212
pub struct ConfigStateChangeBuilder<'a> {
13-
ctx: &'a EventContext,
13+
ctx: ContextRef<'a>,
1414
severity: SeverityId,
1515
status: Option<StatusId>,
1616
state_id: Option<StateId>,
@@ -23,9 +23,9 @@ pub struct ConfigStateChangeBuilder<'a> {
2323

2424
impl<'a> ConfigStateChangeBuilder<'a> {
2525
#[must_use]
26-
pub fn new(ctx: &'a EventContext) -> Self {
26+
pub fn new(ctx: impl Into<ContextRef<'a>>) -> Self {
2727
Self {
28-
ctx,
28+
ctx: ctx.into(),
2929
severity: SeverityId::Informational,
3030
status: None,
3131
state_id: None,

‎crates/openshell-ocsf/src/builders/finding.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
//! Builder for Detection Finding [2004] events.
55
6-
use crate::builders::EventContext;
6+
use crate::builders::ContextRef;
77
use crate::enums::{ActionId, ActivityId, ConfidenceId, DispositionId, RiskLevelId, SeverityId};
88
use crate::events::base_event::BaseEventData;
99
use crate::events::{DetectionFindingEvent, OcsfEvent};
1010
use crate::objects::{Attack, Evidence, FindingInfo, Remediation};
1111

1212
/// Builder for Detection Finding [2004] events.
1313
pub struct DetectionFindingBuilder<'a> {
14-
ctx: &'a EventContext,
14+
ctx: ContextRef<'a>,
1515
activity: ActivityId,
1616
severity: SeverityId,
1717
action: Option<ActionId>,
@@ -30,9 +30,9 @@ pub struct DetectionFindingBuilder<'a> {
3030

3131
impl<'a> DetectionFindingBuilder<'a> {
3232
#[must_use]
33-
pub fn new(ctx: &'a EventContext) -> Self {
33+
pub fn new(ctx: impl Into<ContextRef<'a>>) -> Self {
3434
Self {
35-
ctx,
35+
ctx: ctx.into(),
3636
activity: ActivityId::Open,
3737
severity: SeverityId::Medium,
3838
action: None,

‎crates/openshell-ocsf/src/builders/http.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
//! Builder for HTTP Activity [4002] events.
55
6-
use crate::builders::EventContext;
6+
use crate::builders::ContextRef;
77
use crate::enums::{ActionId, ActivityId, DispositionId, SeverityId, StatusId};
88
use crate::events::base_event::BaseEventData;
99
use crate::events::{HttpActivityEvent, OcsfEvent};
1010
use crate::objects::{Actor, Endpoint, FirewallRule, HttpRequest, HttpResponse};
1111

1212
/// Builder for HTTP Activity [4002] events.
1313
pub struct HttpActivityBuilder<'a, Context = MissingHttpContext> {
14-
ctx: &'a EventContext,
14+
ctx: ContextRef<'a>,
1515
activity: ActivityId,
1616
action: Option<ActionId>,
1717
disposition: Option<DispositionId>,
@@ -56,9 +56,9 @@ impl<'a> HttpActivityBuilder<'a, MissingHttpContext> {
5656
/// HttpActivityBuilder::new(&ctx).build();
5757
/// ```
5858
#[must_use]
59-
pub fn new(ctx: &'a EventContext) -> Self {
59+
pub fn new(ctx: impl Into<ContextRef<'a>>) -> Self {
6060
Self {
61-
ctx,
61+
ctx: ctx.into(),
6262
activity: ActivityId::Unknown,
6363
action: None,
6464
disposition: None,

‎crates/openshell-ocsf/src/builders/lifecycle.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
//! Builder for Application Lifecycle [6002] events.
55
6-
use crate::builders::EventContext;
6+
use crate::builders::ContextRef;
77
use crate::enums::{ActivityId, SeverityId, StatusId};
88
use crate::events::base_event::BaseEventData;
99
use crate::events::{ApplicationLifecycleEvent, OcsfEvent};
1010
use crate::objects::Product;
1111

1212
/// Builder for Application Lifecycle [6002] events.
1313
pub struct AppLifecycleBuilder<'a> {
14-
ctx: &'a EventContext,
14+
ctx: ContextRef<'a>,
1515
activity: ActivityId,
1616
severity: SeverityId,
1717
status: Option<StatusId>,
@@ -20,9 +20,9 @@ pub struct AppLifecycleBuilder<'a> {
2020

2121
impl<'a> AppLifecycleBuilder<'a> {
2222
#[must_use]
23-
pub fn new(ctx: &'a EventContext) -> Self {
23+
pub fn new(ctx: impl Into<ContextRef<'a>>) -> Self {
2424
Self {
25-
ctx,
25+
ctx: ctx.into(),
2626
activity: ActivityId::Unknown,
2727
severity: SeverityId::Informational,
2828
status: None,

‎crates/openshell-ocsf/src/builders/mod.rs‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ pub use process::ProcessActivityBuilder;
157157
pub use ssh::SshActivityBuilder;
158158

159159
use std::net::IpAddr;
160+
use std::sync::Arc;
160161

161162
use crate::OCSF_VERSION;
162163
use crate::enums::StatusId;
@@ -199,6 +200,45 @@ pub struct EventContext {
199200
pub origin: EventOrigin,
200201
}
201202

203+
/// Event context borrowed from the caller or shared by a hosted session.
204+
///
205+
/// Builders accept either form, so events attributed to a hosted supervisor
206+
/// session need not leak a `'static` context for each session.
207+
#[derive(Debug, Clone)]
208+
pub enum ContextRef<'a> {
209+
Borrowed(&'a EventContext),
210+
Shared(Arc<EventContext>),
211+
}
212+
213+
impl std::ops::Deref for ContextRef<'_> {
214+
type Target = EventContext;
215+
216+
fn deref(&self) -> &EventContext {
217+
match self {
218+
Self::Borrowed(context) => context,
219+
Self::Shared(context) => context,
220+
}
221+
}
222+
}
223+
224+
impl<'a> From<&'a EventContext> for ContextRef<'a> {
225+
fn from(context: &'a EventContext) -> Self {
226+
Self::Borrowed(context)
227+
}
228+
}
229+
230+
impl From<Arc<EventContext>> for ContextRef<'_> {
231+
fn from(context: Arc<EventContext>) -> Self {
232+
Self::Shared(context)
233+
}
234+
}
235+
236+
impl<'a> From<&'a ContextRef<'_>> for ContextRef<'a> {
237+
fn from(context: &'a ContextRef<'_>) -> Self {
238+
Self::Borrowed(context)
239+
}
240+
}
241+
202242
impl EventContext {
203243
/// Build the OCSF `Metadata` object for any event.
204244
#[must_use]

‎crates/openshell-ocsf/src/builders/network.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
//! Builder for Network Activity [4001] events.
55
6-
use crate::builders::EventContext;
6+
use crate::builders::ContextRef;
77
use crate::enums::{ActionId, ActivityId, DispositionId, SeverityId, StatusId};
88
use crate::events::base_event::BaseEventData;
99
use crate::events::{NetworkActivityEvent, OcsfEvent};
1010
use crate::objects::{Actor, ConnectionInfo, Endpoint, FirewallRule};
1111

1212
/// Builder for Network Activity [4001] events.
1313
pub struct NetworkActivityBuilder<'a, EndpointState = MissingNetworkEndpoint> {
14-
ctx: &'a EventContext,
14+
ctx: ContextRef<'a>,
1515
activity: ActivityId,
1616
activity_name: Option<String>,
1717
action: Option<ActionId>,
@@ -58,9 +58,9 @@ impl<'a> NetworkActivityBuilder<'a, MissingNetworkEndpoint> {
5858
/// NetworkActivityBuilder::new(&ctx).build();
5959
/// ```
6060
#[must_use]
61-
pub fn new(ctx: &'a EventContext) -> Self {
61+
pub fn new(ctx: impl Into<ContextRef<'a>>) -> Self {
6262
Self {
63-
ctx,
63+
ctx: ctx.into(),
6464
activity: ActivityId::Unknown,
6565
activity_name: None,
6666
action: None,

‎crates/openshell-ocsf/src/builders/process.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
//! Builder for Process Activity [1007] events.
55
6-
use crate::builders::EventContext;
6+
use crate::builders::ContextRef;
77
use crate::enums::{ActionId, ActivityId, DispositionId, LaunchTypeId, SeverityId, StatusId};
88
use crate::events::base_event::BaseEventData;
99
use crate::events::{OcsfEvent, ProcessActivityEvent};
1010
use crate::objects::{Actor, Process};
1111

1212
/// Builder for Process Activity [1007] events.
1313
pub struct ProcessActivityBuilder<'a> {
14-
ctx: &'a EventContext,
14+
ctx: ContextRef<'a>,
1515
activity: ActivityId,
1616
severity: SeverityId,
1717
status: Option<StatusId>,
@@ -26,9 +26,9 @@ pub struct ProcessActivityBuilder<'a> {
2626

2727
impl<'a> ProcessActivityBuilder<'a> {
2828
#[must_use]
29-
pub fn new(ctx: &'a EventContext) -> Self {
29+
pub fn new(ctx: impl Into<ContextRef<'a>>) -> Self {
3030
Self {
31-
ctx,
31+
ctx: ctx.into(),
3232
activity: ActivityId::Unknown,
3333
severity: SeverityId::Informational,
3434
status: None,

0 commit comments

Comments
 (0)