Skip to content

Commit ad29ab9

Browse files
authored
fix(supervisor-network): warn on unsupported L7 access presets (#2177)
Signed-off-by: Yuedong Wu <dwcn22@outlook.com>
1 parent 745512e commit ad29ab9

2 files changed

Lines changed: 126 additions & 65 deletions

File tree

‎crates/openshell-supervisor-network/src/l7/mod.rs‎

Lines changed: 106 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,23 +1463,76 @@ pub fn validate_l7_policies(data_json: &serde_json::Value) -> (Vec<String>, Vec<
14631463
(errors, warnings)
14641464
}
14651465

1466+
/// Map a supported L7 `access` preset to explicit rules for `protocol`.
1467+
///
1468+
/// Returns `None` when `access` is not `read-only`, `read-write`, or `full`.
1469+
/// Graphql and websocket presets differ from REST; all other protocols use
1470+
/// REST presets. Requires prior `validate_l7_policies` so mcp/json-rpc `access`
1471+
/// is rejected before expansion runs.
1472+
fn access_preset_rules(protocol: &str, access: &str) -> Option<Vec<serde_json::Value>> {
1473+
if protocol == "graphql" {
1474+
match access {
1475+
"read-only" => Some(vec![graphql_rule_json("query")]),
1476+
"read-write" => Some(vec![
1477+
graphql_rule_json("query"),
1478+
graphql_rule_json("mutation"),
1479+
]),
1480+
"full" => Some(vec![graphql_rule_json("*")]),
1481+
_ => None,
1482+
}
1483+
} else if protocol == "websocket" {
1484+
match access {
1485+
"read-only" => Some(vec![rule_json("GET", "**")]),
1486+
"read-write" => Some(vec![
1487+
rule_json("GET", "**"),
1488+
rule_json("WEBSOCKET_TEXT", "**"),
1489+
]),
1490+
"full" => Some(vec![rule_json("*", "**")]),
1491+
_ => None,
1492+
}
1493+
} else {
1494+
match access {
1495+
"read-only" => Some(vec![
1496+
rule_json("GET", "**"),
1497+
rule_json("HEAD", "**"),
1498+
rule_json("OPTIONS", "**"),
1499+
]),
1500+
"read-write" => Some(vec![
1501+
rule_json("GET", "**"),
1502+
rule_json("HEAD", "**"),
1503+
rule_json("OPTIONS", "**"),
1504+
rule_json("POST", "**"),
1505+
rule_json("PUT", "**"),
1506+
rule_json("PATCH", "**"),
1507+
]),
1508+
"full" => Some(vec![rule_json("*", "**")]),
1509+
_ => None,
1510+
}
1511+
}
1512+
}
1513+
14661514
/// Expand `access` presets into explicit `rules` in the policy data.
14671515
///
14681516
/// This preprocesses the JSON data so Rego only needs to handle explicit rules.
1469-
pub fn expand_access_presets(data: &mut serde_json::Value) {
1517+
/// Returns warnings for unsupported `access` values that were ignored.
1518+
///
1519+
/// Unsupported preset detection lives here rather than in `validate_l7_policies`
1520+
/// so supported presets stay defined only in `access_preset_rules`.
1521+
pub fn expand_access_presets(data: &mut serde_json::Value) -> Vec<String> {
1522+
let mut warnings = Vec::new();
14701523
let Some(policies) = data
14711524
.get_mut("network_policies")
14721525
.and_then(|v| v.as_object_mut())
14731526
else {
1474-
return;
1527+
return warnings;
14751528
};
14761529

1477-
for (_name, policy) in policies.iter_mut() {
1530+
for (name, policy) in policies.iter_mut() {
14781531
let Some(endpoints) = policy.get_mut("endpoints").and_then(|v| v.as_array_mut()) else {
14791532
continue;
14801533
};
14811534

1482-
for ep in endpoints.iter_mut() {
1535+
for (i, ep) in endpoints.iter_mut().enumerate() {
14831536
let protocol = ep
14841537
.get("protocol")
14851538
.and_then(|v| v.as_str())
@@ -1520,45 +1573,22 @@ pub fn expand_access_presets(data: &mut serde_json::Value) {
15201573
continue;
15211574
}
15221575

1523-
let rules = if protocol == "graphql" {
1524-
match access.as_str() {
1525-
"read-only" => vec![graphql_rule_json("query")],
1526-
"read-write" => vec![graphql_rule_json("query"), graphql_rule_json("mutation")],
1527-
"full" => vec![graphql_rule_json("*")],
1528-
_ => continue,
1529-
}
1530-
} else if protocol == "websocket" {
1531-
match access.as_str() {
1532-
"read-only" => vec![rule_json("GET", "**")],
1533-
"read-write" => vec![rule_json("GET", "**"), rule_json("WEBSOCKET_TEXT", "**")],
1534-
"full" => vec![rule_json("*", "**")],
1535-
_ => continue,
1536-
}
1537-
} else {
1538-
match access.as_str() {
1539-
"read-only" => vec![
1540-
rule_json("GET", "**"),
1541-
rule_json("HEAD", "**"),
1542-
rule_json("OPTIONS", "**"),
1543-
],
1544-
"read-write" => vec![
1545-
rule_json("GET", "**"),
1546-
rule_json("HEAD", "**"),
1547-
rule_json("OPTIONS", "**"),
1548-
rule_json("POST", "**"),
1549-
rule_json("PUT", "**"),
1550-
rule_json("PATCH", "**"),
1551-
],
1552-
"full" => vec![rule_json("*", "**")],
1553-
_ => continue,
1554-
}
1576+
let Some(rules) = access_preset_rules(protocol, &access) else {
1577+
let loc = format!("{name}.endpoints[{i}]");
1578+
let warning = format!(
1579+
"{loc}: unsupported L7 access preset '{access}' ignored; expected one of: read-only, read-write, full"
1580+
);
1581+
warnings.push(warning);
1582+
continue;
15551583
};
15561584

15571585
ep.as_object_mut()
15581586
.unwrap()
15591587
.insert("rules".to_string(), serde_json::Value::Array(rules));
15601588
}
15611589
}
1590+
1591+
warnings
15621592
}
15631593

15641594
fn rule_json(method: &str, path: &str) -> serde_json::Value {
@@ -1849,7 +1879,8 @@ mod tests {
18491879
}
18501880
});
18511881

1852-
expand_access_presets(&mut data);
1882+
let warnings = expand_access_presets(&mut data);
1883+
assert!(warnings.is_empty(), "expected no warnings: {warnings:?}");
18531884
let rules = data["network_policies"]["test"]["endpoints"][0]["rules"]
18541885
.as_array()
18551886
.unwrap();
@@ -2709,7 +2740,8 @@ mod tests {
27092740
}
27102741
}
27112742
});
2712-
expand_access_presets(&mut data);
2743+
let warnings = expand_access_presets(&mut data);
2744+
assert!(warnings.is_empty(), "expected no warnings: {warnings:?}");
27132745
let rules = data["network_policies"]["test"]["endpoints"][0]["rules"]
27142746
.as_array()
27152747
.unwrap();
@@ -2738,7 +2770,8 @@ mod tests {
27382770
}
27392771
}
27402772
});
2741-
expand_access_presets(&mut data);
2773+
let warnings = expand_access_presets(&mut data);
2774+
assert!(warnings.is_empty(), "expected no warnings: {warnings:?}");
27422775
let rules = data["network_policies"]["test"]["endpoints"][0]["rules"]
27432776
.as_array()
27442777
.unwrap();
@@ -2747,6 +2780,36 @@ mod tests {
27472780
assert_eq!(rules[0]["allow"]["path"].as_str().unwrap(), "**");
27482781
}
27492782

