platform

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 17 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 platform gets this agent's NATS credentials from the stone-age.io platform and keeps them current.

On the platform an agent is a Thing: it authenticates as itself against the `things` auth collection, and its NATS credential lives on the related `nats_users` record's `creds_file` field. The platform's access rules are built for exactly this shape — an authenticated thing can see only its own record and only its assigned NATS identity — so no server-side route is needed to read a credential.

Three operations, in the order a device meets them:

EnsureCredentials  First boot: log in with the thing's password and write
                   the .creds file.
Sync               Routine upkeep: keep the platform session token alive and
                   pick up a credential the platform re-minted (an operator
                   pressing Regenerate, say).
Rotate             On demand: ask the platform to re-mint this agent's
                   credential, then take the new one.

Sync deliberately does NOT ask for the credential every time. A .creds file embeds the nkey seed — a private key — so the routine path refreshes the token without expanding any relation and then probes only the credential's `updated` timestamp via ?fields=updated. Key material crosses the network only when it has actually changed. That split is necessary rather than fussy: `fields` is not applied to PocketBase auth responses (apis/record_helpers.go marshals the record by hand), so any auth call with expand=nats_user returns the whole credential whether the caller wants it or not.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoAccountJWTRoute = errors.New("the platform has no " + accountJWTPath + " route (it predates the probe)")

ErrNoAccountJWTRoute is AccountJWT's answer from a platform that predates the probe route: PocketBase answers a route it does not have with 404. The leaf refresh then falls back to reading the full leaf config every run.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client talks to one platform instance on behalf of one agent.

func NewClient

func NewClient(cfg *config.Config, logger *zap.Logger) *Client

NewClient builds a client from validated config. The URL scheme is checked in config.validate() so a misconfigured agent fails at load rather than on the first request.

func (*Client) AccountJWT added in v0.5.0

func (c *Client) AccountJWT() (accountPub, accountJWT string, err error)

AccountJWT reads only this Thing's account JWT and its public key -- the revision probe for the leaf refresh.

The full leaf config carries this Thing's .creds, nkey seed and all, and the refresh only needs to know whether the account JWT moved. This route serves public trust material and nothing else, so a refresh that finds nothing changed has put no key material on the wire. Same lock and session discipline as LeafConfig.

func (*Client) EnsureCredentials

func (c *Client) EnsureCredentials() error

EnsureCredentials writes the .creds file if it is missing. It is a no-op once the file exists, which keeps startup fast and offline-tolerant on every boot after the first.

func (*Client) HubDomain added in v0.2.0

func (c *Client) HubDomain() (string, error)

HubDomain returns the hub's JetStream domain: from the session file if a leaf config has ever been fetched, otherwise by fetching one now.

The cache is what matters. Edge sync needs this value on every start, and an edge box is precisely the thing whose platform may be unreachable — so the common path reads a local file and the network is touched once, on the first start after bootstrap.

It deliberately does not re-fetch to check for drift. A hub's JetStream domain changing is a deployment-wide event that invalidates every leaf's config, not something to poll for; `agent -leaf-config` is the thing that re-reads it.

func (*Client) LastSync added in v0.3.0

func (c *Client) LastSync() time.Time

LastSync reports when this client last completed a sync or rotation, or the zero time if it never has.

This exists because a credential sync that silently stops working is fatal on a delay: the .creds on disk keeps working until the platform re-mints or revokes it, and only then does the agent discover it has been unable to talk to the platform for weeks. Nothing reported that before — creds_sync appeared in cmd.health as a string in a list of task names and nowhere else.

func (*Client) LeafConfig added in v0.2.0

func (c *Client) LeafConfig() (*LeafConfig, error)

LeafConfig fetches this agent's leaf configuration from the platform.

It takes the same lock and session discipline as Sync and NebulaSource.Fetch: every writer of the session file does a read-modify-write under c.mu, or one job drops another's token — the agent's only password-free way back in.

