Documentation
¶
Overview ¶
Package brokerauth holds the credential primitives shared by sqi-server and sqi-worker for NATS broker authentication.
It is a leaf package: it imports neither internal/store nor internal/openjd, so the worker binary, which can never import the latter, may use it directly.
Index ¶
- Constants
- func GenerateAccountSeed() (seed []byte, publicKey string, err error)
- func GenerateSeed() (seed []byte, publicKey string, err error)
- func InboxPrefix(workerID string) string
- func LoadSeed(path string) ([]byte, error)
- func NkeyOption(publicKey string, seed []byte) nats.Option
- func PublicKeyFromSeed(seed []byte) (string, error)
- func ReservedAccount(name string) bool
- func RestrictHint(path string) string
- func SaveSeed(path string, seed []byte) error
- func ServerPermissions() *natsserver.Permissions
- func UserPermissions(p *natsserver.Permissions) jwt.Permissions
- func ValidAccountName(name string) bool
- func ValidWorkerIDToken(workerID string) bool
- func ValidatePublicKey(pk string) error
- func WorkerPermissions(workerID string) *natsserver.Permissions
Constants ¶
const ( // AccountAuth holds sqi's auth-callout connection. AccountAuth = "AUTH" // AccountSystem is the system account the generated cluster config // declares. AccountSystem = "SYS" // AccountGlobal is nats-server's built-in global account. AccountGlobal = "$G" )
The NATS accounts sqi's account layout or nats-server itself defines. An account sqi keeps its data in must not take one of these names, or it would merge with or clobber that account.
Variables ¶
This section is empty.
Functions ¶
func GenerateAccountSeed ¶
GenerateAccountSeed creates a new Ed25519 account nkey and returns its seed and public key (which starts with "A"). It mirrors GenerateSeed for the account identity that signs user JWTs.
func GenerateSeed ¶
GenerateSeed creates a new Ed25519 user nkey and returns its seed and the corresponding public key. The public key is returned alongside so that no caller has to derive it, and so none can derive it wrongly.
func InboxPrefix ¶
InboxPrefix returns the reply-inbox subject prefix reserved for workerID.
nats.go's default prefix is the process-global "_INBOX", under which each connection takes "_INBOX.<connection-nuid>.*". Granting a worker "_INBOX.>" would therefore let it subscribe to every other client's reply inbox on this broker. Work leases are core-NATS request/reply and the batch is delivered with msg.Respond (internal/bus/lease.go), so one enrolled worker could read every other worker's assignment — the OnRun command line and arguments, embedded file contents, job and task parameters, environment variables, the path map and the isolation account — plus the JetStream API replies to sqi-server's own client, without ever leasing a task itself.
Each worker is therefore granted only its own prefix's subtree, and connects with nats.CustomInboxPrefix(InboxPrefix(workerID)) (see internal/worker/natsclient). The server publishes ">" (see ServerPermissions), so msg.Respond and JetStream PubAcks reach whichever prefix a worker uses.
func LoadSeed ¶
LoadSeed reads a seed file, refusing one that is readable beyond its owner.
The seed is the worker's identity: a seed readable by other accounts on a shared render node hands that identity to every one of them.
The check runs on every platform. The property is "nobody else can read this", which POSIX expresses through mode bits and Windows through a DACL; fsutil.IsRestricted checks whichever applies.
func NkeyOption ¶
NkeyOption returns a nats.Option that authenticates a connection as the nkey identified by publicKey, signing the server's nonce with seed. Both sqi-server (its own broker connections, internal/bus.Embedded) and sqi-worker (internal/worker/natsclient.buildOptions) use it to present their own credential to the broker.
func PublicKeyFromSeed ¶
PublicKeyFromSeed derives the public key for a seed.
func ReservedAccount ¶
ReservedAccount reports whether name is one of the accounts sqi or nats-server defines.
func RestrictHint ¶
RestrictHint returns the platform-appropriate remediation for a seed file that is readable beyond its owner.
Only the advice is platform-specific. The check itself (fsutil.IsRestricted) is not: see LoadSeed.
func SaveSeed ¶
SaveSeed writes seed to path with owner-only permissions, creating parent directories as needed.
The write is atomic: seed is written to a temporary file in the same directory as path, synced, and moved into place with os.Rename, so a reader always sees either the previous seed or the new one. A truncated or zero-length seed file would read as "enrolled" to every caller that checks only existence, and the worker could not recover without an operator deleting it. The temp file lives in filepath.Dir(path), not the system temp directory, because a rename across filesystems is not atomic and can fail. The sync before the rename keeps a hard power loss from reverting a saved seed to the previous contents.
The result is owner-only whatever existed at path before: fsutil.WriteSecret restricts the temp file before its first byte is written, and the rename replaces any more permissive file (a leftover from key rotation, or one an operator chmod'd to inspect it) with one that was never readable by others. On NTFS the rename carries the file's own security descriptor, which is protected, so the seed does not re-inherit the directory's access.
The caller needs write permission on the directory containing path, not only on the seed file: a directory locked down to 0500 around an owner-writable seed fails here.
func ServerPermissions ¶
func ServerPermissions() *natsserver.Permissions
ServerPermissions returns the permissions for sqi-server's own broker connections, which need the full subject space: the scheduler consumes every worker subject and publishes cancels, and the broker's admin connection provisions streams.
func UserPermissions ¶
func UserPermissions(p *natsserver.Permissions) jwt.Permissions
UserPermissions converts p to the form carried by a NATS user JWT.
func ValidAccountName ¶
ValidAccountName reports whether name has the shape sqi accepts for the account its data lives in: letters, digits, '_' and '-' only.
func ValidWorkerIDToken ¶
ValidWorkerIDToken reports whether workerID may be used as a single NATS subject token, which every worker→server subject, InboxPrefix and WorkerPermissions require of it.
Worker IDs are UUIDs generated by workerconfig.LoadOrCreateWorkerID, which always pass. The check is still made at three points, for two reasons:
- At enrollment (internal/api's enroll handler, and sqi-server's worker enroll command) the ID is supplied by the caller and becomes a subject pattern in WorkerPermissions. "*" would mint a credential granted "task.status.*.*", "worker.deregister.*", "work.lease.*.*" and the rest, able to publish concrete subjects belonging to any worker, so it could forge status and logs, deregister the farm, and lease work as another worker and receive that worker's assignment batch. ">" instead yields the malformed "task.status.>.*", which nats-server rejects. internal/bus's auth-callout responder re-checks at every connect and refuses, so a row that reached the database some other way cannot do either.
- On the worker's own connect path (internal/worker/natsclient) the ID comes from a file an operator can edit, and one containing "." would silently widen the inbox subtree its grant covers.
func ValidatePublicKey ¶
ValidatePublicKey reports whether pk is a well-formed user nkey.
func WorkerPermissions ¶
func WorkerPermissions(workerID string) *natsserver.Permissions
WorkerPermissions returns the NATS permissions for one worker.
Every worker→server subject carries the worker's ID as a token so that these permissions can bind it: NATS permissions are static per credential and JetStream does not stamp publisher identity onto a message, so only the subject can express "this worker's own traffic".
Types ¶
This section is empty.