Documentation
¶
Overview ¶
Package sshx wraps golang.org/x/crypto/ssh with the connection and authentication policy hlab uses: agent-first auth, strict known_hosts verification, and context-aware dialling.
Index ¶
- func AuthRefused(err error) bool
- func ConnectionLost(err error) bool
- func FingerprintSHA256(key ssh.PublicKey) string
- type Dialer
- func (d *Dialer) Dial(ctx context.Context, t Target) (*ssh.Client, error)
- func (d *Dialer) DialKeyOnly(ctx context.Context, t Target) (*ssh.Client, error)
- func (d *Dialer) DialMoved(ctx context.Context, t Target, newAddress string) (*ssh.Client, error)
- func (d *Dialer) DialPassword(ctx context.Context, t Target, password string) (*ssh.Client, error)
- type ExecResult
- type Executor
- type HostKeyPrompt
- type Options
- type Result
- type Target
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AuthRefused ¶
AuthRefused reports whether a dial failed because the server rejected every credential offered — as opposed to the machine being unreachable, which says nothing about whether those credentials work.
func ConnectionLost ¶
ConnectionLost reports whether an error means the session ended before the command could report a verdict, rather than the command itself failing.
The distinction matters for changes that cut the connection as part of doing their job — reconfiguring the interface hlab is connected over is the case this exists for. Treating a lost session as a failure there reports a change that worked as one that did not; the caller should instead reconnect and look at the machine to decide.
func FingerprintSHA256 ¶
FingerprintSHA256 renders a key fingerprint the way ssh(1) displays it.
Types ¶
type Dialer ¶
type Dialer struct {
// contains filtered or unexported fields
}
Dialer opens SSH connections according to a fixed set of Options. The host-key callback is built once and shared across connections.
func (*Dialer) Dial ¶
Dial opens an SSH connection to the target using key-based authentication: the target's identity file, then any keys held by the ssh-agent. The caller owns the returned client and must Close it.
func (*Dialer) DialKeyOnly ¶
DialKeyOnly authenticates with the target's identity file and nothing else.
Dial falls back to the ssh-agent, which is right for managing a machine and wrong for asking whether one particular key still opens it: an agent key that happens to work would answer yes on that key's behalf.
func (*Dialer) DialMoved ¶
DialMoved connects to a machine at an address other than the one it is recorded under, accepting it only if it presents a host key known_hosts already holds for the recorded address.
That is the proof a DHCP move needs. A name resolving somewhere new says nothing about who is there — DNS is exactly what an attacker would poison — but a machine that holds the private half of the recorded host key is the machine that was recorded. Nothing is trusted that was not already trusted; the key is only allowed to vouch for a second address.
On success the key is recorded under the new address as well, so ordinary verification works there from then on.
func (*Dialer) DialPassword ¶
DialPassword opens an SSH connection using password authentication, falling back to keyboard-interactive since many servers offer passwords only that way. It exists for enrolling a machine that hlab has no key on yet.
The password is used for this handshake and never stored.
type ExecResult ¶
ExecResult is the outcome of running one command on an existing connection.
func Exec ¶
func Exec(ctx context.Context, client *ssh.Client, command string, stdin io.Reader) (ExecResult, error)
Exec runs a single command on an already-open client, optionally feeding it stdin, and returns its output.
A returned error means the command could not be run at all. A command that ran and exited non-zero is reported through ExitCode with a nil error, the same distinction the fan-out executor makes.
type Executor ¶
type Executor struct {
// contains filtered or unexported fields
}
Executor fans a command out across many hosts with bounded concurrency.
func NewExecutor ¶
NewExecutor returns an Executor that runs at most concurrency connections at a time. A concurrency of zero or less means "one at a time".
func (*Executor) Ping ¶
Ping opens a connection to every target and immediately closes it, which verifies reachability, authentication and host key trust without running a remote command.
type HostKeyPrompt ¶
HostKeyPrompt is consulted when a host is not yet in known_hosts. Returning true accepts the key, which is then recorded so later connections verify normally.
It is never called for a host whose recorded key does not match: that is a potential man-in-the-middle and is always refused.
type Options ¶
type Options struct {
// KnownHostsFile is consulted to verify host keys.
KnownHostsFile string
// InsecureIgnoreHostKey disables host key verification entirely. This
// defeats the protection against man-in-the-middle attacks and exists only
// for first-run bootstrapping against machines you physically control.
InsecureIgnoreHostKey bool
// ConnectTimeout bounds the TCP dial and SSH handshake.
ConnectTimeout time.Duration
// OnUnknownHostKey, when set, is asked whether to trust a host that is not
// yet in known_hosts. Accepted keys are recorded. A host already recorded
// under a different key is always refused without consulting this.
OnUnknownHostKey HostKeyPrompt
}
Options configure connection behaviour for every target.
type Result ¶
type Result struct {
Host string `json:"host" yaml:"host"`
Command string `json:"command" yaml:"command"`
Stdout string `json:"stdout" yaml:"stdout"`
Stderr string `json:"stderr" yaml:"stderr"`
ExitCode int `json:"exitCode" yaml:"exitCode"`
// Duration is the wall time for this host. It is not serialised directly
// because time.Duration encodes as nanoseconds; DurationMs is emitted
// instead and is populated by the executor before returning.
Duration time.Duration `json:"-" yaml:"-"`
DurationMs int64 `json:"durationMs" yaml:"durationMs"`
// Err is set when the host could not be reached or the command could not
// be started. A command that ran and exited non-zero is a successful
// execution with a non-zero ExitCode, not an Err.
Err error `json:"-" yaml:"-"`
// Error is the string form of Err, for structured output.
Error string `json:"error,omitempty" yaml:"error,omitempty"`
}
Result is the outcome of running one command against one host.