ssh

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package ssh manages connections, command execution, and SFTP transfers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReconstructCommand

func ReconstructCommand(pipeline *parser.Pipeline, isPSQL bool, toolkitPath bool) string

func ShellQuote

func ShellQuote(token string) string

func ValidHostKeyMode

func ValidHostKeyMode(mode string) bool

ValidHostKeyMode reports whether mode is a recognized host key verification mode.

Types

type Client

type Client interface {
	Execute(ctx context.Context, command string, timeout time.Duration) (ExecResult, error)
	SFTPSession() (SFTPClient, error)
	Close() error
}

type ConnectionParams

type ConnectionParams struct {
	Host         string
	User         string
	Port         int
	IdentityFile string
	Password     string // SSH password auth
	Passphrase   string // decrypt passphrase-protected key
}

type Dialer

type Dialer interface {
	Dial(ctx context.Context, params ConnectionParams) (Client, error)
}

type ExecResult

type ExecResult struct {
	Stdout    string
	Stderr    string
	ExitCode  int
	RuntimeMs int
}

type HostKeyError

type HostKeyError struct {
	Message string
}

HostKeyError is a user-facing error for host key verification failures.

func (*HostKeyError) Error

func (e *HostKeyError) Error() string

type HostKeyMode

type HostKeyMode string

HostKeyMode controls how SSH host keys are verified.

const (
	// HostKeyAcceptNew accepts unknown hosts on first connect (TOFU),
	// writes their key to known_hosts, and rejects key changes.
	HostKeyAcceptNew HostKeyMode = "accept-new"

	// HostKeyStrict requires the host key to already exist in known_hosts.
	HostKeyStrict HostKeyMode = "strict"

	// HostKeyOff disables host key verification entirely.
	HostKeyOff HostKeyMode = "off"
)

type ManagedConnection

type ManagedConnection struct {
	Client Client
	Params ConnectionParams
}

type Option

type Option func(*SSHManager)

func WithConnectTimeout

func WithConnectTimeout(timeout time.Duration) Option

func WithHostKeyChecking

func WithHostKeyChecking(mode HostKeyMode) Option

func WithKnownHostsFile

func WithKnownHostsFile(path string) Option

func WithRetries

func WithRetries(retries int) Option

func WithRetryBackoff

func WithRetryBackoff(backoff time.Duration) Option

type SFTPClient

type SFTPClient interface {
	Stat(path string) (os.FileInfo, error)
	Open(path string) (io.ReadCloser, error)
	Create(path string) (io.WriteCloser, error)
	MkdirAll(path string) error
	Chmod(path string, mode os.FileMode) error
	Close() error
}

type SSHManager

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

func NewSSHManager

func NewSSHManager(dialer Dialer, opts ...Option) *SSHManager

func (*SSHManager) Connect

func (m *SSHManager) Connect(ctx context.Context, params ConnectionParams) error

func (*SSHManager) Connected

func (m *SSHManager) Connected() bool

func (*SSHManager) Disconnect

func (m *SSHManager) Disconnect(_ context.Context, host string) error

func (*SSHManager) Execute

func (m *SSHManager) Execute(ctx context.Context, host, command string, timeout time.Duration) (ExecResult, error)

func (*SSHManager) ExecuteRaw

func (m *SSHManager) ExecuteRaw(ctx context.Context, host, command string, timeout time.Duration) (ExecResult, error)

func (*SSHManager) ResolveConnection

func (m *SSHManager) ResolveConnection(host string) (*ManagedConnection, error)

func (*SSHManager) SFTPSession

func (m *SSHManager) SFTPSession(host string) (SFTPClient, error)

type SystemSSHDialer

type SystemSSHDialer struct {
	// ControlDir is the directory for ControlMaster sockets.
	// Defaults to /tmp/shellguard-ctl.
	ControlDir string
	// contains filtered or unexported fields
}

SystemSSHDialer implements Dialer by spawning the system ssh binary. It uses ControlMaster for connection multiplexing so that only the first Dial pays the SSH handshake cost; subsequent Execute calls reuse the existing control socket.

func (*SystemSSHDialer) CheckBinary

func (d *SystemSSHDialer) CheckBinary() bool

CheckBinary returns true if ssh is available in PATH.

func (*SystemSSHDialer) Dial

func (d *SystemSSHDialer) Dial(ctx context.Context, params ConnectionParams) (Client, error)

Dial establishes a ControlMaster connection by spawning a background ssh process. The process exits after ControlPersist seconds of idle.

type XCryptoDialer

type XCryptoDialer struct {
	ConnectTimeout time.Duration
	HostKeyMode    HostKeyMode
	KnownHostsFile string
}

func (*XCryptoDialer) Dial

func (d *XCryptoDialer) Dial(ctx context.Context, params ConnectionParams) (Client, error)

Jump to

Keyboard shortcuts

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