Skip to content

feat: Phase 1 Kubernetes implementation (stacked on #8) - #10

Closed
chaodu-agent wants to merge 22 commits into
mainfrom
feat/phase1-k8s
Closed

chaodu-agent wants to merge 22 commits into
mainfrom
feat/phase1-k8s

Conversation

@chaodu-agent

@chaodu-agent chaodu-agent commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Stacked PR. Base is spike/phase0 (PR #8, still open) so the diff shows only Phase 1 code. Retarget to main after #8 merges.

What this builds

Phase 1 of auth-proxy as a Rust workspace: ap-core (types, config), ap-broker, ap-proxy (the auth-proxy binary), ap-client, ap-egress (CONNECT-only allowlist egress proxy), the ported Phase 0 matrix and integration tests, Kubernetes deploy assets, docs, CI and a sync script. Kubernetes only. ECS and S2 are deferred.

Owner decision (Q1)

  • Q1-B remains the formal design and is frozen. No new work on its machinery (registry, leases, authority lock, resync).
  • Q1-A static config is an INTERIM path for the first end-to-end Kubernetes deployment. It has no per-session revocation. The owner accepted that.
  • The real-pod hello-race test stays a release gate.
  • ap-egress stays a separate container and UID.

Changes in this update (council items)

  1. aws_targets dormant. The parser, validation and tests stay. A new test fails if any shipped policy under deploy/ uses it. kiro-cli 3.3.0 was measured to send one X-Amz-Target per call, so the assumption holds, but no grant uses it yet.
  2. egress.allow_route_hosts removed (security fix). It let a credential route host also be an egress host, so a CONNECT tunnel could carry another credential straight to a credential host. A policy that sets it is now an unknown-field error. route.connect_to is restricted to the test-resolve cargo feature. Shipped builds reject it. scripts/check-no-test-resolve.sh checks the release binary, and CI runs it.
  3. Admin socket kept. Tests depend on it (session_plane.rs status and revoke-all, the fixture, the matrix harness). It is never merged into the session socket.
  4. Broker trait and ap-client frozen at the UDS client. Docs only, no networked broker.
  5. Audit bytes_sent field. Response-body bytes already relayed when an admitted request fails mid-stream. It is a count only, with no key material. The #[ignore] on the upstream-reset test is removed and the test asserts the value.
  6. Docs updated: decision.md, phase1.md, operations.md, session-plane notes, the pre-release checklist and the kiro-cli recipe. The combined iptables rule set tag moves from [I] to [M] (docs/verification/phase1-k8s-iptables.md).
  7. Interim Q1-A static session. New proxy flag --interim-static-surrogate-sha256 (env AP_INTERIM_STATIC_SURROGATE_SHA256, 64 lowercase hex, else exit 2). The surrogate with that hash becomes session interim-static, bound to every policy credential at proxy start. It has no lease or TTL and is not in the registry, so revoke-all, resync, DELETE and authority lapse do not touch it. Caller key, grants, identity check and audit still apply. The proxy prints an INTERIM / no-revocation line at every start. Added deploy/k8s/pod-interim-static.yaml, deploy/k8s/interim-keygen.sh and docs/interim-static-session.md.

Test results

Full suite on an aarch64 macOS build host via scripts/sync-and-test.sh (nothing built on the laptop):

  • cargo fmt --check: pass
  • cargo clippy --workspace --all-targets -- -D warnings: pass
  • cargo test --workspace: 171 passed, 0 failed, 0 ignored (9 new interim_static tests)
  • cargo test -p ap-core: pass
  • cargo build --release: pass
  • check-no-test-resolve.sh on the release binary: ok, exit 2 on connect_to
  • Matrix on a test-resolve build: 31/31 pass, X5 pass
  • check-egress-deps.sh: ok

Review verdict: APPROVED (no blocking issues).

Release-gate hello race (real k3s pod, code at c339bd1)

PASS for the gate behaviour. After SIGKILL of the agent container, the 90 s authority lock held. Ten rival hello attempts and 19 attempts by the new PID 1 all got 409 authority_active. The fresh PID 1 won hello 100.2 s after the last renew, and no orphan survived. The old session's surrogate returned 403 surrogate_revoked. The test host was cleaned up and compared before and after, with identical state.

Separate finding, not fixed here (product decision): in the shipped deploy/k8s/pod.yaml the proxy (UID 1001) creates the session socket with mode 660, and the agent (UID 1000) shares no group. The agent's hello fails with PermissionDenied and the runtime retries silently. The timed run used socket mode 666. Options: mode 666 on the socket, or a common group for this socket only. The interim pod does not mount the session socket into the agent, so it is not affected.

Still open (as of the earlier update; see the later round below for the current list)

  • kiro-cps route host is a placeholder. The item 4 capture used a local listener and never recorded the real host (pre-release checklist item 5).
  • The interim pod and its kiro-settings init container have not been applied to a cluster. Run it before the first deployment and keep the hello-race gate.
  • Session socket permission on the Q1-B pod (above).
  • The interim session binds only credentials defined at proxy start. A credential added by a reload needs a restart (fails closed).
  • Keep the check-no-test-resolve.sh CI step. It guards the connect_to exclusion, which relies on resolver 2.

Round: session socket fix, interim pod on real k3s (review verdict: APPROVED)

Socket-mode fix (replaces the 666 workaround)

  • proxy and agent containers in deploy/k8s/pod.yaml both set runAsGroup: 2000. The proxy creates session.sock with its primary group, so it is 0660 owner 1001, group 2000, and the agent (UID 1000) connects as a group member. No proxy code change. ap-egress (1002:1002) is not in the group. No fsGroup, no supplementalGroups, no AP_SESSION_SOCKET_MODE.
  • The admin socket stays 0600 in an emptyDir mounted only into proxy.
  • Same-UID caveat: every process in the agent container is in group 2000 and can reach the session socket. This adds nothing beyond the documented best-effort attribution between same-UID sessions: hello is refused (409) while the lease is live, and all other calls need the authority token.
  • New check, scripts/check-session-socket.sh: static mode (default, in CI and sync-and-test.sh) verifies both manifests: shared GID only where the agent mounts the session socket, ap-egress outside it, no fsGroup/supplementalGroups or mode override, no admin socket or credential Secret in the agent. A mutated manifest fails as expected. LIVE=1 mode runs as UID 1000 in the agent container (connect to the session socket, admin socket absent or denied, socket is 0660 group 2000). The LIVE mode itself has not been run; the same facts were measured by hand in the pod run below.

Interim pod on a real k3s node (single-node k3s v1.36.2, Linux x86_64; stub upstream, dummy credential)

Linux binaries built on the node; images imported locally; code at 4bf8ae3.

Claim Result
Pod starts (netinit, kiro-settings init, proxy, ap-egress, agent) PASS
INTERIM start line; no session socket in the agent PASS
Agent holding only the surrogate reaches the upstream; upstream sees the real credential PASS
Real credential never in the agent container (env, cmdline, files, mounts; positive control found) PASS
Direct egress and DNS from the agent UID refused PASS
kiro-cli chat works through the interim session (kiro-cli 2.37.0, not 3.3.0) PASS
Hello race on the Q1-B pod.yaml with group 2000, mode 0660, no override: UID 1000 wins the first hello; same-UID orphan gets 409 while the lease is live; no orphan after SIGKILL; old surrogate gets 403 surrogate_revoked after the lapse; new session admitted PASS
ap-egress has neither socket; admin socket not visible to the agent PASS (by ls, not by a connect attempt)
kiro-cps route host MEASURED management.us-east-1.kiro.dev:443; response side unchecked (below)

Not exercised: a real model response, the real Kiro hosts, kiro-cli 3.3.0. The 90.000 s boundary is pinned by the unit test, not by the pod run. The test node was restored to its prior state (namespace, images, files compared before and after).

kiro-cps route host: measured (commit 553ca90)

kiro-cli 3.3.0, dummy key, throwaway HOME, HTTPS_PROXY to a local listener that logged CONNECT host:port and answered 403: 0 real requests, no DNS. Default api.cps.service host is management.us-east-1.kiro.dev:443 (overriding that endpoint removed every management.* CONNECT); the model path stays runtime.us-east-1.kiro.dev. policy.example.toml is updated. Still unchecked (needs a real first response): pass_headers = ["tokentype"], error_shape = "aws", the Host header inside TLS, and why kiro-cli also probes management.* in other regions (eu-central-1, us-gov-, us-iso), which the example policy would deny. Only the laptop had 3.3.0 (homelab Linux hosts have 2.37.0 / 2.1.1 / none). Report: .agents/tasks/phase1-verify/item7-cps-host.md.

Still open

  • kiro-cps response side (pass_headers, error_shape) and other-region management.* probes: need a real first response.
  • LIVE=1 mode of the session-socket check is unexercised; it needs python3 in the agent image (to be documented or given a shell fallback).
  • Real model response and kiro-cli 3.3.0 are untested in a pod; keep the hello-race gate for release.
  • Test agent image put UID 1000 in distro groups (adm, sudo); real agent images should not.
  • Interim session still has no per-session revocation (owner accepted); a credential added by reload needs a restart.

Not to be merged until reviewed.

ap-core: Q3-A config model and validation, policy snapshot with
stat-per-request PolicyStore (fails closed), aws_targets matcher, grant
evaluation, refusal codes and error dialects, canonicalization, and an
AuditRec that only accepts a route-relative path (no /ap/<caller-key>).

ap-broker: session registry (15 min lease, 12 h absolute TTL cap, resync),
authority lock (90 s lease, hash persisted), S1 FileStore, Broker trait and
LocalBroker. No network I/O; no aws-sm, no ECS pieces.

Verified on macmini (cargo 1.99.1), not locally:
- cargo fmt --check: ok
- cargo clippy --workspace --all-targets -- -D warnings: clean
- cargo test -p ap-core -p ap-broker: 22 unit + 13 config (ap-core),
  24 plane + 8 store (ap-broker), all passed
… tests (FEAT-002)

ap-proxy (bin `auth-proxy`): one loopback agent listener, session-plane and
admin Unix sockets, the section 4 pipeline, audit to stdout. Request bodies
stream with a cap (413), upstream is HTTP/2 via ALPN, redirects are never
followed. No tracing or access-log layer exists, the caller key is dropped by
ApPath::verify, pre-verification refusals audit path "-", upstream error text
is never printed, and the panic hook prints a fixed line (owner condition b).
ap-client: UDS session-plane client, surrogate generation, Authority with
renew loop, resync on unknown, hello retry with backoff. ap-broker gains
SessionPlane::revoke_all for the admin socket.

Tests: hello race after an authority restart (owner condition a), no
/ap/<caller-key> or secret in any log (b), session plane, policy, aws_targets.
tests/matrix ports the 25 Phase 0 rows (Phase 1 expectations) plus 6 rows.

Verified on macmini (macOS, cargo 1.99.1, system python3 3.9.6), after rsync:
- cargo fmt --check: ok
- cargo clippy --workspace --all-targets -- -D warnings: clean
- cargo test --workspace: all pass (ap-core 22+13, ap-broker 25+8, ap-client 6,
  ap-proxy unit 13, hello_race 3, no_caller_key_in_logs 1, policy 5,
  session_plane 13)
- cargo build --release; python3 tests/matrix/matrix.py --bin target/release:
  Phase 0 rows 25/25 PASS, added rows 6/6 PASS
- negative control: logging the request URI to stderr makes
  no_caller_key_in_logs fail (reverted, not committed)
Not run: the k8s iptables rule-set test (no cluster), HTTP/2 upstream (the mock
upstream speaks HTTP/1.1 only).
… (FEAT-003)

Ran on macmini: cargo clippy -p ap-egress --all-targets -D warnings (clean),
cargo test -p ap-egress (6 passed), cargo fmt --check (ok),
scripts/check-egress-deps.sh (exit 0; exit 1 with ap-broker temporarily added, reverted).
…004)

deploy/k8s: netinit image + script (combined rule set, iptables and
ip6tables), 4-container pod manifest, example policy. docs: session-plane,
operations (same-UID attribution best effort, no caller key in logs, RFC
amendment, exit codes, deferred section), kiro-cli recipe, pre-release
checklist, implementation status. tests/k8s-egress/run.sh written, not run.
.github/workflows/ci.yml and scripts/sync-and-test.sh run the same commands.

Ran on macmini: cargo fmt --check ok; clippy --workspace --all-targets
-D warnings ok; cargo test --workspace 115 passed, 0 failed; release build
ok; matrix 31/31 PASS; check-egress-deps ok. pod.yaml and ci.yml parse as
YAML; example policy loads in auth-proxy and ap-egress. Nothing applied to a
cluster.
Ran: kiro-cli 3.3.0 chat --no-interactive against a local 127.0.0.1 capture listener
with a surrogate KIRO_API_KEY and settings endpoint overrides. 0 real requests.
Result: 7 captured POSTs, each with exactly one X-Amz-Target; krs model call is
AmazonCodeWhispererStreamingService.GenerateAssistantResponse. Listener and temp files removed.
Adds crates/ap-proxy/tests/h2_upstream.rs with a real h2-only TLS upstream
(h2 crate, ALPN h2) and mid-stream / early RST_STREAM cases, plus the
verification report docs/verification/phase1-h2-reset.md.

Ran on macmini (cargo test -p ap-proxy --test h2_upstream): 4 passed, 1 ignored,
repeated 9x with the same result; cargo test --workspace all ok.
The ignored test (upstream_reset line records bytes sent) fails with --ignored:
AuditRec has no bytes field, so that design claim is not implemented.
No proxy code changed; dev-dependencies added: bytes, h2, rustls, rustls-pki-types, tokio-rustls.
Verified on aarch64 Debian host (rustc 1.96.0). Run 1 on d69fcbb: test 1 fail
(policy concurrency test race), matrix 30/31 (R4, Python 3.13 strict X509).
Fixes: readers iterate at least once; matrix harness clears VERIFY_X509_STRICT.
Run 2: fmt, clippy ok; cargo test --workspace --no-fail-fast 115 passed 0 failed
0 ignored; race test 30/30; release build ok; matrix 31/31; egress deps ok.
Host cleaned (temp dir removed, no processes left).
Verdict PASS. Ran deploy/k8s/pod.yaml (netinit + real auth-proxy + real ap-egress +
agent UID 1000 container) on an x86_64 k3s v1.36.2 node; only image names and two
netinit test env vars (OTHER_PORTS, TEST_V6_ADDR) were substituted. Binaries built on
the node with a throwaway CARGO_HOME; images imported locally, nothing pushed.

Measured:
- agent UID reaches only 127.0.0.1:18080 and :3128; DNS (UDP/TCP/getaddrinfo), direct
  v4/v6 egress and the listed sidecar port 18190 are refused in <=2 ms (v6 test-aid
  route 1.04 s NDP); proxy UID and ap-egress UID keep DNS and egress.
- NO_PROXY=127.0.0.1,localhost keeps loopback auth-proxy traffic off ap-egress
  (404 from auth-proxy vs 405 from ap-egress without it).
- via ap-egress: github, npm, pypi, pythonhosted, crates index/static, git clone, npm,
  pip, cargo all work; example.com, api.github.com, :80/:22, IP literals, loopback
  targets, plain HTTP, gitlab are refused with 403/405.
- proxy-ignoring tools: curl/wget/git/python/getent/dig/nc fail in <0.1 s; pip 7.9 s,
  cargo 10.9 s, npm 70.7 s (tool retry loops).
- ESTABLISHED rule: inbound reply works with it, times out with it removed (negative control).
- Unlisted loopback port 19999 is reachable by design (rules reject only OTHER_PORTS).
No bug found in the rule set or manifests; no repo source changed.
Cleanup of the test node verified (see report).
…test-resolve builds

Over-design council, security fix. egress.allow_route_hosts let a credential
route host also be an egress host, which breaks the baseline rule (a CONNECT
tunnel could carry another credential straight to a credential host). The field
existed in ap-core EgressCfg and was used only by the egress validation and one
ap-core test. It is removed with no replacement: a policy that sets it is now an
unknown-field error. Tests: a policy with allow_route_hosts (with a route host,
alone, or =false) is rejected; an egress entry that is a credential route host is
still rejected.

route.connect_to (resolver override) is a test aid. It now exists only behind the
ap-core `test-resolve` cargo feature (ap-proxy forwards it). In every shipped
build a policy that sets it is rejected as an unknown key. ap-proxy's
dev-dependency turns the feature on for `cargo test`, so the integration tests
still spawn a binary that accepts it; the matrix is run against a separate
`--features test-resolve` build in target/matrix-bin, and
scripts/check-no-test-resolve.sh checks the shipped release binary exits 2 on
connect_to. scripts/sync-and-test.sh and CI run those steps.

Verification: the combined tree of this and the following commits passed the full
suite on macmini (see the final commit message for the results). This commit was
not built on its own.
Over-design council: keep the aws_targets parser, validation and tests, but no
shipped policy may use it. New test shipped_policies_parse_and_use_no_aws_targets
parses every .toml under deploy/ and fails if a grant uses aws_targets (it also
checks that the example policy parses). The example policy header now says so and
records that kiro-cli 3.3.0 was measured to send exactly one X-Amz-Target per call
(model call AmazonCodeWhispererStreamingService.GenerateAssistantResponse,
docs/verification/phase1-xamz-target.md), so the assumption holds but no grant
uses it yet. The kiro-cps host placeholder is marked as an open item in the example.

Verification: combined tree passed the full suite on macmini (final commit message).
…test

Verification item 2 gap, owner chose option A. AuditRec gets a bytes_sent field:
the response-body bytes already relayed to the client when an admitted request
fails mid-stream. agent.rs counts the bytes of each chunk it streams and passes
the total to AuditRec::failed. The field is null on every other line, so the
record has 13 keys. It is a count only: the audit stays free of secrets and of
the /ap/<caller-key> prefix (the existing scrub tests still pass, plus a new unit
test for the failed line).

Removed #[ignore] from the_upstream_reset_audit_line_records_the_bytes_sent and
made it assert bytes_sent == len("partial-"), no key material in the line, and
null on the allow line. h2_upstream.rs also gets the rustfmt layout cargo fmt
--check requires (the verification commit had left it unformatted).

Verification: combined tree passed the full suite on macmini (final commit message).
Owner decision: Q1-B stays the accepted design and new work on it is frozen;
build only the minimal Q1-A static configuration as a labelled interim path. The
interim deployment has NO per-session revocation and the owner accepted that.

- ap-proxy: --interim-static-surrogate-sha256 (env
  AP_INTERIM_STATIC_SURROGATE_SHA256), 64 lowercase hex, else exit 2. The proxy
  prints an INTERIM / NO per-session revocation line at every start.
- ap-broker: LocalBroker::with_interim_static_session adds a separate admission
  branch: the surrogate with that hash is session "interim-static", generation 0,
  bound to every credential the policy defines at start. No lease, no TTL, not in
  the registry, so revoke-all, resync, DELETE and an authority lapse do not touch
  it. The registry, leases, authority lock and resync are unchanged.
- deploy/k8s/pod-interim-static.yaml (differences from pod.yaml are listed in its
  header; session socket not mounted into the agent), deploy/k8s/interim-keygen.sh,
  docs/interim-static-session.md (states the missing revocation in plain words).
- tests: crates/ap-proxy/tests/interim_static.rs (9 tests: works, labelled audit
  and startup line, no leaks, no flag no session, caller key and grants still
  apply, not ended by revoke-all / hello+resync / DELETE, no lease or TTL, restart
  needs no resync, Q1-B sessions and per-session revocation work beside it,
  malformed hash exits 2). Q1-B suites run unchanged.

Not applied to a cluster; the pod example is untested on one.
Verification: combined tree passed the full suite on macmini (final commit message).
decision.md and phase1.md record the owner's decision (Q1-B stays the accepted
design, new work frozen; minimal Q1-A static config only as a labelled interim
path; the owner accepted that the interim deployment has no per-session
revocation; the real-pod hello-race test stays a release gate; ap-egress stays a
separate container and UID). Also recorded:
- aws_targets dormant, kiro-cli 3.3.0 sends one X-Amz-Target per call (item 4);
- egress.allow_route_hosts removed, route.connect_to test builds only;
- admin socket kept because tests depend on it (session_plane.rs status and
  revoke-all, fixture, matrix harness), never merged into the session socket;
