bus

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 15 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 bus builds the NATS options, buckets and streams that osapi uses.

Index

Constants

View Source
const MaxControlLine = 32 * 1024

MaxControlLine is the longest protocol line, in bytes, that the embedded server takes. A NATS server that osapi does not run needs at least this in its max_control_line, in operator mode.

Variables

View Source
var ErrRole = fmt.Errorf("no such role")

ErrRole is a role that osapi does not have.

Roles are each role, in order.

Functions

func ApplyRoles

func ApplyRoles(
	opts *natsserver.Options,
	serverAuth config.NATSServerAuth,
	namespace string,
	cfg config.NATS,
) error

ApplyRoles limits each user of opts that has a role in serverAuth to the permissions of the role, on the streams and buckets of cfg under namespace. A user with no role keeps each subject.

func AuditStream

func AuditStream(
	namespace string,
	auditCfg config.NATSAudit,
) jetstream.StreamConfig

AuditStream builds a jetstream.StreamConfig from audit config values.

func AuthOptions

func AuthOptions(
	auth config.NATSAuth,
) natsclient.AuthOptions

AuthOptions converts a config NATSAuth to natsclient.AuthOptions.

func Close

func Close(
	nc natsCloser,
)

Close safely closes a NATS client connection.

func ConnectionOptions

func ConnectionOptions(
	conn config.NATSConnection,
	name string,
) (*natsclient.Options, error)

ConnectionOptions are the options of a connection to NATS from conn: its host, port, auth and TLS, with name. It is an error when conn's TLS does not load: the connection does not fall back to plain text.

func Dial

func Dial(
	log *slog.Logger,
	opts *natsclient.Options,
) (*natsclient.Client, error)

Dial connects to NATS with opts.

func EnrollmentKV

func EnrollmentKV(
	namespace string,
	enrollmentCfg config.NATSEnrollment,
) jetstream.KeyValueConfig

EnrollmentKV builds a jetstream.KeyValueConfig from enrollment config values. The enrollment bucket has no TTL so pending requests persist until accepted or rejected.

func FactsKV

func FactsKV(
	namespace string,
	factsCfg config.NATSFacts,
) jetstream.KeyValueConfig

FactsKV builds a jetstream.KeyValueConfig from facts config values.

func FileStateKV

func FileStateKV(
	namespace string,
	fileStateCfg config.NATSFileState,
) jetstream.KeyValueConfig

FileStateKV builds a jetstream.KeyValueConfig from file-state config values. The file-state bucket has no TTL so deployment SHA tracking persists indefinitely.

func JetStreamKey

func JetStreamKey(
	cfg config.NATSServerJetStream,
) (string, error)

JetStreamKey reads the key of the JetStream store from the file of cfg, or is "" when no file is set. A file with no key is an error, because an empty key turns the encryption off.

func JobKV

func JobKV(
	namespace string,
	kvCfg config.NATSKV,
) jetstream.KeyValueConfig

JobKV builds a jetstream.KeyValueConfig from job KV config values. The returned config includes TTL, MaxBytes, Storage, and Replicas from the NATSKV configuration.

func ObjectStore

func ObjectStore(
	namespace string,
	objectsCfg config.NATSObjects,
) jetstream.ObjectStoreConfig

ObjectStore builds a jetstream.ObjectStoreConfig from objects config values.

func Prepare

func Prepare(
	ctx context.Context,
	log *slog.Logger,
	opts *natsclient.Options,
	namespace string,
	cfg config.NATS,
) error

Prepare connects to NATS with opts, creates each stream and bucket with Setup, then closes the connection.

func RegistryKV

func RegistryKV(
	namespace string,
	registryCfg config.NATSRegistry,
) jetstream.KeyValueConfig

RegistryKV builds a jetstream.KeyValueConfig from registry config values.

func SecretsKV

func SecretsKV(
	namespace string,
	secretsCfg config.NATSSecrets,
) jetstream.KeyValueConfig

SecretsKV builds a jetstream.KeyValueConfig from the secrets config values. The bucket has no TTL, and keeps History versions of each key.

func ServerClientAuth

