Skip to content

Commit 6231d64

Browse files
committed
test(proxy): avoid global tracing capture race
Signed-off-by: John Myers <[email protected]>
1 parent e2b271a commit 6231d64

2 files changed

Lines changed: 115 additions & 138 deletions

File tree

‎crates/openshell-supervisor-network/src/proxy.rs‎

Lines changed: 80 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -939,9 +939,18 @@ fn build_forward_policy_deny_ocsf_event(
939939
.build()
940940
}
941941

942+
fn destination_denial_detail(kind: DestinationDenialKind) -> &'static str {
943+
match kind {
944+
DestinationDenialKind::TrustedGateway => "trusted-gateway check failed",
945+
DestinationDenialKind::InvalidAllowedIps => "invalid allowed_ips in policy",
946+
DestinationDenialKind::AllowedIps => "allowed_ips check failed",
947+
DestinationDenialKind::DeclaredEndpoint => "declared endpoint check failed",
948+
DestinationDenialKind::InternalAddress => "internal address",
949+
}
950+
}
951+
942952
#[allow(clippy::too_many_arguments)]
943-
async fn deny_connect_destination(
944-
client: &mut TcpStream,
953+
fn build_connect_destination_deny_ocsf_event(
945954
denial: &DestinationDenial,
946955
peer_addr: SocketAddr,
947956
host: &str,
@@ -950,24 +959,15 @@ async fn deny_connect_destination(
950959
pid: &str,
951960
ancestors: &str,
952961
cmdline: &str,
953-
decision: &EgressDecision,
954-
denial_tx: &Option<mpsc::UnboundedSender<DenialEvent>>,
955-
activity_tx: &Option<ActivitySender>,
956-
) -> Result<()> {
957-
let detail = match denial.kind {
958-
DestinationDenialKind::TrustedGateway => "trusted-gateway check failed",
959-
DestinationDenialKind::InvalidAllowedIps => "invalid allowed_ips in policy",
960-
DestinationDenialKind::AllowedIps => "allowed_ips check failed",
961-
DestinationDenialKind::DeclaredEndpoint => "declared endpoint check failed",
962-
DestinationDenialKind::InternalAddress => "internal address",
963-
};
962+
) -> openshell_ocsf::OcsfEvent {
963+
let detail = destination_denial_detail(denial.kind);
964964
let message = if denial.kind == DestinationDenialKind::InternalAddress {
965965
format!("CONNECT blocked: internal address {host}:{port}")
966966
} else {
967967
format!("CONNECT blocked: {detail} for {host}:{port}")
968968
};
969969

970-
let event = NetworkActivityBuilder::new(openshell_ocsf::ctx::ctx())
970+
NetworkActivityBuilder::new(openshell_ocsf::ctx::ctx())
971971
.activity(ActivityId::Open)
972972
.action(ActionId::Denied)
973973
.disposition(DispositionId::Blocked)
@@ -979,8 +979,68 @@ async fn deny_connect_destination(
979979
.firewall_rule("-", "ssrf")
980980
.message(message)
981981
.status_detail(&denial.reason)
982-
.build();
983-
ocsf_emit!(event);
982+
.build()
983+
}
984+
985+
#[allow(clippy::too_many_arguments)]
986+
fn build_forward_destination_deny_ocsf_event(
987+
denial: &DestinationDenial,
988+
peer_addr: SocketAddr,
989+
method: &str,
990+
host: &str,
991+
port: u16,
992+
path: &str,
993+
binary: &str,
994+
pid: &str,
995+
ancestors: &str,
996+
cmdline: &str,
997+
policy: &str,
998+
) -> openshell_ocsf::OcsfEvent {
999+
let detail = destination_denial_detail(denial.kind);
1000+
let log_detail = if denial.kind == DestinationDenialKind::InternalAddress {
1001+
"internal IP without allowed_ips"
1002+
} else {
1003+
detail
1004+
};
1005+
1006+
HttpActivityBuilder::new(openshell_ocsf::ctx::ctx())
1007+
.activity(ActivityId::Other)
1008+
.action(ActionId::Denied)
1009+
.disposition(DispositionId::Blocked)
1010+
.severity(SeverityId::Medium)
1011+
.status(StatusId::Failure)
1012+
.http_request(HttpRequest::new(
1013+
method,
1014+
OcsfUrl::new("http", host, path, port),
1015+
))
1016+
.dst_endpoint(Endpoint::from_domain(host, port))
1017+
.src_endpoint(Endpoint::from_ip(peer_addr.ip(), peer_addr.port()))
1018+
.actor_process(Process::from_bypass(binary, pid, ancestors).with_cmd_line(cmdline))
1019+
.firewall_rule(policy, "ssrf")
1020+
.message(format!("FORWARD blocked: {log_detail} for {host}:{port}"))
1021+
.status_detail(&denial.reason)
1022+
.build()
1023+
}
1024+
1025+
#[allow(clippy::too_many_arguments)]
1026+
async fn deny_connect_destination(
1027+
client: &mut TcpStream,
1028+
denial: &DestinationDenial,
1029+
peer_addr: SocketAddr,
1030+
host: &str,
1031+
port: u16,
1032+
binary: &str,
1033+
pid: &str,
1034+
ancestors: &str,
1035+
cmdline: &str,
1036+
decision: &EgressDecision,
1037+
denial_tx: &Option<mpsc::UnboundedSender<DenialEvent>>,
1038+
activity_tx: &Option<ActivitySender>,
1039+
) -> Result<()> {
1040+
let detail = destination_denial_detail(denial.kind);
1041+
ocsf_emit!(build_connect_destination_deny_ocsf_event(
1042+
denial, peer_addr, host, port, binary, pid, ancestors, cmdline,
1043+
));
9841044

9851045
emit_denial(
9861046
denial_tx,
@@ -1026,37 +1086,10 @@ async fn deny_forward_destination(
10261086
denial_tx: Option<&mpsc::UnboundedSender<DenialEvent>>,
10271087
activity_tx: Option<&ActivitySender>,
10281088
) -> Result<()> {
1029-
let detail = match denial.kind {
1030-
DestinationDenialKind::TrustedGateway => "trusted-gateway check failed",
1031-
DestinationDenialKind::InvalidAllowedIps => "invalid allowed_ips in policy",
1032-
DestinationDenialKind::AllowedIps => "allowed_ips check failed",
1033-
DestinationDenialKind::DeclaredEndpoint => "declared endpoint check failed",
1034-
DestinationDenialKind::InternalAddress => "internal address",
1035-
};
1036-
let log_detail = if denial.kind == DestinationDenialKind::InternalAddress {
1037-
"internal IP without allowed_ips"
1038-
} else {
1039-
detail
1040-
};
1041-
1042-
let event = HttpActivityBuilder::new(openshell_ocsf::ctx::ctx())
1043-
.activity(ActivityId::Other)
1044-
.action(ActionId::Denied)
1045-
.disposition(DispositionId::Blocked)
1046-
.severity(SeverityId::Medium)
1047-
.status(StatusId::Failure)
1048-
.http_request(HttpRequest::new(
1049-
method,
1050-
OcsfUrl::new("http", host, path, port),
1051-
))
1052-
.dst_endpoint(Endpoint::from_domain(host, port))
1053-
.src_endpoint(Endpoint::from_ip(peer_addr.ip(), peer_addr.port()))
1054-
.actor_process(Process::from_bypass(binary, pid, ancestors).with_cmd_line(cmdline))
1055-
.firewall_rule(policy, "ssrf")
1056-
.message(format!("FORWARD blocked: {log_detail} for {host}:{port}"))
1057-
.status_detail(&denial.reason)
1058-
.build();
1059-
ocsf_emit!(event);
1089+
let detail = destination_denial_detail(denial.kind);
1090+
ocsf_emit!(build_forward_destination_deny_ocsf_event(
1091+
denial, peer_addr, method, host, port, path, binary, pid, ancestors, cmdline, policy,
1092+
));
10601093

10611094
emit_denial_simple(
10621095
denial_tx,

‎crates/openshell-supervisor-network/src/proxy/tests/compatibility.rs‎

Lines changed: 35 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,8 @@
44
//! Compatibility and regression contracts for the shared proxy egress pipeline.
55
66
use super::*;
7-
use std::io::Write;
8-
use std::sync::{Arc, Mutex};
7+
use std::sync::Arc;
98
use tokio::io::{AsyncReadExt, AsyncWriteExt};
10-
use tracing_subscriber::prelude::*;
11-
12-
#[derive(Clone)]
13-
struct SharedBuffer(Arc<Mutex<Vec<u8>>>);
14-
15-
impl Write for SharedBuffer {
16-
fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
17-
self.0.lock().unwrap().extend_from_slice(bytes);
18-
Ok(bytes.len())
19-
}
20-
21-
fn flush(&mut self) -> std::io::Result<()> {
22-
Ok(())
23-
}
24-
}
259

2610
fn allowed_decision(intent: EgressIntent) -> EgressDecision {
2711
EgressDecision {
@@ -166,80 +150,27 @@ async fn destination_denials_preserve_adapter_specific_wire_contracts() {
166150

167151
#[test]
168152
fn representative_adapter_denials_preserve_ocsf_fields() {
169-
let captured = Arc::new(Mutex::new(Vec::new()));
170-
let layer = openshell_ocsf::tracing_layers::OcsfJsonlLayer::new(SharedBuffer(captured.clone()));
171-
let subscriber = tracing_subscriber::registry().with(layer);
172153
let denial_reason = "target.example resolves to internal address 10.0.0.5";
173-
tracing::subscriber::with_default(subscriber, || {
174-
// Other parallel tests may register this callsite while no subscriber
175-
// is active. Refresh the process-wide cache after installing this
176-
// thread-local subscriber so the OCSF events cannot remain disabled.
177-
tracing::callsite::rebuild_interest_cache();
178-
179-
tokio::runtime::Builder::new_current_thread()
180-
.enable_all()
181-
.build()
182-
.unwrap()
183-
.block_on(async {
184-
let denial = DestinationDenial {
185-
kind: DestinationDenialKind::InternalAddress,
186-
reason: denial_reason.to_string(),
187-
};
188-
let peer: SocketAddr = "127.0.0.1:41000".parse().unwrap();
189-
190-
let (_app, mut proxy) = tcp_pair().await;
191-
deny_connect_destination(
192-
&mut proxy,
193-
&denial,
194-
peer,
195-
"target.example",
196-
8443,
197-
"/usr/bin/curl",
198-
"42",
199-
"/usr/bin/sh",
200-
"curl --proxy",
201-
&allowed_decision(EgressIntent::connect("target.example".to_string(), 8443)),
202-
&None,
203-
&None,
204-
)
205-
.await
206-
.unwrap();
207-
208-
let (_app, mut proxy) = tcp_pair().await;
209-
deny_forward_destination(
210-
&mut proxy,
211-
&denial,
212-
peer,
213-
"POST",
214-
"target.example",
215-
8080,
216-
"/v1/items",
217-
"/usr/bin/curl",
218-
"42",
219-
"/usr/bin/sh",
220-
"curl --proxy",
221-
"proxy_compatibility",
222-
&allowed_decision(EgressIntent::forward_http(
223-
"target.example".to_string(),
224-
8080,
225-
)),
226-
None,
227-
None,
228-
)
229-
.await
230-
.unwrap();
231-
});
232-
});
233-
234-
let bytes = captured.lock().unwrap().clone();
235-
let events: Vec<serde_json::Value> = String::from_utf8(bytes)
236-
.unwrap()
237-
.lines()
238-
.map(|line| serde_json::from_str(line).unwrap())
239-
.collect();
240-
assert_eq!(events.len(), 2);
241-
242-
let connect = &events[0];
154+
let denial = DestinationDenial {
155+
kind: DestinationDenialKind::InternalAddress,
156+
reason: denial_reason.to_string(),
157+
};
158+
let peer: SocketAddr = "127.0.0.1:41000".parse().unwrap();
159+
160+
// Build the production events directly rather than routing through the
161+
// global tracing pipeline. Its callsite-interest cache is process-global,
162+
// so parallel tests can otherwise make captured-event assertions flaky.
163+
let connect = serde_json::to_value(build_connect_destination_deny_ocsf_event(
164+
&denial,
165+
peer,
166+
"target.example",
167+
8443,
168+
"/usr/bin/curl",
169+
"42",
170+
"/usr/bin/sh",
171+
"curl --proxy",
172+
))
173+
.unwrap();
243174
assert_eq!(connect["class_name"], "Network Activity");
244175
assert_eq!(connect["activity_name"], "Open");
245176
assert_eq!(connect["action"], "Denied");
@@ -258,7 +189,20 @@ fn representative_adapter_denials_preserve_ocsf_fields() {
258189
);
259190
assert_eq!(connect["status_detail"], denial_reason);
260191

261-
let forward = &events[1];
192+
let forward = serde_json::to_value(build_forward_destination_deny_ocsf_event(
193+
&denial,
194+
peer,
195+
"POST",
196+
"target.example",
197+
8080,
198+
"/v1/items",
199+
"/usr/bin/curl",
200+
"42",
201+
"/usr/bin/sh",
202+
"curl --proxy",
203+
"proxy_compatibility",
204+
))
205+
.unwrap();
262206
assert_eq!(forward["class_name"], "HTTP Activity");
263207
assert_eq!(forward["activity_name"], "Other");
264208
assert_eq!(forward["action"], "Denied");

0 commit comments

Comments
 (0)