Documentation
¶
Overview ¶
Package pki provides Ed25519 keypair management for agent identity, including generation, loading, saving, signing, and verification.
Index ¶
- Constants
- Variables
- func CertFingerprint(pemData []byte) string
- func CertState(err error) string
- func EncodeEncryptionKey(pub *ecdh.PublicKey) string
- func FingerprintOf(pubKey ed25519.PublicKey) string
- func FingerprintOfKeys(signingKey ed25519.PublicKey, encryptionKey string) string
- func ParseEncryptionKey(key string) (*ecdh.PublicKey, error)
- func ParseEncryptionKeyFile(data []byte) (*ecdh.PrivateKey, error)
- func ParsePublicKeyPEM(pemData []byte) (ed25519.PublicKey, error)
- func ValidEncryptionKey(key string) bool
- type Cert
- type CertRecord
- type CertSource
- type ControllerKeyRecord
- type EnrollmentRequest
- type EnrollmentResponse
- type JoinNATS
- type JoinReply
- type JoinRequest
- type Manager
- func (m *Manager) ControllerPublicKey() ed25519.PublicKey
- func (m *Manager) Drop() error
- func (m *Manager) EncryptionKey() *ecdh.PrivateKey
- func (m *Manager) EncryptionPublicKey() string
- func (m *Manager) Fingerprint() string
- func (m *Manager) Load() error
- func (m *Manager) LoadOrGenerate() error
- func (m *Manager) Next() (*Manager, error)
- func (m *Manager) NextControllerPublicKey() ed25519.PublicKey
- func (m *Manager) PreviousControllerPublicKey() ed25519.PublicKey
- func (m *Manager) PreviousEncryptionKey() *ecdh.PrivateKey
- func (m *Manager) PrivateKey() ed25519.PrivateKey
- func (m *Manager) Promote(next *Manager) error
- func (m *Manager) PublicKey() ed25519.PublicKey
- func (m *Manager) Reload() (bool, error)
- func (m *Manager) RotateControllerKey(newKey ed25519.PublicKey)
- func (m *Manager) SetControllerPublicKey(key ed25519.PublicKey)
- func (m *Manager) SetNextControllerPublicKey(key ed25519.PublicKey)
- func (m *Manager) SetPreviousControllerPublicKey(key ed25519.PublicKey)
- func (m *Manager) Sign(data []byte) []byte
- func (m *Manager) VerifyWithGrace(data []byte, signature []byte) bool
- type Option
- type RenewRequest
- type RotateReply
- type RotateRequest
Constants ¶
const ( // CertOK is a certificate that osapi read. CertOK = "ok" // CertNotMade is a certificate whose file is not there, as on a host // where its component has not started. CertNotMade = "not made" )
The states of a certificate.
const ( // EnrollRequestSuffix is the NATS subject suffix for enrollment requests. EnrollRequestSuffix = "enroll.request" // EnrollResponsePrefix is the NATS subject prefix for enrollment responses. // Append ".<machineID>" to form the full subject. EnrollResponsePrefix = "enroll.response" // JoinRequestSuffix is the NATS subject suffix of a join over NATS: a // request that the controller replies to with a JoinReply. JoinRequestSuffix = "join.request" )
const ControllerKeyStateKey = "pki.controller"
ControllerKeyStateKey is the key of the ControllerKeyRecord in the state bucket, which each agent reads at its heartbeat.
const NATSCertStateKey = "pki.cert.nats"
NATSCertStateKey is the key of the CertRecord of the NATS server's certificate in the state bucket, which each agent reads at its heartbeat.
const RenewRequestSuffix = "join.renew"
RenewRequestSuffix is the NATS subject suffix of the renewal of an agent's NATS user in operator mode: a request that the controller replies to with a JoinReply that has a new JWT.
const RotateRequestSuffix = "join.rotate"
RotateRequestSuffix is the NATS subject suffix of the rotation of an agent's keys: a request that the controller replies to with a RotateReply.
Variables ¶
var EncryptionKeyEncoding = base64.RawURLEncoding
EncryptionKeyEncoding is the text form of a public encryption key.
var ErrNoKeys = errors.New("the keys are not on this host")
ErrNoKeys is a Load of keys that are not on the host.
Functions ¶
func CertFingerprint ¶
CertFingerprint is the SHA-256 of the first certificate in pemData, as SHA256:<hex>, or empty when it has none.
func CertState ¶
CertState is why a certificate could not be read: not made when the file is not there, or unreadable with the reason, such as a permission that only root has.
func EncodeEncryptionKey ¶
EncodeEncryptionKey is pub in EncryptionKeyEncoding.
func FingerprintOf ¶
FingerprintOf returns the SHA256 fingerprint of the given public key in the format "SHA256:<hex>". Returns an empty string when the key is empty, so a record without a key never reports a fingerprint.
Callers that hold a bare public key rather than a Manager use this, which keeps one digest format across the codebase.
func FingerprintOfKeys ¶
FingerprintOfKeys returns the fingerprint that covers an agent's two public keys: the SHA-256 of the 32 bytes of the Ed25519 key, then the 65 bytes of the P-256 key, as "SHA256:<hex>". An operator who admits it admits both keys. It is empty when the signing key is empty or the encryption key is not a public P-256 key, so a record without both keys never reports a fingerprint that admits it.
func ParseEncryptionKey ¶
ParseEncryptionKey is the public P-256 key that key holds in EncryptionKeyEncoding.
func ParseEncryptionKeyFile ¶
func ParseEncryptionKeyFile( data []byte, ) (*ecdh.PrivateKey, error)
ParseEncryptionKeyFile is the P-256 key in data, the PKCS #8 PEM block of an {prefix}.ekey file.
func ParsePublicKeyPEM ¶
ParsePublicKeyPEM parses a PEM-encoded Ed25519 public key.
func ValidEncryptionKey ¶
ValidEncryptionKey reports whether key is a public P-256 key in EncryptionKeyEncoding.
Types ¶
type Cert ¶
type Cert struct {
Name string `json:"name"`
State string `json:"state"`
Component string `json:"component"`
Source string `json:"source"`
File string `json:"file"`
Names []string `json:"names,omitempty"`
Expires time.Time `json:"expires,omitzero"`
DaysLeft *int `json:"days_left,omitempty"`
Fingerprint string `json:"fingerprint,omitempty"`
Key string `json:"key,omitempty"`
Signature string `json:"signature,omitempty"`
FIPS *bool `json:"fips_approved,omitempty"`
}
Cert is what a certificate of a host is: osapi pki cert reads it on the host, and the controller and each agent report theirs through the API.
type CertRecord ¶
type CertRecord struct {
// Serial grows by one with each record that the controller writes.
Serial uint64 `json:"serial"`
// Name is the certificate's, as list shows it, such as nats.
Name string `json:"name"`
// Current is the PEM certificate that the server serves.
Current []byte `json:"current"`
// Next is the PEM certificate that the server switches to, during a
// rotation.
Next []byte `json:"next,omitempty"`
// StartedAt is when the rotation in progress started.
StartedAt string `json:"started_at,omitempty"`
// Signature is the controller key's Ed25519 signature of the record.
Signature []byte `json:"signature"`
}
CertRecord is the state of the rotation of a server's certificate that the agents trust: the NATS server's self-signed one, which each agent pins. The controller's key signs it, so an agent takes only a record that a controller key it trusts signed. A record with a Next is a rotation in progress: each agent adds Next to the certificates that it trusts, and its registration reports it. After the switch, Current is the new certificate, which the server serves, and it has no Next.
func (CertRecord) Rotating ¶
func (r CertRecord) Rotating() bool
Rotating reports whether a rotation is in progress: the record has a next certificate that is not its current one.
func (*CertRecord) Sign ¶
func (r *CertRecord) Sign( priv ed25519.PrivateKey, )
Sign signs the record with priv, the controller's private key.
func (CertRecord) SignedWith ¶
func (r CertRecord) SignedWith( verify func(data []byte, signature []byte) bool, ) bool
SignedWith reports whether verify, such as the VerifyWithGrace of an agent's keys, accepts the signature.
type CertSource ¶
type CertSource struct {
Name string
Component string
CertFile string
KeyFile string
SelfSigned bool
}
CertSource is a certificate that a server of osapi serves: its name, the component that serves it, its files, and whether osapi made it.
type ControllerKeyRecord ¶
type ControllerKeyRecord struct {
// Serial grows by one with each record that the controller writes.
Serial uint64 `json:"serial"`
// Current is the public key that the controller signs with.
Current []byte `json:"current"`
// Next is the public key that the controller switches to, during a
// rotation.
Next []byte `json:"next,omitempty"`
// Previous is the public key that the last switch replaced.
Previous []byte `json:"previous,omitempty"`
// StartedAt is when the rotation in progress started.
StartedAt time.Time `json:"started_at,omitzero"`
// SwitchedAt is when the last switch was.
SwitchedAt time.Time `json:"switched_at,omitzero"`
// Signature is the Ed25519 signature of the record by its signer.
Signature []byte `json:"signature"`
}
ControllerKeyRecord is the state of the rotation of the controller's key. The key that is current when the controller writes it signs it: an agent takes only a record that a controller key it trusts signed. A record with a Next is a rotation in progress, which the current key signs. After the switch, Current is the new key, which signs the record, and Previous is the old one.
func (ControllerKeyRecord) Rotating ¶
func (r ControllerKeyRecord) Rotating() bool
Rotating reports whether a rotation is in progress: the record has a next key that is not its current one.
func (*ControllerKeyRecord) Sign ¶
func (r *ControllerKeyRecord) Sign( priv ed25519.PrivateKey, )
Sign signs the record with priv, the private key of its signer.
type EnrollmentRequest ¶
type EnrollmentRequest struct {
MachineID string `json:"machine_id"`
Hostname string `json:"hostname"`
PublicKey []byte `json:"public_key"`
EncryptionKey string `json:"encryption_key"`
Fingerprint string `json:"fingerprint"`
}
EnrollmentRequest is sent by the agent to the controller. It carries both public keys: the Ed25519 signing key and the P-256 encryption key, in EncryptionKeyEncoding.
type EnrollmentResponse ¶
type EnrollmentResponse struct {
Accepted bool `json:"accepted"`
ControllerPublicKey []byte `json:"controller_public_key,omitempty"`
Reason string `json:"reason,omitempty"`
}
EnrollmentResponse is sent by the controller to the agent.
type JoinNATS ¶
type JoinNATS struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
// JWT is, in operator mode, the user JWT of the agent's NATS user key.
JWT string `json:"jwt,omitempty"`
// Callout is, with a NATS server's auth callout, that the agent
// connects with its own NATS user key, which the callout admits.
Callout bool `json:"callout,omitempty"`
}
JoinNATS is the NATS user of an agent, which the join gives it.
type JoinReply ¶
type JoinReply struct {
ControllerPublicKey ed25519.PublicKey `json:"controller_public_key,omitempty"`
NATS JoinNATS `json:"nats"`
// Labels are the labels of the join token, which the agent takes.
Labels map[string]string `json:"labels,omitempty"`
Error string `json:"error,omitempty"`
}
JoinReply is the controller's reply to a join over NATS: its public key, and the NATS user that the agent connects with after the join. Error is why the join did not admit, and the other fields are empty then.
type JoinRequest ¶
type JoinRequest struct {
Token string
MachineID string
Hostname string
PublicKey ed25519.PublicKey
EncryptionKey string
// NATSUserKey is the public key (U...) of the agent's own NATS user, for
// a join in operator mode: the controller mints a JWT for it. The seed
// stays with the agent.
NATSUserKey string `json:",omitempty"`
Signature []byte
}
JoinRequest is what an agent sends to POST /api/agent/join: the join token, who it is, and its two public keys, signed with its signing key. The signature shows that the agent holds the key that it asks the controller to admit.
func (*JoinRequest) Sign ¶
func (r *JoinRequest) Sign( priv ed25519.PrivateKey, )
Sign signs r with priv, the private key of r.PublicKey.
func (JoinRequest) Verify ¶
func (r JoinRequest) Verify() bool
Verify reports whether r's signature is by the private key of r.PublicKey.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager handles Ed25519 keypair lifecycle.
func New ¶
New creates a PKI Manager with the given filesystem, key directory path, and key prefix (e.g., "agent" or "controller"). The prefix determines the key filenames: {prefix}.key and {prefix}.pub, and {prefix}.ekey when the manager has an encryption key (WithEncryptionKey).
func (*Manager) ControllerPublicKey ¶
ControllerPublicKey returns the stored controller public key.
func (*Manager) Drop ¶
Drop removes the manager's keys from the host: the keys of a rotation that the controller refused.
func (*Manager) EncryptionKey ¶
func (m *Manager) EncryptionKey() *ecdh.PrivateKey
EncryptionKey returns the P-256 ECDH key, or nil when the manager has none.
func (*Manager) EncryptionPublicKey ¶
EncryptionPublicKey returns the public P-256 key in EncryptionKeyEncoding, or an empty string when the manager has none.
func (*Manager) Fingerprint ¶
Fingerprint returns the SHA256 fingerprint of the manager's public keys in the format "SHA256:<hex>". A manager with an encryption key covers both keys (FingerprintOfKeys); one without covers the signing key alone. Returns an empty string if no public key is set.
func (*Manager) Load ¶
Load loads the manager's keys, which must be on the host: it makes none. A key that is missing is ErrNoKeys.
func (*Manager) LoadOrGenerate ¶
LoadOrGenerate loads existing PEM-encoded Ed25519 keys from disk, or generates and saves a new keypair when neither file exists. A key file it cannot read, or one half of a pair, is an error rather than a reason to generate: a new key is not the one the controller accepted, so replacing it drops the host out of targeting until an operator enrolls it again.
A manager made WithEncryptionKey then loads or generates its P-256 encryption key by the same rules.
func (*Manager) Next ¶
Next is the manager of the keys that a rotation makes, beside the manager's own: the ones that an earlier rotation made and the controller did not take, or new ones.
func (*Manager) NextControllerPublicKey ¶
NextControllerPublicKey is the key that the controller switches to, as its rotation record gave it, or nil.
func (*Manager) PreviousControllerPublicKey ¶
PreviousControllerPublicKey returns the previous controller public key, if any. This is the key that was current before the last rotation.
func (*Manager) PreviousEncryptionKey ¶
func (m *Manager) PreviousEncryptionKey() *ecdh.PrivateKey
PreviousEncryptionKey returns the encryption key that the last Reload replaced, or nil when no Reload replaced one.
func (*Manager) PrivateKey ¶
func (m *Manager) PrivateKey() ed25519.PrivateKey
PrivateKey returns the Ed25519 private key.
func (*Manager) Promote ¶
Promote moves the keys of next, from Next, in place of the manager's own, and loads them. The encryption key moves first and the public key last, so a Reload during the renames sees the old pair or the new one.
func (*Manager) Reload ¶
Reload reads the manager's keys again, and reports whether they changed: a rotation replaced them while the process ran. The keys stay as they were when the files are not a whole set, such as during the renames of a rotation; a later Reload takes them.
func (*Manager) RotateControllerKey ¶
RotateControllerKey makes newKey the controller key: the current one becomes the previous one, which still verifies, and the next one is cleared.
func (*Manager) SetControllerPublicKey ¶
SetControllerPublicKey stores the controller's public key for verifying controller-signed messages.
func (*Manager) SetNextControllerPublicKey ¶
SetNextControllerPublicKey sets the key that the controller switches to. Nil clears it.
func (*Manager) SetPreviousControllerPublicKey ¶
SetPreviousControllerPublicKey sets the previous controller key, such as the one that a controller read from its rotation record at its start.
func (*Manager) Sign ¶
Sign signs the given data with the manager's private key and returns the signature.
func (*Manager) VerifyWithGrace ¶
VerifyWithGrace checks the signature against each controller key that the manager accepts: the current one, the previous one, which signed the jobs that a rotation of the controller's key left in the queue, and the next one, which the controller signs with once it switches.
type Option ¶
type Option func(*Manager)
Option configures a Manager.
func WithEncryptionKey ¶
func WithEncryptionKey() Option
WithEncryptionKey gives the manager a P-256 ECDH encryption key next to its Ed25519 signing key, in {prefix}.ekey. The agent has one; the controller does not.
type RenewRequest ¶
RenewRequest is what an admitted agent sends to renew the JWT of its NATS user: who it is, its user key, and when, signed with its signing key.
func (*RenewRequest) Sign ¶
func (r *RenewRequest) Sign( priv ed25519.PrivateKey, )
Sign signs r with priv, the agent's signing key.
type RotateReply ¶
type RotateReply struct {
Fingerprint string `json:"fingerprint,omitempty"`
Error string `json:"error,omitempty"`
}
RotateReply is the controller's answer to a RotateRequest: the fingerprint that the admission holds now, or why the controller refused.
type RotateRequest ¶
type RotateRequest struct {
MachineID string `json:"machine_id"`
// PublicKey is the new Ed25519 signing key.
PublicKey ed25519.PublicKey `json:"public_key"`
// EncryptionKey is the new P-256 key, in EncryptionKeyEncoding.
EncryptionKey string `json:"encryption_key"`
At int64 `json:"at"`
// Signature is the signature of the current key.
Signature []byte `json:"signature"`
// NewSignature is the signature of the new key.
NewSignature []byte `json:"new_signature"`
}
RotateRequest is what an admitted agent sends to replace its two keys: who it is, its new public keys, and when. Its current signing key signs it, which the controller checks against the admission, and its new signing key signs it too, which shows that the agent holds the new key.
func (*RotateRequest) Sign ¶
func (r *RotateRequest) Sign( current ed25519.PrivateKey, next ed25519.PrivateKey, )
Sign signs r with current, the agent's signing key, and with next, the new key that r carries.
func (RotateRequest) Verify ¶
func (r RotateRequest) Verify( pub ed25519.PublicKey, ) bool
Verify reports whether pub, the key that the admission holds, signed r, and whether the new key that r carries signed it too.
func (RotateRequest) VerifyNew ¶
func (r RotateRequest) VerifyNew() bool
VerifyNew reports whether the new key that r carries signed it.