ctlkeys

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.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 ctlkeys is the key store of mashctl: the account signing seed (and the operator and system keys of a server `mashctl init --local` stood up), kept as 0600 files in a 0700 directory (ADR 0052). Seed text is never put in an error or a log line.

Index

Constants

View Source
const OperatorJWTFile = "operator.jwt"

OperatorJWTFile is the name, in the key directory, of the copy of the operator JWT that tells mashctl which keys the operator accepts as issuers of an account JWT.

Variables

View Source
var (
	// ErrNoOperatorKey is returned by Operator when operator.nk is absent.
	ErrNoOperatorKey = errors.New("mashctl: no operator key (operator.nk): it exists only for a server `mashctl init --local` stood up")
	// ErrNoSystemKey is returned by System when system.nk is absent.
	ErrNoSystemKey = errors.New("mashctl: no system account key (system.nk): it exists only for a server `mashctl init --local` stood up")
	// ErrNoAccountKey is returned by Account when the account identity key (account.nk) is absent.
	ErrNoAccountKey = errors.New("mashctl: no account identity key (account.nk): run `mashctl init`")
	// ErrNoSigningKey is returned by Signing when signing.nk is absent: the account has no separate signing key
	// configured, and endpoint users are signed by the account identity key.
	ErrNoSigningKey = errors.New("mashctl: no account signing key configured (signing.nk)")
	// ErrSigningKeyLost is returned by Signing when the store records a configured signing key (signing.pub) but
	// signing.nk is missing, unreadable or another key. The identity key must not stand in for it: a strict server
	// rejects what it signs.
	ErrSigningKeyLost = errors.New("mashctl: the account signing key was configured but signing.nk is missing, unreadable or not that key")
)
View Source
var ErrDifferentKey = errors.New("mashctl: the key store already holds a different key")

ErrDifferentKey is wrapped by ImportOperator and ImportAccount when the store already holds another key.

View Source
var ErrDifferentOperatorJWT = errors.New("mashctl: the key directory already holds a different operator.jwt")

ErrDifferentOperatorJWT is wrapped by InstallOperatorJWT when the key directory already holds another operator JWT.

View Source
var ErrLockBusy = errors.New("another process holds the lock")

ErrLockBusy is the error of LockFile when another holder keeps the lock past the wait.

View Source
var ErrLocked = errors.New("another mashctl command that writes the account JWT holds the key directory lock")

ErrLocked is the error of Lock when another holder keeps the lock past the wait.

View Source
var ErrNoGroupKey = errors.New("mashctl: no signing key for owner group")

ErrNoGroupKey is wrapped by GroupKey when the owner group has no key file and create is false.

View Source
var ErrSigningChanged = errors.New("signing_key changed while init ran: the key to import is not the one that was checked")

ErrSigningChanged is ImportSigningChecked finding a seed whose public key is not the one the caller checked.

Functions

func CheckOwned added in v0.5.0

func CheckOwned(fi fs.FileInfo, path, what string) error

CheckOwned refuses a file or directory (described by fi, named path, called what in the error) that the effective uid does not own: the owner check the key store applies to its own files, for a caller that holds a file of the same custody. It is a no-op where the platform reports no owner.

func CheckParentDir

func CheckParentDir(d string) error

CheckParentDir refuses a directory that is not a real directory (a symlink is refused), is not owned by the effective uid, or is writable by other users or by its group (unless the group-writable directory is sticky): a place where a creds file may be created without another user being able to swap or replace it. A missing directory is an error wrapping fs.ErrNotExist.

func CheckStrictParentDir added in v0.5.0

func CheckStrictParentDir(d string) error

CheckStrictParentDir is the rule for a directory that holds a file deciding who may be issued a credential (the list set the policy engine reads): a real directory (a symlink is refused) that the effective uid owns and that no group or other user can write (mode & 0022 == 0). Unlike CheckParentDir it refuses a group-writable directory too, because another member of the group could replace the file by rename. A missing directory is an error wrapping fs.ErrNotExist.

func DirFor

func DirFor(cfg config.Config, env func(string) string) (string, error)

DirFor is the key directory: $MASH_KEYS_DIR when absolute (a relative one is an error), else <cfg.Dir>/keys.

func InstallFile

func InstallFile(path string, data []byte) (created bool, err error)

