editor

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT 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 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

View Source
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

View Source
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

func AdoptKey(e *Editor, profile, name, rel, hostAlias string) error

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

func ParseLifetime(s string) (int, error)

ParseLifetime reads a lifetime as a preset name or a number of days.

func SetKeyTemplate added in v0.1.6

func SetKeyTemplate(e *Editor, profile, tpl string) ([]string, error)

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

func SetNaming(e *Editor, profile string, edit func(*manifest.Naming)) ([]string, error)

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

func SetProfileCode(e *Editor, profile, code string) ([]string, error)

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

func TakenAliases(m *manifest.Manifest) []string

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 New

func New(p paths.Paths) *Editor

New builds a manifest editor.

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

func (e *Editor) AddProfile(name, keyScope string, keyName *string) error

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

func (e *Editor) ApplyHosts(profile string, specs []HostSpec, dryRun bool) ([]ApplyResult, error)

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

func (e *Editor) DeleteKey(ref manifest.KeyRef, revoke bool) (DeleteResult, error)

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

func (e *Editor) EditProfile(name string, keyScope *string, keyName *string) error

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

func (e *Editor) RevokeKey(ref manifest.KeyRef, reason string) (RevokeResult, error)

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

func (e *Editor) SetKeyComment(name, tmpl string) error

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

func (e *Editor) SetKeyLifetime(ref manifest.KeyRef, days int) (LifetimeResult, error)

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

func (e *Editor) Unattended(w io.Writer) *Editor

Unattended makes target calls run with nobody to answer a prompt, their output to w, as a deploy started from the web UI does.

func (*Editor) UnreserveKey added in v0.1.6

func (e *Editor) UnreserveKey(name string, missingOK bool) (bool, error)

UnreserveKey drops a reservation. missingOK makes an absent one a no-op (adopting a key releases its reservation whether or not it had one).

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

func ParseHostSpec(fields []string) (HostSpec, error)

ParseHostSpec parses one spec from its whitespace-separated fields.

func ParseHostSpecLine

func ParseHostSpecLine(line string) (HostSpec, error)

ParseHostSpecLine parses one spec written as a single string.

func ParseHostSpecs

func ParseHostSpecs(r io.Reader) ([]HostSpec, error)

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"}).

func (HostSpec) AllTags

func (s HostSpec) AllTags() []string

AllTags is the role followed by any extra tags, de-duplicated, in order.

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.

type RevokeResult added in v0.1.6

type RevokeResult struct {
	Key         string
	Fingerprint string
	Removed     []string // targets the key was removed from
	NotRemoved  []string // targets it is deployed to but could not be removed from
}

RevokeResult is what RevokeKey did.

Jump to

Keyboard shortcuts

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