Documentation
¶
Overview ¶
Package editor does manifest editing - profile/host add·edit·delete. Edits go through the manifest (never a hand-edited config), are validated, and written atomically. Delete prunes the inventory deployment record so no dangling tracking is left behind.
Note: remote key REVOCATION on delete needs the provider adapters (gh/glab/REST), which are a later wave; until then revoke prunes local deployment tracking but does not call a remote (matching v1's behavior for manual/web-panel providers).
Index ¶
- Constants
- Variables
- func AdoptKey(e *Editor, profile, name, rel, hostAlias string) error
- func AnyAdded(res []ApplyResult) bool
- func FormatApply(profile string, newProfile bool, res []ApplyResult, preview bool) string
- func ParseLifetime(s string) (int, error)
- func SetKeyTemplate(e *Editor, profile, tpl string) ([]string, error)
- func SetNaming(e *Editor, profile string, edit func(*manifest.Naming)) ([]string, error)
- func SetProfileCode(e *Editor, profile, code string) ([]string, error)
- func SetSelection(e *Editor, name string, raw json.RawMessage) error
- func TakenAliases(m *manifest.Manifest) []string
- type ApplyAction
- type ApplyResult
- type CategoryResult
- type DeleteResult
- type Editor
- func (e *Editor) AddHost(profile, alias string, f HostFields) error
- func (e *Editor) AddKey(profile, name string, keyType *string, rotateAfterDays *int, hostAlias string) error
- func (e *Editor) AddProfile(name, keyScope string, keyName *string) error
- func (e *Editor) ApplyCreatingProfile(profile, category string, create bool, specs []HostSpec) ([]ApplyResult, error)
- func (e *Editor) ApplyHosts(profile string, specs []HostSpec, dryRun bool) ([]ApplyResult, error)
- func (e *Editor) DeleteHost(profile, alias string, revoke bool) (DeleteResult, error)
- func (e *Editor) DeleteKey(ref manifest.KeyRef, revoke bool) (DeleteResult, error)
- func (e *Editor) DeleteProfile(name string, revoke bool) (DeleteResult, error)
- func (e *Editor) EditHost(profile, alias string, f HostFields) error
- func (e *Editor) EditProfile(name string, keyScope *string, keyName *string) error
- func (e *Editor) ReserveKey(name string, r manifest.ReservedKey) error
- func (e *Editor) RevokeKey(ref manifest.KeyRef, reason string) (RevokeResult, error)
- func (e *Editor) SetCategory(name, category string) (CategoryResult, error)
- func (e *Editor) SetDefaultLifetime(days int) ([]LifetimeResult, error)
- func (e *Editor) SetKeyComment(name, tmpl string) error
- func (e *Editor) SetKeyLifetime(ref manifest.KeyRef, days int) (LifetimeResult, error)
- func (e *Editor) Unattended(w io.Writer) *Editor
- func (e *Editor) UnreserveKey(name string, missingOK bool) (bool, error)
- type HostFields
- type HostSpec
- type KeyRootResult
- type LifetimeResult
- type RevokeResult
Constants ¶
const ( MinLifetimeDays = 1 MaxLifetimeDays = 5 * 365 )
Lifetime bounds, in days. Five years is the longest any guidance gives a key used for authentication; a day is the shortest that leaves time to deploy.
Variables ¶
var LifetimePresets = map[string]int{"annual": 365, "semiannual": 180, "quarterly": 90, "monthly": 30}
LifetimePresets are the lifetimes people are asked for by policy: a year is the common floor for SSH user keys, and stricter regimes ask for half, a quarter, or a month.
Functions ¶
func AdoptKey ¶ added in v0.1.6
AdoptKey declares a key that already exists where the user keeps it - say ~/.ssh/id_ed25519 - as profile's key name, without moving or renaming it. rel is the private key's path relative to ~/.ssh. With hostAlias it is also wired to that host. The file must exist, be a private key, and resolve inside ~/.ssh (a symlink out of it is refused).
func AnyAdded ¶
func AnyAdded(res []ApplyResult) bool
AnyAdded reports whether the batch added a host.
func FormatApply ¶
func FormatApply(profile string, newProfile bool, res []ApplyResult, preview bool) string
FormatApply is the report `host apply` prints, for a dry run or a real one.
func ParseLifetime ¶ added in v0.1.6
ParseLifetime reads a lifetime as a preset name or a number of days.
func SetKeyTemplate ¶ added in v0.1.6
SetKeyTemplate makes tpl the key-name template, the manifest's when profile is "", else that profile's own, keeping every existing host's key name: only hosts added from now on are named by tpl. It returns the hosts it kept.
func SetNaming ¶ added in v0.1.6
SetNaming changes the naming rules through edit: the manifest's (defaults.naming) when profile is "", else that profile's own. Every existing host keeps its key name.
func SetProfileCode ¶ added in v0.1.6
SetProfileCode sets a profile's code name ("" clears it), keeping every existing host's key name.
func SetSelection ¶ added in v0.1.6
func SetSelection(e *Editor, name string, raw json.RawMessage) error
SetSelection keeps raw as the manifest's saved export selection name (defaults.export.selections), or removes it when raw is nil. Removing one the manifest does not have is an error, so a typo is not a silent no-op.
func TakenAliases ¶ added in v0.1.6
TakenAliases is every host alias in the manifest, for naming the next one.
Types ¶
type ApplyAction ¶
type ApplyAction string
ApplyAction is what ApplyHosts did (or would do) to one host.
const ( HostAdded ApplyAction = "added" HostUpdated ApplyAction = "updated" HostUnchanged ApplyAction = "unchanged" )
type ApplyResult ¶
type ApplyResult struct {
Alias string
Action ApplyAction
Changes []string
}
ApplyResult is one host's outcome; Changes lists the fields that moved.
type CategoryResult ¶
type CategoryResult struct {
From, To string // profile directories, relative to ~/.ssh
Moved bool // the key directory was renamed from From to To
RecordsUpdated int // inventory records re-pointed at the new location
}
CategoryResult reports what SetCategory changed on disk.
type DeleteResult ¶
type DeleteResult struct {
Removed string
Revoked []string
// NotRevoked are targets the key is deployed to that could not be revoked
// from - the provider cannot remove keys, or the call failed - so the key
// must be removed there by hand.
NotRevoked []string
PrunedKeys []string
}
DeleteResult is what one manifest-level deletion changed. It is data for the caller to fold into its own report - it carries no Format of its own, because the manifest half of a deletion is no longer the whole story: the lifecycle service composes this with the config re-render, the known_hosts pruning and whatever happened to the key files, and only it can describe the outcome truthfully.
type Editor ¶
type Editor struct {
// contains filtered or unexported fields
}
Editor edits the manifest at the resolved home.
func (*Editor) AddHost ¶
func (e *Editor) AddHost(profile, alias string, f HostFields) error
AddHost adds a host to a profile.
func (*Editor) AddKey ¶
func (e *Editor) AddKey(profile, name string, keyType *string, rotateAfterDays *int, hostAlias string) error
AddKey declares a key on a profile and, when hostAlias is set, points that host at it. Type and rotateAfterDays are optional overrides of the manifest defaults. It only edits the manifest - minting the key file is the caller's next step, so a failed edit never leaves a key on disk nothing refers to.
func (*Editor) AddProfile ¶
AddProfile adds a new profile. keyScope defaults to per_service.
func (*Editor) ApplyCreatingProfile ¶
func (e *Editor) ApplyCreatingProfile(profile, category string, create bool, specs []HostSpec) ([]ApplyResult, error)
ApplyCreatingProfile runs the batch, first creating the profile when asked. Creating it is a separate save, so if the batch then fails validation the manifest is put back exactly as it was - otherwise a refused batch left an empty profile behind. A new profile has no key directory yet, so restoring the manifest is the whole of the undo.
func (*Editor) ApplyHosts ¶
ApplyHosts upserts specs into a profile: a missing alias is added the way `host add` adds it, an existing one is edited the way `host edit` edits it (hostname, user, port when given) and has its role/tags merged in - never removed. The whole batch is validated by the manifest's own rules and saved once, so a bad spec leaves the manifest untouched. dryRun reports without saving.
func (*Editor) DeleteHost ¶
func (e *Editor) DeleteHost(profile, alias string, revoke bool) (DeleteResult, error)
DeleteHost removes a host, revoking/pruning its key.
func (*Editor) DeleteKey ¶
DeleteKey removes a key's declaration from its profile and drops its inventory record. It refuses while any host still resolves to the key: deleting it then would leave a Host block pointing at an IdentityFile that no longer exists, which ssh reports as a bare permission denial. Key files on disk are the caller's to remove (see the lifecycle service) so an accidental delete costs nothing but a manifest edit.
func (*Editor) DeleteProfile ¶
func (e *Editor) DeleteProfile(name string, revoke bool) (DeleteResult, error)
DeleteProfile removes a profile, revoking/pruning its hosts' keys.
func (*Editor) EditHost ¶
func (e *Editor) EditHost(profile, alias string, f HostFields) error
EditHost updates a host's fields (only the non-nil ones).
func (*Editor) EditProfile ¶
EditProfile updates a profile's key_scope and/or key_name (nil/empty keeps).
func (*Editor) ReserveKey ¶ added in v0.1.6
func (e *Editor) ReserveKey(name string, r manifest.ReservedKey) error
ReserveKey records that ~/.ssh/<name> is kept on purpose. It only edits the manifest; the key file is never touched.
func (*Editor) RevokeKey ¶ added in v0.1.6
RevokeKey withdraws a key without deleting it: every target it is deployed to is asked, through its host's provider, to remove it; the deployments it was removed from are dropped from its record; and its fingerprint goes into revoked.json, so deploy refuses it from then on. The declaration, the hosts and the files stay: the next step is a new key, with `keygen KEY --force` or `rotate`, which gets a fingerprint of its own.
func (*Editor) SetCategory ¶
func (e *Editor) SetCategory(name, category string) (CategoryResult, error)
SetCategory files a profile under category ("" returns it to the flat layout) and brings the tree along: the profile's key directory is renamed into place when it is still at the old location, and inventory records are re-pointed. When the directory has already been moved by hand - the usual way a tree reaches this state - only the records change.
It refuses when both locations exist, rather than choosing one: two key directories for one profile is a conflict for a person to resolve.
func (*Editor) SetDefaultLifetime ¶ added in v0.1.6
func (e *Editor) SetDefaultLifetime(days int) ([]LifetimeResult, error)
SetDefaultLifetime sets defaults.rotate_after_days, the lifetime every key without one of its own inherits, and counts those keys' expiry dates again.
func (*Editor) SetKeyComment ¶
SetKeyComment sets a profile's key_comment template; "" returns it to defaults.key_comment. It applies to keys minted from now on: a key's comment is written into its .pub when ssh-keygen creates it.
func (*Editor) SetKeyLifetime ¶ added in v0.1.6
SetKeyLifetime sets one key's lifetime: its declaration's rotate_after_days (declaring the key on its profile if only a host named it), and its inventory record's, with the expiry date counted again from when it was created. days 0 clears the key's own value, so it inherits the default.
func (*Editor) Unattended ¶ added in v0.1.6
Unattended makes target calls run with nobody to answer a prompt, their output to w, as a deploy started from the web UI does.
type HostFields ¶
type HostFields struct {
Hostname *string
User *string
Port *int
Provider *string
TokenEnv *string
KeyName *string
// Tags are the tags a new host starts with (AddHost only).
Tags []string
// SetTags replaces an existing host's tags with this list (de-duplicated); an
// empty list clears them. nil leaves them unchanged.
SetTags *[]string
// Untag removes these tags after any SetTags; a tag the host lacks is ignored.
Untag []string
// VM marks the host as a local VM whose address the hypervisor supplies at connect time.
VM *manifest.VMSpec
// Proxy names a proxy kind ("cloudflared"); "none" or "" clears it. nil leaves it unchanged.
Proxy *string
}
HostFields are the optional attributes for add/edit host (nil = unset/keep).
type HostSpec ¶
type HostSpec struct {
Alias string `json:"alias"`
Hostname string `json:"host"`
User string `json:"user"`
Port int `json:"port,omitempty"`
Role string `json:"role,omitempty"`
Tags []string `json:"tags,omitempty"`
}
HostSpec is one compact host declaration: `<alias> <host>[:port] <user> [role]`, e.g. `web-01 203.0.113.21 ploi web`. The role, when given, is stored as a tag. Port 0 means "not given": 22 on add, unchanged on update.
func ParseHostSpec ¶
ParseHostSpec parses one spec from its whitespace-separated fields.
func ParseHostSpecLine ¶
ParseHostSpecLine parses one spec written as a single string.
func ParseHostSpecs ¶
ParseHostSpecs reads many specs. Text is one spec per line, with blank lines and `#` comments skipped. Input whose first non-space byte is `[` is read as a JSON array whose items are either spec strings or objects ({"alias","host","user","port","role","tags"}).
type KeyRootResult ¶ added in v0.1.6
type KeyRootResult struct {
// From and To are the key roots before and after, as directory names
// under ~/.ssh.
From, To string
// Moved is true when ~/.ssh/<From> existed and was renamed to ~/.ssh/<To>.
Moved bool
// RecordsUpdated counts the inventory records whose path followed.
RecordsUpdated int
}
KeyRootResult reports what MoveKeyRoot changed.
func MoveKeyRoot ¶ added in v0.1.6
func MoveKeyRoot(e *Editor, root string) (KeyRootResult, error)
MoveKeyRoot makes root the directory under ~/.ssh that holds the profile directories (defaults.key_root), moving the whole tree there: private keys, .pub files, old/ archives and category config files, as one rename. The inventory records follow. Adopted keys stay where they are; they never lived under the root.
It refuses when both directories exist (merge them by hand first) and when the new name is taken by a file. A key root already moved by hand only has its records updated. Any failure after the rename puts the tree back. The caller re-renders ~/.ssh/config, whose IdentityFile and Include lines name the root.
type LifetimeResult ¶ added in v0.1.6
type LifetimeResult struct {
Key string
Days int
ExpiresOn string // "" when the key has no creation date to count from
}
LifetimeResult is what SetKeyLifetime changed.