Repository navigation
fix(ocsf): emit schema-valid event identities #3247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! OCSF `device.type_id` enum. | ||
|
|
||
| use serde_repr::{Deserialize_repr, Serialize_repr}; | ||
|
|
||
| /// OCSF Device Type ID. | ||
| /// | ||
| /// Only the values `OpenShell` can produce are modelled; the schema defines a | ||
| /// wider set (desktop, mobile, firewall, router, ...). | ||
| /// Values come from the OCSF device `type_id` enumeration. | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize_repr, Deserialize_repr)] | ||
| #[repr(u8)] | ||
| pub enum DeviceTypeId { | ||
| /// 0 — Unknown | ||
| Unknown = 0, | ||
| /// 99 — Other | ||
| Other = 99, | ||
| } | ||
|
|
||
| impl DeviceTypeId { | ||
| #[must_use] | ||
| pub fn as_u8(self) -> u8 { | ||
| self as u8 | ||
| } | ||
| } | ||
|
|
||
| impl std::fmt::Display for DeviceTypeId { | ||
| fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| formatter.write_str(match self { | ||
| Self::Unknown => "Unknown", | ||
| Self::Other => "Other", | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn device_type_display_uses_schema_labels() { | ||
| for (device_type, expected) in [ | ||
| (DeviceTypeId::Unknown, "Unknown"), | ||
| (DeviceTypeId::Other, "Other"), | ||
| ] { | ||
| assert_eq!(device_type.to_string(), expected); | ||
| assert_eq!(format!("{device_type}"), expected); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn device_type_json_roundtrip() { | ||
| for (device_type, expected) in [(DeviceTypeId::Unknown, 0), (DeviceTypeId::Other, 99)] { | ||
| let json = serde_json::to_value(device_type).unwrap(); | ||
| assert_eq!(json, serde_json::json!(expected)); | ||
| let decoded: DeviceTypeId = serde_json::from_value(json).unwrap(); | ||
| assert_eq!(decoded, device_type); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! `metadata.uid` identifies the event; `container.uid` identifies the sandbox. | ||
|
|
||
| use std::net::{IpAddr, Ipv4Addr}; | ||
|
|
||
| use openshell_ocsf::{ActivityId, NetworkActivityBuilder, OcsfEvent, SandboxContext, SeverityId}; | ||
|
|
||
| fn sandbox_ctx(container_image: &str) -> SandboxContext { | ||
| SandboxContext { | ||
| sandbox_id: "sb-1".to_string(), | ||
| sandbox_name: "agent-01".to_string(), | ||
| container_image: container_image.to_string(), | ||
| hostname: "openshell-sb-1".to_string(), | ||
| product_version: "0.42.1".to_string(), | ||
| proxy_ip: IpAddr::V4(Ipv4Addr::LOCALHOST), | ||
| proxy_port: 8888, | ||
| } | ||
| } | ||
|
|
||
| fn event(ctx: &SandboxContext) -> OcsfEvent { | ||
| NetworkActivityBuilder::new(ctx) | ||
| .activity(ActivityId::Open) | ||
| .severity(SeverityId::Medium) | ||
| .message("CONNECT api.example.com:443") | ||
| .build() | ||
| } | ||
|
|
||
| #[test] | ||
| fn each_event_gets_its_own_metadata_uid() { | ||
| let ctx = sandbox_ctx("ghcr.io/nvidia/openshell/sandbox:0.42.1"); | ||
| let first = event(&ctx); | ||
| let second = event(&ctx); | ||
|
|
||
| let first_uid = first.base().metadata.uid.clone().expect("uid is set"); | ||
| let second_uid = second.base().metadata.uid.clone().expect("uid is set"); | ||
|
|
||
| assert!(!first_uid.is_empty()); | ||
| assert_ne!(first_uid, second_uid); | ||
| } | ||
|
|
||
| #[test] | ||
| fn metadata_uid_is_no_longer_the_sandbox_id() { | ||
| let event = event(&sandbox_ctx("ghcr.io/nvidia/openshell/sandbox:0.42.1")); | ||
| assert_ne!(event.base().metadata.uid.as_deref(), Some("sb-1")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn the_sandbox_id_is_carried_by_the_container() { | ||
| let json = event(&sandbox_ctx("ghcr.io/nvidia/openshell/sandbox:0.42.1")) | ||
| .to_json() | ||
| .expect("serializes"); | ||
|
|
||
| assert_eq!(json["container"]["uid"], "sb-1"); | ||
| assert_eq!(json["container"]["name"], "agent-01"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_container_without_an_image_omits_the_image() { | ||
| let json = event(&sandbox_ctx("")).to_json().expect("serializes"); | ||
| assert!(json["container"].get("image").is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn device_identifies_the_sandbox_environment() { | ||
| let json = event(&sandbox_ctx("image:1")).to_json().unwrap(); | ||
|
|
||
| assert_eq!(json["device"]["type_id"], 99); | ||
| assert_eq!(json["device"]["type"], "Sandbox"); | ||
| assert_eq!(json["device"]["os"]["name"], "Linux"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn event_identity_survives_serialization() { | ||
| let original = event(&sandbox_ctx("image:1")); | ||
| let decoded: OcsfEvent = serde_json::from_value(original.to_json().unwrap()).unwrap(); | ||
| assert_eq!(decoded.base().metadata.uid, original.base().metadata.uid); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.