- Broker trait and ap-client frozen at the UDS client, no networked broker;
- combined iptables rule set tag [I] -> [M] (docs/verification/phase1-k8s-iptables.md);
- kiro-cps host: the item 4 capture used a local listener and never recorded the
  real host, so it stays a placeholder and is an open item with what is needed
  (pre-release checklist item 5).
operations.md, the pre-release checklist, the kiro-cli recipe and session-plane.md
are updated to match.
Full suite on macmini (aarch64 macOS) on the tree d10fd79, via
scripts/sync-and-test.sh, nothing built on the laptop:
- cargo fmt --check: pass
- cargo clippy --workspace --all-targets -- -D warnings: pass
- cargo test --workspace: 171 passed, 0 failed, 0 ignored (the upstream_reset
  bytes_sent test now runs; 9 new interim_static tests)
- cargo test -p ap-core: pass (runs the shipped-build connect_to rejection test)
- cargo build --release: pass
- scripts/check-no-test-resolve.sh on the release binary: ok, exit 2 on connect_to
- tests/matrix/matrix.py on a test-resolve build: 31/31 PASS (25 Phase 0 + 6), X5 PASS
- scripts/check-egress-deps.sh: ok
Details in docs/verification/phase1-council-apply.md. Not run: the interim pod on
a cluster; the real-pod hello race (still a release gate).
…rld-writable socket

