Skip to content

fix(kubernetes): scope NetworkPolicyEdgeBuilder connections to same namespace#7585

Open
AlexanderSanin wants to merge 1 commit into
bridgecrewio:mainfrom
AlexanderSanin:fix/network-policy-namespace-scoping
Open

fix(kubernetes): scope NetworkPolicyEdgeBuilder connections to same namespace#7585
AlexanderSanin wants to merge 1 commit into
bridgecrewio:mainfrom
AlexanderSanin:fix/network-policy-namespace-scoping

Conversation

@AlexanderSanin

Copy link
Copy Markdown
Contributor

Summary

Fixes false-positive PASS results in CKV2_K8S_6 (Require all pods to have a NetworkPolicy) caused by NetworkPolicyEdgeBuilder ignoring namespace scoping.

Kubernetes NetworkPolicies are namespace-scoped: a policy in namespace-a does not protect pods in namespace-b. Before this fix, the edge builder connected a NetworkPolicy with an empty podSelector: {} to every Pod in the entire graph, regardless of namespace. This meant a single wildcard NetworkPolicy in any namespace caused pods in all other namespaces to incorrectly pass the check.

Root cause: In NetworkPolicyEdgeBuilder.find_connections(), both the empty-podSelector wildcard branch and the matchLabels branch appended pod connections without comparing namespaces.

Fix: Extract the namespace from the NetworkPolicy and Pod vertex attributes (falling back to "default" when unset, matching the Kubernetes API default), and skip the connection when namespaces differ.

Closes #7474

Test plan

  • Existing test test_LabelSelectorEdgeBuilder_on_templates_with_network_policy continues to pass (all resources in the same default namespace — behaviour unchanged)
  • New test test_NetworkPolicyEdgeBuilder_respects_namespace_scoping verifies that a NetworkPolicy in namespace-a with an empty podSelector and a label-based selector only connects to pods in namespace-a, not pods in namespace-b
  • All 15 tests in tests/kubernetes/graph/test_local_graph.py pass

…amespace

Kubernetes NetworkPolicies are namespace-scoped resources: a policy in
namespace A has no effect on pods in namespace B. However,
NetworkPolicyEdgeBuilder was connecting a NetworkPolicy to every Pod in
the graph regardless of namespace, producing false-positive PASS results
in CKV2_K8S_6 for pods that shared no NetworkPolicy in their own namespace.

The fix extracts the namespace from each NetworkPolicy and Pod vertex
(falling back to "default" when not set, matching the Kubernetes API
behaviour) and skips the connection when they differ.

Both the empty-podSelector wildcard path and the matchLabels path now
respect namespace scoping.

Closes bridgecrewio#7474

Signed-off-by: Oleksandr Sanin <alexaaander.sanin@gmail.com>
@AlexanderSanin

Copy link
Copy Markdown
Contributor Author

Hey @maxamel @omriyoffe-panw. Could you, please, have a look at this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CKV2_K8S_6: NetworkPolicyEdgeBuilder ignores namespace scoping — single NetworkPolicy with empty podSelector passes all pods across all namespaces

1 participant