Skip to content

Commit 4c8dfc9

Browse files
test(features): check OCSF network, HTTP and SSH activity classes in ocsf
Merges the class, attribution and schema checks of ocsf_jsonl_conformance into the stderr test. The sandbox denies a TCP connect and an L7 POST to an in-process host server, and the exec sessions add SSH Activity. ocsf_jsonl_conformance stays in e2e/rust for its /var/log file-sink check until that drop is decided. Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
1 parent 5de6d98 commit 4c8dfc9

3 files changed

Lines changed: 61 additions & 17 deletions

File tree

‎tests/suites/features/Cargo.lock‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/suites/features/ocsf/Cargo.toml‎

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

44
[package]
55
name = "ocsf"
6-
description = "Features suite: a container supervisor writes opt-in OCSF JSON audit records to its stderr that conform to the OCSF schema"
6+
description = "Features suite: a container supervisor writes opt-in OCSF JSON audit records to its stderr that conform to the OCSF schema and cover network, HTTP and SSH activity"
77
version = "0.0.0"
88
edition = "2024"
99
rust-version = "1.94"
@@ -13,6 +13,8 @@ publish = false
1313

1414
[dependencies]
1515
container-engine-support = { path = "../../../support/container-engine" }
16+
host-server-support = { path = "../../../support/host-server" }
1617
openshell-ocsf-validate = { path = "../../../../crates/openshell-ocsf-validate" }
1718
openshell-testkit = { path = "../../../testkit/testkit" }
19+
sandbox-clients-support = { path = "../../../support/sandbox-clients" }
1820
serde_json = "1"

‎tests/suites/features/ocsf/tests/ocsf_stderr.rs‎

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,30 @@
1111
use std::time::Duration;
1212

1313
use container_engine_support::Engine;
14+
use host_server_support::{HttpRecorder, TEST_HOST};
15+
use sandbox_clients_support::{Request, connect, http};
16+
use testkit::policy::{self, Access, Endpoint as _};
1417
use testkit::sdk::raw::proto::setting_value::Value as SettingValue;
15-
use testkit::{Context, Error, Poll, ResultExt as _};
18+
use testkit::{Context, Error, PYTHON_BINARIES, Poll, ResultExt as _};
1619

1720
const JSON_SETTING: &str = "ocsf_json_enabled";
1821
const JSON_MARKER: &str = " OCSF-JSON ";
19-
/// A TCP connection to an endpoint the default policy doesn't allow.
20-
const DENIED_CONNECT: &str = "echo > /dev/tcp/1.1.1.1/443 || true";
22+
/// An endpoint the policy doesn't allow, for a Network Activity denial.
23+
const DENIED_HOST: &str = "1.1.1.1";
24+
const DENIED_PORT: u16 = 443;
25+
/// Classes the traffic must produce: Network Activity for the denied
26+
/// connect, HTTP Activity for the denied POST, and SSH Activity for the exec
27+
/// sessions that run the clients.
28+
const REQUIRED_CLASSES: [u64; 3] = [4001, 4002, 4007];
2129
/// How long the setting may take to reach the supervisor, which polls for it.
2230
const JSON_TIMEOUT: Duration = Duration::from_secs(40);
2331

