Skip to content

[M1.2c2] Enroll and renew agents with pinned TLS - #33

Merged
samuelmbabhazi merged 1 commit into
mainfrom
feat/m1.2c2-agent-enrollment
Nov 5, 2026
Merged

samuelmbabhazi merged 1 commit into
mainfrom
feat/m1.2c2-agent-enrollment

Conversation

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator

Implements M1.2c2: the Go agent can enroll and rotate its Ed25519 key/certificate through the TLS gateway. Depends on #32 and targets its branch.

Before sending a join token, a payload-free TLS probe verifies the installation CA fingerprint, server chain, hostname and validity. Enrollment then uses standard TLS verification against that pinned root. The gateway now includes its public CA chain in its server handshake. Returned credentials are bound to the same CA and exact node/organization identity; renewal rotates both key and certificate. Unary calls have a 30-second deadline and 64 KiB message limits. Remote error text is sanitized to avoid token echoing.

Validation: real TLS/gRPC Go tests with race detection; wrong pin, rogue CA, hostname mismatch, expired server, oversized request and token-echo tests; golangci-lint; pnpm lint; real PostgreSQL/TLS integration suite. Existing Go gRPC dependencies are now declared directly in the agent module.

Security: the bootstrap-only InsecureSkipVerify is paired with a mandatory custom verifier that performs chain, hostname, validity and pin checks. It carries no application data. All RPCs use normal TLS verification. Missing pins are allowed only in explicit development mode, consistent with existing configuration.

  • M1.2c2 acceptance tests
  • No shell invocation, secret logging or extra database writes
  • Specification and threat model updated
  • CI and maintainer review

The daemon connection loop and gateway streaming remain in M1.2c3/c4. No merge requested.

@samuelmbabhazi
samuelmbabhazi force-pushed the feat/m1.2c1-agent-credentials branch from a4ce5a7 to f043040 Compare November 5, 2026 13:36
@samuelmbabhazi
samuelmbabhazi force-pushed the feat/m1.2c2-agent-enrollment branch from d5bce5e to f342eb2 Compare November 5, 2026 13:37
@samuelmbabhazi
samuelmbabhazi force-pushed the feat/m1.2c1-agent-credentials branch from f043040 to 5465689 Compare November 5, 2026 14:25
@samuelmbabhazi
samuelmbabhazi changed the base branch from feat/m1.2c1-agent-credentials to main November 5, 2026 14:26
# Conflicts:
#	agent/go.mod
#	agent/go.sum
#	docs/THREAT_MODEL.md
@samuelmbabhazi
samuelmbabhazi force-pushed the feat/m1.2c2-agent-enrollment branch from f342eb2 to ac31d21 Compare November 5, 2026 14:27
@samuelmbabhazi
samuelmbabhazi merged commit 3718557 into main Nov 5, 2026
2 of 11 checks passed
@samuelmbabhazi
samuelmbabhazi deleted the feat/m1.2c2-agent-enrollment branch November 5, 2026 14:28
samuelmbabhazi added a commit that referenced this pull request Nov 5, 2026
…34)

Implements M1.2c3: authenticated agents can keep a bidirectional Connect
stream open. Depends on #33 and targets its branch.

The first frame must identify the authenticated node and current epoch.
The gateway replies with Welcome and a five-second heartbeat interval.
Duplicate streams replace the previous connection; idle peers time out
after 15 seconds. Certificate registration, revocation, expiry and node
removal are checked at every frame and once per second during silence.
Database failures close the stream with sanitized errors. Stream
deadlines are separate from unary deadlines; message sizes remain
bounded to 64 KiB.

Validation: TypeScript lint/typecheck and unit tests; 22 PostgreSQL/TLS
integration tests, including forged identities, stale epochs, live
revocation without another request and duplicate connections. Fake-timer
tests cover idle expiry and timer cleanup. An asynchronous
socket-rejection regression found during testing was fixed by observing
pending reads immediately.

Scope: this is transport only. Durable leases, action dispatch and state
reports remain in their planned milestones. No lease is granted by
Welcome yet; unsupported frames fail explicitly. ExecStream remains
authenticated and Unimplemented until session authorization exists. No
persistent state changes or unaudited database writes are introduced.

- [x] M1.2c3 acceptance and security tests
- [x] Specification and threat model updated
- [x] No new dependencies, shell execution or secret logging
- [ ] CI and maintainer review

The Go daemon's connection loop and real cross-language restart test
follow in M1.2c4. No merge requested.
samuelmbabhazi added a commit that referenced this pull request Nov 5, 2026
…103)

## Task

Review follow-ups for the M1.2 stack (#31, #33, #35). Stacked on #36.
Refs #14

## What changed

**Gateway TLS (#31)**
- `gatewaySecureContext()` holds every TLS setting and is used both when
the server starts and at the daily certificate refresh. Node's
`setSecureContext` resets any option it is not given. Before this fix,
the first refresh silently dropped the gateway from TLS 1.3 to Node's
TLS 1.2 default, where client certificates travel unencrypted.
- A unit test reproduces Node's behaviour and checks that the minimum
survives a refresh.

**Certificates and enrollment errors (#31, #33)**
- Node and gateway certificates start 5 minutes before issuance
(`CLOCK_SKEW_ALLOWANCE_MS`). The 30-day validity still runs from
issuance.
- Before this fix, an agent whose clock was even one second behind
rejected its brand-new certificate. The join token was already burned
and the node name stuck in `enrolling`. During renewal, the same skew
bricked the node.
- An oversized CSR now returns `InvalidInputError` (gRPC
InvalidArgument) instead of `ConflictError` (AlreadyExists).
- Two concurrent enrollments racing for the same node name now get
`node_name_taken` instead of a raw P2002 mapped to Internal.

**Agent renewal durability (#35)**
- `RenewCertificate` and the following save run under
`context.WithoutCancel` with a 30 s budget. A SIGTERM during renewal no
longer cancels an RPC the server may already have committed.
- A failed save of the renewed credentials (full disk, I/O error) is
retried with bounded backoff instead of halting with the only valid key
in memory and the old certificate already revoked.
- SPEC 3.2 documents all of the above.

## How it was tested

- API: `nx run-many -t lint typecheck test -p api` passes, with new and
updated tests for TLS settings, backdated node and gateway certificates,
and the oversized-CSR error kind.
- Agent: `go test -race ./agent/... ./lib/...` and `golangci-lint` pass.
New tests: a renewal survives two failed saves and is persisted
afterwards; a renewal started after cancellation still completes and is
persisted.
- Integration and browser tests could not run locally (no Docker in the
sandbox), and GitHub Actions is blocked by the account spending limit.
CI must pass before merge.

## Security considerations

Restores the TLS 1.3-only guarantee of the agent gateway. Backdating by
5 minutes slightly extends the validity window into the past and does
not change expiry. No change to the action catalogue.

## Checklist

- [x] Lint, TS tests, Go tests pass locally
- [x] Regression tests added
- [x] No new shell invocation and no new secret exposure path
- [x] Audit events unchanged
- [x] SPEC updated
- [ ] CI green (blocked by billing)
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