Documentation
¶
Overview ¶
Package mutation is the guard every command that changes state goes through: the advisory lock that serializes sshmgr processes, the crash-residue sweep, and the ~/.ssh snapshot that makes the change reversible.
It moved here from the command layer so that something other than the CLI - the web server - can guard its changes the same way.
Index ¶
- Variables
- func Begin(s Scope)
- func End(o Outcome)
- func Exclusive(wait time.Duration) (release func(), err error)
- func Held() bool
- func InstallSink()
- func LockForMutation(p paths.Paths)
- func Release()
- func Scoped(p paths.Paths, s Scope, fn func() error) error
- func SnapshotBeforeMutation(p paths.Paths, retain int) string
- func SourcesFor(p paths.Paths) history.Sources
- func Suspend() func()
- type Outcome
- type Scope
Constants ¶
This section is empty.
Variables ¶
var ( // Disabled turns recording off. The tests that are not about history set // it, and so does $SSH_MANAGER_HISTORY=0. Disabled = os.Getenv("SSH_MANAGER_HISTORY") == "0" )
var ErrBusy = errors.New("another change is being made from this authama serve; wait for it to finish, then try again")
ErrBusy reports that another change is being made by this process: in a server, a preview, a confirmed change or a job.
var ErrDeclined = errors.New("declined")
ErrDeclined marks an error that means the person, or the state, said no: a scope that ends with it is recorded as aborted, not as a failure.
Functions ¶
func Begin ¶
func Begin(s Scope)
Begin starts a scope. Scopes nest: the TUI is one scope, and every action chosen from its menu is its own scope inside it, recorded separately.
func End ¶
func End(o Outcome)
End closes the innermost scope and, if it mutated, records how it ended and what the manifest and inventory were afterwards.
func Exclusive ¶
Exclusive waits up to wait for the process's one change slot, and returns the function that gives it back. A job can hold the slot for as long as it waits on a person, so callers wait a short while and then say why.
func InstallSink ¶
func InstallSink()
InstallSink routes audit events into the history. The command layer calls it once at start-up; util/log cannot import the store itself.
func LockForMutation ¶
LockForMutation takes the advisory lock, once per process. A mutating command calls it before it reads the manifest or inventory, not just before it writes: one that loaded state first and then waited for the lock saved that stale copy afterwards, silently dropping whatever the command it waited for had written. SnapshotBeforeMutation calls it too, so a command that forgets still holds the lock while it mutates.
func Release ¶
func Release()
Release gives up the lock this process holds, if any. A CLI command never needs to - exiting releases it - but a test that runs many commands in one process does, and so does a long-lived server between operations.
func Scoped ¶
Scoped runs fn as one operation under the lock and releases the lock when fn returns. It is for a long-lived process: a CLI command holds the lock until it exits, which a server cannot do without blocking every other sshmgr command for as long as it runs. fn must not call Scoped itself.
func SnapshotBeforeMutation ¶
SnapshotBeforeMutation is the native mutation guard (mirrors the Facade's _mutating): take the advisory lock so concurrent commands serialize, sweep crash residue, then snapshot ~/.ssh so the change is reversible. The lock is best- effort - a failure to acquire doesn't block the operation. Returns the snapshot path ("" if none was made).
The lock is taken once per process, not once per call. flock is per open file descriptor, and Acquire blocks until it is free, so a second call in the same process waits forever on a lock that same process is holding - a deadlock with no output at all. One CLI command mutates once and exits, which is why this went unnoticed; the TUI is one process that mutates repeatedly (reconcile, then pin, then rotate) and hangs on the second action. Nothing is given up by holding it: the lock exists to serialize separate processes, and within one process these run in sequence anyway.
func SourcesFor ¶
SourcesFor names what a backfill reads for the config home p.
func Suspend ¶
func Suspend() func()
Suspend stops recording until the returned function is called. A preview runs a change against a throwaway copy of the home, and what it does there is not something that happened. The caller must keep anything real from running meanwhile; change does, by previewing and applying one at a time.
Types ¶
type Scope ¶
type Scope struct {
Command string // "host add"
Args []string // already redacted
Source string // history.SourceCLI, SourceTUI, SourceWeb
DryRun bool
Subjects []history.Subject
OpID string // optional: the id to record under, so a caller can link to it
}
Scope is one command a person asked for. It becomes an operation in the history only if it mutates: the guard opens the operation the first time the command takes the lock, so a read-only command records nothing, and a mutating one is recorded without code of its own.