Documentation
¶
Overview ¶
Package pki implements the cluster's own certificate authority (ADR 0006, FA-PKI-01..04): an ECDSA P-256 CA created at bootstrap, node certificates issued from CSRs, peer verification against the CA bundle and the revocation list, and one-time join tokens that pin the CA fingerprint.
Node identities are carried in a URI SAN "uc://<cluster-id>/<role>/<node-id>[?tenant=<tenant-id>]"; host names are not used, because nodes move and advertise addresses change (FA-CLU-11).
Index ¶
- Constants
- Variables
- func ClientConfig(h *Holder, v *Verifier, expectNodeID string, check PeerCheck, ...) *tls.Config
- func Fingerprint(cert *x509.Certificate) string
- func HashSecret(secret string) string
- func JoinClientConfig(fingerprint string, ca *atomic.Pointer[x509.Certificate]) *tls.Config
- func Leaf(leaf *x509.Certificate, key any, ca *x509.Certificate) *tls.Certificate
- func NewCSR(key *ecdsa.PrivateKey) ([]byte, error)
- func NewKey() (*ecdsa.PrivateKey, error)
- func OnlyVoters(id Identity) error
- func RenewAt(cert *x509.Certificate) time.Time
- func SecretMatches(secret, storedHash string) bool
- func SerialString(cert *x509.Certificate) string
- func ServerConfig(h *Holder, v *Verifier, requireClient bool, check PeerCheck, ...) *tls.Config
- type CA
- type Holder
- type Identity
- type JoinToken
- type PeerCheck
- type Role
- type Verifier
Constants ¶
const ( CAValidity = 10 * 365 * 24 * time.Hour LeafValidity = 90 * 24 * time.Hour // Backdate tolerates clock skew between nodes (PF-11). Backdate = 5 * time.Minute )
Validity periods (ADR 0006, FA-PKI-03).
Variables ¶
var ( ErrNoPeerCertificate = errors.New("peer presented no certificate") ErrRevoked = errors.New("peer certificate is revoked") ErrWrongCluster = errors.New("peer belongs to another cluster") ErrWrongPeer = errors.New("peer is not the expected node") )
Errors of peer verification.
var ErrInvalidToken = errors.New("invalid join token")
ErrInvalidToken means the token is malformed. Callers report every token failure the same way, so a client cannot tell why it was rejected.
Functions ¶
func ClientConfig ¶
func ClientConfig(h *Holder, v *Verifier, expectNodeID string, check PeerCheck, nextProtos ...string) *tls.Config
ClientConfig returns the TLS configuration for connecting to another node. The server is verified against the CA bundle instead of a host name; with expectNodeID set, only that node is accepted.
func Fingerprint ¶
func Fingerprint(cert *x509.Certificate) string
Fingerprint identifies a CA by the SHA-256 of its public key info, as embedded in join tokens (FA-PKI-02). It survives re-signing the CA certificate with the same key.
func HashSecret ¶
HashSecret is what the cluster stores. The secret has 256 bits of entropy, so a plain domain-separated SHA-256 is sufficient (no password hashing).
func JoinClientConfig ¶
JoinClientConfig is used by a joining node that has no certificate yet. It trusts only a voter certificate issued by a CA whose fingerprint matches the join token (FA-PKI-02); the server must send the CA certificate after its leaf. It returns the pinned CA through ca once the handshake succeeded.
func Leaf ¶
func Leaf(leaf *x509.Certificate, key any, ca *x509.Certificate) *tls.Certificate
Leaf builds the TLS certificate of a node: its leaf followed by the CA, so joining nodes can pin the CA (JoinClientConfig).
func NewCSR ¶
func NewCSR(key *ecdsa.PrivateKey) ([]byte, error)
NewCSR creates a certificate signing request for key. The private key never leaves the node.
func NewKey ¶
func NewKey() (*ecdsa.PrivateKey, error)
NewKey creates a node or CA key (ECDSA P-256).
func OnlyVoters ¶
OnlyVoters accepts voter certificates only (Raft, replication between voters).
func RenewAt ¶
func RenewAt(cert *x509.Certificate) time.Time
RenewAt returns when a certificate should be renewed: after two thirds of its lifetime (ADR 0006).
func SecretMatches ¶
SecretMatches compares a presented secret with a stored hash in constant time.
func SerialString ¶
func SerialString(cert *x509.Certificate) string
SerialString is the canonical form of a serial number in the revocation list.
func ServerConfig ¶
func ServerConfig(h *Holder, v *Verifier, requireClient bool, check PeerCheck, nextProtos ...string) *tls.Config
ServerConfig returns the TLS configuration of the cluster port. With requireClient false a client may connect without a certificate (only the Join RPC allows that); a presented certificate is always verified, and callers must check ConnectionState().PeerCertificates per RPC.
Session tickets are disabled and all checks run in VerifyConnection, which also runs for resumed sessions, so a revoked certificate cannot reconnect through resumption.
Types ¶
type CA ¶
type CA struct {
Cert *x509.Certificate
Key *ecdsa.PrivateKey
}
CA is the cluster certificate authority.
func (*CA) Issue ¶
Issue signs a node certificate for the public key of csrDER. The identity comes from the caller (join token or existing certificate), never from the CSR, whose subject and SANs are ignored.
func (*CA) MarshalKey ¶
MarshalKey returns the CA key as PKCS#8 DER, for encryption (ADR 0011).
type Holder ¶
type Holder struct {
// contains filtered or unexported fields
}
Holder serves the node's current certificate to TLS callbacks; replacing it takes effect at the next handshake (FA-PKI-03).
func (*Holder) Get ¶
func (h *Holder) Get() *tls.Certificate
Get returns the current certificate, or nil.
type Identity ¶
type Identity struct {
ClusterID string
NodeID string
Role Role
// TenantID is set for private probes of a tenant (FA-PRB-05).
TenantID string
}
Identity is who a node certificate belongs to.
func IdentityOf ¶
func IdentityOf(cert *x509.Certificate) (Identity, error)
IdentityOf reads the identity from a node certificate. It does not verify the certificate; use Verifier for that.
type JoinToken ¶
JoinToken is a parsed join token.
func NewJoinToken ¶
NewJoinToken creates a token for the CA with the given fingerprint.
func ParseJoinToken ¶
ParseJoinToken checks the format strictly.
type Verifier ¶
type Verifier struct {
ClusterID string
// ClusterIDFunc, if set, replaces ClusterID; nodes learn the cluster ID
// only after bootstrap or join.
ClusterIDFunc func() string
// Roots returns the trusted CA certificates (several during CA rotation).
Roots func() []*x509.Certificate
// Revoked reports whether a serial (SerialString) is revoked.
Revoked func(serial string) bool
// RevokedNode returns when a node was revoked (removed from the cluster);
// its certificates issued before that time are rejected, a later re-join
// gets a valid one.
RevokedNode func(nodeID string) (time.Time, bool)
// Now is the clock; nil uses time.Now.
Now func() time.Time
}
Verifier checks peer certificates against the trusted CA bundle and the revocation list (FA-PKI-01, FA-PKI-04). Its functions are read on every handshake, so updates (CA rotation, new revocations) apply immediately.
func (*Verifier) Verify ¶
func (v *Verifier) Verify(chain []*x509.Certificate, usage x509.ExtKeyUsage) (Identity, error)
Verify checks a peer chain (leaf first) for the given usage and returns the peer's identity. Only certificates issued directly by a trusted CA are accepted.