cryptokeys

package
v2.36.1 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: AGPL-3.0 Imports: 24 Imported by: 0

Documentation

Overview

Package cryptokeys provides an abstraction for fetching internally used cryptographic keys mainly for JWT signing and verification.

Index

Constants

View Source
const (
	WorkspaceAppsTokenDuration = time.Minute
	OIDCConvertTokenDuration   = time.Minute * 5
	TailnetResumeTokenDuration = time.Hour * 24
	// NATSCAOverlap is how long a NATS cluster CA certificate stays valid past
	// the end of its active-signing window (startsAt + keyDuration). The next CA
	// becomes the active signer at the window's end, but replicas keep minting
	// leaves with the old CA until their key cache refreshes onto the new one.
	// This overlap keeps the old CA valid through that transition, so it must
	// exceed the cache refresh interval (plus a small leaf clamp buffer). Leaf
	// lifetime imposes nothing here: leaves are clamped to just before their
	// signing CA's NotAfter (see coderd/x/nats mintLeaf).
	NATSCAOverlap = time.Minute * 30

	// DefaultKeyDuration is the default duration for which a key is valid. It applies to all features.
	DefaultKeyDuration = time.Hour * 24 * 30
)

Variables

View Source
var (
	ErrKeyNotFound    = xerrors.New("key not found")
	ErrKeyInvalid     = xerrors.New("key is invalid for use")
	ErrClosed         = xerrors.New("closed")
	ErrInvalidFeature = xerrors.New("invalid feature for this operation")
)

Functions

func DefaultRotatedFeatures added in v2.35.0

func DefaultRotatedFeatures() []database.CryptoKeyFeature

DefaultRotatedFeatures returns the crypto key features the rotator manages by default. It excludes experiment-gated features such as the NATS CA.

func StartRotator

func StartRotator(ctx context.Context, logger slog.Logger, db database.Store, opts ...RotatorOption)

StartRotator starts a background process that rotates keys in the database. It ensures there's at least one valid key per feature prior to returning. Canceling the provided context will stop the background process.

Types

type CacheOption

type CacheOption func(*cache)

func WithCacheClock

func WithCacheClock(clock quartz.Clock) CacheOption

type DBFetcher

type DBFetcher struct {
	DB database.Store
}

func (*DBFetcher) Fetch

type EncryptionKeycache

type EncryptionKeycache interface {
	// EncryptingKey returns the latest valid key for encrypting payloads. A valid
	// key is one that is both past its start time and before its deletion time.
	EncryptingKey(ctx context.Context) (id string, key interface{}, err error)
	// DecryptingKey returns the key with the provided id which maps to its sequence
	// number. The key is valid for decryption as long as it is not deleted or past
	// its deletion date. We must allow for keys prior to their start time to
	// account for clock skew between peers (one key may be past its start time on
	// one machine while another is not).
	DecryptingKey(ctx context.Context, id string) (key interface{}, err error)
	io.Closer
}

func NewEncryptionCache

func NewEncryptionCache(ctx context.Context, logger slog.Logger, fetcher Fetcher,
	feature codersdk.CryptoKeyFeature, opts ...func(*cache),
) (EncryptionKeycache, error)

type Fetcher

type Fetcher interface {
	Fetch(ctx context.Context, feature codersdk.CryptoKeyFeature) ([]codersdk.CryptoKey, error)
}

type NATSCA added in v2.36.0

type NATSCA struct {
	// Sequence is the crypto_keys sequence of the row this CA came from.
	Sequence int32
	// Cert is the CA certificate used to sign or verify leaf certificates.
	Cert *x509.Certificate
	// Key is the CA private key, used to sign leaves.
	Key crypto.Signer
}

NATSCA is the decoded form of a single nats_ca crypto key row, produced by the generic crypto key cache (see idSecret). The CA signs the ephemeral leaf certificates that replicas use for NATS cluster mTLS.

The active CA is served by a SigningKeycache.SigningKey call for the nats_ca feature; a specific historical CA (for verifying a peer leaf minted under an earlier CA during a rotation overlap) is served by VerifyingKey with that row's sequence.

type NoopSigningKeycache added in v2.36.0

type NoopSigningKeycache struct{}

NoopSigningKeycache is a SigningKeycache that holds no keys: SigningKey and VerifyingKey always report ErrKeyNotFound. It lets a subsystem that only needs real keys once an optional feature is enabled (for example NATS cluster mTLS, which only signs leaves under enterprise HA) be constructed without a database dependency, then be swapped for a real cache when the feature turns on.

func (NoopSigningKeycache) Close added in v2.36.0

func (NoopSigningKeycache) Close() error

func (NoopSigningKeycache) SigningKey added in v2.36.0

func (NoopSigningKeycache) SigningKey(context.Context) (string, interface{}, error)

func (NoopSigningKeycache) VerifyingKey added in v2.36.0

func (NoopSigningKeycache) VerifyingKey(context.Context, string) (interface{}, error)

type RotatorOption

type RotatorOption func(*rotator)

func WithClock

func WithClock(clock quartz.Clock) RotatorOption

func WithFeatures added in v2.36.0

func WithFeatures(features []database.CryptoKeyFeature) RotatorOption

WithFeatures sets the crypto key features the rotator manages, replacing the default set. Use this to opt experiment- or deployment-gated features (such as the NATS cluster CA) into rotation only when their owner is active.

func WithKeyDuration

func WithKeyDuration(keyDuration time.Duration) RotatorOption

type SigningKeycache

type SigningKeycache interface {
	// SigningKey returns the latest valid key for signing. A valid key is one
	// that is both past its start time and before its deletion time.
	SigningKey(ctx context.Context) (id string, key interface{}, err error)
	// VerifyingKey returns the key with the provided id which should map to its
	// sequence number. The key is valid for verifying as long as it is not deleted
	// or past its deletion date. We must allow for keys prior to their start time
	// to account for clock skew between peers (one key may be past its start time
	// on one machine while another is not).
	VerifyingKey(ctx context.Context, id string) (key interface{}, err error)
	io.Closer
}

func NewSigningCache

func NewSigningCache(ctx context.Context, logger slog.Logger, fetcher Fetcher,
	feature codersdk.CryptoKeyFeature, opts ...func(*cache),
) (SigningKeycache, error)

NewSigningCache instantiates a cache. Close should be called to release resources associated with its internal timer.

Jump to

Keyboard shortcuts

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