Documentation
¶
Overview ¶
Package hostkeys reads, writes and collects SSH host keys.
The host key file is the site's trust anchor: it is kept under version control and every connection is checked against it. Editing it with ssh-keygen -R and appending with ssh-keyscan, as the shell tools did, is neither atomic nor locked, so two administrators refreshing at once could lose an entry. Everything here goes through one locked, atomic update.
Index ¶
Constants ¶
const ( // MarkerRevoked says the key on the line must never be accepted. MarkerRevoked = "@revoked" // MarkerCertAuthority says the key on the line signs host certificates; // it is not the key of a host. MarkerCertAuthority = "@cert-authority" )
Markers a known_hosts line may start with.
Variables ¶
var DefaultAlgorithms = []string{ ssh.KeyAlgoED25519, ssh.KeyAlgoECDSA256, ssh.KeyAlgoRSASHA256, ssh.KeyAlgoRSA, }
DefaultAlgorithms are the host key algorithms collected for a host, best first. Ed25519 is preferred; RSA is kept for service processors and older systems that offer nothing else.
ssh-rsa comes last. It names the same RSA key as rsa-sha2-256 and differs only in the SHA-1 signature over the handshake, which matters for a session but not for reading the key. A server older than OpenSSH 7.2 signs with nothing else, and those are the hosts legacyAlgorithms exists for.
Functions ¶
func DialCommand ¶ added in v0.3.0
DialCommand starts a command whose standard input and output are the connection, the way ssh's ProxyCommand works. With "ssh -W host:port jump" it reaches a host through its jump host, over the site's own ssh configuration and host key checks for the jump.
Types ¶
type Entry ¶
type Entry struct {
// Marker is "@revoked", "@cert-authority" or empty for a plain host key.
Marker string `json:"marker,omitempty" yaml:"marker,omitempty"`
// Hosts are the patterns the line applies to: names, wildcards,
// negations and hashed names.
Hosts []string `json:"hosts" yaml:"hosts"`
// Type is the key algorithm, "ssh-ed25519" and the like.
Type string `json:"type" yaml:"type"`
// Key is the base64 encoded public key.
Key string `json:"key" yaml:"key"`
// Comment is whatever followed the key on the line.
Comment string `json:"comment,omitempty" yaml:"comment,omitempty"`
// Notes are the comment lines written above the entry. They travel with
// it when the file is sorted, so that a note stays with the key it is
// about.
Notes []string `json:"notes,omitempty" yaml:"notes,omitempty"`
}
Entry is one line of a known_hosts file.
func (Entry) IsHostKey ¶ added in v0.3.0
IsHostKey reports whether the line is a plain host key, rather than a revocation or a certificate authority.
type File ¶
type File struct {
// Header holds the comment lines at the top of the file, which sites use
// to say what the file is and how it is maintained.
Header []string
// Entries are the host keys, revocations and certificate authorities.
// A comment between entries is kept as a note of the entry under it.
Entries []Entry
// Trailer holds the comment lines after the last entry.
Trailer []string
}
File is a parsed known_hosts file: the comment block at the top, the entries under it and any comment after the last entry.
func (*File) Add ¶
Add puts a host key in, replacing one of the same host and type and keeping its notes.
func (*File) Find ¶
Find returns the host keys that cover a host. Revocations and certificate authorities are not host keys and are left out.
func (*File) Remove ¶
Remove drops the host keys naming a host and reports how many went.
Only a pattern that names the host itself, literally or hashed, goes. A wildcard also covers other hosts, and a revocation or a certificate authority is a decision someone made on purpose, so neither is touched.
func (*File) Render ¶
Render writes the file back, keeping the header on top and sorting the entries so that two refreshes produce the same file and a diff stays readable. Each entry is written under its notes.
type Scanner ¶
type Scanner struct {
// Timeout bounds one host, from the dial to the key.
Timeout time.Duration
// Port is the SSH port; empty means 22.
Port string
// Dial connects. It defaults to a TCP connection; a host behind a jump
// host is reached through DialCommand instead, and tests replace it.
Dial func(ctx context.Context, network, address string) (net.Conn, error)
}
Scanner collects host keys by starting an SSH handshake and stopping as soon as the server has presented its key.
func (*Scanner) Scan ¶
Scan collects the host key a host offers. The algorithms are offered in one handshake, best first, so the server picks the strongest it has and an unreachable host costs one timeout rather than one for each algorithm.
A host whose key could not be collected did not answer as an ssh server does, whatever stopped it, so the error is a transport failure, the code the scan commands exit with.