homek8s/docs/network-isolation.md

94 lines
5.7 KiB
Markdown
Raw Permalink Normal View History

# Namespace network isolation
**State since 2026-11-27 (audit finding F3 / A5-1, card `t_fed52919`).** Before that the cluster
was one flat L2/L3 domain: a pod in any namespace could reach every other namespace's services, the
API server, the internal Gateway ports on `10.10.0.2` (`:8081` Grafana, `:8084` Hubble,
`:8085` Metabase, `:8086/:8087` customer1) and the internet.
## What is covered
Each app namespace carries two objects in `apps/base/<ns>/networkpolicy.yaml`,
applied by the `apps` Flux Kustomization:
| namespace | default-deny | allow policy | internet egress | Garage/S3 | CI/backup SSH | API |
|---|---|---|---|---|---|---|
| `customer1` | yes | `customer1-allow` | **no** (by design) | no | no | no |
| `osint` | yes | `osint-allow` | yes | no | no | no |
| `sirius-growth` | yes | `sirius-growth-allow` | yes | 3900 | 22 | yes |
| `piedmont` | yes | `piedmont-allow` | yes | no | no | no |
| `siriusdevops` | yes | `siriusdevops-allow` | yes | 3900 | no | no |
| `forgejo` | yes | `forgejo-allow` | yes | no | 22 | yes |
`customer1` also carries `quota.yaml`: a `ResourceQuota` + `LimitRange` sized from the declared
1-replica shape of the parked sandbox (2 CPU / 4Gi requests, 4 CPU / 10Gi limits, 45Gi storage,
`max` 1 CPU / 2Gi per container). See the comments in that file for the arithmetic.
`metabase`, `monitoring`, `logging`, `kube-system`, `ci-runners`, `netsec` and `secops` are **not**
isolated yet — they are the next tier (monitoring/logging legitimately reach everything).
## How the deny is expressed
* **Deny**: `networking.k8s.io/v1 NetworkPolicy` named `default-deny`,
`podSelector: {}`, `policyTypes: [Ingress, Egress]`, no rules. A stock NetworkPolicy is
unambiguous in Cilium's k8s-policy path, and unlike a `CiliumClusterwideNetworkPolicy` with
`endpointSelector: {}` it cannot silently select `kube-system`/CoreDNS/Flux.
* **Allow**: `CiliumNetworkPolicy` per namespace with `endpointSelector: {}`. Cilium *unions* all
policies that select an endpoint, so deny + allow = the effective policy. The Cilium CRD is needed
because the permitted sources/destinations are Cilium **entities**.
## The five rules every namespace needs
```yaml
ingress:
- fromEntities: [ingress, host, remote-node, health] # Gateway proxy + kubelet + cilium-health
- fromEndpoints: # INTRA-NAMESPACE, see below
- matchLabels: {io.kubernetes.pod.namespace: <ns>}
egress:
- toEndpoints: kube-dns :53 (UDP+TCP)
- toEndpoints: own namespace
- toEntities: [world] # internet + LAN + VPN peers, NOT pods/nodes
```
Entities that matter here:
| entity | what it is | why it appears |
|---|---|---|
| `ingress` | Cilium Gateway/listener proxy | **measured**: `hubble observe --to-namespace forgejo --to-port 3000` shows the source identity as `reserved:ingress` (identity 8), not `host`. Traffic arrives via cloudflared → hostNetwork Envoy → pod. |
| `host` | the local node IP | kubelet probes and host-network datapath |
| `remote-node` | other nodes' IPs | cross-node gateway ingress; Garage (`10.10.0.1`, `10.10.0.5`) and the Linode SSH target live here, **not** in `world` |
| `kube-apiserver` | the apiserver identity | attached to the **whole node IP** `10.10.0.2` because apiserver is host-networked — always port-scope it (see below) |
| `health` | cilium-health | inter-node health probes |
## Gotchas that cost time (all measured 2026-11-27)
1. **Cilium enforces at both ends.** An egress rule alone does not let a pod reach its own
Service: the destination pod's ingress policy must allow it too. Symptom: a probe pod in the
namespace gets `TimeoutError` on `own-service.<ns>.svc:5432` while the public route works fine
(that one enters as `ingress`). Add the same-namespace `fromEndpoints` ingress rule as well.
Confirmed by re-running the probe after adding it: `OPEN`.
2. **`toEntities: [kube-apiserver]` without `toPorts` opens the whole gateway node.** The apiserver
is host-networked on `10.10.0.2`, and Cilium attaches the `kube-apiserver` identity to that IP,
so an unscoped rule also allowed `:8085`, `:8081`, `:9100`, `:10250`. Measured, then fixed with
`toPorts: [6443, 443]` (443 = the service port the CNPG instance manager / `kubectl` dial;
6443 = the backend port). Re-probe: `apiserver OPEN`, `:8085/:8081/:9100/:10250 CLOSED`.
3. **`ipBlock`/`toCIDR` is inert for node IPs.** `10.10.0.1` (Garage on the Pi) resolves to
`reserved:remote-node`, and CIDR rules are only evaluated for `reserved:world`. Use
`toEntities: [remote-node]` + `toPorts`.
4. **`world` is not "the internet".** It is everything outside the cluster: public IPs **and** the
LAN (`192.168.1.0/24`) and VPN peers. Cluster pods and node IPs are *not* in `world`, which is
what makes `toEntities: [world]` a safe "internet only" rule.
5. **A deny shows up as `TimeoutError`, an empty Service as `ConnectionRefusedError`.** Useful when
reading probes: `pg.customer1.svc:5432` refusing proves the policy *permits* the path and the
namespace is simply parked at 0 replicas.
## Adding a dependency
1. Add the rule to that namespace's allow policy — DNS, same-namespace and the entity list are
already there; most additions are one `toEntities`/`toPorts` block.
2. Prove it both ways: the new path must go `OPEN`, and `forgejo:3000`, `metabase:3000`,
`grafana:80`, `kubernetes.default:443` (where not needed) and `10.10.0.2:8085` must stay
`CLOSED` from that namespace.
3. Watch the deny side while you test: `hubble observe --verdict DROPPED -o compact` on the
namespace you changed (`policy-verdict:...DENIED` lines name the exact port).
4. Push through a PR; Flux owns the objects. A `kubectl apply` is only for testing and will be
adopted by the next reconcile.