Skip to content

fix(k8s): an in-namespace egress allow skips the ingress rules #6708

Closed

Description

@dan-petty

Problem

Checked on the live cluster on 2026-11-21 (k3s v1.37.2+k3s1 on 5 nodes, devops-cli main 11fee3f).

  • The network policy controller of this cluster does not apply the ingress rules of the destination pod when an egress rule of the source pod allows the traffic. A controlled probe with temporary namespaces (busybox httpd server, an allowed and a denied client, ingress on TCP 8080 only from allowed; all namespaces deleted after the test) gave the exit codes of nc -z -w 3 <server pod IP> 8080:

    Policy shape Nodes allowed denied
    Ingress only same node, and two nodes 0 1 (refused)
    Ingress and Egress, egress to: [podSelector: {}] with no ports same node, and two nodes 0 0 (passes)
  • Retest of 2026-11-22: with two separate policies (an ingress-only policy on the server and an egress-only policy on the clients) the denied client is refused, on one node and across nodes, on two nodes and twice. The first two-policy result was a timing effect. The bypass needs one policy with both types, and single-type policies work.

  • The NetworkPolicy API says that a connection needs both the egress rules of the source and the ingress rules of the destination. Here the egress allow is enough.

  • 8 namespaces have a perimeter with an in-namespace egress rule with no ports and ingress rules that limit a port or a peer, so their in-namespace ingress rules do not hold: argocd (for example argocd-redis), gitea, llm (for example llm-gateway-db-perimeter, valkey-perimeter, valkey-devops-perimeter, valkey-runs-perimeter), logging, mattermost, monitoring (grafana-image-renderer-ingress), object-store and otel. Query: each policy with an egress rule whose only peer is a podSelector and that has no ports.

  • The object store has its own item, because its rule answers an advisory with no fix: fix(k8s): narrow the in-namespace egress of the object store #6707.

  • The k8s check rows read each policy as the standard says, so they pass on these perimeters.