InstallFile writes data to path with the custody rules of the key files: a 0600 temp file in the same directory (O_EXCL), fsync, a hard link that cannot replace an existing file, a sync of the directory. An existing path is left untouched and reported with created false; the caller compares its content. The directory must exist.

func LockFile added in v0.5.0

func LockFile(path string, wait time.Duration) (unlock func(), err error)

LockFile takes an exclusive advisory lock on the file at path, creating it 0600 if it is missing, waiting up to wait for another holder, and returns the function that releases it (safe to call once, on every exit path). The file is opened without following a symlink and must be a regular file owned by the effective uid. The caller checks the directory. A holder that outlasts wait yields an error wrapping ErrLockBusy. On platforms without flock it is a no-op. It is the one locking scheme of the key store and of the files that share its custody.

func ParseAccountSeed

func ParseAccountSeed(path string) (nkeys.KeyPair, string, error)

ParseAccountSeed is ParseOperatorSeed for an account identity seed (account prefix).

func ParseOperatorSeed

func ParseOperatorSeed(path string) (nkeys.KeyPair, string, error)

ParseOperatorSeed reads the operator seed file at path (0600, a regular file, an operator-prefix seed) and returns the key pair and its public key. The seed is never put in an error.

func ParseSigningSeed added in v0.5.7

func ParseSigningSeed(path string) (nkeys.KeyPair, string, error)

ParseSigningSeed is ParseAccountSeed for the account signing seed (an account-prefix seed).

func ReadOwnedFile added in v0.5.6

func ReadOwnedFile(path, what string) ([]byte, error)

ReadOwnedFile reads the credentials file at path under the key store's rules: it is opened once with O_NOFOLLOW (a symlink is refused), every check runs on that descriptor (a regular file, owned by the effective uid, no wider than mode 0600), and the bytes come from the same descriptor. what names the file in an error ("system_creds"). The error carries the path, never content.

func SetSyncDirHook

func SetSyncDirHook(h func(string) error) (restore func())

SetSyncDirHook replaces the directory sync that InstallFile and SyncDir use and returns the function that restores it. It exists so tests in other packages can inject a failure.

func SyncDir

func SyncDir(path string) error

SyncDir fsyncs the directory at path so a new or renamed entry in it survives power loss.

Types

type Authority

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

Authority is the key store rooted at one directory.

func Open

func Open(dir, expectSigningPub string) (*Authority, error)

Open opens the key store at dir. It creates nothing; a missing dir is an empty store.

expectSigningPub is config.json's signing_key_pub, the public key of the signing key mashctl init was given ("" when the config records none). When it is set, Signing refuses, as ErrSigningKeyLost, unless signing.nk and signing.pub both hold exactly that key: the identity key never stands in for a signing key that was configured.

func (*Authority) Account

func (a *Authority) Account() (nkeys.KeyPair, string, error)

Account returns the account identity key pair (account.nk) and its public key.

func (*Authority) Dir

func (a *Authority) Dir() string

Dir is the key directory.

func (*Authority) GroupKey added in v0.5.0

func (a *Authority) GroupKey(group string, create bool) (nkeys.KeyPair, error)

GroupKey returns the scoped signing key of owner group "owner.group", kept in groups/<owner>.<group>.nk under the same custody rules as the other keys; with create it generates and writes one if absent. It never overwrites.

func (*Authority) GroupPublics added in v0.5.0

func (a *Authority) GroupPublics() (map[string]string, error)

GroupPublics maps every owner group with a key file to the public key of that key. Each key pair is wiped after its public key is read; no seed leaves this function.

func (*Authority) Groups added in v0.5.0

func (a *Authority) Groups() ([]string, error)

Groups lists the owner groups that have a key file, sorted.

func (*Authority) ImportAccount

func (a *Authority) ImportAccount(path string) (pub string, created bool, err error)

ImportAccount is ImportOperator for the account identity seed (account.nk).

func (*Authority) ImportOperator

func (a *Authority) ImportOperator(path string) (pub string, created bool, err error)

ImportOperator copies the operator seed file at path into the store as operator.nk (0600, no clobber, fsync). The same key already stored is not an error (created is false); a different one is ErrDifferentKey and nothing changes.

func (*Authority) ImportSigning added in v0.5.7

func (a *Authority) ImportSigning(path string) (pub string, created bool, err error)

