Documentation
¶
Overview ¶
Package transport implements remote command execution — the low-level primitives an orchestration tool's task/module logic runs commands and moves files through, whether the target is the control node itself (Local), a Unix-like host over SSH (SSH), or a Windows host over WinRM (WinRM) — plus privilege escalation (Become: sudo/su/doas) as a decorator over any of them.
It is deliberately scoped to these primitives and nothing tool-specific: no playbooks, no catalogs, no task manifests. Ansible's modules and Puppet Bolt's tasks both reduce, in the end, to "run this command" and "put this file there" — this package is that reduction, shared instead of reimplemented per tool.
Index ¶
- type BecomeConfig
- type BecomeMethod
- type Connection
- type EnvExecer
- type HTTPDoer
- type Local
- func (l *Local) Close() error
- func (l *Local) Exec(ctx context.Context, cmd string, stdin io.Reader) (Result, error)
- func (l *Local) ExecEnv(ctx context.Context, cmd string, env map[string]string, stdin io.Reader) (Result, error)
- func (l *Local) Fetch(ctx context.Context, remotePath, localPath string) error
- func (l *Local) NewSession(ctx context.Context) (Session, error)
- func (l *Local) Put(ctx context.Context, localPath, remotePath string, opts PutOptions) error
- func (l *Local) Remove(ctx context.Context, remotePath string) error
- func (l *Local) TempPath(base string) string
- type PutOptions
- type Result
- type SSH
- func (s *SSH) Close() error
- func (s *SSH) Exec(ctx context.Context, cmd string, stdin io.Reader) (Result, error)
- func (s *SSH) Fetch(ctx context.Context, remotePath, localPath string) error
- func (s *SSH) NewSession(ctx context.Context) (Session, error)
- func (s *SSH) Put(ctx context.Context, localPath, remotePath string, opts PutOptions) error
- func (s *SSH) Remove(ctx context.Context, remotePath string) error
- func (s *SSH) TempPath(base string) string
- type SSHConfig
- type Session
- type Streamer
- type WinRM
- func (w *WinRM) Close() error
- func (w *WinRM) Exec(ctx context.Context, cmd string, stdin io.Reader) (Result, error)
- func (w *WinRM) ExecArgv(ctx context.Context, command string, args []string, stdin io.Reader) (Result, error)
- func (w *WinRM) Fetch(ctx context.Context, remotePath, localPath string) error
- func (w *WinRM) Put(ctx context.Context, localPath, remotePath string, opts PutOptions) error
- func (w *WinRM) Remove(ctx context.Context, remotePath string) error
- func (w *WinRM) TempPath(base string) string
- type WinRMConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BecomeConfig ¶
type BecomeConfig struct {
Method BecomeMethod // default BecomeSudo
User string // default "root"
Password string // optional; empty assumes passwordless (NOPASSWD sudoers, or doas persist)
// Exe overrides the program the method runs, Ansible's become_exe
// (ansible_become_exe / ANSIBLE_BECOME_EXE). Empty means the
// method's own name, which is what Ansible does: `become_exe` in
// the real sudo plugin reads `self.get_option('become_exe') or
// self.name`. A full path is the usual reason to set it.
Exe string
// Flags replaces the method's default flags, Ansible's become_flags
// (ansible_become_flags / ANSIBLE_BECOME_FLAGS). Empty means the
// method's own default -- "-H -S -n" for sudo, "" for su and doas,
// read from each real become plugin's DOCUMENTATION. Set it and it
// replaces that default outright rather than adding to it, which is
// also what Ansible does.
//
// With a password set, sudo's own plugin strips -n/--non-interactive
// out of the flags before use, since a non-interactive sudo can
// never read the password it was just given. The same stripping
// happens here, including the -n folded into a short cluster like
// -Hn, so a caller that sets Flags and a Password does not have to
// know to remove it.
Flags string
}
BecomeConfig configures privilege escalation, Ansible's `become:`.
type BecomeMethod ¶
type BecomeMethod string
BecomeMethod names a privilege-escalation program.
const ( BecomeSudo BecomeMethod = "sudo" BecomeSu BecomeMethod = "su" BecomeDoas BecomeMethod = "doas" )
type Connection ¶
type Connection interface {
// Exec runs cmd through the target's shell (one command line, not an
// argv — matching how both Ansible and Bolt build task invocations)
// and returns its captured output and exit code. A non-zero exit is
// not itself an error; err is only for a failure to run the command
// at all. stdin, if non-nil, is streamed to the command's standard
// input — how a become/run-as password reaches sudo/su without ever
// appearing in argv or an environment variable.
Exec(ctx context.Context, cmd string, stdin io.Reader) (Result, error)
// Put copies the local file at localPath to remotePath on the
// target.
Put(ctx context.Context, localPath, remotePath string, opts PutOptions) error
// Fetch copies the file at remotePath on the target to the local
// file at localPath.
Fetch(ctx context.Context, remotePath, localPath string) error
// Remove deletes remotePath on the target. Removing a path that
// does not exist is not an error.
Remove(ctx context.Context, remotePath string) error
// TempPath returns a fresh path under the target's temp directory
// ending in base, using the target's own path syntax (forward
// slashes for Local/SSH, backslashes under WinRM).
TempPath(base string) string
// Close releases any resources (an SSH/WinRM connection, an open
// shell).
Close() error
}
Connection is how a caller reaches its target: run a shell command, move files in either direction, and clean up after itself. Every task/module operation is expressed in terms of these primitives so the same higher-level logic runs unchanged whether the connection is local, SSH, or WinRM.
func Become ¶
func Become(conn Connection, cfg BecomeConfig) Connection
Become wraps conn so every Exec runs as BecomeConfig.User via the configured escalation method, and so do Put, Fetch and Remove: they are carried out through that same escalated Exec.
They used to pass through unchanged, on the reasoning that Ansible's become only affects command execution: the controller writes the file into a temporary directory as the connection's own user, and an escalated module moves it into place. That reasoning holds for Ansible and not for the callers of this package. go-ansible's copy, template, lineinfile and blockinfile Put straight to the destination, so with `become: true` on a stock Ubuntu cloud image every one of them failed on a root-owned file:
writing /etc/hosts: transport: put /etc/hosts: exit 1: bash: line 1: /etc/hosts: Permission denied
-- the task asked for root and the write was made as `ubuntu`. Doing the transfer through the wrapper's own Exec makes the file land with the privileges the task asked for, whichever module wrote it. Close still passes through.
Known gap: the wrapper returned here does not implement Streamer, even when conn does. Exec's become handling works by printing a random marker after escalation succeeds and slicing everything before it out of the buffered result (see successMarker below) — doing the equivalent safely on a live stream means scanning the stdout stream for that marker in real time and only starting to hand bytes to the caller once it has been seen, while still correctly forwarding a become password to the escalation program's stdin ahead of the wrapped command's own stdin. That is buildable, but not something to get subtly wrong under time pressure, so it is deliberately left undone rather than shipped half-right: conn.(transport.Streamer) on a Become-wrapped connection fails the type assertion, same as WinRM. Callers needing both become and a live interactive session need to wait for a follow-up that implements this deliberately, or run their interactive session unprivileged.
type EnvExecer ¶ added in v0.4.0
type EnvExecer interface {
ExecEnv(ctx context.Context, cmd string, env map[string]string, stdin io.Reader) (Result, error)
}
EnvExecer is implemented by a Connection that can put environment variables on the command's OWN PROCESS rather than on the command LINE.
⛔ SECURITY, and the reason this exists. A caller that needs to hand a credential to a third-party CLI has two shapes available:
TOKEN=secret some-cli ... // a shell prefix
ExecEnv(ctx, "some-cli ...", map[string]string{"TOKEN": "secret"}, nil)
They are not equivalent, but the difference is NARROWER than it first looks and the measurement is worth recording precisely.
Exec runs its command as `sh -c "<cmd>"`. For a SINGLE command the shell execs straight into it, so nothing keeps that argv and the prefix is not visible anywhere -- measured: `sh -c "VAR=x sleep 4"` leaves only `sleep 4` in the process table. Add a second statement and the shell must stay, and then its argv carries the credential for the command's whole lifetime, for every local user to read:
/bin/sh -c MY_TOKEN=canary-fc8d34e8eadc sleep 4; true
So the exposure is real and demonstrated, and it applies to commands that are not a lone exec -- a pipeline, an `&&`, or an assignment followed by `;`, which is the shape that actually bit in go-ansible/modules' keyring.go (two passwords, one live shell).
A first version of this comment claimed the single-command form was exposed too. It is not; that reading came from a contaminated probe whose own heredoc contained the canary.
Connection.Exec's own doc already states this principle for the become password, which goes through stdin "without ever appearing in argv or an environment variable". ExecEnv is the same principle for everything else.
WHAT IT CANNOT FIX, stated because the limit is real: over SSH the environment is set with the protocol's own env request, which sshd refuses by default for anything outside its AcceptEnv list (usually just LANG and LC_*). A caller that must work against an unmodified sshd still needs the prefix, or stdin, or a mode-0600 file. Real Ansible has the same constraint and resolves it the same way for its own `environment:` keyword. This is therefore a fix for LOCAL connections and for SSH servers configured to accept it, not a universal one -- which is why it is an OPTIONAL interface a caller type-asserts for rather than a method on Connection.
type HTTPDoer ¶ added in v0.1.3
HTTPDoer is the seam through which WinRM performs HTTP requests. The default implementation is an *http.Client; tests inject a fake.
type Local ¶
type Local struct {
// Shell is the interpreter commands are run through. Defaults to
// "sh" resolved via PATH.
Shell string
// TempDir is where TempPath builds paths under. Defaults to
// os.TempDir().
TempDir string
// Dir is the working directory commands run in, and the directory
// a RELATIVE remote path resolves against. Empty (the default)
// means the process's own working directory, which is what this
// connection did before Dir existed.
//
// It exists because "the target" for a local connection is this
// machine, and a caller generally means paths relative to
// something of its own rather than to wherever its process happens
// to have been started. Ansible is the case in point: it runs a
// local module with the working directory set to the PLAYBOOK's
// directory, so `path: files/data.csv` in a playbook resolves
// beside that playbook however the command was invoked -- measured
// against ansible-core 2.21.4, from three different working
// directories, inside a role and out.
Dir string
}
Local is the "local" connection: the target IS the control node. Running the command through a real shell (rather than an argv exec.Command) is not a shortcut — it's what a local task/module runner needs, since a command's args are shell syntax (pipes, redirects, globs) that only a shell interprets.
func NewLocal ¶
func NewLocal() *Local
NewLocal returns a Local connection using "sh" resolved via PATH.
This is deliberately the bare name "sh", not the absolute path "/bin/sh": os/exec's Windows LookPath only recognizes "\" and ":" as path separators, not "/", so an absolute-looking POSIX path like "/bin/sh" is instead treated as a literal bare command name and searched for on %PATH% — where it can never exist — failing every local exec on Windows with "executable file not found in %PATH%". A bare "sh" resolves correctly on both POSIX (finds /bin/sh or /usr/bin/sh via PATH) and Windows (finds Git for Windows' sh.exe, which GitHub's windows-latest runners — and most developer machines with Git installed — already have on PATH).
func (*Local) ExecEnv ¶ added in v0.4.0
func (l *Local) ExecEnv(ctx context.Context, cmd string, env map[string]string, stdin io.Reader) (Result, error)
ExecEnv is Exec with env set on the child PROCESS rather than on the command line, so a credential never reaches the shell's argv. See EnvExecer for why that distinction matters.
func (*Local) NewSession ¶ added in v0.1.4
NewSession opens a live streaming session for a local command, implementing Streamer. The command itself is not known until Start, so this builds the *exec.Cmd now with an empty placeholder "-c" argument, which Start fills in later — letting StdinPipe/StdoutPipe/StderrPipe use os/exec's own Cmd.StdinPipe/StdoutPipe/StderrPipe (real OS pipes) instead of a manually bridged io.Pipe.
That distinction matters, and cost a real deadlock to learn: assigning an arbitrary io.Reader/io.Writer to Cmd.Stdin/Stdout/Stderr makes os/exec spawn its own internal copy goroutine, and Cmd.Wait blocks until every one of those finishes — including the stdin one, which only sees io.EOF once the write end is closed. A caller that requests StdinPipe (as any real interactive session does, to be able to forward keystrokes) but has no need to write anything — a command that doesn't read stdin, or a caller closing the session on an unrelated event before ever touching stdin — would then hang in Wait forever, since nothing else ever closes that write end. Cmd's own *Pipe methods don't have this problem: they hand back a real OS pipe end directly, with no bridging goroutine for Wait to wait on.
type PutOptions ¶
type PutOptions struct {
// Executable chmods the remote file +x after writing (POSIX targets
// only; WinRM implementations ignore it — Windows has no exec bit).
Executable bool
// MkdirParents creates the destination's parent directory first.
MkdirParents bool
}
PutOptions configures a file upload.
type Result ¶
Result is the outcome of running one command: its captured output and exit code.
func ExecWithEnv ¶ added in v0.4.0
func ExecWithEnv(ctx context.Context, conn Connection, cmd string, env map[string]string, stdin io.Reader) (res Result, onProcess bool, err error)
ExecWithEnv runs cmd with env set on the process when conn can do that, and otherwise falls back to a shell prefix -- so a caller gets the safer behaviour wherever it is available without having to branch.
The fallback is NOT silent in the sense that matters: it is the same exposure the caller would have had anyway, never a weaker one, and the ok return says which happened so a caller that must not leak can refuse instead.
type SSH ¶
type SSH struct {
// contains filtered or unexported fields
}
SSH is a live SSH connection to one target host. Every Exec opens its own session, matching how SSH sessions work (one command per session).
func DialSSH ¶
DialSSH connects and authenticates to cfg.Host, trying (in order) explicit password, private key, and ssh-agent — whichever of those cfg populates.
func (*SSH) NewSession ¶ added in v0.1.4
NewSession opens a live streaming session over a fresh *ssh.Session on the already-dialed client, implementing Streamer. TTY is honored the same best-effort way Exec honors it: a RequestPty before Start, ignored if the server refuses it.
type SSHConfig ¶
type SSHConfig struct {
Host string
Port int // default 22
User string
Password string // optional
PrivateKeyFile string // optional, e.g. ~/.ssh/id_ed25519
PrivateKeyBytes []byte // optional, an already-loaded key (e.g. from inventory)
PrivateKeyPassphrase string
UseAgent bool // authenticate via SSH_AUTH_SOCK
// HostKeyCheck: when true (the default), the remote host key is
// verified against KnownHostsFile (default ~/.ssh/known_hosts) and
// the connection is refused on any mismatch or unknown host.
HostKeyCheck bool
KnownHostsFile string
// TempDir is the remote directory TempPath builds paths under.
// Defaults to /tmp.
TempDir string
// TTY requests a pseudo-terminal for every Exec (best-effort: a
// server that refuses it does not fail the command).
TTY bool
// ProxyJump reaches the target THROUGH one or more bastions,
// dialled in order: the first the ordinary way, each later one
// through the previous, and the last one carrying the target's own
// handshake.
//
// Hops are given already resolved rather than as OpenSSH's
// `[user@]host[:port]` string, because the caller is what knows the
// syntax it came from -- go-ansible/playbook parses
// `ansible_ssh_common_args: -o ProxyJump=...` along with the rest
// of that option surface, and a second parser here would be one to
// keep in step for nothing.
//
// A hop need only name its Host; User defaults to the target's and
// Port to 22, and a hop that names no credential of its own
// inherits the target's, which is what makes a bare
// `-o ProxyJump=bastion` work with the key the run already has. A
// hop that DOES name its own key keeps it. See
// inheritJumpCredentials.
ProxyJump []SSHConfig
Timeout time.Duration // default 30s
// Dialer establishes the transport-level TCP connection. Defaults
// to net.Dialer honoring Timeout. Tests inject a dialer that reaches
// an in-process server.
Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
}
SSHConfig configures an SSH connection to a Unix-like remote host.
type Session ¶ added in v0.1.4
type Session interface {
// StdinPipe returns a writer for the session's standard input. Call
// before Start.
StdinPipe() (io.WriteCloser, error)
// StdoutPipe returns a reader streaming standard output as it is
// produced. Call before Start.
StdoutPipe() (io.Reader, error)
// StderrPipe returns a reader streaming standard error as it is
// produced. Call before Start.
StderrPipe() (io.Reader, error)
// Start begins running cmd. Non-blocking.
Start(cmd string) error
// Wait blocks until the command exits and returns its exit code. A
// non-zero exit is not itself an error; err is only for a failure to
// wait on the command at all.
Wait() (int, error)
// Close releases the session's resources, terminating the remote
// process if Wait has not yet returned, and closing the readers
// StdoutPipe/StderrPipe returned. Safe to call after Wait, and safe
// to call more than once.
//
// ORDER MATTERS when a caller drains those readers from their own
// goroutines. A session's output survives the reap on purpose, so
// the read ends stay open until Close and nothing else closes them:
//
// - command exited on its own: drain first, then Close. Everything
// it wrote is readable, and the readers reach EOF by themselves.
// - you are force-closing it: Close FIRST, then join the drainers.
// EOF only arrives once every holder of the write end is gone,
// and a grandchild the command forked is not covered by killing
// the command. Waiting for that EOF never returns.
Close() error
}
Session is a live, streaming command execution — the interactive counterpart to Connection.Exec's single buffered call. Where Exec hands back one Result after the command has already finished, a Session lets the caller drive its stdin/stdout/stderr directly as plain pipes while the command runs: an interactive multi-host shell needs to forward keystrokes to a remote process and show its output as it is produced, not after the fact. Nothing is buffered by the library — the caller owns pacing and framing.
The usual sequence is: request the pipes you need, Start the command, pump stdin/stdout/stderr concurrently, then Wait for the exit code. Close releases the session's resources at any point, terminating the remote process if Wait has not yet returned.
type Streamer ¶ added in v0.1.4
Streamer is implemented by a Connection that can also open a live Session for callers needing real-time interactivity instead of Exec's buffered result. Local and SSH implement it.
WinRM does not: WS-Management's Command/Send/Receive shell protocol is poll-based (Receive returns whatever output has accumulated since the last poll, not a continuous stream), so it does not map onto the continuous-pipe shape Session assumes without either fabricating fake liveness or accepting periodic stalls that would silently degrade what "streaming" promises. Left unimplemented rather than shipped half-true.
Become-wrapped connections also do not implement Streamer, for a narrower reason: see Become's doc comment.
Callers should type-assert (conn.(transport.Streamer)) and handle absence explicitly, not assume every Connection supports it.
type WinRM ¶
type WinRM struct {
// contains filtered or unexported fields
}
WinRM is a live WinRM connection to one target host: one WS-Man shell, opened at Dial time and reused across every Exec/Put/Fetch/Remove until Close.
func DialWinRM ¶
func DialWinRM(ctx context.Context, cfg WinRMConfig) (*WinRM, error)
DialWinRM opens a WS-Man shell on cfg.Host.
func (*WinRM) Exec ¶
Exec runs cmd through cmd.exe /c on the target's open shell. cmd is parsed by cmd.exe itself, with its own quoting rules; a caller that already has a program and a clean argv (no cmd.exe requoting wanted — e.g. running an uploaded executable with arguments that may contain spaces or quotes) should use WinRM.ExecArgv instead.
func (*WinRM) ExecArgv ¶ added in v0.1.2
func (w *WinRM) ExecArgv(ctx context.Context, command string, args []string, stdin io.Reader) (Result, error)
ExecArgv runs command with args as the WS-Man protocol's own Command and Arguments elements — no cmd.exe requoting layer, so each argument reaches the target byte for byte regardless of embedded spaces or quotes. Prefer this over Exec whenever the caller already has a program and a clean argv.
func (*WinRM) Fetch ¶
Fetch reads remotePath by streaming its base64 encoding to stdout via PowerShell.
type WinRMConfig ¶
type WinRMConfig struct {
Host string
Port int // default 5986 (SSL) or 5985
User string
Password string
SSL bool // whether to reach the endpoint over TLS
CACert string // path to a CA certificate PEM (custom trust root)
// InsecureSkipVerify disables TLS certificate verification. It is
// named for what it does, and phrased so the dangerous state is the
// one a caller has to type -- the same reason crypto/tls spells its
// own field this way.
//
// It replaces an SSLVerify field whose comment said "default true"
// while the code read `if !c.SSLVerify { InsecureSkipVerify = true }`.
// A bool's zero value is false, so every config that did not mention
// verification got none, and the credentials this connection sends as
// HTTP Basic went to an unverified endpoint. The rename is deliberate
// rather than a flip of the old field's meaning: inverting SSLVerify
// in place would have changed both known callers' behaviour with no
// compile error to notice it by, and one of them was relying on the
// accidental default. A build failure at each call site is the point.
InsecureSkipVerify bool
// Transport selects the auth scheme: "negotiate" (default), "basic",
// or "ssl" (TLS client certificate — set ClientCert/ClientKey).
Transport string
ClientCert string
ClientKey string
ConnectTimeout time.Duration // default 60s
TempDir string // default `C:\Windows\Temp`
Path string // WS-Man endpoint path, default "/wsman"
// Environment is set on the shell at creation time (the WS-Man
// protocol's own Environment block), so every Exec/ExecArgv on this
// connection sees these variables — e.g. Bolt's PT_-prefixed task
// parameters for the "environment" input method.
Environment map[string]string
// NewDoer builds the HTTP client used to reach the target. Defaults
// to buildWinRMClient(cfg). Tests inject a doer that reaches an
// in-process WS-Man server.
NewDoer func(WinRMConfig) (HTTPDoer, error)
}
WinRMConfig configures a WinRM connection to a Windows remote host. It speaks WS-Management (SOAP 1.2 with WS-Addressing over HTTP/HTTPS) driving the MS-WSMV shell protocol: Create shell -> Command -> Send (stdin) -> Receive (stdout/stderr/CommandState/ExitCode) -> Signal (terminate) -> Delete shell.
Authentication is selected by Transport: "basic" sends HTTP Basic credentials, "negotiate" (the default) performs NTLM via the pure-Go github.com/Azure/go-ntlmssp round-tripper, and "ssl" uses TLS client-certificate authentication. Kerberos is not implemented.