natsauth

package
v0.60.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package natsauth implements Auth V2 Phase 3: decentralized NATS auth via per-Account JWTs (the "NSC model" — see docs/AUTH-V2.md §Phase 3).

At runtime ppz-server holds a single Account signing key. On /auth/exchange it generates a fresh user nkey pair, mints a User JWT with subject permissions scoped to the caller's org, and hands (jwt, seed) back to the daemon. NATS validates the chain locally — no callback to ppz-server in the data path.

Index

Constants

View Source
const DefaultMaxControlLine = 32768

DefaultMaxControlLine is the control-line budget the embedded server runs with when EmbeddedConfig leaves it unset.

nats-server defaults to 4096. That was survivable while a user JWT carried a single per-account wildcard, but ACL Phase 3 (internal/acl.Compile) emits one subject per pipe per handle, and the JWT rides inside the CONNECT control line. Production crossed 4096 at four handles (a 4493-byte credential) and every connect began failing in the protocol parser, before authentication — reported to users as E_NATS_UNREACHABLE with advice to re-login, which cannot help because the freshly minted credential is the same size.

32 KiB is ~7x the credential that broke production and leaves room for roughly 30 more handles at the observed ~880 bytes each. The cost is per-connection read-buffer headroom, not steady-state allocation.

This raises the ceiling; it does not remove it. The credential still grows without bound as an org grows — see docs/AUTH-V2.md §Phase 3.5 on per-account isolation, which is the fix that ends the class.

Variables

This section is empty.

Functions

func MintAccountJWT

func MintAccountJWT(operatorKP nkeys.KeyPair, accountName string, accountKP, signingKP nkeys.KeyPair) (string, error)

MintAccountJWT signs a fresh Account JWT for accountName, with the supplied signing-key public registered as an authorized signer. The Operator key (`operatorKP`) signs the JWT — caller is responsible for that key's security (production: env var).

JetStream limits are set to unlimited (-1) — a single ppz-server is the only client of every account anyway, so per-account quotas don't add safety, only friction.

func MintUserJWTInAccount

func MintUserJWTInAccount(accountPub string, signingKP nkeys.KeyPair, name string, pubAllow, subAllow []string, expUnix int64) (jwtStr, seed string, err error)

MintUserJWTInAccount mints a User JWT in the supplied account, signed by that account's signing key. Decoupled from `Account` so callers that hold (account_pub, signing_kp) directly — like per-org account loaders in Phase 3.5 — can mint without constructing an Account struct.

expUnix is the JWT's `exp` claim (seconds since epoch). The `nbf` claim is set 30s before now to absorb clock skew.

func MintUserJWTWithPermissions added in v0.58.0

func MintUserJWTWithPermissions(accountPub string, signingKP nkeys.KeyPair, name string, p acl.Permissions, expUnix int64) (jwtStr, seed string, err error)

MintUserJWTWithPermissions signs a user JWT carrying exactly the permissions the ACL compiler produced.

func StartEmbeddedNATSWithAuth

func StartEmbeddedNATSWithAuth(cfg EmbeddedConfig) (*natsserver.Server, error)

StartEmbeddedNATSWithAuth boots an embedded nats-server with decentralized auth: the supplied operatorJWT is set as the trusted operator, and the account JWTs are preloaded into the in-memory resolver. JetStream + auth requires a system account.

Returns the running server. Caller is responsible for Shutdown().

Types

type Account

type Account struct {
	AccountPub string        // public nkey of the Account
	SigningKey nkeys.KeyPair // derived from PPZ_NATS_ACCOUNT_SIGNING_SEED; held in memory only
	AccountJWT string        // signed by Operator; loaded into the embedded NATS resolver at boot
}

Account is the runtime state ppz-server needs to mint User JWTs. Loaded once at boot from environment via LoadAccountFromEnv.

func LoadAccountFromEnv

func LoadAccountFromEnv() (*Account, error)

LoadAccountFromEnv reads the Account credentials from process env vars (PPZ_NATS_ACCOUNT_JWT, PPZ_NATS_ACCOUNT_SIGNING_SEED). Fails fast at boot if either is missing or malformed.

func (*Account) MintServerUserJWT

func (a *Account) MintServerUserJWT(expiry time.Duration) (jwtStr, seed string, err error)

MintServerUserJWT mints a User JWT with broad pub/sub permissions in this Account. ppz-server uses this to authenticate its own in-process NATS connection (which manages JetStream streams across every org) — no cross-org isolation needed.

func (*Account) MintUserJWT

func (a *Account) MintUserJWT(accountID string, expiry time.Duration) (jwtStr, seed string, err error)

MintUserJWT generates a fresh user nkey pair, signs a short-lived User JWT (with the subject scope for accountID embedded), and returns (jwt, seed) for handing to a daemon over /auth/exchange. The seed is the user's own private key — ppz-server keeps no copy.

type EmbeddedConfig

type EmbeddedConfig struct {
	Host             string // "" → 127.0.0.1
	Port             int    // 0 → OS picks
	OperatorJWT      string
	AccountJWT       string
	SystemAccountJWT string // optional — required for JetStream
	JetStream        bool   // false → JS disabled (auth-only mode, used by tests)

	// StoreDir is the persistent on-disk path for JetStream FileStorage.
	// Empty falls back to a per-process os.MkdirTemp — fine for tests,
	// loses every stream on restart in any long-lived deployment.
	StoreDir string

	// MaxControlLine bounds the NATS protocol control line — the line
	// that carries the User JWT inside CONNECT. 0 → DefaultMaxControlLine.
	// Set explicitly only to test the bound or to raise it further; see
	// DefaultMaxControlLine for why the stock 4096 is not survivable.
	MaxControlLine int32
}

EmbeddedConfig groups the config for StartEmbeddedNATSWithAuth. All JWT fields are required; SystemAccountJWT enables JetStream.

type OperatorChain added in v0.48.0

type OperatorChain struct {
	OperatorJWT      string
	OperatorSeed     string
	SystemAccountJWT string
}

OperatorChain is the NSC trust root ppz-server needs at boot: the operator JWT + seed it signs per-org Account JWTs with, and the system account JWT the embedded NATS runs under.

func BootstrapOperator added in v0.48.0

func BootstrapOperator() (OperatorChain, error)

BootstrapOperator mints a fresh operator + system-account chain. Shared by cmd/ppz-natsbootstrap (env-var output for compose/Pulumi) and ppz-server --ephemeral (in-process). Regenerating invalidates every previously issued JWT — callers own that decision.

type Subjects

type Subjects struct {
	Publish   []string
	Subscribe []string
	Deny      []string
}

Subjects describes the NATS subject scope embedded in a User JWT.

func SubjectsForOrgUser

func SubjectsForOrgUser(accountID string) Subjects

SubjectsForOrgUser returns the standard ppz subject scope for a user in accountID. The org_id is the lowercase-hyphenated UUID, used as the subject root (matches natsubj.SubjectFor).

pub: <accountID>.>      — own data-plane subjects
     _INBOX.>       — reply subjects for NATS request/reply
     $JS.API.>      — JetStream API (stream/consumer mgmt)
sub: <accountID>.>      — own data-plane subjects
     _INBOX.>       — receive replies

Known limitation: $JS.API.> isn't subject-pattern scopable per-org (stream names occupy a single subject token, so wildcards can't constrain the prefix). All orgs share the same JetStream API surface. Data-plane isolation (the load-bearing case for broadcasts) IS enforced via the <accountID>.> scope. Per-org account isolation is the proper fix; tracked in V3+ out-of-scope.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL