Documentation
¶
Overview ¶
Package identity persists the node's enrollment result in the state directory:
node.key ECDSA P-256 private key, PKCS#8 PEM, mode 0600
node.crt node client certificate issued by the internal CA
ca.crt internal CA certificate (verified against the pin)
identity.json node id, cluster id, name and console address
run.lock held (flock) by `edgeweir-node run` while it runs
identity.lock held (flock) while the console address is changed
enroll.lock held (flock) by `edgeweir-node enroll` while it enrolls
enroll.key the key of an enrollment not completed yet, mode 0600
decommissioned written when the console decommissioned the node; the
agent does not start while it exists, an enrollment
removes it
enroll.key is written before the token and CSR are sent and reused by every retry until an enrollment is saved (then it is node.key): an attempt whose answer was lost (the SSH session or the connection died after the console issued the certificate) is retried with the same public key, which lets the console answer with the node it already created instead of a second one.
identity.json is written last and is the "enrolled" marker; the other files are always complete when it exists. Every file is replaced atomically (temp file, fsync, rename, fsync dir).
A probe (`edgeweir-node probe`) keeps its identity in its own state directory with the same rules: probe.key (0600), probe.crt, ca.crt and probe.json (probe id, name, region and console address).
Index ¶
- Constants
- Variables
- func SameIdentity(a, b Identity) bool
- func SameServer(a, b Identity) bool
- type Identity
- type Loaded
- type Store
- func (s Store) CertFile() string
- func (s Store) Enrolled() bool
- func (s Store) EnsureDir() error
- func (s Store) IdentityFile() string
- func (s Store) KeyFile() string
- func (s Store) Load() (*Loaded, error)
- func (s Store) LockEnroll() (release func(), err error)
- func (s Store) LockRun() (release func(), err error)
- func (s Store) Path(name string) string
- func (s Store) PendingKey() (key crypto.Signer, reused bool, err error)
- func (s Store) ReadIdentity() (*Identity, error)
- func (s Store) Remove() error
- func (s Store) Save(id Identity, keyPEM, certPEM, caPEM []byte) error
- func (s Store) SwapCertificate(keyPEM, certPEM []byte) error
- func (s Store) UpdateServer(expect Identity, update func(*Identity)) (*Identity, error)
- func (s Store) WaitForEnrollment(ctx context.Context, interval time.Duration, log *slog.Logger) error
Constants ¶
const ( KeyFile = "node.key" CertFile = "node.crt" CAFile = "ca.crt" IdentityFile = "identity.json" // ConfigDir holds the last-known-good configuration (see configstore). ConfigDir = "config" // RunLockFile is locked by a running agent (LockRun). RunLockFile = "run.lock" // IdentityLockFile is locked while the console address in the identity // file is changed (UpdateServer). IdentityLockFile = "identity.lock" // EnrollLockFile is locked by a running enrollment (LockEnroll). EnrollLockFile = "enroll.lock" // PendingKeyFile is the key of an enrollment not completed yet. PendingKeyFile = "enroll.key" // DecommissionedFile marks a node the console decommissioned. DecommissionedFile = "decommissioned" // The probe layout (Store.Probe). ProbeKeyFile = "probe.key" ProbeCertFile = "probe.crt" ProbeIdentityFile = "probe.json" )
File names inside the state directory.
const ( ServerSourceConsole = "console" ServerSourceCommand = "command" )
How Identity.ServerURL was set; empty means the enrollment.
Variables ¶
var ErrEnrolling = errors.New("another enrollment is running with this state directory")
ErrEnrolling is returned by LockEnroll when another enrollment runs with the state directory.
var ErrIdentityReplaced = errors.New("the identity was replaced meanwhile (enrolled again)")
ErrIdentityReplaced is returned by UpdateServer when the identity file names another enrollment than the one expected.
var ErrNotEnrolled = errors.New("node is not enrolled")
ErrNotEnrolled is returned by Load when identity.json does not exist.
var ErrProbeNotEnrolled = errors.New("probe is not enrolled")
ErrProbeNotEnrolled is returned by Load when probe.json does not exist.
var ErrRunning = errors.New("an edgeweir-node agent is running with this state directory")
ErrRunning is returned by LockRun when an agent already runs with the state directory.
Functions ¶
func SameIdentity ¶ added in v1.2.0
SameIdentity reports whether a and b name the same enrollment (the console address aside).
func SameServer ¶ added in v1.2.0
SameServer reports whether a and b carry the same console addresses.
Types ¶
type Identity ¶
type Identity struct {
NodeID string `json:"node_id,omitempty"`
ClusterID string `json:"cluster_id,omitempty"`
NodeName string `json:"node_name,omitempty"`
ProbeID string `json:"probe_id,omitempty"`
ProbeName string `json:"probe_name,omitempty"`
// RegionID is the region a probe measures from.
RegionID string `json:"region_id,omitempty"`
ServerURL string `json:"server_url"`
ServerName string `json:"server_name"`
CASHA256 string `json:"ca_sha256"`
EnrolledAt time.Time `json:"enrolled_at"`
// The console address used before the last change of ServerURL (the
// agent tries it while ServerURL fails), how ServerURL was set
// (ServerSource*) and when. Empty until the address first changes.
FallbackServerURL string `json:"fallback_server_url,omitempty"`
FallbackServerName string `json:"fallback_server_name,omitempty"`
ServerSource string `json:"server_source,omitempty"`
ServerChangedAt time.Time `json:"server_changed_at,omitzero"`
}
Identity is the non-secret enrollment metadata: the node fields for a node, the probe fields for a probe.
type Loaded ¶
type Loaded struct {
Identity
Key crypto.Signer
Certificate *x509.Certificate
TLS tls.Certificate
CA *x509.Certificate
CAPool *x509.CertPool
}
Loaded is a fully parsed identity ready to build TLS configurations.
type Store ¶
type Store struct {
Dir string
// Probe selects the probe layout (probe.key, probe.crt, probe.json).
Probe bool
}
Store reads and writes identity files in a state directory.
func (Store) IdentityFile ¶
func (Store) Load ¶
Load reads and validates the identity. A certificate swap interrupted by a crash (see SwapCertificate) is completed transparently.
func (Store) LockEnroll ¶ added in v1.7.0
LockEnroll takes the enrollment lock of the state directory (EnrollLockFile, flock): two enrollments of one host at the same time (a retried batch install while the first attempt still runs) would both use a token and leave the console with two nodes. It fails with ErrEnrolling when another process holds it.
func (Store) LockRun ¶
LockRun takes the run lock of the state directory (RunLockFile, flock): `edgeweir-node run` holds it while it uses the identity, `enroll --force` while it replaces one, so that an identity is never replaced under a running agent. It fails with ErrRunning when another process holds it.
func (Store) PendingKey ¶ added in v1.7.0
PendingKey returns the key of an enrollment not completed yet (PendingKeyFile), or creates and stores a new one. reused tells which. An unreadable pending key is replaced.
func (Store) ReadIdentity ¶
ReadIdentity reads only identity.json (probe.json) without keys or certificates.
func (Store) Remove ¶
Remove deletes the identity and the cached configuration (used by `enroll --force`). The identity marker is removed first.
func (Store) Save ¶
Save writes key, certificate, CA and identity (in that order, identity last) atomically.
func (Store) SwapCertificate ¶
SwapCertificate installs a renewed key pair. Both files are staged as *.new first, then renamed key-first; Load repairs an interrupted swap.
func (Store) UpdateServer ¶ added in v1.2.0
UpdateServer changes the console addresses of the identity file under the identity lock: update gets the identity as stored, and the file is replaced atomically with what it leaves. The other fields (and the keys) stay as they are; expect is the enrollment the caller loaded.