keyrotation

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: 14 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 keyrotation rotates the controller's key with no new admission of the agents. The controller makes the next key, and writes a record to the state bucket that its current key signs. Each agent keeps the next key 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 switches to the next key, and writes a record that the new key signs. The agents then take the new key, and keep the old one as the previous one, which still verifies the jobs that it signed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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 Start and the switch read and
	// replace.
	Keys *pki.Manager
	// 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 controller's key.

func New

func New(
	cfg Config,
) *Rotation

New is a Rotation with cfg.

func (*Rotation) Load

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

Load takes the record at the start of the controller: the previous key after a switch keeps verifying the records that it signed, and a switch that stopped after its renames writes its record.

func (*Rotation) Run

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

Run switches the key 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 key beside the current one, and writes the record that gives it 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 key when each agent that the switch waits for has 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: the agents in the registry now, with the controller keys 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 key that the controller signs with.
	Current string
	// Next is the fingerprint of the key 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 has the next key.
	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 controller's key, for its tests.
Package mocks holds the generated doubles of the dependencies of the rotation of the controller's key, for its tests.

Jump to

Keyboard shortcuts

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