Skip to content

Commit f054e20

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 5c734f6 commit f054e20

3 files changed

Lines changed: 75 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: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,31 @@
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;
17+
use testkit::sdk::raw::proto;
1418
use testkit::sdk::raw::proto::setting_value::Value as SettingValue;
15-
use testkit::{Context, Error, Poll, ResultExt as _};
19+
use testkit::{Context, Error, PYTHON_BINARIES, Poll, ResultExt as _};
1620

1721
const JSON_SETTING: &str = "ocsf_json_enabled";
1822
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";
23+
/// An endpoint the policy doesn't allow, for a Network Activity denial.
24+
const DENIED_HOST: &str = "1.1.1.1";
25+
const DENIED_PORT: u16 = 443;
26+
/// Classes the traffic must produce: Network Activity for the denied
27+
/// connect, HTTP Activity for the denied POST, and SSH Activity for the exec
28+
/// sessions that run the clients.
29+
const REQUIRED_CLASSES: [u64; 3] = [4001, 4002, 4007];
2130
/// How long the setting may take to reach the supervisor, which polls for it.
2231
const JSON_TIMEOUT: Duration = Duration::from_secs(40);
2332

2433
/// OCSF JSON is opt-in. With `ocsf_json_enabled`, the supervisor writes each
2534
/// 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.
35+
/// shorthand line. Denied TCP, a POST the L7 policy denies, and the exec
36+
/// sessions produce Network, HTTP and SSH Activity records. Every record
37+
/// conforms to the OCSF schema and names the sandbox's container and image.
38+
/// The logs the gateway collects keep only the shorthand.
2939
#[testkit::test]
3040
async fn network_events_are_json_on_supervisor_stderr_only(cx: &mut Context) -> testkit::Result {
3141
let driver = cx.compute_driver().await?;
@@ -34,11 +44,14 @@ async fn network_events_are_json_on_supervisor_stderr_only(cx: &mut Context) ->
3444
"this suite needs a Docker or Podman gateway; the gateway uses {driver}"
3545
);
3646
let engine = Engine::from_env()?;
37-
let image = testkit::base_image();
47+
let server = HttpRecorder::start("ocsf").await?;
48+
let allowed = l7_policy(server.port)?;
49+
let image = testkit::python_image();
3850
let sandbox = cx
3951
.sandbox("oc")
4052
.image(&image)
4153
.command(["sleep", "infinity"])
54+
.policy(allowed)
4255
.create_ready()
4356
.await?;
4457
let supervisor = engine
@@ -57,24 +70,39 @@ async fn network_events_are_json_on_supervisor_stderr_only(cx: &mut Context) ->
5770
sandbox
5871
.set_setting(JSON_SETTING, SettingValue::BoolValue(true))
5972
.await?;
73+
let post = Request::new("POST", "/allowed").body("x");
6074
let (logs, records) = testkit::poll_until(
61-
"a JSON network denial on the supervisor's stderr",
75+
"JSON Network, HTTP and SSH Activity on the supervisor's stderr",
6276
JSON_TIMEOUT,
6377
Duration::from_secs(1),
6478
async || {
65-
sandbox.exec(["bash", "-c", DENIED_CONNECT]).await?;
79+
connect(&sandbox, DENIED_HOST, DENIED_PORT).await?;
80+
let denied = http(&sandbox, TEST_HOST, server.port, &post).await?;
81+
testkit::ensure!(
82+
denied.status() == Some(403),
83+
"the L7 policy did not deny the POST: {denied:?}"
84+
);
6685
let logs = engine.logs(&supervisor).await?;
6786
testkit::ensure!(
6887
!logs.stdout.contains(JSON_MARKER),
6988
"the supervisor wrote OCSF JSON to stdout:\n{}",
7089
logs.stdout
7190
);
7291
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-
})
92+
let missing: Vec<u64> = REQUIRED_CLASSES
93+
.into_iter()
94+
.filter(|class| !records.iter().any(|record| record["class_uid"] == *class))
95+
.collect();
96+
Ok(
97+
if missing.is_empty() && records.iter().any(is_denied_connect) {
98+
Poll::Ready((logs, records))
99+
} else {
100+
Poll::Pending(format!(
101+
"missing classes {missing:?}; stderr:\n{}",
102+
logs.stderr
103+
))
104+
},
105+
)
78106
},
79107
)
80108
.await?;
@@ -99,6 +127,28 @@ async fn network_events_are_json_on_supervisor_stderr_only(cx: &mut Context) ->
99127
Ok(())
100128
}
101129

130+
/// Lets the image's Python reach the server on `port` with an enforced L7
131+
/// rule that allows only `GET /allowed`, so a POST is an HTTP Activity denial.
132+
fn l7_policy(port: u16) -> testkit::Result<proto::SandboxPolicy> {
133+
policy::base_with(&format!(
134+
r"network_policies:
135+
ocsf_l7:
136+
name: ocsf_l7
137+
endpoints:
138+
- host: {TEST_HOST}
139+
port: {port}
140+
protocol: rest
141+
enforcement: enforce
142+
rules:
143+
- allow:
144+
method: GET
145+
path: /allowed
146+
binaries: {binaries}
147+
",
148+
binaries = policy::binaries_yaml(PYTHON_BINARIES),
149+
))
150+
}
151+
102152
/// The JSON records in `stderr`, one per ` OCSF-JSON ` line. Each must be a
103153
/// whole JSON object with no terminal escapes.
104154
fn json_records(stderr: &str) -> testkit::Result<Vec<serde_json::Value>> {
@@ -115,9 +165,9 @@ fn json_records(stderr: &str) -> testkit::Result<Vec<serde_json::Value>> {
115165
.collect()
116166
}
117167

118-
fn is_denied_443(record: &serde_json::Value) -> bool {
168+
fn is_denied_connect(record: &serde_json::Value) -> bool {
119169
record["class_uid"] == 4001
120-
&& record["dst_endpoint"]["port"] == 443
170+
&& record["dst_endpoint"]["port"] == DENIED_PORT
121171
&& record["action"] == "Denied"
122172
}
123173

@@ -137,6 +187,10 @@ fn check_record(
137187
.is_some_and(|uid| !uid.is_empty()),
138188
"metadata.uid is empty"
139189
);
190+
testkit::ensure!(
191+
record.get("container").is_none(),
192+
"the container is at the top level, not under device"
193+
);
140194
let container = &record["device"]["container"];
141195
testkit::ensure!(
142196
container["name"] == sandbox,

0 commit comments

Comments
 (0)