ImportSigning is ImportAccount for the account signing seed (signing.nk): same custody, never overwritten.

It also records the public key beside it (signing.pub, 0600, never replaced), so a later loss of signing.nk is told from "no signing key configured" (Signing). Importing the same key again is a no-op; a key that differs from the record is ErrDifferentKey and nothing changes.

func (*Authority) ImportSigningChecked added in v0.5.7

func (a *Authority) ImportSigningChecked(path, wantPub string) (pub string, created bool, err error)

ImportSigningChecked is ImportSigning for a caller that validated the file earlier: the file is read once, and a seed whose public key is not wantPub (when wantPub is not "") is ErrSigningChanged before anything is written.

func (*Authority) InitAccount

func (a *Authority) InitAccount() (pub string, created bool, err error)

InitAccount creates the account signing key if absent. It never overwrites an existing seed.

func (*Authority) InitOperator

func (a *Authority) InitOperator() (pub string, created bool, err error)

InitOperator creates the operator key (operator.nk) if absent; used by `mashctl init --local`. It never overwrites.

func (*Authority) InitSystem

func (a *Authority) InitSystem() (pub string, created bool, err error)

InitSystem creates the system account's identity key (system.nk) if absent. It never overwrites.

func (*Authority) InstallOperatorJWT

func (a *Authority) InstallOperatorJWT(token string) (created bool, err error)

InstallOperatorJWT stores token as <keydir>/operator.jwt (0600, never replacing a file). With a file already in place, created is false and the error says so when its content differs. The token is not validated here.

func (*Authority) Lock

func (a *Authority) Lock(wait time.Duration) (unlock func(), err error)

Lock takes an exclusive advisory lock on <keydir>/apply.lock (0600), waiting up to wait for another holder, and returns the function that releases it. The lock covers appliers that share this key directory; it does nothing for copies of the keys on other hosts, and on platforms without flock it is a no-op.

func (*Authority) Operator

func (a *Authority) Operator() (nkeys.KeyPair, string, error)

Operator returns the operator key pair and its public key.

func (*Authority) OperatorJWT

func (a *Authority) OperatorJWT() (string, error)

OperatorJWT returns the operator JWT stored in the key directory, or "" when there is none.

func (*Authority) Signing added in v0.5.7

func (a *Authority) Signing() (nkeys.KeyPair, string, error)

Signing returns the account SIGNING key pair (signing.nk) and its public key: the key that signs endpoint users when the operator requires signing keys (strict_signing_key_usage). It is ErrNoSigningKey when none was imported; Account is then the signer.

When the store holds the record of a configured signing key (signing.pub, written by ImportSigning) and signing.nk is missing, unreadable or another key, it is ErrSigningKeyLost, never ErrNoSigningKey.

func (*Authority) SigningRecord added in v0.5.7

func (a *Authority) SigningRecord() (string, error)

SigningRecord is the public key recorded in signing.pub by ImportSigning, "" when there is no record.

func (*Authority) System

func (a *Authority) System() (nkeys.KeyPair, string, error)

System returns the system account's identity key pair and its public key.

type SeedContentError added in v0.5.7

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

SeedContentError is a seed file that was read but does not hold the seed it should: unlike a mode or ownership refusal, the file's content is what is wrong.

func (*SeedContentError) Error added in v0.5.7

func (e *SeedContentError) Error() string

type SigningInvalidError added in v0.5.9

type SigningInvalidError struct {
	Dir string
	Err error
}

SigningInvalidError is what Signing returns when signing.nk holds no valid signing key. Err is the cause. It is an ErrSigningKeyLost.

func (*SigningInvalidError) As added in v0.5.9

func (e *SigningInvalidError) As(target any) bool

As gives the error's cliframe form.

func (*SigningInvalidError) Error added in v0.5.9

func (e *SigningInvalidError) Error() string

func (*SigningInvalidError) Is added in v0.5.9

func (e *SigningInvalidError) Is(target error) bool

Is makes the error an ErrSigningKeyLost.

func (*SigningInvalidError) Unwrap added in v0.5.9

func (e *SigningInvalidError) Unwrap() error

type SigningKeyMismatchError added in v0.5.9

type SigningKeyMismatchError struct{ Held, Want, WantFrom, Dir string }

