Normalize endpoint port/ports in JSON data. YAML policies may use `port: N` (single) or `ports: [N, M]` (multi). This normalizes all endpoints to have a `ports` array so Rego rules only need to reference `endpoint.ports[_]`.
(data: &mut serde_json::Value)
| 768 | /// This normalizes all endpoints to have a `ports` array so Rego rules |
| 769 | /// only need to reference `endpoint.ports[_]`. |
| 770 | fn normalize_endpoint_ports(data: &mut serde_json::Value) { |
| 771 | let Some(policies) = data |
| 772 | .get_mut("network_policies") |
| 773 | .and_then(|v| v.as_object_mut()) |
| 774 | else { |
| 775 | return; |
| 776 | }; |
| 777 | |
| 778 | for (_name, policy) in policies.iter_mut() { |
| 779 | let Some(endpoints) = policy.get_mut("endpoints").and_then(|v| v.as_array_mut()) else { |
| 780 | continue; |
| 781 | }; |
| 782 | |
| 783 | for ep in endpoints.iter_mut() { |
| 784 | let Some(ep_obj) = ep.as_object_mut() else { |
| 785 | continue; |
| 786 | }; |
| 787 | |
| 788 | // If "ports" already exists and is non-empty, keep it. |
| 789 | let has_ports = ep_obj |
| 790 | .get("ports") |
| 791 | .and_then(|v| v.as_array()) |
| 792 | .is_some_and(|a| !a.is_empty()); |
| 793 | |
| 794 | if !has_ports { |
| 795 | // Promote scalar "port" to "ports" array. |
| 796 | let port = ep_obj |
| 797 | .get("port") |
| 798 | .and_then(serde_json::Value::as_u64) |
| 799 | .unwrap_or(0); |
| 800 | if port > 0 { |
| 801 | ep_obj.insert( |
| 802 | "ports".to_string(), |
| 803 | serde_json::Value::Array(vec![serde_json::json!(port)]), |
| 804 | ); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | // Remove scalar "port" — Rego only uses "ports". |
| 809 | ep_obj.remove("port"); |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | fn normalize_l7_config_aliases(data: &mut serde_json::Value) -> Vec<String> { |
| 815 | let mut errors = Vec::new(); |
no test coverage detected