Proposed remedy

  • In each of the 8 namespaces (with object-store-perimeter, which fix(k8s): narrow the in-namespace egress of the object store #6707 narrows first), split each policy with both types into an ingress-only policy and an egress-only policy with the same rules (single-type policies work), and give the in-namespace egress the same peers and ports as the in-namespace ingress rules of its destinations, so the egress check of the source holds the rule under this controller and under the standard rule. Where a destination admits only some pods (Redis, Valkey, a database), add the egress to it in a policy that selects only those pods.
  • Add a k8s check row that refuses a NetworkPolicy whose policyTypes holds both Ingress and Egress, and an egress rule with a podSelector peer in its own namespace and no ports in a namespace whose ingress rules limit a port or a peer. The row names the policy and the controller behavior, so the problem cannot come back while the controller has it (AGENTS.md rule 11).
  • Write the controller behavior and the egress rule in the NetworkPolicy section of k8s/README.md.
  • Report the controller behavior upstream in private, as a security report: the private vulnerability reporting of kube-router and a note to the k3s security contact (person step: the owner sends both).
  • A part of this item that does not merge before plan step 10 of feat(argocd): read the environment from the homelab repo #5134 lands in dan-petty/homelab, because the step 12 freeze refuses changes to the old environment paths here.

Acceptance criteria

  • uv run devops ci --check --only k8s passes, and the new row fails on a fixture with an in-namespace egress rule with no ports and a port-limited ingress rule.
  • The query of the Problem section finds no policy on the live cluster after the sync.
  • For each namespace, a probe from a pod that the ingress rules do not admit to a limited port exits 1, and each Application of the namespace stays Synced and Healthy.

Dependencies

  • Related: dan-petty/homelab#346 (more perimeters, and the API-server egress on TCP 6443).

Proposed design

Problem statement

The k3s cluster's NetworkPolicy controller, kube-router, keeps one chain per policy. A policy with both Ingress and Egress types writes its ingress and egress rules into that one chain, and the chain of a pod selected by the policy accepts traffic that an egress rule of the same policy allows before the destination ingress rules run. So a two-type policy with an in-namespace egress allow bypasses the ingress rules of the destination pod. The live query in the issue found this shape in eight namespaces, and the object store stays with #6707, so this item changes seven. The change splits each two-type policy into an ingress-only policy and an egress-only policy, aligns the in-namespace egress peers and ports with the destination ingress rules, adds a k8s gate row, and documents the behavior. The object store stays with #6707, and a deferred issue carries the private upstream report. The change must merge before plan step 10 of #5134, or it moves to dan-petty/homelab.

Acceptance criteria

Requirement: Gate refuses a two-type policy with a broad in-namespace egress allow

The k8s gate SHALL refuse a NetworkPolicy that has both Ingress and Egress types and an egress rule whose podSelector peer overlaps the policy's podSelector and whose ports the ingress rules do not admit from those pods.

Scenario: Fixture with a two-type policy

  • WHEN uv run devops ci --check --only k8s runs, and the check module builds a fixture with a two-type NetworkPolicy, an egress rule whose podSelector peer overlaps the policy's podSelector and has no ports, and an ingress rule that limits a port or a peer.
  • THEN The rule returns a problem that names the fixture policy and the controller behavior, and the k8s check exits zero with the fixture case passed.

Requirement: Gate accepts single-type and aligned policies

The k8s gate SHALL accept a single-type policy and a two-type policy whose in-namespace egress ports and peers match the ingress rules of the destination pods.

Scenario: Fixture with compliant policies

  • WHEN uv run devops ci --check --only k8s runs, and the check module builds an ingress-only policy, an egress-only policy, and a two-type policy whose egress rule to an overlapping podSelector peer names the port that the ingress rule admits.
  • THEN The rule returns no problem for the three policies, and the k8s check exits zero with the compliant case passed.

Requirement: Seven namespaces have no bypass policy

Each in-scope namespace SHALL have no NetworkPolicy that has both Ingress and Egress types and an egress rule whose podSelector peer overlaps the policy's podSelector and whose ports the ingress rules do not admit.

Scenario: Manifest scan of the seven namespaces

  • WHEN uv run devops ci --check --only k8s runs on the environment tree after the change.
  • THEN The k8s check exits zero and reports no such policy in argocd, gitea, llm, logging, mattermost, monitoring and otel.

Requirement: Live cluster has no broad in-namespace egress allow

Each in-scope namespace SHALL have no two-type NetworkPolicy with an egress rule whose podSelector peer overlaps the policy's podSelector and has no ports after Argo CD sync.

Scenario: Live query after Argo CD sync

  • WHEN The live check kubectl get networkpolicy -A -o json runs with a jq filter for each two-type policy with an egress rule whose podSelector peer overlaps the policy's podSelector and has no ports after Argo CD syncs.
  • THEN The filter returns no policy in argocd, gitea, llm, logging, mattermost, monitoring and otel.

Requirement: Denied pod cannot reach a limited port

For each in-scope namespace, a pod that the ingress rules do not admit MUST fail to connect to a limited port.

Scenario: Probe from a denied pod on one node and across nodes

  • WHEN The live probe nc -z -w 3 <server pod IP> <limited port> runs from a pod that the ingress rules do not admit in each in-scope namespace, on the node of the server pod and on a second node, after Argo CD syncs.
  • THEN The probe exits 1.

Requirement: In-scope Applications stay Synced and Healthy

Each Argo CD Application of an in-scope namespace SHALL stay Synced and Healthy after the policy change.

Scenario: Argo CD status after sync

  • WHEN devops argo cd apps status <app> runs for each Application of the seven in-scope namespaces after the policy change and sync.
  • THEN Each command shows the Application as Synced and Healthy.

Requirement: NetworkPolicy section states the controller behavior

A NetworkPolicy section of k8s/README.md SHALL state the kube-router behavior and the egress rule.

Scenario: Docs check

  • WHEN uv run devops ci --check --only docs runs.
  • THEN The docs check passes.

Existing Tool Considered

No alternatives check has run. No open, maintained tool covers this controller-specific rule. kube-linter is the closest existing tool: this repository runs its non-isolated-pod check on NetworkPolicy objects (.kube-linter.yaml:26), and it lints Kubernetes workloads and policies, but it has no check for a two-type NetworkPolicy whose in-namespace egress peer and ports do not match the destination ingress rules. The change adds one row to the existing k8s gate at tests/gate/k8s_checks/network_policies.py, which tests/gate/k8s.py:11-16 runs. The written cost to build, keep and fix that row until it matches a tool is about 15k tokens and 2 hours of elapsed time.

Key questions

  • Which namespaces does this item change?: The item changes argocd, gitea, llm, logging, mattermost, monitoring and otel. The object-store namespace stays with fix(k8s): narrow the in-namespace egress of the object store #6707. [sources: k8s/argocd/networkpolicy.yaml, k8s/gitea/networkpolicy.yaml, k8s/llm/networkpolicy.yaml, k8s/logging/networkpolicy.yaml, k8s/mattermost/networkpolicy.yaml, k8s/monitoring/networkpolicy.yaml, k8s/otel/networkpolicy.yaml, k8s/object-store/networkpolicy.yaml]
  • What is the controller behavior and its scope?: kube-router keeps one chain per policy, and a policy with both Ingress and Egress types writes its ingress and egress rules into that one chain (kube-router policy.go, fetched 2026-11-22). Each pod's firewall chain references the chain of each policy that selects it, and a matched rule sets the policy mark (DeepWiki, fetched 2026-11-22). So when the same two-type policy selects the source and has an egress rule to the destination, the egress rule sets the mark and the destination chain accepts the traffic before the destination ingress rules run. A policy selects only pods of its own namespace, so the bypass needs a source and a destination in one namespace; an egress rule to another namespace does not skip the destination ingress rules. [sources: https://github.com/cloudnativelabs/kube-router/blob/master/pkg/controllers/netpol/policy.go, https://deepwiki.com/cloudnativelabs/kube-router/5.3-network-policy-enforcement, https://kubernetes.io/docs/concepts/services-networking/network-policies/]
  • How should the in-namespace egress rules change?: Split each policy with both Ingress and Egress types into an ingress-only policy and an egress-only policy with the same rules, because a single-type policy does not bypass the destination ingress rules. Then mirror the peers and ports of the destination ingress rules in the egress-only rule. When a destination admits only some pods, add a separate egress policy that selects only those destination pods. (best practice) [sources: https://github.com/cloudnativelabs/kube-router/blob/master/pkg/controllers/netpol/policy.go, k8s/llm/networkpolicy.yaml:66-69, k8s/llm/valkey.yaml:191, k8s/llm/gateway-db.yaml:21-55]
    • Chosen: Split each two-type policy into single-type policies, then mirror the destination ingress peers and ports in the egress-only policy, with a destination-specific policy where needed. This removes the two-type policy that the controller lets bypass the ingress rules, and it keeps the egress allow and the ingress limit aligned under both the controller and the standard.
    • Rejected: Keep the two-type policy and mirror the ingress peers and ports in its egress rule. The two-type policy keeps the chain that accepts the egress allow before the destination ingress rules, so the bypass remains.
    • Rejected: Remove the in-namespace egress rule and rely on ingress only. This opens all egress or blocks in-namespace traffic, and it does not keep the egress isolation.
    • Rejected: Use an empty podSelector egress allow. The controller lets it bypass the ingress rules, and it admits every pod.
  • Where should the new check row live?: Add the row to the existing k8s gate module tests/gate/k8s_checks/network_policies.py, so uv run devops ci --check --only k8s runs it. tests/gate/k8s.py:11-16 imports the package, and the runner calls each function named check_* (tests/gate/runner.py:40-47). (best practice) [sources: tests/gate/k8s.py:11-16, tests/gate/runner.py:40-47, tests/gate/k8s_checks/network_policies.py, AGENTS.md:17, docs/adr/0004-ci-is-the-authoritative-gate.md]
    • Chosen: Add a row to the existing k8s gate. The gate already reads NetworkPolicy manifests and runs in CI, and ADR 0004 makes CI the merge gate.
    • Rejected: Add a Kyverno ClusterPolicy. This adds a new admission controller and still needs a custom policy.
    • Rejected: Add a kube-linter custom check. kube-linter has no check for this controller behavior, and a custom check is new code.
  • How should the gate prove the new row fails?: Use an offline fixture in the check module and a check_* case that asserts the rule returns a problem for it, as tests/gate/k8s_checks/object_store.py:710-740 does for the admin port. The fixture is a two-type policy with an in-namespace egress rule whose podSelector peer overlaps the policy's podSelector and has no ports, and an ingress rule that limits a port or a peer. The gate row stays green when the rule catches the fixture, so uv run devops ci --check --only k8s proves the rule without a live cluster. (best practice) [sources: tests/gate/k8s_checks/object_store.py:710-740, tests/gate/runner.py:40-47, AGENTS.md:17]
    • Chosen: Offline fixture with a self-test case in the k8s gate. CI can check the rule without a live cluster, and the case fails when the rule stops catching the fixture.
    • Rejected: Live cluster only. The gate cannot depend on the live cluster in CI.
    • Rejected: No negative fixture. A row that never fails proves nothing.
  • Where does the controller behavior go in the documentation?: Add a NetworkPolicy section to k8s/README.md. The README has no such section today, and it already holds the cluster-specific policy notes: the monitoring perimeter paragraph (k8s/README.md:306) and the object store Network bullet (k8s/README.md:1120). The general guideline points to the environment tree README for the decisions of a cluster (docs/guidelines/kubernetes.md:3). (best practice) [sources: k8s/README.md:306, k8s/README.md:1120, docs/guidelines/kubernetes.md:3]
    • Chosen: Add a NetworkPolicy section to k8s/README.md. The environment tree README holds the decisions of this cluster, and the behavior is specific to this controller.
    • Rejected: Add to docs/guidelines/kubernetes.md. The guideline applies to each managed project and names no issue, date, node or path of an environment tree; it points to the environment tree README for the decisions of a cluster.
  • Who sends the private upstream security report?: The owner sends the report to the kube-router private vulnerability reporting and to the k3s security contact. The report is a deferred issue and is not part of the pull request. (a person decides)

Open questions

  • Who sends the private upstream security report? (a person decides)

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    type/bug
    scope/k8s
    source/agent
    status/new
    effort/medium
    value/high
    on Nov 21, 2026
  2. dan-petty commented on Nov 21, 2026

    @dan-petty
    OwnerAuthor
    • Priority: P1-High. Live security vulnerability: 8 namespaces have broken network isolation where denied pods can reach restricted services.
    • Related: fix(k8s): narrow the in-namespace egress of the object store #6707, which asks for a different change. Both fix the same k3s controller behavior where an in-namespace egress rule with no ports bypasses ingress rules, but dan-petty/devops-cli#6707 narrows the object store egress while the candidate narrows the other 7 namespaces and adds a check row.
  3. added
    priority/p1-high
    on Nov 21, 2026
  4. devops-cli-service commented on Nov 22, 2026

    @devops-cli-service
    Contributor

    Next step: the alternatives check records a verdict. See the section Proposed design of the body.

  5. dan-petty commented on Nov 22, 2026

    @dan-petty
    OwnerAuthor

    The source of kube-router shows the mechanism of the bypass, and limits it to one namespace.

    • setupPodNetpolRules (pkg/controllers/netpol/pod.go) adds to the chain of each pod a jump to the chain of each policy that selects the pod. For a policy with the types Ingress and Egress, the jump has no -d or -s match. For a single-type policy, the jump has -d <pod IP> (Ingress) or -s <pod IP> (Egress).
    • One policy has one chain, and the chain holds the ingress rules and the egress rules (processIngressRules and processEgressRules in policy.go). An egress rule matches the source ipset of the target pods of the policy and the destination ipset of its peers.
    • So for traffic from pod A to pod B, the chain of B runs the egress rules of each two-type policy that selects B. When the same policy selects A and has an egress rule to B, the rule sets the mark, and the chain of B accepts the traffic. The chain of each pod resets the mark at its end (dropUnmarkedTrafficRules), so an egress allow in another policy does not pass on to the chain of B. This agrees with the retest of 2026-11-22: two single-type policies work.
    • A policy selects only pods of its own namespace. So the bypass needs a source and a destination in one namespace. An egress rule that reaches another namespace, for example rule 3 of llm-default-perimeter (ipBlock 0.0.0.0/0 with no ports), does not skip the ingress rules of the destination namespace.

    So the row of this item can stay in-namespace: a two-type policy with an egress rule whose peers include pods that the same policy selects, and ports that its ingress rules do not admit from those pods. PR #6712 limits the object store text to the same scope.

  6. added
    owner/agent
    status/in-progress
    and removed
    status/new
    on Nov 22, 2026
  7. devops-cli-service commented on Nov 22, 2026

    @devops-cli-service
    Contributor

    Owner question: See the first point of Open questions in the section Proposed design of the body.

    Options

    1. Decide the question: The job refines the item with your answer.
    2. Leave it to the job: The job records the best-practice answer and goes on.

    Recommendation: Decide the question: only a person can answer it.

    Evidence

  8. added
    status/in-review
    and removed
    status/in-progress
    on Nov 22, 2026
  9. removed
    status/in-review
    owner/agent
    on Nov 22, 2026
  10. devops-cli-service commented on Nov 22, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-22 03:31 UTC

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p1-highscope/k8ssource/agenttype/bugvalue/high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions