Documentation
¶
Overview ¶
Package sshutil has some utility functions for dealing with SSH clients that are shared between the various consumers of the golang SSH implementation.
Index ¶
- Constants
- func DialSSHWithContext(ctx context.Context, network, addr string, config *ssh.ClientConfig) (*ssh.Client, error)
- type Helper
- func (obj *Helper) HostKeyCallback(hostkey ssh.PublicKey, knownHosts string) ssh.HostKeyCallback
- func (obj *Helper) KeySigner(p string) (ssh.Signer, error)
- func (obj *Helper) KeySigners() ([]ssh.Signer, error)
- func (obj *Helper) KnownHostsAlgorithms(addr string, knownHosts string) ([]string, error)
- func (obj *Helper) KnownHostsKey(hostkey string) (ssh.PublicKey, error)
- func (obj *Helper) PrioritizeHostKeyAlgorithms(allHostKeyAlgos []string, hostKey ssh.PublicKey, knownTypes ...string) []string
Constants ¶
const ( // DefaultSSHDir is the default directory to scan for private keys. DefaultSSHDir = "~/.ssh/" // DefaultKnownHostsPath is the default known_hosts file location. DefaultKnownHostsPath = "~/.ssh/known_hosts" )
Variables ¶
This section is empty.
Functions ¶
func DialSSHWithContext ¶
func DialSSHWithContext(ctx context.Context, network, addr string, config *ssh.ClientConfig) (*ssh.Client, error)
DialSSHWithContext wraps ssh.Dial so that we can have a context to cancel.
Types ¶
type Helper ¶
type Helper struct {
// Debug represents if we're running in debug mode or not.
Debug bool
// Logf is a logger which should be used.
Logf func(format string, v ...interface{})
}
Helper is a struct of common SSH client operations which want a logger. Fill in the fields and then use the methods.
func (*Helper) HostKeyCallback ¶
HostKeyCallback is a helper function to get the ssh callback function needed. If a nil hostkey is specified, then it only checks the known_hosts file, otherwise it checks that key first. The knownHosts path selects the file to check against; an empty string uses the default location.
func (*Helper) KeySigners ¶
KeySigners gets a list of possible key signers found in the default SSH directory. These are used to get the available types of the keys, and the auth methods.
func (*Helper) KnownHostsAlgorithms ¶
KnownHostsAlgorithms returns the host key types that we already trust for this host, based on the entries in the known_hosts file. This lets us prioritize the host key algorithms we offer during the handshake, the same way OpenSSH prefers the key types it has already recorded for a host, so that we negotiate a host key we can actually verify. The addr should be in host:port form. The knownHosts path selects the file to read; an empty string uses the default location. If the host is not found (or there is no known_hosts file), an empty list is returned without error.
func (*Helper) KnownHostsKey ¶
KnownHostsKey takes a known_hosts key entry (just the base64 key part) and turns it into the ssh.PublicKey needed for HostKeyCallback. This excerpt was taken from: x/crypto/ssh:keys.go:func parseAuthorizedKey
func (*Helper) PrioritizeHostKeyAlgorithms ¶
func (obj *Helper) PrioritizeHostKeyAlgorithms(allHostKeyAlgos []string, hostKey ssh.PublicKey, knownTypes ...string) []string
PrioritizeHostKeyAlgorithms returns the host key algorithms that we tell the server that we support. The order matters, because the server returns only one host key. The preferred key must be a known server key, not an unrelated key used for client authentication. The optional knownTypes are host key types (for example from the known_hosts file) that we already trust for this host; prioritizing them makes the handshake negotiate a key we can actually verify, the same way OpenSSH does.