hostkeys

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: GPL-3.0, LGPL-3.0 Imports: 10 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 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

This section is empty.

Variables

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.

Functions

func Modify

func Modify(ctx context.Context, path string, change func(*File) error) error

Modify reads, changes and writes a known_hosts file under a lock, so that two administrators refreshing at the same time cannot lose an entry.

func Save

func Save(path string, f *File) error

Save writes a known_hosts file atomically.

Types

type Entry

type Entry struct {
	// Hosts are the patterns the line applies to.
	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"`
}

Entry is one line of a known_hosts file.

func (Entry) Matches

func (e Entry) Matches(host string) bool

Matches reports whether the entry covers a host name.

func (Entry) String

func (e Entry) String() string

String renders the entry as a known_hosts line.

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.
	Entries []Entry
}

File is a parsed known_hosts file: the comment block at the top and the entries under it.

func Load

func Load(path string) (*File, error)

Load reads a known_hosts file, treating a missing file as empty.

func Parse

func Parse(data []byte) (*File, error)

Parse reads a known_hosts file.

func (*File) Add

func (f *File) Add(e Entry)

Add puts an entry in, replacing one of the same host and type.

func (*File) Find

func (f *File) Find(host string) []Entry

Find returns the entries that cover a host.

func (*File) Remove

func (f *File) Remove(host string) int

Remove drops every entry covering a host and reports how many went.

func (*File) Render

func (f *File) Render() []byte

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.

type Scanner

type Scanner struct {
	// Algorithms are the key types to ask for, best first.
	Algorithms []string
	// Timeout bounds one connection attempt.
	Timeout time.Duration
	// Port is the SSH port; empty means 22.
	Port string
	// Dial connects; it is replaced in tests.
	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

func (s *Scanner) Scan(ctx context.Context, host string) ([]Entry, error)

Scan collects the host keys a host offers, best algorithm first. It returns the first key it obtains, which is the strongest algorithm the host supports out of those asked for.

Jump to

Keyboard shortcuts

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