func ServerClientAuth(
	serverAuth config.NATSServerAuth,
) natsclient.AuthOptions

ServerClientAuth is how the embedded server's own setup and heartbeat connect to it: as its first user for user_pass, with nkey_file for nkey, with creds_file for operator, and without auth otherwise.

func ServerOptions

func ServerOptions(
	host string,
	port int,
	storeDir string,
	serverAuth config.NATSServerAuth,
	serverTLS config.ServerTLS,
	keyDir string,
) (*natsserver.Options, error)

ServerOptions is the embedded server's options: host, port and JetStream, and the authentication serverAuth describes. For operator auth the options start from serverAuth.ResolverFile, read by NATS's own config parser, so the trusted operator, the system account and the preloaded account JWTs are exactly what nsc wrote; the host, port and JetStream settings are laid over them.

func Setup

func Setup(
	ctx context.Context,
	nc JetStream,
	namespace string,
	cfg config.NATS,
) error

Setup creates or updates each stream and bucket that osapi uses, with the names in namespace. A second run with the same config changes nothing. A bucket with no name in cfg is not made.

func StateKV

func StateKV(
	namespace string,
	stateCfg config.NATSState,
) jetstream.KeyValueConfig

StateKV builds a jetstream.KeyValueConfig from state config values. The state bucket has no TTL so drain flags and timeline events persist indefinitely.

func StorageType

func StorageType(
	s string,
) jetstream.StorageType

StorageType maps "memory"/"file" strings to jetstream.StorageType.

func Telemetry

func Telemetry(
	opts *natsclient.Options,
	mp *sdkmetric.MeterProvider,
) *natsclient.Options

Telemetry turns on the spans and the metrics of nats-client in opts. The spans use the global tracer provider, which tracing sets. The metrics go to mp, the meter provider of the component; nil sends them to the global meter provider, which records nothing unless a program sets one.

Types

type JetStream

type JetStream interface {
	StreamCreateOrUpdate(ctx context.Context, config jetstream.StreamConfig) error
	KVCreateOrUpdate(ctx context.Context, config jetstream.KeyValueConfig) error
	ObjectStoreCreateOrUpdate(ctx context.Context, config jetstream.ObjectStoreConfig) error
}

JetStream is the access to NATS that Setup uses. A *natsclient.Client is one.

type Permissions

type Permissions struct {
	Publish   []string `json:"publish"`
	Subscribe []string `json:"subscribe"`
	// Responses lets the role answer each request that it receives, once,
	// on the requester's reply subject, which it cannot otherwise publish
	// to: allow_responses in a server's config, --allow-pub-response in nsc.
	Responses bool `json:"responses"`
}

Permissions are the subjects that a role may publish and subscribe to, as NATS user permissions take them.

func RolePermissions

func RolePermissions(
	role Role,
	namespace string,
	cfg config.NATS,
) (Permissions, error)

RolePermissions are the permissions of role on a NATS server with the streams and buckets of cfg, under namespace: the least that the role needs. The names come from the same functions that make the streams and buckets, so a renamed bucket changes them too.

type Role

type Role string

Role is an osapi process, as NATS sees it: what it publishes and what it subscribes to.

const (
	// RoleController is the controller: it sends jobs, reads their answers,
	// keeps the admission records and the audit log.
	RoleController Role = "controller"
	// RoleAgent is an agent: it takes the jobs of its host, and writes their
	// answers, its registration, its facts and its state.
	RoleAgent Role = "agent"
	// RoleSetup makes and updates each stream and bucket: osapi nats setup,
	// and the embedded server, which also writes its own registration.
	RoleSetup Role = "setup"
	// RoleJoin is the bootstrap user of a join over NATS: it can send the
	// join request and get its reply, and nothing else. The join string
	// carries it.
	RoleJoin Role = "join"
)

The roles of osapi on NATS.

Directories

Path Synopsis
Package mocks provides mock implementations for testing.
Package mocks provides mock implementations for testing.
Package server runs the embedded NATS server that osapi starts for itself, and writes its heartbeat.
Package server runs the embedded NATS server that osapi starts for itself, and writes its heartbeat.

Jump to

Keyboard shortcuts

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