certrotation

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: 16 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 certrotation rotates the NATS server's self-signed certificate with no restart and no new join of the agents. Each agent pins that certificate, so the controller makes the next one beside it, and writes a record to the state bucket that its key signs, with both certificates. Each agent adds the next one to the certificates that it trusts at its next heartbeat, and its registration reports it. When each admitted agent that reported within the grace period has it, or the grace period ends, the controller renames the next files into place. The NATS server serves the new certificate at its next handshake, and the controller writes the record of the switch.

Index

Constants

This section is empty.

Variables

View Source
var ErrOperatorCert = errors.New("the certificate is the operator's cert_file: replace that file")

ErrOperatorCert is a rotation of a certificate that the operator's cert_file names: osapi rotates only the self-signed one that it made.

Functions

func NextFiles

func NextFiles(
	src pki.CertSource,
) (string, string)

NextFiles are the files of the next certificate of src, beside its own: nats.next.crt and nats.next.key for nats.crt.

Types

type Admitted

type Admitted interface {
	List(ctx context.Context) ([]admission.Record, error)
}

Admitted lists the admission records: the agents that the switch waits for are the admitted ones.

type Agent

type Agent struct {
	MachineID string
	Hostname  string
	HasNext   bool
}

Agent is one admitted agent of a rotation.

type Config

type Config struct {
	// KV and Bucket are the state bucket, which holds the record.
	KV     KV
	Bucket string
	// Keys are the controller's keys, which sign the record and check it.
	Keys *pki.Manager
	// Source is the NATS server's certificate.
	Source pki.CertSource
	// Admitted and Seen are the admitted agents and when each last
	// reported. A Rotation with none of them, such as the one of the CLI,
	// can start a rotation and show it, and does not switch.
	Admitted Admitted
	Seen     Seen
	// Grace is how long a rotation waits for the agents before it switches.
	Grace time.Duration
	// Logger logs the start and the switch.
	Logger *slog.Logger
	// Now is the clock. Nil is time.Now.
	Now func() time.Time
}

Config is what a Rotation uses.

type KV

type KV interface {
	KVGet(ctx context.Context, bucket string, key string) (jetstream.KeyValueEntry, error)
	KVCreate(ctx context.Context, bucket string, key string, value []byte) (uint64, error)
	KVUpdate(
		ctx context.Context,
		bucket string,
		key string,
		value []byte,
		revision uint64,
	) (uint64, error)
}

KV is the part of a NATS client that the rotation uses: the record in the state bucket. A *natsclient.Client is one.

type Rotation

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

Rotation rotates the NATS server's certificate.

func New

func New(
	cfg Config,
) *Rotation

New is a Rotation with cfg.

func (*Rotation) Run

func (r *Rotation) Run(
	ctx context.Context,
) error

Run switches the certificate when Step says so, every interval, until ctx is done. An error of one step is logged, and the next step tries again.

func (*Rotation) Start

func (r *Rotation) Start(
	ctx context.Context,
) (Status, error)

Start starts a rotation: it makes the next certificate beside the current one, and writes the record that gives both to the agents. A rotation in progress is not started again: Start shows it.

func (*Rotation) Status

func (r *Rotation) Status(
	ctx context.Context,
) (Status, error)

Status is the rotation now.

func (*Rotation) Step

func (r *Rotation) Step(
	ctx context.Context,
) (bool, error)

Step switches to the next certificate when each agent that the switch waits for trusts it, or the grace period ended. It reports whether it switched.

type Seen

type Seen interface {
	ListAgents(ctx context.Context) ([]job.AgentInfo, error)
	AgentsLastSeen(ctx context.Context) (map[string]time.Time, error)
}

Seen tells which admitted agents still report, with the next NATS certificate that each reports, and the last report of each machine. The job client is one.

type Status

type Status struct {
	// Current is the fingerprint of the certificate that the server serves.
	Current string
	// Next is the fingerprint of the certificate that it switches to,
	// during a rotation.
	Next string
	// StartedAt is when the rotation started, and SwitchBy when it switches
	// at the latest.
	StartedAt time.Time
	SwitchBy  time.Time
	// Agents are the admitted agents that the switch waits for, and whether
	// each trusts the next certificate.
	Agents []Agent
}

Status is a rotation as the API shows it.

Directories

Path Synopsis
Package mocks holds the generated doubles of the dependencies of the rotation of the NATS server's certificate, for its tests.
Package mocks holds the generated doubles of the dependencies of the rotation of the NATS server's certificate, for its tests.

Jump to

Keyboard shortcuts

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