2783+
#[test]
2784+
fn expand_unsupported_access_preset_warns_and_leaves_rules_unexpanded() {
2785+
let mut data = serde_json::json!({
2786+
"network_policies": {
2787+
"allow-my-service": {
2788+
"endpoints": [{
2789+
"host": "my-service.example.com",
2790+
"port": 80,
2791+
"protocol": "rest",
2792+
"access": "allow"
2793+
}],
2794+
"binaries": []
2795+
}
2796+
}
2797+
});
2798+
let warnings = expand_access_presets(&mut data);
2799+
assert!(
2800+
data["network_policies"]["allow-my-service"]["endpoints"][0]
2801+
.get("rules")
2802+
.is_none(),
2803+
"unsupported access preset must not expand rules"
2804+
);
2805+
assert_eq!(
2806+
warnings,
2807+
vec![
2808+
"allow-my-service.endpoints[0]: unsupported L7 access preset 'allow' ignored; expected one of: read-only, read-write, full".to_string()
2809+
]
2810+
);
2811+
}
2812+
27502813
#[test]
27512814
fn expand_graphql_readonly_preset() {
27522815
let mut data = serde_json::json!({
@@ -2762,7 +2825,8 @@ mod tests {
27622825
}
27632826
}
27642827
});
2765-
expand_access_presets(&mut data);
2828+
let warnings = expand_access_presets(&mut data);
2829+
assert!(warnings.is_empty(), "expected no warnings: {warnings:?}");
27662830
let rules = data["network_policies"]["test"]["endpoints"][0]["rules"]
27672831
.as_array()
27682832
.unwrap();
@@ -2835,7 +2899,8 @@ mod tests {
28352899
}
28362900
}
28372901
});
2838-
expand_access_presets(&mut data);
2902+
let warnings = expand_access_presets(&mut data);
2903+
assert!(warnings.is_empty(), "expected no warnings: {warnings:?}");
28392904
assert!(
28402905
data["network_policies"]["test"]["endpoints"][0]
28412906
.get("rules")

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

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -344,17 +344,7 @@ impl OpaEngine {
344344

345345
// Validate BEFORE expanding presets
346346
let (errors, warnings) = crate::l7::validate_l7_policies(&data);
347-
for w in &warnings {
348-
openshell_ocsf::ocsf_emit!(
349-
openshell_ocsf::ConfigStateChangeBuilder::new(openshell_ocsf::ctx::ctx())
350-
.severity(openshell_ocsf::SeverityId::Medium)
351-
.status(openshell_ocsf::StatusId::Success)
352-
.state(openshell_ocsf::StateId::Enabled, "validated")
353-
.unmapped("warning", serde_json::json!(w.clone()))
354-
.message(format!("L7 policy validation warning: {w}"))
355-
.build()
356-
);
357-
}
347+
emit_l7_config_warnings(&warnings, "L7 policy validation warning");
358348
if !errors.is_empty() {
359349
return Err(miette::miette!(
360350
"L7 policy validation failed:\n{}",
@@ -365,7 +355,8 @@ impl OpaEngine {
365355
normalize_l7_policy_rule_aliases(&mut data);
366356

367357
// Expand access presets to explicit rules after validation
368-
crate::l7::expand_access_presets(&mut data);
358+
let expansion_warnings = crate::l7::expand_access_presets(&mut data);
359+
emit_l7_config_warnings(&expansion_warnings, "L7 access preset expansion warning");
369360

370361
let data_json = data.to_string();
371362
let mut engine = regorus::Engine::new();
@@ -1032,6 +1023,20 @@ fn parse_process_policy(val: &regorus::Value) -> ProcessPolicy {
10321023
}
10331024
}
10341025

1026+
fn emit_l7_config_warnings(warnings: &[String], prefix: &str) {
1027+
for w in warnings {
1028+
openshell_ocsf::ocsf_emit!(
1029+
openshell_ocsf::ConfigStateChangeBuilder::new(openshell_ocsf::ctx::ctx())
1030+
.severity(openshell_ocsf::SeverityId::Medium)
1031+
.status(openshell_ocsf::StatusId::Success)
1032+
.state(openshell_ocsf::StateId::Enabled, "validated")
1033+
.unmapped("warning", serde_json::json!(w))
1034+
.message(format!("{prefix}: {w}"))
1035+
.build()
1036+
);
1037+
}
1038+
}
1039+
10351040
/// Preprocess YAML policy data: parse, normalize, validate, expand access presets, return JSON.
10361041
fn preprocess_yaml_data(
10371042
yaml_str: &str,
@@ -1073,17 +1078,7 @@ fn preprocess_yaml_data(
10731078
}
10741079

10751080
let (errors, warnings) = crate::l7::validate_l7_policies(&data);
1076-
for w in &warnings {
1077-
openshell_ocsf::ocsf_emit!(
1078-
openshell_ocsf::ConfigStateChangeBuilder::new(openshell_ocsf::ctx::ctx())
1079-
.severity(openshell_ocsf::SeverityId::Medium)
1080-
.status(openshell_ocsf::StatusId::Success)
1081-
.state(openshell_ocsf::StateId::Enabled, "validated")
1082-
.unmapped("warning", serde_json::json!(w.clone()))
1083-
.message(format!("L7 policy validation warning: {w}"))
1084-
.build()
1085-
);
1086-
}
1081+
emit_l7_config_warnings(&warnings, "L7 policy validation warning");
10871082
if !errors.is_empty() {
10881083
return Err(miette::miette!(
10891084
"L7 policy validation failed:\n{}",
@@ -1094,7 +1089,8 @@ fn preprocess_yaml_data(
10941089
normalize_l7_policy_rule_aliases(&mut data);
10951090

10961091
// Expand access presets to explicit rules after validation
1097-
crate::l7::expand_access_presets(&mut data);
1092+
let expansion_warnings = crate::l7::expand_access_presets(&mut data);
1093+
emit_l7_config_warnings(&expansion_warnings, "L7 access preset expansion warning");
10981094

10991095
serde_json::to_string(&data).map_err(|e| miette::miette!("failed to serialize data: {e}"))
11001096
}

0 commit comments

Comments
 (0)