Skip to content

[M1.2c4a] Reconnect agent streams and schedule credential renewal - #35

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

samuelmbabhazi merged 1 commit into
mainfrom
feat/m1.2c4a-agent-reconnect

Conversation

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator

Implements the connection runner portion of M1.2c4, split from daemon wiring and the cross-language integration test to keep review focused. Depends on #34 and targets its branch.

The runner sends Hello, validates the returned node/epoch and heartbeat interval, and owns the receive goroutine through cancellation. Control-plane interruptions reconnect with bounded exponential backoff and jitter. Certificates rotate at two thirds of validity and are persisted atomically before reconnecting. Explicit authentication/epoch rejection and renewal persistence failure halt connection attempts without silently re-enrolling. Dead connections use TLS/gRPC keepalive; remote error text is excluded from logs.

Validation: Go race tests over real TLS/gRPC cover reconnect, heartbeats, renewal persistence, authentication error propagation, shutdown and backoff bounds; golangci-lint passes. Existing TypeScript and protobuf checks are unchanged. This runner dispatches no actions and grants no leases; unsupported frames fail explicitly pending their planned handlers.

  • M1.2c4a behavior and tests
  • No new dependency, shell invocation or secret exposure
  • Plan, specification and threat model updated
  • CI and maintainer review

Daemon integration and the NestJS-to-Go restart/revocation test remain M1.2c4b. No merge requested.

@samuelmbabhazi
samuelmbabhazi force-pushed the feat/m1.2c3-gateway-stream branch from 4d2ceb8 to 02ea7c9 Compare November 5, 2026 13:38
@samuelmbabhazi
samuelmbabhazi force-pushed the feat/m1.2c4a-agent-reconnect branch from 18be0ea to 6f08f0c Compare November 5, 2026 13:38
@samuelmbabhazi
samuelmbabhazi force-pushed the feat/m1.2c3-gateway-stream branch from 02ea7c9 to 8257f06 Compare November 5, 2026 14:29
@samuelmbabhazi
samuelmbabhazi changed the base branch from feat/m1.2c3-gateway-stream to main November 5, 2026 14:29
@samuelmbabhazi
samuelmbabhazi force-pushed the feat/m1.2c4a-agent-reconnect branch from 6f08f0c to 0c412b3 Compare November 5, 2026 14:30
@samuelmbabhazi
samuelmbabhazi merged commit 7cb7fc2 into main Nov 5, 2026
2 of 11 checks passed
@samuelmbabhazi
samuelmbabhazi deleted the feat/m1.2c4a-agent-reconnect branch November 5, 2026 14:31
samuelmbabhazi added a commit that referenced this pull request Nov 5, 2026
…36)

Completes M1.2c by wiring enrollment and the reconnect/renewal runner
into the Go daemon. Depends on #35 and targets its branch.

An unregistered production agent reads a bounded regular 0600 join-token
file, verifies the installation CA, enrolls, atomically saves its
identity and only then removes the token file. Subsequent starts use the
persisted credentials. Production fails closed on missing or invalid
credentials; development without a token remains an idle skeleton.
Configuration, example and README document the flow.

Validation: 23 PostgreSQL/TLS integration tests, now including the
actual Go daemon against NestJS through enrollment, reused-token
rejection, full application restart on the same port, reconnect and live
revocation. This test checks persisted identity, token cleanup and log
redaction. Go race tests, golangci-lint, TypeScript lint/typecheck/unit
tests, protobuf checks and API production build pass. The agent binary
remains below 30 MB.

The integration-only helper launches a fixed `go test` argv without a
shell; it is excluded from the production API build and passes no secret
in argv. Fixtures are private temporary files removed during teardown.
The targeted Semgrep exception is limited to this test harness,
following the existing OpenAPI generator precedent.

- [x] All M1.2c acceptance criteria met
- [x] New input and lifecycle tests
- [x] No production process spawning or secret logging
- [x] Plan, spec, threat model and operator example updated
- [ ] CI and maintainer review

M1.2 remains open for the REST join-token/install-command dependencies
already recorded under M1.2b (M2.3/M8.3). Workload execution and durable
leases retain their own milestones. 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