Skip to content

[M1.2b2] Serve agent enrollment and renewal over TLS gRPC - #31

Merged
samuelmbabhazi merged 3 commits into
mainfrom
feat/m1.2b2-agent-gateway
Nov 5, 2026
Merged

samuelmbabhazi merged 3 commits into
mainfrom
feat/m1.2b2-agent-gateway

Conversation

@samuelmbabhazi

@samuelmbabhazi samuelmbabhazi commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Task

[M1.2b2] TLS gateway and unary agent RPCs. Part of #14. Stacked on #30: review and merge #30 first, then retarget this PR to main. Neither PR should be merged automatically.

What changed

Start a dedicated HTTP/2 gRPC listener using Connect-RPC after Nest initializes. Serve Enroll and RenewCertificate through the existing fleet commands. Use a separate Ed25519 server-auth certificate signed by the installation CA, configure its DNS/IP SAN with AGENT_GATEWAY_SERVER_NAME, and refresh the in-memory credentials daily.

Client certificates are optional at the TLS handshake solely to support initial enrollment. Every other RPC requires a verified TLS peer, client-auth purpose, SPIFFE identity and a current matching database certificate record. Identity headers are ignored and revoked serials are rejected on every request, including reused connections. Bound message sizes, sanitize errors and close HTTP/2 sessions during shutdown.

Make enrollment transactional before exposing it over the network: concurrent uses of one join token produce one node and one certificate, and audit failure rolls back the node, certificate and token consumption. The regression test first reproduced two persisted nodes for one token before this correction.

Scope and dependencies

M1.2b stays open: the CA fingerprint is already returned by IssueJoinToken, but the REST endpoint and installation-command UI depend on M2.3/M8.3. Connect stream lifecycle and revocation-driven disconnection land with M1.2c. Streaming RPCs currently authenticate and return Unimplemented; no leases or actions are claimed to work yet. These boundaries are recorded in PLAN, SPEC, README and THREAT_MODEL.

How it was tested

  • Lint, TypeScript typecheck and unit tests.
  • Real PostgreSQL and HTTP/2 TLS tests: initial enrollment, reused token, missing certificate, spoofed headers, authenticated streaming boundaries, renewal, revocation on a reused connection, unregistered certificate, rogue CA copying a valid identity/serial, server CA trust, hostname checks and message size limits.
  • Enrollment concurrency and audit-failure rollback tests against PostgreSQL; 19 integration tests in total.
  • Server certificate tests for DNS/IP SANs, server-only EKU and private-key redaction.
  • pnpm gen succeeds without changes to generated contracts; API build succeeds.
  • Go tests and protobuf lint; custom Semgrep rules.

Security considerations

The rejectUnauthorized: false server option permits the certificate-free enrollment handshake. It is not client-side disabling of server verification: the application gate requires TLSSocket.authorized on protected RPCs and also checks database registration/revocation. Tests prove a rogue CA cannot bypass the gate even by copying an enrolled node identity and serial.

The generic Semgrep bypass-tls-verification rule flags this server configuration. Its precise finding is suppressed inline with the server-only rationale above; all other rules remain enabled, and the full CI rule set is rerun. This does not weaken any client-side TLS verification.

Connect-RPC dependencies are Apache-2.0. No action catalogue change, host shell execution, plaintext key persistence or new public REST endpoint.

Checklist

  • M1.2b2 criteria met and ticked; parent M1.2b remains open for dependent work
  • Lint, TS tests, Go tests and buf lint pass
  • Transport and malicious-input tests added
  • No new shell invocation or secret exposure path
  • State changes use fleet commands and audit events
  • SPEC, THREAT_MODEL, PLAN and README updated

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

Rebased onto the rebased #30.

@samuelmbabhazi
samuelmbabhazi force-pushed the feat/m1.2b1-certificate-lifecycle branch from 3b136a7 to 2c5e133 Compare November 5, 2026 14:21
@samuelmbabhazi
samuelmbabhazi changed the base branch from feat/m1.2b1-certificate-lifecycle to main November 5, 2026 14:22
@samuelmbabhazi
samuelmbabhazi force-pushed the feat/m1.2b2-agent-gateway branch from 60615f9 to 34bbf2a Compare November 5, 2026 14:23
@samuelmbabhazi
samuelmbabhazi merged commit 9413073 into main Nov 5, 2026
2 of 11 checks passed
@samuelmbabhazi
samuelmbabhazi deleted the feat/m1.2b2-agent-gateway branch November 5, 2026 14:24
samuelmbabhazi added a commit that referenced this pull request Nov 5, 2026
Implements the first part of M1.2c: node keys and credentials that
remain coherent across enrollment and renewal. Depends on #31; this PR
intentionally targets its branch.

The agent generates Ed25519 keys and signed CSRs, validates the
key/certificate pair against the installation CA and exact tenant/node
SPIFFE identity, and persists one atomic credential generation. Loading
rejects symlinks, loose permissions, oversized files, mismatched
identities and invalid certificates. The private key uses the redacting
Secret wrapper and is exposed only at TLS and explicit persistence
boundaries.

Validation: Go tests with the race detector, golangci-lint, TypeScript
lint/typecheck and unit tests, protobuf lint/format. Regression tests
cover forged identities, substituted keys/CAs, failed replacements,
secret formatting and file permissions. No new dependencies, remote
actions or database writes. Plan, specification and threat model
updated.

- [x] M1.2c1 acceptance criteria and regression tests
- [x] No shell invocation or secret logging
- [x] Atomic persistence with 0600 files
- [x] Documentation updated
- [ ] CI and maintainer review

M1.2c remains open for enrollment, streaming and reconnect/renewal
scheduling. 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant