Skip to content

Commit e8ba30f

Browse files
authored
feat(audit): record user and operator gateway mutations as OCSF events (#5606)
The gateway recorded authentication and authorization outcomes but not the state changes users and operators made, so there was no structured record of who created, changed, or deleted a resource or whether the call succeeded. Every call by a user or mTLS operator to a write-scoped RPC now emits one API Activity event naming the actor, the method and its create, update, delete, or other activity, the request ID, and the outcome derived from the gRPC status. A streamed call is recorded when its response stream ends, with the status from its trailers. A call or stream dropped before it finished, such as when its client disconnected, records an unknown outcome, since the change it started may still apply. An authorization denial raised for the call stands as its only record. A catalog in the gateway lists the audited RPCs, and a test fails when a write-scoped RPC is missing from it. Refs #2911 Signed-off-by: Kris Hicks <khicks@nvidia.com>
1 parent 93ffdc6 commit e8ba30f

7 files changed

Lines changed: 760 additions & 25 deletions

File tree

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! Builder for API Activity [6003] events.
55
66
use crate::builders::{ContextRef, EventOrigin};
7-
use crate::enums::{ActionId, DispositionId, SeverityId, StatusId};
7+
use crate::enums::{ActionId, ActivityId, DispositionId, SeverityId, StatusId};
88
use crate::events::base_event::BaseEventData;
99
use crate::events::{ApiActivityEvent, OcsfEvent};
1010
use crate::objects::{
@@ -29,6 +29,7 @@ fn sanitize_model_name(name: &str) -> String {
2929
/// Builder for API Activity [6003] events.
3030
pub struct ApiActivityBuilder<'a> {
3131
ctx: ContextRef<'a>,
32+
activity: ActivityId,
3233
severity: SeverityId,
3334
status: Option<StatusId>,
3435
message: Option<String>,
@@ -52,6 +53,7 @@ impl<'a> ApiActivityBuilder<'a> {
5253
pub fn new(ctx: impl Into<ContextRef<'a>>, operation: impl Into<String>) -> Self {
5354
Self {
5455
ctx: ctx.into(),
56+
activity: ActivityId::Other,
5557
severity: SeverityId::Informational,
5658
status: None,
5759
message: None,
@@ -184,8 +186,8 @@ impl<'a> ApiActivityBuilder<'a> {
184186
"API Activity",
185187
6,
186188
"Application Activity",
187-
99,
188-
"Other",
189+
self.activity.as_u8(),
190+
self.activity.api_label(),
189191
self.severity,
190192
metadata,
191193
);
@@ -223,13 +225,14 @@ impl<'a> ApiActivityBuilder<'a> {
223225
}
224226
}
225227

228+
impl_activity_setter!(ApiActivityBuilder);
226229
impl_action_disposition_setters!(ApiActivityBuilder);
227230

228231
#[cfg(test)]
229232
mod tests {
230233
use super::*;
231234
use crate::builders::{test_gateway_context, test_sandbox_context};
232-
use crate::enums::{HttpMethod, UserTypeId};
235+
use crate::enums::{ActivityId, HttpMethod, UserTypeId};
233236
use crate::objects::Url;
234237

235238
fn gateway_denial() -> serde_json::Value {
@@ -298,6 +301,29 @@ mod tests {
298301
assert!(json.get("container").is_none());
299302
}
300303

304+
#[test]
305+
fn activity_names_the_api_operation_kind() {
306+
let ctx = test_gateway_context();
307+
for (activity, id, name) in [
308+
(ActivityId::Open, 1, "Create"),
309+
(ActivityId::Close, 2, "Read"),
310+
(ActivityId::Reset, 3, "Update"),
311+
(ActivityId::Fail, 4, "Delete"),
312+
(ActivityId::Other, 99, "Other"),
313+
] {
314+
let json = ApiActivityBuilder::new(&ctx, "DeleteSandbox")
315+
.activity(activity)
316+
.build()
317+
.to_json()
318+
.unwrap();
319+
320+
assert_eq!(json["activity_id"], id, "{name}");
321+
assert_eq!(json["activity_name"], name);
322+
assert_eq!(json["type_uid"], 600_300 + id, "{name}");
323+
openshell_ocsf_validate::validate_event(&json).unwrap_or_else(|e| panic!("{e}"));
324+
}
325+
}
326+
301327
#[test]
302328
fn gateway_denial_shorthand_names_principal_operation_and_reason() {
303329
let ctx = test_gateway_context();

‎crates/openshell-ocsf/src/enums/activity.rs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,19 @@ impl ActivityId {
138138
}
139139
}
140140

141+
/// Returns a human-readable label for API Activity context.
142+
#[must_use]
143+
pub fn api_label(self) -> &'static str {
144+
match self {
145+
Self::Unknown => "Unknown",
146+
Self::Open => "Create",
147+
Self::Close => "Read",
148+
Self::Reset => "Update",
149+
Self::Fail => "Delete",
150+
_ => "Other",
151+
}
152+
}
153+
141154
/// Returns a human-readable label for Process Activity context.
142155
#[must_use]
143156
pub fn process_label(self) -> &'static str {

0 commit comments

Comments
 (0)