Lightweight nftables-based network policy enforcement agent for Kubernetes. Designed for ZeroTrust lateral movement defense across internal network segments.
network-policy-enforcer runs as a DaemonSet on each node.
On every refresh cycle it:
- Fetches the centrally-managed policy from
POLICY_URL(defaults to the InfoSec-maintained baseline atnetwork-policy-enforcer-policy) and writes it toPOLICY_FILEas a cache. - Discovers Pod IPs in
TEAM_NSvia the Kubernetes API. - Renders and applies an nftables
inettable per namespace with two drop rules:- per-source PPS rate limit on traffic to
destination_cidrs, - per-source TCP SYN rate limit on intra-namespace Pod-to-Pod traffic
(
peer_syn_rate_pps; set to0to disable).
- per-source PPS rate limit on traffic to
The agent is a single static Go binary (no jq / curl runtime dependency)
shipped on top of alpine with the nftables userspace tool. The image
intentionally does not ship a baked-in policy: the policy is always
fetched from POLICY_URL so the central baseline is the only source of truth.
Multi-arch (linux/amd64, linux/arm64) images are published to GitHub Container
Registry by GitHub Actions on every push to main and on every vX.Y.Z tag.
| Tag | Source | Use case |
|---|---|---|
:latest |
latest commit on main |
quickstart / dev clusters |
:sha-<short> |
every main push |
pinning to a specific commit |
:vX.Y.Z |
SemVer release tag | stable release |
docker pull ghcr.io/cloudnativedaysjp/network-policy-enforcer:latestdocker build -t network-policy-enforcer:dev .Or, with a local Go toolchain:
go build -o enforcer ./cmd/enforcerThe policy is fetched from POLICY_URL on startup and on every refresh cycle.
The default points at the canonical baseline maintained by the InfoSec team:
https://raw.githubusercontent.com/cloudnativedaysjp/network-policy-enforcer-policy/refs/heads/main/policy.json
Updates merged into that repository's main branch propagate to every
enforcer in the fleet within one REFRESH_INTERVAL cycle (default 30s)
without a DaemonSet restart.
Important
The image does not ship a fallback policy. If POLICY_URL is
unreachable at startup, the enforcer fails fast (CrashLoopBackOff).
Once a policy has been fetched successfully, transient POLICY_URL
failures are logged as WARN and the last-known-good policy remains in
effect.
Mounting a ConfigMap to override the cache file at POLICY_FILE does
not survive the next refresh — the next successful fetch from
POLICY_URL overwrites it. Per-cluster customization should be done by
pointing POLICY_URL at a different file (subject to InfoSec approval).
{
"version": "v2.5.0",
"policy": {
"name": "lateral-movement-baseline",
"source_cidrs": ["..."],
"destination_cidrs": ["..."],
"destination_exclude_cidrs": ["..."],
"rate_limit_pps": 2000,
"peer_syn_rate_pps": 100,
"on_exceed": "drop"
}
}| Field | Type | Description |
|---|---|---|
version |
string | Policy schema version |
policy.name |
string | Human-readable policy name |
policy.source_cidrs |
string[] | Optional. Additional source CIDR filter applied on top of the per-namespace team_pods set. Empty means "any team pod IP". |
policy.destination_cidrs |
string[] | List of destination CIDRs to enforce rate limiting on. |
policy.destination_exclude_cidrs |
string[] | Optional. Destination CIDRs to subtract from destination_cidrs (e.g. cluster-internal CIDRs to exclude when destination_cidrs is 0.0.0.0/0). |
policy.rate_limit_pps |
int | Packets per second threshold per source Pod IP for traffic to destination_cidrs. Non-positive values (or the field omitted from the JSON) disable the rule entirely with a WARN log — nftables rejects limit rate over 0/second, so emitting such a rule would fail the whole nft -f apply. |
policy.peer_syn_rate_pps |
int | New TCP connection rate threshold per source Pod IP for intra-namespace peer-to-peer traffic (saddr ∈ team Pods AND daddr ∈ team Pods). Anti-port-scan / fanout heuristic. Set to 0 to disable. |
policy.on_exceed |
string | Action when rate is exceeded: drop |
The destination_cidrs rate-limit rule matches traffic where:
saddr ∈ team_pods
∧ (source_cidrs empty ∨ saddr ∈ source_cidrs)
∧ daddr ∈ destination_cidrs
∧ (destination_exclude_cidrs empty ∨ daddr ∉ destination_exclude_cidrs)
| Variable | Required | Default | Description |
|---|---|---|---|
TEAM_NS |
Yes | — | Kubernetes namespace to discover Pod IPs from. Typically set via the downward API (metadata.namespace). |
POLICY_URL |
Yes (has a default) | https://raw.githubusercontent.com/cloudnativedaysjp/network-policy-enforcer-policy/refs/heads/main/policy.json |
URL to fetch the policy JSON from. Setting this to an empty string is a fatal error — the image does not include a fallback policy. |
POLICY_FILE |
No | /tmp/policy.json |
On-disk cache target for fetched policy. Must be writable by the container. |
APISERVER |
No | https://kubernetes.default.svc |
Kubernetes API server URL. |
REFRESH_INTERVAL |
No | 30 |
Seconds between policy refresh cycles. |
Deploy as a DaemonSet with hostNetwork: true and NET_ADMIN capability.
The agent requires:
- a ServiceAccount with
get,list,watchpermissions onpodsin the target namespace, - outbound HTTPS to
POLICY_URL.
Since the DaemonSet uses hostNetwork: true, set dnsPolicy: ClusterFirstWithHostNet
so the agent can resolve kubernetes.default.svc via cluster DNS.
On termination, the agent's preStop hook and signal handler delete the
nftables table, leaving no residual rules on the node.
The agent emits structured stdout lines for operational visibility:
network-policy-enforcer <version> startingloaded policy: <name> (<version>)policy refresh ok (<N> endpoints)— every refresh cycledrop counter: {"packets":N,"bytes":M}— destination_cidrs rule, every refresh cyclepeer-syn drops: {"packets":N,"bytes":M}— peer_syn_rate_pps rule, every refresh cyclepolicy reloaded (<md5>)— when the fetched policy content changesWARN: policy fetch failed, keeping last-known-good <file>: <err>— transientPOLICY_URLfailurepolicy load failed (<err>)— on nftables apply error
Dropped packets are also logged to the kernel ring buffer with prefixes
zte_drop: (destination_cidrs rule) and peer_syn_drop: (peer_syn_rate_pps rule).
Cut a versioned image by pushing a SemVer tag:
git tag v2.4.0
git push origin v2.4.0The build workflow publishes :v2.4.0, :2.4, and :2.4.0 tags to GHCR and
the release job creates a corresponding GitHub Release. SemVer pre-release
tags (e.g. v2.5.0-rc.1) are auto-marked as Pre-release on the GitHub
Release page and do not update the :latest image tag.
.
├── .github/workflows/build.yaml # CI: multi-arch build & push to GHCR
├── cmd/
│ └── enforcer/
│ └── main.go # CLI entrypoint (config + run loop)
├── internal/
│ ├── policy/ # Policy struct, Load (file), Fetch (URL→file)
│ ├── k8s/ # ServiceAccount-based Pod IP discovery
│ └── nft/ # Render, Apply, DeleteTable, ReadCounter
├── Dockerfile # multi-stage: golang builder → alpine + nftables
├── go.mod # Go module (no external deps)
├── LICENSE
└── README.md