It carries this Thing's .creds, so the leaf refresh does not poll it: it polls AccountJWT and calls this only when the account JWT moved (or on a platform that predates that route). `agent -leaf-config` calls it once.

func (*Client) NebulaSource added in v0.2.0

func (c *Client) NebulaSource() *NebulaSource

NebulaSource returns a nebula.Source backed by this platform client.

func (*Client) Rotate

func (c *Client) Rotate() (bool, error)

Rotate asks the platform to re-mint this agent's NATS credential and adopts the result, reporting whether the .creds file changed.

Rotation is not revocation: the previous credential stays valid until it expires or an operator revokes it on the platform.

func (*Client) Sync

func (c *Client) Sync() (bool, error)

Sync renews the platform session token and adopts the credential the platform currently holds for this agent, reporting whether the .creds file changed.

This is the routine path, so it stays off the credential unless the revision moved: refresh (no expand) then probe (?fields=updated), and only then read the key.

type LeafConfig added in v0.2.0

type LeafConfig struct {
	// Code is this Thing's code, and Domain is the same value: the JetStream
	// domain is the code, computed by the platform and stored nowhere. Both are
	// returned because the generated nats-leaf.conf writes them into different
	// directives — `server_name` and `jetstream { domain }` — and a reader
	// should not have to know they are the same string.
	Code   string `json:"code"`
	Domain string `json:"domain"`

	Creds string `json:"creds"`

	AccountJWT string `json:"account_jwt"`
	AccountPub string `json:"account_pub"`

	OperatorJWT string `json:"operator_jwt"`

	// The $SYS account's JWT and public key. Not optional and not a $SYS
	// identity: the operator JWT names a system account, and a leaf running
	// `resolver: MEMORY` has nowhere to fetch it, so without it preloaded
	// nats-server refuses to start with "error resolving system account:
	// account missing" — before JetStream is ever reached. Connecting AS $SYS
	// needs a $SYS user credential, which is never served.
	SysAccountJWT string `json:"sys_account_jwt"`
	SysAccountPub string `json:"sys_account_pub"`

	// Where this deployment's hub accepts leaf connections, and the hub's own
	// JetStream domain. Deployment-wide facts held once on the Control Plane
	// rather than configured by hand on every edge box.
	HubLeafURL string `json:"hub_leaf_url"`
	HubDomain  string `json:"hub_domain"`
}

LeafConfig is everything a gateway needs to stand up its NATS leaf server.

Only one of these values is otherwise unreachable: the operator JWT lives in a superuser-only collection, which is the entire reason the route exists. The rest travels with it because the caller needs it in the same breath — and because a single call before NATS is up is simpler than four.

Nothing here is secret beyond Creds. Operator and account JWTs are public trust material, verified by every server in the network; Creds is this Thing's own credential, which it must hold to connect at all. Account seeds and signing keys are never served.

type NebulaSource added in v0.2.0

type NebulaSource struct {
	// contains filtered or unexported fields
}

NebulaSource reads this agent's Nebula config from the platform.

It is a distinct type rather than more methods on Client because `Fetch` means something specific to the nebula package and nothing in particular to a credential client. It shares Client's authentication deliberately: a thing has one platform identity, and duplicating the session handling to read a second record off the same login would be silly. The argument in docs/nebula-design.md against a shared library is about sharing across repositories, not within one binary.

func (*NebulaSource) Fetch added in v0.2.0

func (s *NebulaSource) Fetch(knownRevision string) (nebula.Fetched, error)

Fetch returns the Nebula config the platform currently holds for this agent, or reports it unchanged.

Same shape, and same reasoning, as Client.Sync: refresh the session without expanding anything, probe only the `updated` timestamp, and read the body only when that revision has moved. A Nebula config carries the host's private key inline — Nebula's PKI requires it there — so the routine path must not pull it across the network just to discover nothing changed.

An empty knownRevision means the caller has nothing yet, and skips the probe.

func (*NebulaSource) Name added in v0.2.0

func (s *NebulaSource) Name() string

Jump to

Keyboard shortcuts

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