Skip to content

Commit 73b367d

Browse files
committed
feat(observability): record policy generation on allow and deny decisions
Decision events named the matched rule but not the policy version that made the decision, and policy load events carried the policy hash but not the engine generation, so nothing in the record linked a decision to its policy. Record the evaluating policy engine generation in the OCSF firewall_rule.version field on L4 CONNECT, forward-proxy and transparent-TCP decisions, including staged transparent denials and the closes made when a policy change invalidates a pinned tunnel, and on L7 REST, JSON-RPC/MCP, GraphQL, route-selection, transformed-body and WebSocket decisions. Record the activated generation as unmapped.active_generation on policy load, atomic runtime reload and initial acknowledgement events, reusing the key the validation-failure events already use. The transparent-TCP denial and the policy-change close now print [policy:- engine:opa] in the shorthand log. Other shorthand lines are unchanged. Closes #3840 Signed-off-by: Rakesh Gohel <mocha384@gmail.com>
1 parent b4a6541 commit 73b367d

11 files changed

Lines changed: 582 additions & 76 deletions

File tree

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,22 @@ impl<'a, Context> HttpActivityBuilder<'a, Context> {
171171
self.firewall_rule = Some(FirewallRule::new(name, rule_type));
172172
self
173173
}
174+
175+
/// Set the firewall rule and the policy generation that evaluated it.
176+
///
177+
/// Use this for allow and deny decisions so the event names the exact
178+
/// policy generation, not only the rule name.
179+
#[must_use]
180+
pub fn firewall_rule_at_generation(
181+
mut self,
182+
name: &str,
183+
rule_type: &str,
184+
generation: u64,
185+
) -> Self {
186+
self.firewall_rule =
187+
Some(FirewallRule::new(name, rule_type).with_policy_generation(generation));
188+
self
189+
}
174190
}
175191

176192
impl_activity_setter!(HttpActivityBuilder<Context>);
@@ -272,4 +288,27 @@ mod tests {
272288
assert_eq!(json["unmapped"]["cached"], true);
273289
assert_eq!(json["action_id"], 2); // Denied
274290
}
291+
292+
#[test]
293+
fn test_http_decision_records_policy_generation() {
294+
let ctx = test_sandbox_context();
295+
let event = HttpActivityBuilder::new(&ctx)
296+
.activity(ActivityId::Other)
297+
.action(ActionId::Allowed)
298+
.disposition(DispositionId::Allowed)
299+
.severity(SeverityId::Informational)
300+
.dst_endpoint(Endpoint::from_domain("api.github.com", 443))
301+
.http_request(HttpRequest::new(
302+
"GET",
303+
Url::new("https", "api.github.com", "/zen", 443),
304+
))
305+
.firewall_rule_at_generation("github_api", "l7", 12)
306+
.build();
307+
308+
let json = event.to_json().unwrap();
309+
assert_eq!(json["firewall_rule"]["name"], "github_api");
310+
assert_eq!(json["firewall_rule"]["version"], "12");
311+
openshell_ocsf_validate::validate_event(&json)
312+
.unwrap_or_else(|e| panic!("firewall_rule.version must stay schema-valid: {e}"));
313+
}
275314
}

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,22 @@ impl<'a, EndpointState> NetworkActivityBuilder<'a, EndpointState> {
203203
self.firewall_rule = Some(FirewallRule::new(name, rule_type));
204204
self
205205
}
206+
207+
/// Set the firewall rule and the policy generation that evaluated it.
208+
///
209+
/// Use this for allow and deny decisions so the event names the exact
210+
/// policy generation, not only the rule name.
211+
#[must_use]
212+
pub fn firewall_rule_at_generation(
213+
mut self,
214+
name: &str,
215+
rule_type: &str,
216+
generation: u64,
217+
) -> Self {
218+
self.firewall_rule =
219+
Some(FirewallRule::new(name, rule_type).with_policy_generation(generation));
220+
self
221+
}
206222
}
207223

