clusterpki

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: AGPL-3.0 Imports: 18 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 clusterpki runs the cluster CA on top of the replicated state (ADR 0006, FA-PKI-01..04): it creates the CA at bootstrap, issues and redeems join tokens, issues and renews node certificates, keeps the revocation list at hand for TLS handshakes and manages this node's own certificate.

Index

Constants

View Source
const (
	DefaultTokenTTL = time.Hour
	MaxTokenTTL     = 24 * time.Hour
)

Join token lifetimes (FA-PKI-02).

Variables

View Source
var ErrInvalidToken = errors.New("invalid join token")

ErrInvalidToken is the only error a joining node sees for a bad token, whatever the reason (unknown, used, expired, wrong secret or CA).

View Source
var ErrNoCA = errors.New("cluster CA does not exist yet")

ErrNoCA means the cluster has no CA yet (bootstrap still running).

Functions

This section is empty.

Types

type Applier

type Applier interface {
	Apply(ctx context.Context, cmd *pb.Command) (cluster.ApplyResult, error)
}

Applier proposes commands to the Raft cluster.

type Authority

type Authority struct {
	State   store.State
	Cluster Applier
	Keyring Keyring
	// Now is the clock; nil uses time.Now.
	Now func() time.Time
}

Authority is the cluster CA. Issuing needs the CA key and therefore runs on the leader; reading the bundle works on every node.

func (*Authority) Bundle

func (a *Authority) Bundle(ctx context.Context) ([]*x509.Certificate, string, error)

Bundle returns the trusted CA certificates and the cluster ID.

func (*Authority) CreateJoinToken

func (a *Authority) CreateJoinToken(ctx context.Context, role pki.Role, tenantID string, ttl time.Duration) (string, time.Time, error)

CreateJoinToken stores a new one-time token and returns it for the operator; the secret itself is never stored (FA-PKI-02). ttl 0 means DefaultTokenTTL.

func (*Authority) Ensure

func (a *Authority) Ensure(ctx context.Context, isLeader bool) error

Ensure creates the cluster CA if none exists. Only the leader creates it; followers receive it through Raft. Concurrent attempts are harmless: the FSM keeps the first CA and rejects any other.

func (*Authority) IssueSelf

func (a *Authority) IssueSelf(ctx context.Context, nodeID string, csrDER []byte) (Issued, error)

IssueSelf issues a voter certificate for this node directly, without a token: the bootstrap node and the leader renewing its own certificate.

func (*Authority) Redeem

func (a *Authority) Redeem(ctx context.Context, token, nodeID string, csrDER []byte) (Issued, error)

Redeem enrolls a node with a join token: it checks the token, uses it up through Raft and issues a certificate with the token's role and tenant. The CSR is checked before the token is used, so a broken request does not burn it. Every token problem yields ErrInvalidToken.

func (*Authority) Renew

func (a *Authority) Renew(ctx context.Context, peer pki.Identity, csrDER []byte) (Issued, error)

Renew issues a new certificate for an authenticated peer, keeping its identity (FA-PKI-03). The caller must have verified the peer's current certificate (mTLS) and passes the identity from it.

func (*Authority) Revoke

func (a *Authority) Revoke(ctx context.Context, cert *x509.Certificate) error

Revoke adds a certificate to the revocation list (FA-PKI-04).

type IssueFunc

type IssueFunc func(ctx context.Context, csrDER []byte) (Issued, error)

IssueFunc obtains a certificate for a CSR: from the CA on the leader, through RenewCertificate on the leader for followers.

type Issued

type Issued struct {
	Identity pki.Identity
	Leaf     *x509.Certificate
	// ChainDER is leaf then CA, as nodes present it (pki.Leaf).
	ChainDER [][]byte
	// BundleDER are the trusted CA certificates.
	BundleDER [][]byte
}

Issued is a node certificate with the trust bundle to send to the node.

type Keyring

type Keyring interface {
	Encrypt(plaintext []byte, aad string) (string, error)
	Decrypt(ctx context.Context, ciphertext, aad string) ([]byte, error)
}

Keyring encrypts the CA key (ADR 0011).

type NodeCert

type NodeCert struct {
	DataDir string
	NodeID  string
	Holder  pki.Holder
	// Now is the wall clock for renewal decisions; nil uses time.Now. Tests
	// set it to simulate clock skew between nodes (ADR 0020).
	Now func() time.Time
	// contains filtered or unexported fields
}

NodeCert manages this node's key and certificate and serves them through Holder; a renewed certificate applies from the next handshake (FA-PKI-03).

func (*NodeCert) CA

func (n *NodeCert) CA() *x509.Certificate

CA returns the CA certificate the node certificate was issued by, or nil. It is the trust anchor of a joined node until the replicated state with the CA bundle has arrived.

func (*NodeCert) EnsureSelf

func (n *NodeCert) EnsureSelf(ctx context.Context, a *Authority, isLeader bool, now time.Time) error

EnsureSelf gives the leader a valid certificate issued directly by the CA (bootstrap node, leader renewing its own); on followers it only loads.

func (*NodeCert) Install

func (n *NodeCert) Install(key *ecdsa.PrivateKey, issued Issued) error

Install stores key and certificate (key file 0600, written atomically) and makes them the active certificate.

func (*NodeCert) Leaf

func (n *NodeCert) Leaf() *x509.Certificate

Leaf returns the current certificate, or nil.

func (*NodeCert) Load

func (n *NodeCert) Load() error

Load reads the stored key and certificate, if any. A certificate whose key does not match is an error, not a reason to silently start over.

func (*NodeCert) NeedsRenewal

func (n *NodeCert) NeedsRenewal(now time.Time) bool

NeedsRenewal reports whether there is no certificate or it reached its renewal time.

func (*NodeCert) NewCSR

func (n *NodeCert) NewCSR() (*ecdsa.PrivateKey, []byte, error)

NewCSR creates a fresh key for the next certificate and returns its CSR; Install activates key and certificate together.

func (*NodeCert) Renew

func (n *NodeCert) Renew(ctx context.Context, now time.Time, issue IssueFunc) error

Renew loads the stored certificate if needed and, when there is none or it is due (FA-PKI-03), obtains a new one with a fresh key through issue. A nil issue only loads.

func (*NodeCert) RunRenewal

func (n *NodeCert) RunRenewal(ctx context.Context, issuer func() IssueFunc, interval time.Duration, log *slog.Logger)

RunRenewal checks every interval whether the certificate is due and renews it through the issuer chosen at that moment (FA-PKI-03).

type Revocations

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

Revocations caches the revocation list for TLS handshakes (FA-PKI-04): revoked serials and removed nodes. Both lists are small.

func (*Revocations) Reload

func (r *Revocations) Reload(ctx context.Context, state store.State) error

Reload reads the lists from the state.

func (*Revocations) Revoked

func (r *Revocations) Revoked(serial string) bool

Revoked reports whether a serial is revoked. Before the first load nothing is revoked.

func (*Revocations) RevokedNode

func (r *Revocations) RevokedNode(nodeID string) (time.Time, bool)

RevokedNode returns when a node was revoked (pki.Verifier.RevokedNode).

func (*Revocations) Run

func (r *Revocations) Run(ctx context.Context, state store.State, interval time.Duration, log *slog.Logger)

Run reloads the list every interval, so revocations replicated through Raft reach the handshakes of every node within that time.

Jump to

Keyboard shortcuts

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