kubernetes: NetworkPolicy tests for blocking north/south traffic
The NP docs point out that NP’s semantics for north/south traffic are not very clearly defined:
Cluster ingress and egress mechanisms often require rewriting the source or destination IP of packets. In cases where this happens, it is not defined whether this happens before or after NetworkPolicy processing, and the behavior may be different for different combinations of network plugin, cloud provider,
Serviceimplementation, etc.In the case of ingress, this means that in some cases you may be able to filter incoming packets based on the actual original source IP, while in other cases, the “source IP” that the NetworkPolicy acts on may be the IP of a
LoadBalanceror of the Pod’s node, etc.For egress, this means that connections from pods to
ServiceIPs that get rewritten to cluster-external IPs may or may not be subject toipBlock-based policies.
However, what’s not ambiguous is that a pod which is fully isolated for ingress should not accept E/W or N/S traffic. Regardless of how the ingress/LB/cloud handles the traffic, it should end up getting blocked. (Well, unless it gets masqueraded to the pod’s node IP, because then it hits that exception to the rules.)
Unfortunately we can’t say the same for egress; I think we assume that a pod-to-service-IP connection will be allowed to reach kube-proxy even if the pod is fully isolated-for-egress, but we explicitly don’t require that ipBlock policies get applied after service proxying.
Anyway, we should be able to add a test to test/e2e/network/netpol/network_policy.go that confirms that cluster-ingress traffic to a fully isolated-for-ingress pod is not allowed. In particular, if we create a LoadBalancer Service with externalTrafficPolicy: Local, and a NetworkPolicy blocking all ingress to that Service’s Pods, then we should not be able to connect to the service via either the LoadBalancer IP/name or via its NodePort (on one of the correct nodes).
/sig network /area network-policy /priority backlog /help /good-first-issue
About this issue
- Original URL
- State: open
- Created 4 years ago
- Comments: 27 (17 by maintainers)
Commits related to this issue
- Implemented deny-all NP test for LB without SNAT This adds a new end-to-end test in test/e2e/network/netpol/network_policy.go that verifies a default-deny ingress NetworkPolicy truly blocks north–sou... — committed to justinlyon12/kubernetes by justinlyon12 a year ago
- Moved netpol e2e tests up level to e2e/network dir Moved network_policy.go up a level so the actual e2e tests are in the e2e/network directory with all other e2e tests and the e2e/network/netpol now ... — committed to justinlyon12/kubernetes by justinlyon12 a year ago
- Implemented deny-all NP test for LB without SNAT This adds a new end-to-end test in test/e2e/network/netpol/network_policy.go that verifies a default-deny ingress NetworkPolicy truly blocks north–sou... — committed to justinlyon12/kubernetes by justinlyon12 a year ago
- Moved netpol e2e tests up level to e2e/network dir Moved network_policy.go up a level so the actual e2e tests are in the e2e/network directory with all other e2e tests and the e2e/network/netpol now ... — committed to justinlyon12/kubernetes by justinlyon12 a year ago
- Implemented deny-all NP test for LB without SNAT This adds a new end-to-end test in test/e2e/network/netpol/network_policy.go that verifies a default-deny ingress NetworkPolicy truly blocks north–sou... — committed to justinlyon12/kubernetes by justinlyon12 a year ago
- Moved netpol e2e tests up level to e2e/network dir Moved network_policy.go up a level so the actual e2e tests are in the e2e/network directory with all other e2e tests and the e2e/network/netpol now ... — committed to justinlyon12/kubernetes by justinlyon12 a year ago
Probably
tests/e2e/network/netpol/network_policy.goshould be moved up a level totest/e2e/network/network_policy.go(based on the comments in that file, it seems like maybe that was the original plan?), andnetpol/should only contain the helpers, not the actual e2e tests themselves. And then the new test can be added totest/e2e/network/network_policy.go?Note that wherever it ends up, the test will need both
feature.NetworkPolicyandfeature.LoadBalancer.The non-
externalTrafficPolicy: Localcase is expected to do SNAT and theexternalTrafficPolicy: Localcase for NodePort is not interestingly different from the LoadBalancer IP case, so there’s probably no point in testing this separately.It’s not really “non-determinism”; any given environment is deterministic. And some people will run the e2e suite in environments that will fail the test due to SNAT. So I think the result should be:
I think having the test skip itself when it detects SNAT is preferable to just telling people “you should skip this test if your environment doesn’t support it”, because you’re only supposed to skip it if the connection gets through because your load balancer did the “wrong” thing, not if the connection gets through because your network plugin does the wrong thing.