(policy: &mut SandboxPolicy, rule_name: Option<&str>, host: &str, port: u32)
| 867 | } |
| 868 | |
| 869 | fn remove_endpoint(policy: &mut SandboxPolicy, rule_name: Option<&str>, host: &str, port: u32) { |
| 870 | let target_keys: Vec<String> = if let Some(rule_name) = rule_name { |
| 871 | if policy.network_policies.contains_key(rule_name) { |
| 872 | vec![rule_name.to_string()] |
| 873 | } else { |
| 874 | vec![] |
| 875 | } |
| 876 | } else { |
| 877 | let mut keys: Vec<_> = policy.network_policies.keys().cloned().collect(); |
| 878 | keys.sort(); |
| 879 | keys |
| 880 | }; |
| 881 | |
| 882 | let mut empty_rules = Vec::new(); |
| 883 | for key in target_keys { |
| 884 | if let Some(rule) = policy.network_policies.get_mut(&key) { |
| 885 | rule.endpoints.retain_mut(|endpoint| { |
| 886 | if !endpoint_matches_host_port(endpoint, host, port) { |
| 887 | return true; |
| 888 | } |
| 889 | |
| 890 | let mut remaining_ports = canonical_ports(endpoint); |
| 891 | remaining_ports.retain(|existing_port| *existing_port != port); |
| 892 | remaining_ports.sort_unstable(); |
| 893 | remaining_ports.dedup(); |
| 894 | |
| 895 | if remaining_ports.is_empty() { |
| 896 | return false; |
| 897 | } |
| 898 | |
| 899 | endpoint.port = remaining_ports[0]; |
| 900 | endpoint.ports = remaining_ports; |
| 901 | true |
| 902 | }); |
| 903 | |
| 904 | if rule.endpoints.is_empty() { |
| 905 | empty_rules.push(key); |
| 906 | } |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | for key in empty_rules { |
| 911 | policy.network_policies.remove(&key); |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | #[cfg(test)] |
| 916 | mod tests { |
no test coverage detected