Proxy and agent runAsGroup 2000; socket 0660 1001:2000; admin socket proxy-only.
Add scripts/check-session-socket.sh (static manifest check wired into CI and
sync-and-test; LIVE=1 mode for UID 1000 in a pod). Docs: phase1.md section.
Ran: check-session-socket.sh 18 PASS; scripts/sync-and-test.sh on macmini
(fmt, clippy -D warnings, cargo test --workspace, matrix) exit 0.
…de (item 6)

Ran at 4bf8ae3 on a k3s test node (mini): interim pod with a stub HTTPS upstream, dummy
credential and kiro-cli 2.37.0, then deploy/k8s/pod.yaml with the shared group 2000 and no
AP_SESSION_SOCKET_MODE override.
- interim: 3/3 containers incl. kiro-settings init; surrogate-only agent reaches the stub, the
  stub sees the dummy real credential; wrong/no surrogate and wrong caller key 403; credential
  absent from the agent (proc, files, mounts); direct egress and DNS refused (<0.25 s); kiro-cli
  chat goes through (session=interim-static). PASS.
- hello race: agent UID reaches session.sock (1001:2000, 0660) first try; after SIGKILL no orphan,
  27 hellos 409 during the lease, fresh PID 1 wins at lapse+backoff, old surrogate 403
  surrogate_revoked. PASS.
