Skip to content

feat(nats pubusb): add NATS CA cert generation#26590

Open
cstyan wants to merge 2 commits into
mainfrom
callum/nats-cert-gen
Open

feat(nats pubusb): add NATS CA cert generation#26590
cstyan wants to merge 2 commits into
mainfrom
callum/nats-cert-gen

Conversation

@cstyan

@cstyan cstyan commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

NOTE: The changes in this PR were generated with heavy AI help. I have verified general functionality (manual test) and reviewed the code structure itself. Crypto details are outside my current knowledge base, but I believe relying on the stdlib x509 package is safe here. Please nit pick both the code and implementation details if anything smells off at all.

AI summary of changes:

Adds cryptokeys support for a CA that will sign the ephemeral leaf certificates replicas use for NATS cluster mTLS:

  • New crypto_key_feature enum value nats_ca; the rotator generates a self-signed ECDSA P-256 CA (PEM cert+key bundle in the secret column) and rotates it on the shared key duration with a 30 day token duration matching the maximum leaf lifetime.
  • FetchNATSCA accessor returns the active CA (parsed cert and signer) plus a trust bundle of all valid CA rows and the active row sequence so callers can detect rotation. It creates the CA on first fetch under an advisory lock since the NATS pubsub is constructed before the rotator starts.
  • The CA bundle contains a private key and stays excluded from the wsproxy crypto-keys endpoint allowlist and the signing/encryption keycaches.

The feature is dormant: nothing fetches the CA until cli/server.go is wired up in a follow-up branch.

cstyan added 2 commits June 12, 2026 22:18
Adds cryptokeys support for a CA that will sign the ephemeral leaf
certificates replicas use for NATS cluster mTLS:

- New crypto_key_feature enum value nats_ca; the rotator generates a
  self-signed ECDSA P-256 CA (PEM cert+key bundle in the secret column)
  and rotates it on the shared key duration with a 30 day token
  duration matching the maximum leaf lifetime.
- FetchNATSCA accessor returns the active CA (parsed cert and signer)
  plus a trust bundle of all valid CA rows and the active row sequence
  so callers can detect rotation. It creates the CA on first fetch
  under an advisory lock since the NATS pubsub is constructed before
  the rotator starts.
- The CA bundle contains a private key and stays excluded from the
  wsproxy crypto-keys endpoint allowlist and the signing/encryption
  keycaches.

The feature is dormant: nothing fetches the CA until cli/server.go is
wired up in a follow-up branch.
FetchNATSCA bootstrapped the nats_ca crypto key under a dedicated
LockIDNATSCACreate advisory lock, separate from the rotator's
LockIDCryptoKeyRotation. This was safe in practice: the server only
starts the rotator (coderd.New) after FetchNATSCA returns during pubsub
setup, so the two writers never overlap and never contend on the
crypto_keys (feature, sequence) primary key.

This is a defensive consistency change, not a bug fix. Reusing the
rotator's lock makes the bootstrap path and the rotator mutually
exclusive regardless of call ordering, so a future caller that invokes
FetchNATSCA concurrently with the rotator can't race the primary key.
It also keeps nats_ca consistent with how the other crypto key features
are created (single advisory lock). The now-unused LockIDNATSCACreate is
removed.
@datadog-coder-official

datadog-coder-official Bot commented Jun 22, 2026

Copy link
Copy Markdown

Pipelines

⚠️ Warnings

🚦 11 Pipeline jobs failed

ci | gen   View in Datadog   GitHub Actions

ci | lint   View in Datadog   GitHub Actions

ci | required   View in Datadog   GitHub Actions

View all 11 failed jobs.

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 774ad4b | Docs | Give us feedback!

@cstyan cstyan changed the title Callum/nats cert gen feat(nats pubusb): add NATS CA cert generation Jun 22, 2026
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