@@ -1271,7 +1271,7 @@ func TestTranslatePolicy(t *testing.T) {
12711271 },
12721272 }
12731273 expectedIptEntries = append (expectedIptEntries , nonKubeSystemEntries ... )
1274- expectedIptEntries = append (expectedIptEntries , getDefaultDropEntries ("testnamespace" , targetSelector , true , false )... )
1274+ expectedIptEntries = append (expectedIptEntries , getDefaultDropEntries ("testnamespace" , targetSelector , false , false )... )
12751275 if ! reflect .DeepEqual (iptEntries , expectedIptEntries ) {
12761276 t .Errorf ("translatedPolicy failed @ ALLOW-all-TO-app:frontend-FROM-all-namespaces-policy policy comparison" )
12771277 marshalledIptEntries , _ := json .Marshal (iptEntries )
@@ -2018,7 +2018,7 @@ func TestTranslatePolicy(t *testing.T) {
20182018 }
20192019
20202020 expectedIptEntries = append (expectedIptEntries , nonKubeSystemEntries ... )
2021- expectedIptEntries = append (expectedIptEntries , getDefaultDropEntries ("dangerous" , targetSelector , true , false )... )
2021+ expectedIptEntries = append (expectedIptEntries , getDefaultDropEntries ("dangerous" , targetSelector , false , false )... )
20222022 if ! reflect .DeepEqual (iptEntries , expectedIptEntries ) {
20232023 t .Errorf ("translatedPolicy failed @ ALLOW-ALL-TO-app:backdoor-policy policy comparison" )
20242024 marshalledIptEntries , _ := json .Marshal (iptEntries )
@@ -2325,7 +2325,6 @@ func TestTranslatePolicy(t *testing.T) {
23252325 PolicyTypes : []networkingv1.PolicyType {
23262326 networkingv1 .PolicyTypeEgress ,
23272327 },
2328- Egress : []networkingv1.NetworkPolicyEgressRule {},
23292328 },
23302329 }
23312330
@@ -2361,6 +2360,90 @@ func TestTranslatePolicy(t *testing.T) {
23612360 t .Errorf ("expectedIptEntries: %s" , marshalledExpectedIptEntries )
23622361 }
23632362
2363+ targetSelector = metav1.LabelSelector {
2364+ MatchLabels : map [string ]string {
2365+ "app" : "backend" ,
2366+ },
2367+ }
2368+
2369+ //////
2370+ /// This policy tests the case where pods should have unlimited egress traffic
2371+ //////
2372+ allowAllEgress := & networkingv1.NetworkPolicy {
2373+ ObjectMeta : metav1.ObjectMeta {
2374+ Name : "ALLOW-all-FROM-app:backend-policy" ,
2375+ Namespace : "testnamespace" ,
2376+ },
2377+ Spec : networkingv1.NetworkPolicySpec {
2378+ PodSelector : targetSelector ,
2379+ PolicyTypes : []networkingv1.PolicyType {
2380+ networkingv1 .PolicyTypeEgress ,
2381+ },
2382+ Egress : []networkingv1.NetworkPolicyEgressRule {networkingv1.NetworkPolicyEgressRule {}},
2383+ },
2384+ }
2385+
2386+ sets , lists , iptEntries = translatePolicy (allowAllEgress )
2387+
2388+ expectedSets = []string {
2389+ "app:backend" ,
2390+ }
2391+ if ! reflect .DeepEqual (sets , expectedSets ) {
2392+ t .Errorf ("translatedPolicy failed @ ALLOW-all-FROM-app:backend-policy sets comparison" )
2393+ t .Errorf ("sets: %v" , sets )
2394+ t .Errorf ("expectedSets: %v" , expectedSets )
2395+ }
2396+
2397+ expectedLists = []string {
2398+ util .KubeAllNamespacesFlag ,
2399+ }
2400+ if ! reflect .DeepEqual (lists , expectedLists ) {
2401+ t .Errorf ("translatedPolicy failed @ ALLOW-all-FROM-app:backend-policy lists comparison" )
2402+ t .Errorf ("lists: %v" , lists )
2403+ t .Errorf ("expectedLists: %v" , expectedLists )
2404+ }
2405+
2406+ expectedIptEntries = []* iptm.IptEntry {}
2407+ expectedIptEntries = append (
2408+ expectedIptEntries ,
2409+ getAllowKubeSystemEntries ("testnamespace" , targetSelector )... ,
2410+ )
2411+
2412+ nonKubeSystemEntries = []* iptm.IptEntry {
2413+ & iptm.IptEntry {
2414+ Chain : util .IptablesAzureEgressPortChain ,
2415+ Specs : []string {
2416+ util .IptablesModuleFlag ,
2417+ util .IptablesSetModuleFlag ,
2418+ util .IptablesMatchSetFlag ,
2419+ util .GetHashedName ("app:backend" ),
2420+ util .IptablesSrcFlag ,
2421+ util .IptablesModuleFlag ,
2422+ util .IptablesSetModuleFlag ,
2423+ util .IptablesMatchSetFlag ,
2424+ util .GetHashedName (util .KubeAllNamespacesFlag ),
2425+ util .IptablesDstFlag ,
2426+ util .IptablesJumpFlag ,
2427+ util .IptablesAccept ,
2428+ util .IptablesModuleFlag ,
2429+ util .IptablesCommentModuleFlag ,
2430+ util .IptablesCommentFlag ,
2431+ "ALLOW-ALL-FROM-app:backend-TO-" +
2432+ util .KubeAllNamespacesFlag ,
2433+ },
2434+ },
2435+ }
2436+ expectedIptEntries = append (expectedIptEntries , nonKubeSystemEntries ... )
2437+ // has egress, but empty map means allow all
2438+ expectedIptEntries = append (expectedIptEntries , getDefaultDropEntries ("testnamespace" , targetSelector , false , false )... )
2439+ if ! reflect .DeepEqual (iptEntries , expectedIptEntries ) {
2440+ t .Errorf ("translatedPolicy failed @ ALLOW-all-FROM-app:backend-policy policy comparison" )
2441+ marshalledIptEntries , _ := json .Marshal (iptEntries )
2442+ marshalledExpectedIptEntries , _ := json .Marshal (expectedIptEntries )
2443+ t .Errorf ("iptEntries: %s" , marshalledIptEntries )
2444+ t .Errorf ("expectedIptEntries: %s" , marshalledExpectedIptEntries )
2445+ }
2446+
23642447 targetSelector = metav1.LabelSelector {}
23652448 denyAllFromNsUnsafePolicy := & networkingv1.NetworkPolicy {
23662449 ObjectMeta : metav1.ObjectMeta {
@@ -2835,8 +2918,8 @@ func TestAllowPrecedenceOverDeny(t *testing.T) {
28352918 }
28362919 denyAllPolicy := & networkingv1.NetworkPolicy {
28372920 ObjectMeta : metav1.ObjectMeta {
2838- Name : "default-deny" ,
2839- Namespace : "default" ,
2921+ Name : "default-deny" ,
2922+ Namespace : "default" ,
28402923 },
28412924 Spec : networkingv1.NetworkPolicySpec {
28422925 PodSelector : targetSelector ,
0 commit comments