knownhosts

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2022 License: Apache-2.0 Imports: 4 Imported by: 69

README

Go known_hosts host key algorithm lookup

build status godoc

This repo is brought to you by Skeema, a declarative pure-SQL schema management system for MySQL and MariaDB. Our Premium edition products include extensive SSH tunnel functionality, which internally makes use of this technique for known_hosts interactions.

Go provides excellent functionality for OpenSSH known_hosts files in its external package golang.org/x/crypto/ssh/knownhosts. However, that package has a painful omission: it does not expose a straightforward way to look up known_hosts entries, and this is often problematic for hosts that have multiple host keys of different types. If the host's first public key is not in known_hosts, but a key of a different type is, the HostKeyCallback returns an error. The caller must populate ssh.ClientConfig.HostKeyAlgorithms to prevent this, but golang.org/x/crypto/ssh/knownhosts does not provide an obvious way to do this.

This package provides a solution to this problem. It is a thin wrapper around golang.org/x/crypto/ssh/knownhosts, using a subtle trick to look up the list of host key algorithms for a given host. It may be used like so, instead of using golang.org/x/crypto/ssh/knownhosts directly:

import (
	"golang.org/x/crypto/ssh"
	"github.com/skeema/knownhosts"
)

func sshConfigForHost(hostWithPort string) (*ssh.ClientConfig, error) {
	kh, err := knownhosts.New("/home/myuser/.ssh/known_hosts")
	if err != nil {
		return nil, err
	}
	config := &ssh.ClientConfig{
		User:              "myuser",
		Auth:              []ssh.AuthMethod{ /* ... */ },
		HostKeyCallback:   kh.HostKeyCallback(), // or, equivalently, use ssh.HostKeyCallback(kh)
		HostKeyAlgorithms: kh.HostKeyAlgorithms(hostWithPort),
	}
	return config, nil
}

How it works

It turns out you can use the callback from golang.org/x/crypto/ssh/knownhosts to perform lookups for a host. It just requires a hack: invoke the callback on the hostname in question, while also supplying a bogus key that won't match anything in known_hosts. You can then iterate over the resulting KeyError.Want to see which keys are actually present for that host. That's what this package's HostKeyAlgorithms function does.

Documentation

Overview

Package knownhosts is a thin wrapper around golang.org/x/crypto/ssh/knownhosts, adding the ability to obtain the list of host key algorithms for a known host.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HostKeyAlgorithms

func HostKeyAlgorithms(cb ssh.HostKeyCallback, hostWithPort string) []string

HostKeyAlgorithms is a convenience function for performing host key algorithm lookups on an ssh.HostKeyCallback directly. It is intended for use in code paths that stay with the New method of golang.org/x/crypto/ssh/knownhosts rather than this package's New method.

Types

type HostKeyCallback

type HostKeyCallback ssh.HostKeyCallback

HostKeyCallback wraps ssh.HostKeyCallback with an additional method to perform host key algorithm lookups from the known_hosts entries.

func New

func New(files ...string) (HostKeyCallback, error)

New creates a host key callback from the given OpenSSH host key files. The returned value may be used in ssh.ClientConfig.HostKeyCallback by casting it to ssh.HostKeyCallback, or using its HostKeyCallback method. Otherwise, it operates the same as the New function in golang.org/x/crypto/ssh/knownhosts.

func (HostKeyCallback) HostKeyAlgorithms

func (hkcb HostKeyCallback) HostKeyAlgorithms(hostWithPort string) (algos []string)

HostKeyAlgorithms returns a slice of host key algorithms for the supplied host:port found in the known_hosts file(s), or an empty slice if the host is not already known. The result may be used in ssh.ClientConfig.HostKeyAlgorithms, either as-is or after filtering (if you wish to prefer or ignore particular algorithms). For hosts with multiple known_hosts entries, note that the order of this function's result does not match the order of lines in the known_hosts files.

func (HostKeyCallback) HostKeyCallback

func (hkcb HostKeyCallback) HostKeyCallback() ssh.HostKeyCallback

HostKeyCallback simply casts the receiver back to ssh.HostKeyCallback, for use in ssh.ClientConfig.HostKeyCallback.

Jump to

Keyboard shortcuts

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