Documentation
¶
Overview ¶
Package transport runs commands on remote hosts through the OpenSSH client.
OpenSSH is driven rather than replaced, so ProxyJump, ControlMaster, Kerberos, the agent and the administrator's own configuration keep working exactly as they do from a shell.
A remote command is never built by joining words into a string. The argument vector is quoted once, handed to ssh as a single argument, and split back by the remote shell into exactly the arguments that were given, so a glob, an apostrophe or repeated whitespace survives the trip.
Index ¶
- Constants
- func RemoteCommand(req Request) (string, error)
- type Call
- type Client
- func (c *Client) Args(target Target, req Request) ([]string, error)
- func (c *Client) Binary() string
- func (c *Client) Command() ([]string, error)
- func (c *Client) ConfigPath() (string, error)
- func (c *Client) Copy(ctx context.Context, target Target, req CopyRequest) (*Result, error)
- func (c *Client) CopyArgs(target Target, req CopyRequest) ([]string, error)
- func (c *Client) Destination(target Target) (string, error)
- func (c *Client) Interactive(ctx context.Context, target Target, req Request) error
- func (c *Client) Run(ctx context.Context, target Target, req Request) (*Result, error)
- func (c *Client) ScpBinary() string
- type CopyRequest
- type Options
- type Recorder
- type Request
- type Result
- type Runner
- type TTY
- type Target
Constants ¶
const DefaultMaxOutput = 32 << 20
DefaultMaxOutput is how much of each output stream Run keeps unless a request says otherwise. A host controls its own output for as long as the command runs, and without a bound it could fill the memory of the CLI or the MCP server.
Variables ¶
This section is empty.
Functions ¶
func RemoteCommand ¶
RemoteCommand renders a request as the single argument ssh is given.
The result is one shell command line. The remote shell splits it back into the original argument vector, because every word was quoted.
Types ¶
type Call ¶
type Call struct {
Target Target
Request Request
// Command is the shell command line the request renders to, which is
// what --dry-run prints and what the tests assert on.
Command string
}
Call is one request a Recorder was asked to run.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client drives the OpenSSH client.
func (*Client) Args ¶
Args builds the full argument vector, ssh included, that a request runs as. Commands use it for --dry-run and for reporting what they would do.
func (*Client) Command ¶ added in v0.3.0
Command returns ssh with the generated configuration, the part of every argument vector that decides how a host is reached and which host key it must present. A program that runs ssh itself, such as sshuttle, is given this command, so that it connects the way every other connection does.
func (*Client) ConfigPath ¶
ConfigPath returns the generated ssh configuration, writing it once.
func (*Client) CopyArgs ¶
func (c *Client) CopyArgs(target Target, req CopyRequest) ([]string, error)
CopyArgs builds the scp argument vector for a transfer.
func (*Client) Destination ¶ added in v0.3.0
Destination renders the [user@]host a target is reached as, with the account resolved and checked as it is for a command.
func (*Client) Interactive ¶
Interactive runs a request wired to the terminal, for a login shell or anything that needs to talk to the user.
type CopyRequest ¶
type CopyRequest struct {
// Sources are local paths when Upload is set, and remote paths
// otherwise.
Sources []string
// Destination is the remote path when Upload is set, and a local path
// otherwise.
Destination string
// Upload says which way the transfer goes.
Upload bool
// Recursive copies directories.
Recursive bool
// Preserve keeps modification times and modes.
Preserve bool
}
CopyRequest describes a file transfer.
type Options ¶
type Options struct {
// SSH is the transport configuration of the site.
SSH v1alpha1.SSHSpec
// Roles are the infrastructure roles, used for per role settings and
// for resolving a jump host by name.
Roles map[string]v1alpha1.HostRole
// StateDir holds the generated configuration and the control sockets.
StateDir string
// KnownHostsFile is the host key file, already made absolute.
KnownHostsFile string
// DefaultUser is the account used where nothing names one.
DefaultUser string
// NoTerminal says that nobody is at a terminal to answer a prompt, as
// under the MCP server. ssh is then run in batch mode and in a session
// of its own, so that it fails instead of asking for a password or a
// passphrase on whatever terminal the process was started from.
NoTerminal bool
}
Options configure a client.
func (Options) Validate ¶ added in v0.3.0
Validate checks everything the generated ssh configuration is built from.
Each value is written into a line of the file, so a line break in one would add a directive and a space would split it; ssh would then reject the whole file, or worse, obey the line. Jump chains are checked for cycles, because ssh follows one without end.
type Recorder ¶
type Recorder struct {
// Responses are returned in order; once they run out, an empty
// successful result is returned. A response may be keyed to a target
// name through Reply instead.
Responses []*Result
// Reply returns the result for a call. It wins over Responses.
Reply func(Target, Request) (*Result, error)
// contains filtered or unexported fields
}
Recorder is a Runner that records requests instead of running them.
It backs --dry-run, where the point is to show exactly what would be sent, and the tests, where a real ssh is neither available nor wanted.
type Request ¶
type Request struct {
// Argv is the remote command. An empty Argv and Script start an
// interactive login.
Argv []string
// Script is a shell program run with the shell's -c, as one argument.
// Argv and Script are mutually exclusive.
Script string
// Shell runs Script; it defaults to bash.
Shell string
// TTY controls pseudo terminal allocation.
TTY TTY
// Timeout bounds the command. It is enforced on the target with
// timeout(1), because killing the local ssh leaves the remote process
// running.
Timeout time.Duration
// Env is exported for the command. It is passed through env(1) rather
// than SendEnv, which needs the server to allow each name.
Env map[string]string
// Stdin is fed to the command. Secrets travel this way so that they
// never appear in an argument vector, where ps shows them.
Stdin io.Reader
// Stdout and Stderr receive the output when the command is run
// interactively. Run captures them instead.
Stdout, Stderr io.Writer
// MaxOutput bounds how much of each of standard output and standard
// error Run keeps; zero keeps up to DefaultMaxOutput.
MaxOutput int
// NoShell says the host's command line is not a POSIX shell, as on a
// power distribution unit, so the command is sent without the guard that
// tells its exit status 255 from ssh's, which it could not run. A 255
// from such a command reads as a connection failure.
NoShell bool
}
Request is what to run on a target.
type Result ¶
type Result struct {
Target Target `json:"target" yaml:"target"`
ExitCode int `json:"exitCode" yaml:"exitCode"`
Stdout string `json:"stdout,omitempty" yaml:"stdout,omitempty"`
Stderr string `json:"stderr,omitempty" yaml:"stderr,omitempty"`
Duration time.Duration `json:"-" yaml:"-"`
// Truncated says that the output was cut off at the request's
// MaxOutput. Such a result has failed: what was kept is not all the
// host said.
Truncated bool `json:"truncated,omitempty" yaml:"truncated,omitempty"`
// Err is set when the command could not be run or did not exit zero.
Err error `json:"-" yaml:"-"`
}
Result is the outcome of one remote command.
func ExitResult ¶ added in v0.3.0
ExitResult is the result Run reports for a command that ran and exited with a status, with its error set the same way. A stand-in for the client, such as a test's, uses it so that a failure looks as it would over ssh.
func (Result) MarshalJSON ¶ added in v0.3.0
MarshalJSON adds the error text to the result, which the machine formats would otherwise leave out: Err is an error value, not a string.
type Runner ¶
Runner runs a command on a target. Commands depend on this rather than on the client, so that a dry run or a test can stand in for real ssh.
type TTY ¶
type TTY int
TTY says whether a pseudo terminal is allocated for a remote command.
const ( // TTYAuto allocates a terminal for an interactive login and none for a // command, which is what ssh does on its own. TTYAuto TTY = iota // TTYForce allocates one even for a command, for anything that prompts. TTYForce // TTYNone never allocates one, so that output is not echoed back. TTYNone )
type Target ¶
type Target struct {
// Name is the node or role as the administrator named it, used in
// output and errors.
Name string `json:"name" yaml:"name"`
// Host is the name ssh connects to.
Host string `json:"host" yaml:"host"`
// User is the remote account; empty uses what the configuration says.
User string `json:"user" yaml:"user"`
// Role names the infrastructure role, when the target is one.
Role string `json:"role,omitempty" yaml:"role,omitempty"`
// ForwardAgent and ForwardX11 are requested per command, on top of what
// the role already asks for.
ForwardAgent bool `json:"forwardAgent,omitempty" yaml:"forwardAgent,omitempty"`
ForwardX11 bool `json:"forwardX11,omitempty" yaml:"forwardX11,omitempty"`
}
Target is a host to run something on.