2432
/// OCSF JSON is opt-in. With `ocsf_json_enabled`, the supervisor writes each
2533
/// event as one ` OCSF-JSON <record>` line on stderr, never stdout, next to the
26-
/// shorthand line. Records conform to the OCSF schema and name the sandbox's
27-
/// container and image. The logs the gateway collects keep only the
28-
/// shorthand.
34+
/// shorthand line. Denied TCP, a POST the L7 policy denies, and the exec
35+
/// sessions produce Network, HTTP and SSH Activity records. Every record
36+
/// conforms to the OCSF schema and names the sandbox's container and image.
37+
/// The logs the gateway collects keep only the shorthand.
2938
#[testkit::test]
3039
async fn network_events_are_json_on_supervisor_stderr_only(cx: &mut Context) -> testkit::Result {
3140
let driver = cx.compute_driver().await?;
@@ -34,11 +43,23 @@ async fn network_events_are_json_on_supervisor_stderr_only(cx: &mut Context) ->
3443
"this suite needs a Docker or Podman gateway; the gateway uses {driver}"
3544
);
3645
let engine = Engine::from_env()?;
37-
let image = testkit::base_image();
46+
let server = HttpRecorder::start("ocsf").await?;
47+
// Python reaches the server, but only to read: the POST is an L7 denial.
48+
let mut allowed = policy::base();
49+
policy::allow(
50+
&mut allowed,
51+
"server",
52+
TEST_HOST,
53+
server.port,
54+
PYTHON_BINARIES,
55+
)
56+
.rest(Access::ReadOnly);
57+
let image = testkit::python_image();
3858
let sandbox = cx
3959
.sandbox("oc")
4060
.image(&image)
4161
.command(["sleep", "infinity"])
62+
.policy(allowed)
4263
.create_ready()
4364
.await?;
4465
let supervisor = engine
@@ -57,24 +78,39 @@ async fn network_events_are_json_on_supervisor_stderr_only(cx: &mut Context) ->
5778
sandbox
5879
.set_setting(JSON_SETTING, SettingValue::BoolValue(true))
5980
.await?;
81+
let post = Request::new("POST", "/allowed").body("x");
6082
let (logs, records) = testkit::poll_until(
61-
"a JSON network denial on the supervisor's stderr",
83+
"JSON Network, HTTP and SSH Activity on the supervisor's stderr",
6284
JSON_TIMEOUT,
6385
Duration::from_secs(1),
6486
async || {
65-
sandbox.exec(["bash", "-c", DENIED_CONNECT]).await?;
87+
connect(&sandbox, DENIED_HOST, DENIED_PORT).await?;
88+
let denied = http(&sandbox, TEST_HOST, server.port, &post).await?;
89+
testkit::ensure!(
90+
denied.status() == Some(403),
91+
"the L7 policy did not deny the POST: {denied:?}"
92+
);
6693
let logs = engine.logs(&supervisor).await?;
6794
testkit::ensure!(
6895
!logs.stdout.contains(JSON_MARKER),
6996
"the supervisor wrote OCSF JSON to stdout:\n{}",
7097
logs.stdout
7198
);
7299
let records = json_records(&logs.stderr)?;
73-
Ok(if records.iter().any(is_denied_443) {
74-
Poll::Ready((logs, records))
75-
} else {
76-
Poll::Pending(format!("stderr:\n{}", logs.stderr))
77-
})
100+
let missing: Vec<u64> = REQUIRED_CLASSES
101+
.into_iter()
102+
.filter(|class| !records.iter().any(|record| record["class_uid"] == *class))
103+
.collect();
104+
Ok(
105+
if missing.is_empty() && records.iter().any(is_denied_connect) {
106+
Poll::Ready((logs, records))
107+
} else {
108+
Poll::Pending(format!(
109+
"missing classes {missing:?}; stderr:\n{}",
110+
logs.stderr
111+
))
112+
},
113+
)
78114
},
79115
)
80116
.await?;
@@ -115,9 +151,9 @@ fn json_records(stderr: &str) -> testkit::Result<Vec<serde_json::Value>> {
115151
.collect()
116152
}
117153

118-
fn is_denied_443(record: &serde_json::Value) -> bool {
154+
fn is_denied_connect(record: &serde_json::Value) -> bool {
119155
record["class_uid"] == 4001
120-
&& record["dst_endpoint"]["port"] == 443
156+
&& record["dst_endpoint"]["port"] == DENIED_PORT
121157
&& record["action"] == "Denied"
122158
}
123159

@@ -137,6 +173,10 @@ fn check_record(
137173
.is_some_and(|uid| !uid.is_empty()),
138174
"metadata.uid is empty"
139175
);
176+
testkit::ensure!(
177+
record.get("container").is_none(),
178+
"the container is at the top level, not under device"
179+
);
140180
let container = &record["device"]["container"];
141181
testkit::ensure!(
142182
container["name"] == sandbox,

0 commit comments

Comments
 (0)