Documentation
¶
Overview ¶
Package sshx dials hosts from the inventory (with jump-host chains, keychain/agent/password auth and TOFU host-key checking) and runs commands.
Index ¶
- Constants
- Variables
- func BuildCommand(c *Client, r RunRequest) (cmd string, sudoPass []byte, err error)
- func CleanPSError(s string) string
- func CountPinned(addr string) (int, error)
- func EnvExports(c *Client, extra map[string]string) (string, error)
- func ExpandProxy(cmd string, h *store.Host) string
- func FetchHostKey(ctx context.Context, st *store.State, h *store.Host, o Options) (ssh.PublicKey, error)
- func HeldFor(st *store.State, h *store.Host) *store.SSHLogin
- func HostKeyMismatch(host, offered string, pinned []string) *out.Error
- func HostKeyRevoked(key ssh.PublicKey) (bool, error)
- func HostKeys() hostKeyStore
- func Output(ctx context.Context, c *Client, cmd string, timeout time.Duration) (string, int, error)
- func PSArray(items []string) string
- func PSQ(s string) string
- func ParseAdhoc(s string) (*store.Host, error)
- func PinHostKey(addr string, key ssh.PublicKey, replace bool) (removed int, err error)
- func PinnedKeys(addr string) ([]ssh.PublicKey, error)
- func Pipe(ctx context.Context, c *Client, cmd string, stdin io.Reader, stdout io.Writer) (int, string, error)
- func Q(s string) string
- func RevokeHostKeys(keys []ssh.PublicKey) error
- func Route(st *store.State, h *store.Host) (*store.Vpn, []*store.Host, error)
- func RunPS(ctx context.Context, c *Client, script string, stdin io.Reader, ...) (int, string, error)
- func SameKey(a, b ssh.PublicKey) bool
- func SaveInput(n int, path string) string
- func ServeHeld(ln net.Listener, up *ssh.Client, token string, key ssh.Signer, used func())
- func TargetOf(h *store.Host) string
- func ValidEnvKey(k string) bool
- func WithDeadlines(c net.Conn) net.Conn
- func WritePinsFile(dir string, addrs []string) (string, error)
- type Client
- type NewHostKey
- type Options
- type RunRequest
- type RunResult
- type WinShell
Constants ¶
const HeldUser = "komando"
HeldUser is the user name a command logs in to a held login's socket with.
const PSTrailer = "\nif (-not $?) { if ($LASTEXITCODE) { exit $LASTEXITCODE }; exit 1 }; $global:LASTEXITCODE = 0\n"
PSTrailer ends every script: a failed last command (a cmdlet error, or a program that exited non-zero) makes the exit code non-zero, as in sh.
Variables ¶
var Ask func(host, text string, hidden bool) (string, error)
Ask, when set, asks the person at the terminal what a login wants that komando cannot answer by itself: the password when none is stored, a one-time code (a two-factor login), or any other question the server asks, the way ssh does. host is the server's name in komando; text is the server's own words (Ask makes them safe to show); hidden is false only when the server lets the answer show. The cli sets it only for a person at a terminal: AI agents, scripts and background processes have no one to ask, so they get NO_AUTH or AUTH_FAILED with a hint instead.
var HostKeyPinned func(NewHostKey)
HostKeyPinned, when set, is told about every key pinned on first use (for the output and the audit log). It is called after the pin is saved, while the connection waits: it must be quick.
var PSCommandLine = "powershell -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -EncodedCommand " + encodePS(psBootstrap)
PSCommandLine is the program komando starts on a Windows server.
var PersonAtTerminal = func() bool { return false }
PersonAtTerminal reports whether a person at a terminal (not an AI agent or a script) runs komando; the cli package sets it. Only a person is told how to re-trust a changed host key.
Functions ¶
func BuildCommand ¶
func BuildCommand(c *Client, r RunRequest) (cmd string, sudoPass []byte, err error)
BuildCommand assembles the final remote shell line: env exports, cd, sudo wrapping. Exposed so --dry-run can show exactly what would run. sudoPass is the sudo password Run feeds on request, or nil. Values from the vault are not in it: Run sends them on the command's input. On a Windows server cmd is the PowerShell script (docs/design/windows.md).
func CleanPSError ¶ added in v0.9.0
CleanPSError turns PowerShell's error stream back into text, without PowerShell 7's colours. Windows PowerShell writes it as CLIXML ("#< CLIXML" and XML) when its output is not a console, as over SSH or WinRM: the error and warning lines are kept, progress records are dropped.
func CountPinned ¶ added in v0.12.0
CountPinned is how many pins addr (host:port) has, as PinHostKey with replace would remove them, keys that no longer parse included.
func EnvExports ¶ added in v0.2.0
EnvExports is the "export K='v'; " prefix for the host's env and extra, secret values included: for a private file only (job start), never for a command line.
func ExpandProxy ¶ added in v0.2.0
ExpandProxy fills in a ProxyCommand's tokens like OpenSSH: %h address, %p port, %r user, %n the name in the inventory, %% a percent sign.
func FetchHostKey ¶ added in v0.3.0
func FetchHostKey(ctx context.Context, st *store.State, h *store.Host, o Options) (ssh.PublicKey, error)
FetchHostKey fetches h's host key the way komando reaches h: directly, through a VPN, its proxy command or its --via path (those jump hosts are checked and logged in to as usual). h itself is not logged in to, and nothing is pinned for it: the caller compares and decides.
func HostKeyMismatch ¶ added in v0.3.0
HostKeyMismatch is the error for a server whose key is not the pinned one: offered is the fingerprint the server shows now, pinned the ones komando knows (either may be empty). An AI agent must not re-trust the key itself: it reports it, and a person checks the new fingerprint another way first.
func HostKeyRevoked ¶ added in v0.12.0
HostKeyRevoked reports key on the never-trust list.
func HostKeys ¶ added in v0.12.0
func HostKeys() hostKeyStore
HostKeys is where this process keeps pins: the sealed store.
func PSQ ¶ added in v0.9.0
PSQ quotes s as a PowerShell string literal: in single quotes nothing is expanded, and a quote is doubled. PowerShell also reads the typographic single quotes (U+2018 to U+201B) as quotes, so they are doubled too.
func ParseAdhoc ¶
ParseAdhoc parses user@host:port into a transient host.
func PinHostKey ¶ added in v0.3.0
PinHostKey pins key for addr (host:port). A key that is pinned already is not written again. With replace, every other pin for addr goes in the same write, so a failure never leaves the server unpinned; removed is how many pins went. Whether a pin may be replaced is the caller's decision (`host trust --reset` needs a person's yes).
func PinnedKeys ¶ added in v0.3.0
PinnedKeys are the keys komando pins for addr (host:port).
func Pipe ¶ added in v0.2.0
func Pipe(ctx context.Context, c *Client, cmd string, stdin io.Reader, stdout io.Writer) (int, string, error)
Pipe runs cmd and streams stdin and stdout without buffering them, for transfers too large for Run (database dumps). stderr is kept up to 64 KiB for error messages. It returns the remote exit code.
func RevokeHostKeys ¶ added in v0.12.0
RevokeHostKeys puts keys on the never-trust list: no server is reached with them, and none can be pinned (`komando seal` carries a known_hosts file's @revoked lines here).
func Route ¶ added in v0.2.0
Route is the way to h: the VPN it starts with (or nil) and the SSH hops, ending with h itself. It reads the inventory only; nothing is dialed.
func RunPS ¶ added in v0.9.0
func RunPS(ctx context.Context, c *Client, script string, stdin io.Reader, stdout io.Writer) (int, string, error)
RunPS runs a PowerShell script on a Windows server with stdin after it and stdout streamed, for transfers too large for Run. stderr is kept up to 64 KiB. It returns the script's exit code.
func SaveInput ¶ added in v0.7.0
SaveInput is a shell step that saves the next n bytes of the command's input in path (a shell word, such as "$d/env"), readable by the user only. dd reads one byte at a time, so what follows on the input stays for the next step: secrets go to a file without being on a command line.
func ServeHeld ¶ added in v0.8.0
ServeHeld serves up (the person's login) on ln until ln closes: each client that knows token gets its channels passed on to up.
func TargetOf ¶ added in v0.8.0
TargetOf is where h logs in: a held login is used only while the host still points there (a changed address or user logs in anew).
func ValidEnvKey ¶ added in v0.2.0
ValidEnvKey reports whether k can be exported as a shell variable. The key goes into the command line unquoted, so anything else could inject a command.
func WithDeadlines ¶ added in v0.2.0
SSH channels do not support deadlines, but database and cache clients use them for timeouts. WithDeadlines emulates them: when a deadline passes the connection is closed, which ends any blocked read or write.
Types ¶
type Client ¶
type Client struct {
*ssh.Client
Host *store.Host
// Win carries the commands of a Windows server reached over WinRM;
// such a Client has no SSH connection (Client is nil)
Win WinShell
// contains filtered or unexported fields
}
Client wraps an ssh.Client plus the path of jump hosts it came through (shared with other servers behind the same hosts, shared.go).
type NewHostKey ¶ added in v0.7.0
type NewHostKey struct {
Host string `json:"host"`
Address string `json:"address"`
Type string `json:"key_type"`
Fingerprint string `json:"fingerprint"` // SHA256:...
}
NewHostKey is a host key komando pinned the first time it reached a server (host_key_policy accept-new). Nothing proved that key is the server's: the person compares the fingerprint with the server's own.
type Options ¶
type Options struct {
ConnectTimeout time.Duration
HostKeyPolicy string // accept-new | strict | off
// Fresh logs in anew, never through a held login (`komando login`
// itself, which makes one)
Fresh bool
// WinRM lets Dial reach a server over WinRM: the caller only runs
// PowerShell through Run and RunPS (no SSH sessions, no tunnels)
WinRM bool
// contains filtered or unexported fields
}
type RunRequest ¶
type RunRequest struct {
Command string
Sudo bool
SudoUser string
Env map[string]string
NoEnv bool // leave out the exports (job start writes them to a private file instead)
Dir string
Stdin []byte
Timeout time.Duration
MaxOutput int
PTY bool
// Stream, when set, receives output chunks as they arrive (follow mode).
Stream func(stream string, chunk []byte)
}
type RunResult ¶
type RunResult struct {
ExitCode int `json:"exit_code"`
Stdout string `json:"stdout"`
Stderr string `json:"stderr"`
StdoutTruncated bool `json:"stdout_truncated,omitempty"`
StderrTruncated bool `json:"stderr_truncated,omitempty"`
TimedOut bool `json:"timed_out,omitempty"`
Signal string `json:"signal,omitempty"`
DurationMS int64 `json:"duration_ms"`
}