Documentation
¶
Overview ¶
Package knownhosts pins host keys via ssh-keyscan. It scans and fingerprints host keys (data; the surface confirms before trust) and appends confirmed lines, deduped, with the right perms - into the single, user-wide ~/.ssh/known_hosts trust store every rendered host block points at.
Every line sshmgr writes carries a trailing "sshmgr" comment tag (see sshd(8)'s "marker hostnames keytype key comment" line format). That tag is what makes cleanup safe: prune only removes tagged lines, and only once no remaining manifest host resolves to that host:port, so deleting one profile can never strand or unpin a host another profile still uses. Untagged lines - anything the user or another tool pinned - are never touched unless explicitly adopted.
Index ¶
- func HostInKnownHosts(path, token string) bool
- func ProfileOfAlias(m *manifest.Manifest, alias string) string
- type Entry
- type HostPinResult
- type InitReport
- type MigrationReport
- type ScannedKey
- type Service
- func (s *Service) Add(lines []string) (int, error)
- func (s *Service) Adopt(m *manifest.Manifest) (int, error)
- func (s *Service) AdoptCandidates(m *manifest.Manifest) ([]Entry, error)
- func (s *Service) AutoPin(m *manifest.Manifest, profiles map[string]bool, getenv func(string) string) map[string]int
- func (s *Service) Ensure() (bool, error)
- func (s *Service) EntriesFor(token string) []Entry
- func (s *Service) Init(m *manifest.Manifest, profile string, allProfiles, force bool) (InitReport, error)
- func (s *Service) MigrateLegacyStores() (MigrationReport, error)
- func (s *Service) Path() string
- func (s *Service) Prune(m *manifest.Manifest) (int, error)
- func (s *Service) PruneCandidates(m *manifest.Manifest) ([]Entry, error)
- func (s *Service) Scan(host string, port int) []ScannedKey
- func (s *Service) ScanChecked(host string, port int) ([]ScannedKey, error)
- type Target
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HostInKnownHosts ¶
HostInKnownHosts reports whether token (a hostname or [host]:port) is a pinned host in path. Shared with doctor.
It has to understand hashed entries as well as plaintext ones. Comparing host fields as strings would report every host this tool pinned as unpinned, which would send doctor and auto-pin into re-pinning hosts forever.
Types ¶
type Entry ¶
type Entry struct {
Token string // the host name it matched, in the clear; "" when unknown
Field string // the host field as stored (an HMAC when hashed)
Marker string // @cert-authority / @revoked, empty for an ordinary line
Keytype string
Fingerprint string
Hashed bool
Tagged bool // written (or adopted) by sshmgr
}
Entry is one trust-store line, decoded for display.
type HostPinResult ¶
type HostPinResult struct {
Profile string
Alias string
Hostname string
Port int
Status string // pinned | already-trusted | unreachable | no-keys
Fingerprints []string
}
HostPinResult is the outcome of initializing one host's known_hosts entry.
type InitReport ¶
type InitReport struct {
Profiles []string
Created []string
Results []HostPinResult
}
InitReport summarizes a knownhosts init run.
func (InitReport) Format ¶
func (r InitReport) Format() string
Format renders the human-readable init report (mirrors KnownHostsInitReport.format).
type MigrationReport ¶
type MigrationReport struct {
Merged int // lines merged in (post-dedup)
Removed []string // profiles/<name>/known_hosts files deleted, ssh-dir-relative
}
MigrationReport summarizes a one-shot migration of legacy per-profile known_hosts stores into the single ~/.ssh/known_hosts.
type ScannedKey ¶
ScannedKey is one host key returned by ssh-keyscan, with its fingerprint.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service manages the single, user-wide known_hosts trust store.
func (*Service) Add ¶
Add appends confirmed host-key lines to the trust store, deduped, atomically. Host names are hashed on the way in and every line is tagged as sshmgr-owned. Returns the count added.
Dedup cannot be string equality any more. Every hashed line carries a fresh random salt, so re-pinning the same host produces different bytes each time and a naive comparison would append a duplicate on every run. Membership is decided on (host name, key type, key) instead, computing each existing entry's HMAC under its own salt.
func (*Service) Adopt ¶
Adopt tags every untagged line matching a live manifest host, making it eligible for future pruning. Opt-in: an untagged pin is presumed to be the user's own until they explicitly ask for sshmgr to manage it. Returns the count adopted.
func (*Service) AdoptCandidates ¶
AdoptCandidates returns the untagged lines Adopt would tag. These do match a live host, so each one can be named in the clear.
func (*Service) AutoPin ¶
func (s *Service) AutoPin(m *manifest.Manifest, profiles map[string]bool, getenv func(string) string) map[string]int
AutoPin creates/updates each profile's known_hosts with its hosts' keys so a freshly minted profile works without a separate pin. Trust-on-first-use only (never overrides an already-pinned host), skips unreachable hosts (short 2s probe, since the caller holds the lock), and is disabled by AUTHAMA_AUTO_PIN =0/false/no/off. Returns {profile: keys added}. Mirrors facade._auto_pin.
func (*Service) Ensure ¶
Ensure creates known_hosts (empty, correct perms) if absent so the path the rendered config references always exists. Returns true if created.
func (*Service) EntriesFor ¶
EntriesFor returns the trust-store lines that pin token (a hostname or the [host]:port form), decoded so they can be shown to a human.
Reading the file is not enough any more: sshmgr hashes the names it writes and pins HashKnownHosts for the ones ssh writes, so the host field is an HMAC and the store cannot be grepped. Matching goes through the same hash-aware comparison the rest of the package uses, and only the fingerprint and key type are surfaced - never the key blob.
func (*Service) Init ¶
func (s *Service) Init(m *manifest.Manifest, profile string, allProfiles, force bool) (InitReport, error)
Init initializes known_hosts and pins reachable hosts (trust-on-first-use). Scope: one profile or allProfiles selects which manifest hosts to scan, not which file to write - every host is pinned into the same store. Mirrors facade.init_known_hosts. Caller handles the mutation guard (snapshot).
func (*Service) MigrateLegacyStores ¶
func (s *Service) MigrateLegacyStores() (MigrationReport, error)
MigrateLegacyStores merges every profiles/*/known_hosts left over from before the trust store consolidated into one file, then deletes them. Lines still in plaintext are hashed on the way in, and every merged line is tagged, exactly as any other call to Add. A tree with none is a no-op (zero merged, zero removed), so callers can run this unconditionally on every render without cost once the migration has already happened.
func (*Service) Prune ¶
Prune removes sshmgr-tagged lines that no longer correspond to any manifest host. A tagged line survives if any remaining host - in any profile - still resolves to its host:port, so deleting one profile's hosts can never strand or unpin a host another profile still uses. Untagged lines (the user's own pins, or anything else in the file) are never touched. Returns the count removed.
func (*Service) PruneCandidates ¶
PruneCandidates returns the lines Prune would remove, decoded for display. Their host names are unrecoverable when hashed - that is the point of hashing - so they are identified by key type and fingerprint instead.
func (*Service) Scan ¶
func (s *Service) Scan(host string, port int) []ScannedKey
Scan ssh-keyscans a host and fingerprints each key (no writes).
func (*Service) ScanChecked ¶
func (s *Service) ScanChecked(host string, port int) ([]ScannedKey, error)
ScanChecked is Scan that says why nothing came back: ssh-keyscan is missing, or the host answered with no SSH host key (a closed or filtered port, or a non-SSH service such as a web server). A caller that pins must not report success for either.