Skip to content

fix: Remove privileged mode from proxy-init using DNAT instead of REDIRECT - #242

Merged
huang195 merged 2 commits into
rossoctl:mainfrom
huang195:feat/remove-privileged-init
Mar 16, 2026
Merged

huang195 merged 2 commits into
rossoctl:mainfrom
huang195:feat/remove-privileged-init

Conversation

@huang195

Copy link
Copy Markdown
Member

Summary

  • Replace privileged: true with capabilities: [NET_ADMIN, NET_RAW] in the proxy-init init container
  • Switch iptables from REDIRECT to DNAT $POD_IP for the ztunnel inbound interception rule, eliminating the need for sysctl route_localnet=1 (which requires privileged mode)
  • Add POD_IP environment variable via Kubernetes Downward API (status.podIP)
  • Update all static deployment manifests to match

Context

On OpenShift, agent pods fail to start because the anyuid SCC doesn't allow privileged: true containers. See rossoctl/rossoctl#933.

The init-iptables.sh script previously required privileged mode for two reasons:

  1. iptables manipulation — only needs NET_ADMIN + NET_RAW capabilities
  2. sysctl -w net.ipv4.conf.all.route_localnet=1 — needed because REDIRECT in the OUTPUT chain rewrites dst to 127.0.0.1, and when ztunnel delivers traffic with a non-local source IP (via IP_TRANSPARENT), the kernel treats it as martian and drops it

This PR solves (2) by using DNAT to the pod's own IP instead of REDIRECT. The pod IP is a routable non-loopback address, so no martian filtering occurs and route_localnet is no longer needed. SO_ORIGINAL_DST (used by Envoy) works identically with both DNAT and REDIRECT.

Supersedes #234, which drops privileged mode but does not address the sysctl dependency in init-iptables.sh.

Files changed

  • kagenti-webhook/internal/webhook/injector/container_builder.go — capabilities + POD_IP env var
  • AuthBridge/AuthProxy/init-iptables.sh — DNAT instead of REDIRECT, remove sysctl
  • AuthBridge/AuthProxy/k8s/auth-proxy-deployment.yaml — capabilities + POD_IP
  • AuthBridge/demos/single-target/k8s/authbridge-deployment{,-no-spiffe}.yaml — same
  • AuthBridge/demos/multi-target/k8s/authbridge-deployment{,-no-spiffe}.yaml — same

Test plan

  • CI builds and passes
  • Weather-service agent deploys successfully on Kind
  • Weather-service agent deploys successfully on OpenShift with custom SCC (NET_ADMIN/NET_RAW only)
  • Istio ambient mesh coexistence: inbound traffic intercepted correctly via DNAT

@huang195
huang195 requested a review from Ladas March 16, 2026 19:31
@huang195
huang195 marked this pull request as draft March 16, 2026 19:34
@huang195
huang195 force-pushed the feat/remove-privileged-init branch 2 times, most recently from 3ed1c24 to 15f507a Compare March 16, 2026 20:01
@huang195

Copy link
Copy Markdown
Member Author

E2E Test Results — Kind cluster with Istio ambient mesh

Tested on a local Kind cluster (kagenti) with Istio ambient mesh (ztunnel), SPIRE, and Keycloak.

With Istio Ambient Mesh (authbridge namespace, istio.io/dataplane-mode=ambient)

Test Result Details
proxy-init starts without privileged PASS privileged: false, caps NET_ADMIN+NET_RAW, drop: ALL
POD_IP injected via Downward API PASS Resolved to pod IP
iptables rules use DNAT (not REDIRECT) PASS nftables shows dnat to <pod-ip>:15124
No sysctl route_localnet call PASS Logs show no sysctl, no errors
Outbound token exchange PASS agent -> auth-target:8081 via Envoy -> 200 authorized
Inbound JWT validation (no token) PASS test-client -> agent:8080 -> 401 unauthorized
Inbound JWT validation (invalid token) PASS -> 401 token validation failed
Inbound JWT validation (valid token) PASS -> 200 authorized
Ambient mesh DNAT interception PASS 3 packets matched the DNAT rule in PROXY_OUTPUT
Webhook injection (clean pod) PASS Pod in team1 got privileged=false, capabilities, POD_IP

Without Istio Ambient Mesh (e2e-no-ambient namespace, no ambient label)

Test Result Details
proxy-init starts without privileged PASS privileged: false, caps NET_ADMIN+NET_RAW, drop: ALL
iptables rules installed PASS PROXY_OUTPUT (REDIRECT to 15123), PROXY_INBOUND (REDIRECT to 15124)
DNAT rule present (0 hits) PASS DNAT rule installed but correctly idle — no ztunnel traffic
Outbound token exchange PASS agent -> auth-target:8081 -> 200 authorized
Inbound JWT validation (no token) PASS test-client -> agent:8080 -> 401 unauthorized
Inbound JWT validation (invalid token) PASS -> 401 token validation failed
Inbound JWT validation (valid token) PASS -> 200

