|
| 1 | +/* |
| 2 | +Copyright 2022 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package tests |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + v1 "k8s.io/api/core/v1" |
| 25 | + networkingv1 "k8s.io/api/networking/v1" |
| 26 | + "k8s.io/kubernetes/test/e2e/framework" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 28 | + |
| 29 | + "sigs.k8s.io/network-policy-api/apis/v1alpha1" |
| 30 | + "sigs.k8s.io/network-policy-api/conformance/utils/kubernetes" |
| 31 | + "sigs.k8s.io/network-policy-api/conformance/utils/suite" |
| 32 | +) |
| 33 | + |
| 34 | +func init() { |
| 35 | + ConformanceTests = append(ConformanceTests, |
| 36 | + AdminNetworkPolicyIntegration, |
| 37 | + ) |
| 38 | +} |
| 39 | + |
| 40 | +var AdminNetworkPolicyIntegration = suite.ConformanceTest{ |
| 41 | + ShortName: "AdminNetworkPolicyIntegration", |
| 42 | + Description: "Tests integration support for gress traffic between ANP, NP and BANP using PASS action based on a server and client model", |
| 43 | + Features: []suite.SupportedFeature{ |
| 44 | + suite.SupportAdminNetworkPolicy, |
| 45 | + }, |
| 46 | + Manifests: []string{"tests/admin-network-policy-core-integration_base.yaml"}, |
| 47 | + Test: func(t *testing.T, s *suite.ConformanceTestSuite) { |
| 48 | + |
| 49 | + t.Run("Should Deny traffic from slytherin to gryffindor respecting ANP", func(t *testing.T) { |
| 50 | + ctx, cancel := context.WithTimeout(context.Background(), s.TimeoutConfig.GetTimeout) |
| 51 | + defer cancel() |
| 52 | + // This test uses `pass-example` ANP |
| 53 | + // harry-potter-0 is our server pod in gryffindor namespace |
| 54 | + clientPod := &v1.Pod{} |
| 55 | + err := s.Client.Get(ctx, client.ObjectKey{ |
| 56 | + Namespace: "network-policy-conformance-gryffindor", |
| 57 | + Name: "harry-potter-0", |
| 58 | + }, clientPod) |
| 59 | + framework.ExpectNoError(err, "unable to fetch the server pod") |
| 60 | + // draco-malfoy-0 is our client pod in slytherin namespace |
| 61 | + // ensure ingress is DENIED to gryffindor from slytherin |
| 62 | + // inressRule at index0 will take effect |
| 63 | + success := kubernetes.PokeServer(t, "network-policy-conformance-slytherin", "draco-malfoy-0", "tcp", |
| 64 | + clientPod.Status.PodIP, int32(80), s.TimeoutConfig.RequestTimeout, false) |
| 65 | + assert.Equal(t, true, success) |
| 66 | + // draco-malfoy-1 is our client pod in slytherin namespace |
| 67 | + success = kubernetes.PokeServer(t, "network-policy-conformance-slytherin", "draco-malfoy-1", "tcp", |
| 68 | + clientPod.Status.PodIP, int32(8080), s.TimeoutConfig.RequestTimeout, false) |
| 69 | + assert.Equal(t, true, success) |
| 70 | + }) |
| 71 | + |
| 72 | + t.Run("Should Deny traffic to slytherin from gryffindor respecting ANP", func(t *testing.T) { |
| 73 | + ctx, cancel := context.WithTimeout(context.Background(), s.TimeoutConfig.GetTimeout) |
| 74 | + defer cancel() |
| 75 | + // This test uses `pass-example` ANP |
| 76 | + // draco-malfoy-0 is our server pod in slytherin namespace |
| 77 | + clientPod := &v1.Pod{} |
| 78 | + err := s.Client.Get(ctx, client.ObjectKey{ |
| 79 | + Namespace: "network-policy-conformance-slytherin", |
| 80 | + Name: "draco-malfoy-0", |
| 81 | + }, clientPod) |
| 82 | + framework.ExpectNoError(err, "unable to fetch the server pod") |
| 83 | + // harry-potter-0 is our client pod in gryffindor namespace |
| 84 | + // ensure ingress is DENIED to gryffindor from slytherin |
| 85 | + // egressRule at index0 will take effect |
| 86 | + success := kubernetes.PokeServer(t, "network-policy-conformance-gryffindor", "harry-potter-0", "tcp", |
| 87 | + clientPod.Status.PodIP, int32(80), s.TimeoutConfig.RequestTimeout, false) |
| 88 | + assert.Equal(t, true, success) |
| 89 | + // harry-potter-1 is our client pod in gryffindor namespace |
| 90 | + success = kubernetes.PokeServer(t, "network-policy-conformance-gryffindor", "harry-potter-1", "tcp", |
| 91 | + clientPod.Status.PodIP, int32(8080), s.TimeoutConfig.RequestTimeout, false) |
| 92 | + assert.Equal(t, true, success) |
| 93 | + }) |
| 94 | + |
| 95 | + t.Run("Should support a 'pass-ingress' policy for ANP and respect the match for network policy", func(t *testing.T) { |
| 96 | + ctx, cancel := context.WithTimeout(context.Background(), s.TimeoutConfig.GetTimeout) |
| 97 | + defer cancel() |
| 98 | + // This test uses `pass example` ANP |
| 99 | + anp := &v1alpha1.AdminNetworkPolicy{} |
| 100 | + err := s.Client.Get(ctx, client.ObjectKey{ |
| 101 | + Name: "pass-example", |
| 102 | + }, anp) |
| 103 | + framework.ExpectNoError(err, "unable to fetch the admin network policy") |
| 104 | + // change ingress rule from "deny" to "pass" |
| 105 | + anp.Spec.Ingress[0].Action = v1alpha1.AdminNetworkPolicyRuleActionPass |
| 106 | + err = s.Client.Update(ctx, anp) |
| 107 | + framework.ExpectNoError(err, "unable to update the admin network policy") |
| 108 | + // harry-potter-0 is our server pod in gryffindor namespace |
| 109 | + clientPod := &v1.Pod{} |
| 110 | + err = s.Client.Get(ctx, client.ObjectKey{ |
| 111 | + Namespace: "network-policy-conformance-gryffindor", |
| 112 | + Name: "harry-potter-0", |
| 113 | + }, clientPod) |
| 114 | + framework.ExpectNoError(err, "unable to fetch the server pod") |
| 115 | + // draco-malfoy-0 is our client pod in slytherin namespace |
| 116 | + // ensure ingress is PASSED to gryffindor from slytherin - the network policy ALLOW should take effect |
| 117 | + // inressRule at index0 will take effect |
| 118 | + success := kubernetes.PokeServer(t, "network-policy-conformance-slytherin", "draco-malfoy-0", "tcp", |
| 119 | + clientPod.Status.PodIP, int32(80), s.TimeoutConfig.RequestTimeout, true) |
| 120 | + assert.Equal(t, true, success) |
| 121 | + // draco-malfoy-1 is our client pod in slytherin namespace |
| 122 | + success = kubernetes.PokeServer(t, "network-policy-conformance-slytherin", "draco-malfoy-1", "tcp", |
| 123 | + clientPod.Status.PodIP, int32(8080), s.TimeoutConfig.RequestTimeout, true) |
| 124 | + assert.Equal(t, true, success) |
| 125 | + }) |
| 126 | + |
| 127 | + t.Run("Should support a 'pass-egress' policy for ANP and respect the match for network policy", func(t *testing.T) { |
| 128 | + ctx, cancel := context.WithTimeout(context.Background(), s.TimeoutConfig.GetTimeout) |
| 129 | + defer cancel() |
| 130 | + // This test uses `pass example` ANP |
| 131 | + anp := &v1alpha1.AdminNetworkPolicy{} |
| 132 | + err := s.Client.Get(ctx, client.ObjectKey{ |
| 133 | + Name: "pass-example", |
| 134 | + }, anp) |
| 135 | + framework.ExpectNoError(err, "unable to fetch the admin network policy") |
| 136 | + // change egress rule from "deny" to "pass" |
| 137 | + anp.Spec.Egress[0].Action = v1alpha1.AdminNetworkPolicyRuleActionPass |
| 138 | + err = s.Client.Update(ctx, anp) |
| 139 | + framework.ExpectNoError(err, "unable to update the admin network policy") |
| 140 | + // draco-malfoy-0 is our server pod in slytherin namespace |
| 141 | + clientPod := &v1.Pod{} |
| 142 | + err = s.Client.Get(ctx, client.ObjectKey{ |
| 143 | + Namespace: "network-policy-conformance-slytherin", |
| 144 | + Name: "draco-malfoy-0", |
| 145 | + }, clientPod) |
| 146 | + framework.ExpectNoError(err, "unable to fetch the server pod") |
| 147 | + // harry-potter-0 is our client pod in gryffindor namespace |
| 148 | + // ensure ingress is PASSED to gryffindor from slytherin - the underlying network policy ALLOW should take effect |
| 149 | + // egressRule at index0 will take effect |
| 150 | + success := kubernetes.PokeServer(t, "network-policy-conformance-gryffindor", "harry-potter-0", "tcp", |
| 151 | + clientPod.Status.PodIP, int32(80), s.TimeoutConfig.RequestTimeout, true) |
| 152 | + assert.Equal(t, true, success) |
| 153 | + // harry-potter-1 is our client pod in gryffindor namespace |
| 154 | + success = kubernetes.PokeServer(t, "network-policy-conformance-gryffindor", "harry-potter-1", "tcp", |
| 155 | + clientPod.Status.PodIP, int32(8080), s.TimeoutConfig.RequestTimeout, true) |
| 156 | + assert.Equal(t, true, success) |
| 157 | + }) |
| 158 | + |
| 159 | + t.Run("Should support a 'pass-ingress' policy for ANP and respect the match for baseline admin network policy", func(t *testing.T) { |
| 160 | + ctx, cancel := context.WithTimeout(context.Background(), s.TimeoutConfig.GetTimeout) |
| 161 | + defer cancel() |
| 162 | + // This test uses `default` BANP |
| 163 | + np := &networkingv1.NetworkPolicy{} |
| 164 | + err := s.Client.Get(ctx, client.ObjectKey{ |
| 165 | + Namespace: "network-policy-conformance-gryffindor", |
| 166 | + Name: "allow-gress-from-to-slytherin-to-gryffindor", |
| 167 | + }, np) |
| 168 | + framework.ExpectNoError(err, "unable to fetch the network policy") |
| 169 | + // delete network policy so that BANP takes effect |
| 170 | + err = s.Client.Delete(ctx, np) |
| 171 | + framework.ExpectNoError(err, "unable to delete the network policy") |
| 172 | + // harry-potter-0 is our server pod in gryffindor namespace |
| 173 | + clientPod := &v1.Pod{} |
| 174 | + err = s.Client.Get(ctx, client.ObjectKey{ |
| 175 | + Namespace: "network-policy-conformance-gryffindor", |
| 176 | + Name: "harry-potter-0", |
| 177 | + }, clientPod) |
| 178 | + framework.ExpectNoError(err, "unable to fetch the server pod") |
| 179 | + // draco-malfoy-0 is our client pod in slytherin namespace |
| 180 | + // ensure ingress is PASSED to gryffindor from slytherin - the baseline admin network policy DENY should take effect |
| 181 | + // inressRule at index0 will take effect |
| 182 | + success := kubernetes.PokeServer(t, "network-policy-conformance-slytherin", "draco-malfoy-0", "tcp", |
| 183 | + clientPod.Status.PodIP, int32(80), s.TimeoutConfig.RequestTimeout, false) |
| 184 | + assert.Equal(t, true, success) |
| 185 | + // draco-malfoy-1 is our client pod in slytherin namespace |
| 186 | + success = kubernetes.PokeServer(t, "network-policy-conformance-slytherin", "draco-malfoy-1", "tcp", |
| 187 | + clientPod.Status.PodIP, int32(8080), s.TimeoutConfig.RequestTimeout, false) |
| 188 | + assert.Equal(t, true, success) |
| 189 | + }) |
| 190 | + |
| 191 | + t.Run("Should support a 'pass-egress' policy for ANP and respect the match for baseline admin network policy", func(t *testing.T) { |
| 192 | + ctx, cancel := context.WithTimeout(context.Background(), s.TimeoutConfig.GetTimeout) |
| 193 | + defer cancel() |
| 194 | + // This test uses `default` BANP |
| 195 | + // draco-malfoy-0 is our server pod in slytherin namespace |
| 196 | + clientPod := &v1.Pod{} |
| 197 | + err := s.Client.Get(ctx, client.ObjectKey{ |
| 198 | + Namespace: "network-policy-conformance-slytherin", |
| 199 | + Name: "draco-malfoy-0", |
| 200 | + }, clientPod) |
| 201 | + framework.ExpectNoError(err, "unable to fetch the server pod") |
| 202 | + // harry-potter-0 is our client pod in gryffindor namespace |
| 203 | + // ensure ingress is PASSED to gryffindor from slytherin - the underlying baseline admin network policy DENY should take effect |
| 204 | + // egressRule at index0 will take effect |
| 205 | + success := kubernetes.PokeServer(t, "network-policy-conformance-gryffindor", "harry-potter-0", "tcp", |
| 206 | + clientPod.Status.PodIP, int32(80), s.TimeoutConfig.RequestTimeout, false) |
| 207 | + assert.Equal(t, true, success) |
| 208 | + // harry-potter-1 is our client pod in gryffindor namespace |
| 209 | + success = kubernetes.PokeServer(t, "network-policy-conformance-gryffindor", "harry-potter-1", "tcp", |
| 210 | + clientPod.Status.PodIP, int32(8080), s.TimeoutConfig.RequestTimeout, false) |
| 211 | + assert.Equal(t, true, success) |
| 212 | + }) |
| 213 | + }, |
| 214 | +} |
0 commit comments