@@ -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
15641594fn 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" )
0 commit comments