- kiro-cps host: item 4 has no real host, left open with the exact need.
- cleanup: namespace, images, files removed; image list, host iptables, ~ and disk identical.
No manifest or script defect found; fixed two stale "not yet run on a cluster" sentences and
noted the measurement in the pre-release checklist. Reports: .agents/tasks/phase1-verify/item6-*.md
ran: cargo fmt --check, clippy --workspace --all-targets -D warnings, cargo test --workspace,
cargo test -p ap-core, check-session-socket.sh, cargo build --release, matrix.py (31/31),
check-egress-deps.sh, check-no-test-resolve.sh: all PASS. Homebrew python3 3.14 on PATH breaks the mock upstream (env only).
…a CONNECT-logging proxy, 0 real requests)

Checked on macmini: fmt, clippy -D warnings, cargo test --workspace, check-egress-deps, check-no-test-resolve all pass.
@chaodu-agent
chaodu-agent changed the base branch from spike/phase0 to main November 16, 2026 22:52
@chaodu-agent

Copy link
Copy Markdown
Contributor Author

Closing: this branch was built in parallel without noticing that main already carries a complete Phase 1 (runtime split with the hello proof, CONNECT-only egress, the matrix, manifests and cluster evidence). Most of it is superseded by main. The findings that still apply to main are being ported as small separate PRs: removal of egress.allow_route_hosts, bytes_sent on the upstream_reset audit line with an HTTP/2 upstream test, the kiro-cli X-Amz-Target measurement, and the kiro-cps route host. The branch feat/phase1-k8s is kept for reference.

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.

1 participant