knownhosts

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 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 authama writes carries a trailing "authama" 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

Constants

View Source
const (
	PinNone    = "unpinned"
	PinMatch   = "pinned"
	PinChanged = "changed"
)

Pin states for a host checked against freshly scanned keys.

View Source
const InitNotice = "knownhosts init scans the hosts it can reach and pins their keys " +
	"into ~/.ssh/known_hosts (trust on first use)."

InitNotice is what `knownhosts init` confirms with, at a terminal and in the web UI: it is trust on first use, so both say so in the same words.

Variables

This section is empty.

Functions

func HostInKnownHosts

func HostInKnownHosts(path, token string) bool

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.

func HostToken added in v0.1.3

func HostToken(hostname string, port int) string

HostToken is the name ssh looks a host up by in known_hosts: hostname, or [hostname]:port off port 22.

func ProfileOfAlias

func ProfileOfAlias(m *manifest.Manifest, alias string) string

ProfileOfAlias returns the profile that defines alias, or "" if none.

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 authama
}

Entry is one trust-store line, decoded for display.

func (Entry) Name

func (e Entry) Name() string

Name is how to refer to the entry on screen. A hashed line that matches no manifest host cannot be named: recovering the host name from the HMAC is exactly what hashing prevents, so the key type and fingerprint identify it instead.

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

type ScannedKey struct {
	Host        string
	Port        int
	Keytype     string
	Line        string
	Fingerprint string
}

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 New

func New(sshDir string) *Service

New builds a known-hosts service over ~/.ssh.

func (*Service) Add

func (s *Service) Add(lines []string) (int, error)

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 authama-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

func (s *Service) Adopt(m *manifest.Manifest) (int, error)

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 authama to manage it. Returns the count adopted.

func (*Service) AdoptCandidates

func (s *Service) AdoptCandidates(m *manifest.Manifest) ([]Entry, error)

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

func (s *Service) Ensure() (bool, error)

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

func (s *Service) EntriesFor(token string) []Entry

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: authama 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) InitEach added in v0.1.6

func (s *Service) InitEach(m *manifest.Manifest, profile string, allProfiles, force bool,
	each func(done, total int, alias string)) (InitReport, error)

InitEach is Init, calling each before every host it scans with how many hosts are done, how many there are, and the one about to be scanned, so a caller can show progress. each may be nil.

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) Path

func (s *Service) Path() string

Path is the one trust store every profile's hosts are pinned into.

func (*Service) PinAs added in v0.1.3

func (s *Service) PinAs(token string, scanned []ScannedKey) (int, error)

PinAs pins scanned keys under token rather than the address they were scanned at - a VM host is reached through a ProxyCommand, so ssh checks the key under the alias, while the scan had to go to the hypervisor's address.

func (*Service) PinState added in v0.1.3

func (s *Service) PinState(token string, scanned []ScannedKey) string

PinState compares the trust store's entries for token with scanned keys: unpinned (no entry names token), pinned (a scanned key is already trusted) or changed (entries name token, but none holds a key the host offers now).

func (*Service) Prune

func (s *Service) Prune(m *manifest.Manifest) (int, error)

Prune removes authama-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

func (s *Service) PruneCandidates(m *manifest.Manifest) ([]Entry, error)

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.

func (*Service) UnpinTagged added in v0.1.3

func (s *Service) UnpinTagged(token string) (removed, untagged int, err error)

UnpinTagged removes the authama-tagged entries naming token and reports how many untagged ones (the user's own) also name it; those are never touched.

type Target

type Target struct {
	Profile  string
	Alias    string
	Hostname string
	Port     int
}

Target is one manifest host to pin.

func Targets

func Targets(m *manifest.Manifest) ([]Target, error)

Targets returns (profile, alias, hostname, port) for every manifest host, deduped by (profile, hostname, port). Mirrors facade.known_hosts_targets.

Jump to

Keyboard shortcuts

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