208224
impl_activity_setter!(NetworkActivityBuilder<EndpointState>);
@@ -292,5 +308,30 @@ mod tests {
292308
assert_eq!(json["device"]["container"]["name"], "my-sandbox");
293309
assert_eq!(json["device"]["hostname"], "sandbox-abc123");
294310
assert_eq!(json["is_src_dst_assignment_known"], true);
311+
assert!(
312+
json["firewall_rule"].get("version").is_none(),
313+
"decisions built without a generation leave firewall_rule.version unset"
314+
);
315+
}
316+
317+
#[test]
318+
fn test_network_decision_records_policy_generation() {
319+
let ctx = test_sandbox_context();
320+
let event = NetworkActivityBuilder::new(&ctx)
321+
.activity(ActivityId::Open)
322+
.action(ActionId::Denied)
323+
.disposition(DispositionId::Blocked)
324+
.severity(SeverityId::Medium)
325+
.dst_endpoint(Endpoint::from_domain("httpbin.org", 443))
326+
.firewall_rule_at_generation("-", "opa", 4)
327+
.message("CONNECT denied httpbin.org:443")
328+
.build();
329+
330+
let json = event.to_json().unwrap();
331+
assert_eq!(json["firewall_rule"]["name"], "-");
332+
assert_eq!(json["firewall_rule"]["type"], "opa");
333+
assert_eq!(json["firewall_rule"]["version"], "4");
334+
openshell_ocsf_validate::validate_event(&json)
335+
.unwrap_or_else(|e| panic!("firewall_rule.version must stay schema-valid: {e}"));
295336
}
296337
}

‎crates/openshell-ocsf/src/objects/firewall_rule.rs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ pub struct FirewallRule {
1717
/// (not OCSF-enumerated) with runtime-dynamic values from the policy engine.
1818
#[serde(rename = "type")]
1919
pub rule_type: String,
20+
21+
/// Version of the policy the rule belongs to, as evaluated for this event.
22+
///
23+
/// `OpenShell` sets this to the supervisor's policy engine generation: a
24+
/// counter the engine advances whenever it replaces its active
25+
/// configuration. Policy activation events record the same counter in
26+
/// `unmapped.active_generation`, so a consumer can resolve the policy
27+
/// behind each decision without relying on timestamps.
28+
#[serde(default, skip_serializing_if = "Option::is_none")]
29+
pub version: Option<String>,
2030
}
2131

2232
impl FirewallRule {
@@ -26,8 +36,16 @@ impl FirewallRule {
2636
Self {
2737
name: name.to_string(),
2838
rule_type: rule_type.to_string(),
39+
version: None,
2940
}
3041
}
42+
43+
/// Record the policy generation under which the rule was evaluated.
44+
#[must_use]
45+
pub fn with_policy_generation(mut self, generation: u64) -> Self {
46+
self.version = Some(generation.to_string());
47+
self
48+
}
3149
}
3250

3351
#[cfg(test)]
@@ -40,5 +58,20 @@ mod tests {
4058
let json = serde_json::to_value(&rule).unwrap();
4159
assert_eq!(json["name"], "default-egress");
4260
assert_eq!(json["type"], "mechanistic");
61+
assert!(
62+
json.get("version").is_none(),
63+
"version is omitted when no generation is recorded"
64+
);
65+
}
66+
67+
#[test]
68+
fn test_firewall_rule_policy_generation_serializes_as_version() {
69+
let rule = FirewallRule::new("github_api", "opa").with_policy_generation(7);
70+
let json = serde_json::to_value(&rule).unwrap();
71+
assert_eq!(json["name"], "github_api");
72+
assert_eq!(json["version"], "7");
73+
74+
let round_trip: FirewallRule = serde_json::from_value(json).unwrap();
75+
assert_eq!(round_trip, rule);
4376
}
4477
}

0 commit comments

Comments
 (0)