SigningKeyMismatchError is what Signing returns when signing.nk holds another key than the one wanted. Want is the key signing.pub records, or, when signing.pub is gone, the one config.json records; WantFrom names which. It is an ErrSigningKeyLost.

func (*SigningKeyMismatchError) As added in v0.5.9

func (e *SigningKeyMismatchError) As(target any) bool

As gives the error's cliframe form.

func (*SigningKeyMismatchError) Error added in v0.5.9

func (e *SigningKeyMismatchError) Error() string

func (*SigningKeyMismatchError) Is added in v0.5.9

func (e *SigningKeyMismatchError) Is(target error) bool

Is makes the error an ErrSigningKeyLost.

type SigningLostBothError added in v0.5.9

type SigningLostBothError struct{ Pub, Dir string }

SigningLostBothError is what Signing returns when config.json records a configured signing key (signing_key_pub) and the key store holds neither signing.nk nor signing.pub. It is an ErrSigningKeyLost: the identity key must not stand in for the signing key.

func (*SigningLostBothError) As added in v0.5.9

func (e *SigningLostBothError) As(target any) bool

As gives the error's cliframe form.

func (*SigningLostBothError) Error added in v0.5.9

func (e *SigningLostBothError) Error() string

func (*SigningLostBothError) Is added in v0.5.9

func (e *SigningLostBothError) Is(target error) bool

Is makes the error an ErrSigningKeyLost.

type SigningNkMissingError added in v0.5.9

type SigningNkMissingError struct{ Recorded, Dir string }

SigningNkMissingError is what Signing returns when signing.pub records a signing key and signing.nk is gone. Recorded is the key signing.pub records. It is an ErrSigningKeyLost.

func (*SigningNkMissingError) As added in v0.5.9

func (e *SigningNkMissingError) As(target any) bool

As gives the error's cliframe form.

func (*SigningNkMissingError) Error added in v0.5.9

func (e *SigningNkMissingError) Error() string

func (*SigningNkMissingError) Is added in v0.5.9

func (e *SigningNkMissingError) Is(target error) bool

Is makes the error an ErrSigningKeyLost.

type SigningPubMissingError added in v0.5.9

type SigningPubMissingError struct{ Pub, Dir string }

SigningPubMissingError is what Signing returns when signing.nk is in the key store and signing.pub is gone. Pub is the key signing.nk holds. It is an ErrSigningKeyLost.

func (*SigningPubMissingError) As added in v0.5.9

func (e *SigningPubMissingError) As(target any) bool

As gives the error's cliframe form.

func (*SigningPubMissingError) Error added in v0.5.9

func (e *SigningPubMissingError) Error() string

func (*SigningPubMissingError) Is added in v0.5.9

func (e *SigningPubMissingError) Is(target error) bool

Is makes the error an ErrSigningKeyLost.

type SigningRecordMismatchError added in v0.5.9

type SigningRecordMismatchError struct{ Expected, Recorded, Dir string }

SigningRecordMismatchError is what Signing returns when signing.pub records another key than config.json's signing_key_pub. It is an ErrSigningKeyLost.

func (*SigningRecordMismatchError) As added in v0.5.9

func (e *SigningRecordMismatchError) As(target any) bool

As gives the error's cliframe form.

func (*SigningRecordMismatchError) Error added in v0.5.9

func (*SigningRecordMismatchError) Is added in v0.5.9

func (e *SigningRecordMismatchError) Is(target error) bool

Is makes the error an ErrSigningKeyLost.

type SigningUnreadableError added in v0.5.9

type SigningUnreadableError struct {
	Dir string
	Err error
}

SigningUnreadableError is what Signing returns when signing.nk is there and cannot be read (its mode, owner or type is refused, or reading it failed). Err is the cause. It is an ErrSigningKeyLost.

func (*SigningUnreadableError) As added in v0.5.9

func (e *SigningUnreadableError) As(target any) bool

As gives the error's cliframe form.

func (*SigningUnreadableError) Error added in v0.5.9

func (e *SigningUnreadableError) Error() string

func (*SigningUnreadableError) Is added in v0.5.9

func (e *SigningUnreadableError) Is(target error) bool

Is makes the error an ErrSigningKeyLost.

func (*SigningUnreadableError) Unwrap added in v0.5.9

func (e *SigningUnreadableError) Unwrap() error

Jump to

Keyboard shortcuts

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