sshx

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: GPL-3.0 Imports: 15 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 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

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthRefused

func AuthRefused(err error) bool

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

func ConnectionLost(err error) bool

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

func FingerprintSHA256(key ssh.PublicKey) string

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 NewDialer

func NewDialer(opts Options) (*Dialer, error)

NewDialer validates the options and prepares the host key verifier.

func (*Dialer) Dial

func (d *Dialer) Dial(ctx context.Context, t Target) (*ssh.Client, error)

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

func (d *Dialer) DialKeyOnly(ctx context.Context, t Target) (*ssh.Client, error)

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

func (d *Dialer) DialMoved(ctx context.Context, t Target, newAddress string) (*ssh.Client, error)

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

func (d *Dialer) DialPassword(ctx context.Context, t Target, password string) (*ssh.Client, error)

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

type ExecResult struct {
	Stdout   string
	Stderr   string
	ExitCode int
}

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.

func (ExecResult) OK

func (r ExecResult) OK() bool

OK reports whether the command exited zero.

type Executor

type Executor struct {
	// contains filtered or unexported fields
}

Executor fans a command out across many hosts with bounded concurrency.

func NewExecutor

func NewExecutor(d *Dialer, concurrency int) *Executor

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

func (e *Executor) Ping(ctx context.Context, targets []Target) []Result

Ping opens a connection to every target and immediately closes it, which verifies reachability, authentication and host key trust without running a remote command.

func (*Executor) Run

func (e *Executor) Run(ctx context.Context, targets []Target, command string) []Result

Run executes command on every target and returns one Result per target, sorted by host name. It does not return an error for individual host failures; inspect the Results. Cancelling ctx aborts in-flight work.

type HostKeyPrompt

type HostKeyPrompt func(host string, remote net.Addr, key ssh.PublicKey) (bool, error)

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.

func (Result) OK

func (r Result) OK() bool

OK reports whether the host was reachable and the command exited zero.

type Target

type Target struct {
	Name         string
	Address      string
	Port         int
	User         string
	IdentityFile string
}

Target is everything sshx needs to reach one machine. It is deliberately independent of the inventory package so the two can evolve separately.

Jump to

Keyboard shortcuts

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