Skip to content

[M1.2b1] Authenticate and renew node certificates - #30

Merged
samuelmbabhazi merged 1 commit into
mainfrom
feat/m1.2b1-certificate-lifecycle
Nov 5, 2026
Merged

samuelmbabhazi merged 1 commit into
mainfrom
feat/m1.2b1-certificate-lifecycle

Conversation

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator

Task

[M1.2b1] Certificate authentication and renewal. Part of #14; the gateway follows in M1.2b2 and the agent in M1.2c.

What changed

Authenticate registered certificates against their organization, node, validity and revocation state. Renew certificates using the authenticated identity, with a compare-and-swap that permits only one concurrent renewal. Certificate replacement, revocation and audit recording share a PostgreSQL transaction, including rollback if an event subscriber fails.

Split M1.2b into two reviewable PRs in PLAN.md. No REST endpoint is exposed before RBAC exists.

Why

The TLS gateway needs a server-side identity check and renewal command before accepting agent traffic. A failed renewal must leave the previous certificate usable and must not leave unaudited credentials.

How it was tested

  • TypeScript unit tests, lint and typecheck.
  • Eight integration tests against disposable PostgreSQL containers, including concurrent renewal, audit rollback, cross-organization identity and malformed CSR rejection.
  • Go tests across agent, CLI and lib; protobuf lint and format checks.
  • Custom Semgrep rules and git diff whitespace checks.

Security considerations

Only a transport-verified certificate identity may reach these commands. CSR names cannot change the identity. The new transaction context is used by certificate and audit repositories; existing enrollment remains unchanged.

As required by the existing one-active-certificate invariant, successful renewal immediately revokes the old serial. If the response is lost after commit, operator-assisted re-enrollment is required. Retry-safe recovery is explicitly recorded as an open question in SPEC.md.

Checklist

  • M1.2b1 acceptance criteria met and task ticked; M1.2b remains open
  • Lint, TS tests, Go tests and buf lint pass
  • Tests added, including malicious inputs and concurrent attempts
  • No new shell invocation or secret exposure path
  • Renewal state change emits an audit event
  • SPEC and PLAN updated

Leave this PR open for human review. Do not merge automatically.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

Rebased onto the current main after the M2.2/M2.3 stack merged.

Main now has a shared transaction context (PrismaUnitOfWork, used by the audit store and every identity/tenant store). This PR had added a second one (PrismaService.client()/run() with its own AsyncLocalStorage). Keeping both would have broken rule 6: a certificate renewal and its audit row would no longer share one transaction. During the rebase:

  • PrismaService is back to main's version (no transaction helper).
  • PrismaNodeCredentialStore uses PrismaUnitOfWork.client/run.
  • FLEET_TRANSACTION is provided by PrismaUnitOfWork.
    Lint, typecheck and unit tests pass locally; the integration suite needs CI.

@samuelmbabhazi
samuelmbabhazi force-pushed the feat/m1.2b1-certificate-lifecycle branch from 3b136a7 to 2c5e133 Compare November 5, 2026 14:21
@samuelmbabhazi
samuelmbabhazi merged commit 028cae8 into main Nov 5, 2026
2 of 11 checks passed
@samuelmbabhazi
samuelmbabhazi deleted the feat/m1.2b1-certificate-lifecycle branch November 5, 2026 14:22
samuelmbabhazi added a commit that referenced this pull request Nov 5, 2026
## 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

- [x] M1.2b2 criteria met and ticked; parent M1.2b remains open for
dependent work
- [x] Lint, TS tests, Go tests and buf lint pass
- [x] Transport and malicious-input tests added
- [x] No new shell invocation or secret exposure path
- [x] State changes use fleet commands and audit events
- [x] SPEC, THREAT_MODEL, PLAN and README updated
samuelmbabhazi added a commit that referenced this pull request Nov 5, 2026
…111)

## What changed

The PLAN now states what remains after the post-merge audit of PRs #30
to #110.

- **New status section.** It covers:
  - what is done;
- the verification gap: CI has been blocked by billing since #87, so
integration, browser, CodeQL and gosec runs are missing. New task CI-1
reruns them first;
  - the next tasks in dependency order;
  - previously unowned work, now assigned to tasks;
  - the decisions waiting for the maintainer.
- **M1.2 and M1.2b ticked.** Their acceptance is met. The join-token
REST endpoint, node list/revoke and the install command with the CA
fingerprint move to a new task, **M2.7**, which depends on M2.3d and
M2.3g1.
- **New splits:**
  - M1.4a/b/c (action dispatch over `Connect` becomes M1.4a);
- M2.3f1/f2 and M2.3g1/g2. M2.3g1 records that `node_certificates` lacks
`organization_id` and needs an ADR;
  - M2.4a/b (partly done: append-only grants and their test exist);
  - M2.5a/b.
- **Stale M2.3 bullet removed.** Live owner authorization for API tokens
was delivered in M2.3d2.
- **M2.6 marked partly done** by M2.2e. The SSE endpoint is assigned to
it.
- **`ExecStream`, `CollectFacts`, `UpgradeAgent` and retry-safe
renewal** are assigned to M6.1, M1.6, M8.3 and M4.5.
- **SPEC 4.** It now names the `public` schema actually used, instead of
`shipkeep`.
- **SPEC 16.** New open question on confirmation tokens for in-place
`RestoreVolume`/`RestoreDatabase`. This would change the action list, so
the maintainer decides.

## How it was tested

Docs only. Prettier and the local gate pass.

## Security considerations

None in code. The document records a missing confirmation requirement
and an unapproved license (`argparse`, PSF-2.0) as maintainer decisions.
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