Key observations

  • The DNAT rule (dnat to <pod-ip>:15124) is harmless when ambient mesh is not active (0 packet matches), while correctly intercepting ztunnel inbound delivery when ambient is active.
  • sysctl route_localnet=1 is no longer called — the init container runs successfully with just NET_ADMIN + NET_RAW capabilities.
  • SO_ORIGINAL_DST works identically with DNAT as with REDIRECT — Envoy's original_dst cluster resolves the correct upstream.

@huang195
huang195 marked this pull request as ready for review March 16, 2026 20:09
@pdettori

Copy link
Copy Markdown
Member

does this PR encompasses #234 or focus on additional problems?

@pdettori pdettori left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Well-crafted security hardening PR. Replaces privileged: true with the minimum capabilities (NET_ADMIN, NET_RAW) by switching from REDIRECT (which requires route_localnet=1 sysctl, hence privileged mode) to DNAT to the pod IP. The technical justification is correct and thoroughly documented.

Areas reviewed: Go, Shell, YAML/K8s, Docs
Commits: 2 commits, all signed-off ✓
CI status: all passing (17/17 checks)

Check Result
DCO sign-off ✓ All commits signed
PR title/body ✓ Conventional prefix, summary + context + test plan
Security ✓ Positive — removes privileged, drops ALL caps except NET_ADMIN/NET_RAW
Manifest consistency ✓ All 5 YAML manifests + Go webhook code updated consistently
Shell safety ✓ POD_IP validated and quoted; set -e active
Docs ✓ CLAUDE.md and README.md updated

One non-blocking suggestion about IPv6 edge case noted inline. LGTM — this is a clear security improvement.

${IPT} -t nat -A PROXY_OUTPUT -m mark --mark ${ZTUNNEL_MARK} -m owner ! --uid-owner "${PROXY_UID}" -m addrtype --dst-type LOCAL -p tcp -j DNAT --to-destination "${POD_IP}:${INBOUND_PROXY_PORT}"

# --- Rule 2: Skip all remaining ztunnel traffic ---
# After rule 1 captured ztunnel's inbound delivery, any other ztunnel traffic

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (non-blocking): IPv6 edge case — if POD_IP is an IPv6 address (dual-stack or v6-only cluster), iptables --to-destination will reject it since iptables is IPv4-only. This isn't a regression (the existing REDIRECT approach also doesn't handle IPv6), but worth considering a guard:

case "${POD_IP}" in
  *:*) echo "ERROR: POD_IP is IPv6 (${POD_IP}); iptables DNAT requires IPv4" >&2; exit 1 ;;
esac

Or even better, skip the DNAT rule for IPv6 with a warning if that's an acceptable degradation.

sysctl -w net.ipv4.conf.all.route_localnet=1
# POD_IP is required for the ztunnel inbound interception rule (DNAT target).
# It must be passed via the Kubernetes Downward API (status.podIP) or set manually.
# We use DNAT to the pod IP instead of REDIRECT to avoid needing route_localnet=1,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: Clean fail-fast guard with actionable error message. Good pattern.

# routable non-loopback address, so no martian filtering occurs.
# SO_ORIGINAL_DST (used by Envoy's original_dst cluster) works identically
# with DNAT — conntrack stores the pre-NAT original tuple for both targets.
${IPT} -t nat -A PROXY_OUTPUT -m mark --mark ${ZTUNNEL_MARK} -m owner ! --uid-owner "${PROXY_UID}" -m addrtype --dst-type LOCAL -p tcp -j DNAT --to-destination "${POD_IP}:${INBOUND_PROXY_PORT}"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: Excellent inline documentation explaining the REDIRECT → DNAT rationale. The conntrack/SO_ORIGINAL_DST note is particularly valuable for future maintainers.

- README: Replace "Privileged: Yes" with "Capabilities: NET_ADMIN, NET_RAW"
- AuthBridge/CLAUDE.md: Replace route_localnet sysctl reference with DNAT to POD_IP
- AuthBridge/CLAUDE.md: Add POD_IP to init-iptables.sh environment variables table

Signed-off-by: Hai Huang <[email protected]>
@huang195
huang195 force-pushed the feat/remove-privileged-init branch from 15f507a to 7f5c5df Compare March 16, 2026 23:32
@huang195
huang195 merged commit 9eb014b into rossoctl:main Mar 16, 2026
18 checks passed
@huang195
huang195 deleted the feat/remove-privileged-init branch March 16, 2026 23:37
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.

2 participants