Skip to content

Commit 1da4322

Browse files
authored
feat(policy): show effective transport scope in rule review (#5942)
* test(policy): cover effective request scope in proposal review Signed-off-by: Adrien Langou <alangou@nvidia.com> * feat(policy): show effective transport scope in rule review Signed-off-by: Adrien Langou <alangou@nvidia.com> --------- Signed-off-by: Adrien Langou <alangou@nvidia.com>
1 parent 9551cba commit 1da4322

3 files changed

Lines changed: 85 additions & 4 deletions

File tree

‎crates/openshell-cli/src/run.rs‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7175,6 +7175,9 @@ fn format_endpoint(endpoint: &openshell_core::proto::NetworkEndpoint) -> String
71757175
} else {
71767176
endpoint.protocol.as_str()
71777177
};
7178+
if endpoint.protocol.is_empty() || endpoint.protocol.eq_ignore_ascii_case("tcp") {
7179+
return format!("{host_port} [{layer_tag}, no HTTP method/path filter]");
7180+
}
71787181
tags.push(layer_tag.to_string());
71797182

71807183
if endpoint.access != 0 {
@@ -8777,9 +8780,30 @@ mod tests {
87778780
let l4 = NetworkEndpoint {
87788781
host: "host.example.test".to_string(),
87798782
port: 443,
8783+
access: openshell_core::proto::NetworkAccessPreset::ReadOnly as i32,
8784+
rules: vec![L7Rule {
8785+
allow: Some(L7Allow {
8786+
method: "GET".into(),
8787+
path: "/orders".into(),
8788+
..Default::default()
8789+
}),
8790+
}],
87808791
..Default::default()
87818792
};
8782-
assert_eq!(format_endpoint(&l4), "host.example.test:443 [L4]");
8793+
assert_eq!(
8794+
format_endpoint(&l4),
8795+
"host.example.test:443 [L4, no HTTP method/path filter]"
8796+
);
8797+
let tcp = NetworkEndpoint {
8798+
protocol: "tcp".to_string(),
8799+
access: 0,
8800+
rules: vec![],
8801+
..l4
8802+
};
8803+
assert_eq!(
8804+
format_endpoint(&tcp),
8805+
"host.example.test:443 [tcp, no HTTP method/path filter]"
8806+
);
87838807

87848808
let l7_readonly = NetworkEndpoint {
87858809
host: "host.example.test".to_string(),

‎crates/openshell-tui/src/ui/sandbox_draft.rs‎

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,11 @@ fn format_endpoint_summary(endpoint: &NetworkEndpoint) -> String {
829829
endpoint.host.clone()
830830
};
831831

832-
let mut tags = vec![endpoint_layer_label(endpoint).to_string()];
832+
let layer = endpoint_layer_label(endpoint);
833+
if is_transport_endpoint(endpoint) {
834+
return format!("{host_port} [{layer}]");
835+
}
836+
let mut tags = vec![layer.to_string()];
833837
if endpoint.access != 0 {
834838
tags.push(format!(
835839
"access={}",
@@ -850,8 +854,9 @@ fn format_endpoint_summary(endpoint: &NetworkEndpoint) -> String {
850854

851855
fn format_endpoint_details(endpoint: &NetworkEndpoint) -> Vec<String> {
852856
let mut details = Vec::new();
857+
let transport_only = is_transport_endpoint(endpoint);
853858

854-
if !endpoint.path.is_empty() {
859+
if !transport_only && !endpoint.path.is_empty() {
855860
details.push(format!("Path scope: {}", endpoint.path));
856861
}
857862
if endpoint.tls != 0 {
@@ -860,7 +865,7 @@ fn format_endpoint_details(endpoint: &NetworkEndpoint) -> Vec<String> {
860865
openshell_policy::network_tls_mode_to_str(endpoint.tls).unwrap_or("unknown")
861866
));
862867
}
863-
if endpoint.enforcement != 0 {
868+
if !transport_only && endpoint.enforcement != 0 {
864869
details.push(format!(
865870
"Enforcement: {}",
866871
openshell_policy::network_enforcement_mode_to_str(endpoint.enforcement)
@@ -873,6 +878,10 @@ fn format_endpoint_details(endpoint: &NetworkEndpoint) -> Vec<String> {
873878
if endpoint.websocket_credential_rewrite {
874879
details.push("WebSocket credential rewrite".to_string());
875880
}
881+
if transport_only {
882+
details.push("no HTTP method/path filter".to_string());
883+
return details;
884+
}
876885
for rule in &endpoint.rules {
877886
if let Some(allow) = &rule.allow {
878887
details.push(format!("Allow: {}", format_allow_rule(allow)));
@@ -885,6 +894,10 @@ fn format_endpoint_details(endpoint: &NetworkEndpoint) -> Vec<String> {
885894
details
886895
}
887896

897+
fn is_transport_endpoint(endpoint: &NetworkEndpoint) -> bool {
898+
endpoint.protocol.is_empty() || endpoint.protocol.eq_ignore_ascii_case("tcp")
899+
}
900+
888901
fn endpoint_layer_label(endpoint: &NetworkEndpoint) -> &str {
889902
if endpoint.protocol.eq_ignore_ascii_case("rest") {
890903
"L7 rest"
@@ -1178,6 +1191,49 @@ mod tests {
11781191
reason
11791192
}
11801193

1194+
#[test]
1195+
fn approval_detail_explains_l4_request_scope() {
1196+
for (protocol, summary, unfiltered) in [
1197+
("", "alias.test:80 [L4]", true),
1198+
("tcp", "alias.test:80 [tcp]", true),
1199+
("rest", "alias.test:80 [L7 rest, allow GET /orders]", false),
1200+
] {
1201+
let mut endpoint = NetworkEndpoint {
1202+
host: "alias.test".into(),
1203+
port: 80,
1204+
protocol: protocol.into(),
1205+
..Default::default()
1206+
};
1207+
if unfiltered {
1208+
endpoint.tls = openshell_core::proto::NetworkTlsMode::Skip as i32;
1209+
}
1210+
if protocol != "tcp" {
1211+
endpoint.path = "/orders/**".into();
1212+
endpoint.rules.push(openshell_core::proto::L7Rule {
1213+
allow: Some(L7Allow {
1214+
method: "GET".into(),
1215+
path: "/orders".into(),
1216+
..Default::default()
1217+
}),
1218+
});
1219+
}
1220+
assert_eq!(format_endpoint_summary(&endpoint), summary);
1221+
let chunk = PolicyChunk {
1222+
proposed_rule: Some(openshell_core::proto::NetworkPolicyRule {
1223+
endpoints: vec![endpoint],
1224+
..Default::default()
1225+
}),
1226+
..make_chunk("pending", "")
1227+
};
1228+
let (screen, _) = render(&chunk, 80, 24, 0);
1229+
assert!(screen.contains(summary));
1230+
assert_eq!(screen.contains("no HTTP method/path filter"), unfiltered);
1231+
assert_eq!(screen.contains("Allow: GET /orders"), !unfiltered);
1232+
assert_eq!(screen.contains("Path scope: /orders/**"), !unfiltered);
1233+
assert_eq!(screen.contains("TLS: skip"), unfiltered);
1234+
}
1235+
}
1236+
11811237
#[test]
11821238
fn long_guidance_head_and_tail_are_both_reachable() {
11831239
let chunk = PolicyChunk {

‎docs/how-it-works/policies/advisor.mdx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ agent's reason, and the results of the risk checks:
121121
openshell rule get <sandbox-name> --status pending
122122
```
123123

124+
A transport-level endpoint (`L4` or `tcp`) sets no HTTP method or path filter of its own.
124125
Approve a proposal when its rule grants only the access you intend. Use the ID
125126
from the proposal's `Chunk` line:
126127

0 commit comments

Comments
 (0)