ssh

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 21 Imported by: 1 module, 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 ssh provides strict, profile-based SSH command execution for local NFlow runtimes. Hostnames, usernames, identities, and known_hosts trust are loaded from local profiles; commands are bounded and every host key is checked against OpenSSH known_hosts data.

Package ssh provides profile-based SSH command execution.

Typical use:

profiles, err := ssh.LoadProfiles(ssh.DefaultProfilesPath())
result, err := ssh.RunCommand(ctx, profiles["staging"], "uname -s", 30*time.Second)

Index

Constants

View Source
const (
	DefaultCommandTimeout = 30 * time.Second
	MaxOperationTimeout   = 10 * time.Minute
	MaxCommandOutputBytes = 1 << 20
)

Variables

View Source
var ErrNoMatchingHostKey = errors.New("ssh trust: no matching valid host key; fetch the fingerprint again")

ErrNoMatchingHostKey indicates that a trust request contained no valid key for the exact requested host and port.

View Source
var ErrOutputLimit = errors.New("ssh command output exceeded the configured byte limit")
View Source
var ErrTrustInputLimit = errors.New("ssh trust: key data exceeds allowed limits")

ErrTrustInputLimit indicates the supplied host-key data exceeds safe bounds.

Functions

func DefaultKeyDirectory added in v0.2.0

func DefaultKeyDirectory() string

DefaultKeyDirectory returns the current user's ~/.ssh directory.

func DefaultKnownHostsPath added in v0.2.0

func DefaultKnownHostsPath() string

DefaultKnownHostsPath returns the current user's ~/.ssh/known_hosts path.

func DefaultProfilesPath

func DefaultProfilesPath() string

DefaultProfilesPath returns the platform's user config path for SSH profiles.

func LoadProfiles

func LoadProfiles(filename string) (map[string]Profile, error)

LoadProfiles reads and validates a bounded YAML profile file. Relative identity and known_hosts paths are resolved relative to the profile file.

func NormalizeHostPort added in v0.2.0

func NormalizeHostPort(host, port string) (string, string, error)

NormalizeHostPort validates a host and port and supplies the OpenSSH default port when the caller leaves it blank.

func PrepareOpenSSHArgs added in v0.2.0

func PrepareOpenSSHArgs(options OpenSSHCommandOptions) ([]string, error)

PrepareOpenSSHArgs returns arguments for an interactive `ssh` invocation. The executable name is intentionally separate so the host can choose its process/session lifecycle and shell quoting.

func PrepareOpenSSHTestArgs added in v0.2.0

func PrepareOpenSSHTestArgs(options OpenSSHCommandOptions) ([]string, error)

PrepareOpenSSHTestArgs returns strict, non-interactive arguments for one key-authentication test. Host-key checking remains enabled.

func PrepareWindowsPrivateKey added in v0.3.0

func PrepareWindowsPrivateKey(string) error

PrepareWindowsPrivateKey is available only on Windows.

func ResolveLocalPath added in v0.2.0

func ResolveLocalPath(value string) (string, error)

ResolveLocalPath expands the current user's ~ home and returns an absolute path. Environment-variable expansion remains the caller's responsibility.

func ResolveOpenSSHExecutable added in v0.2.0

func ResolveOpenSSHExecutable(tool string) (string, error)

ResolveOpenSSHExecutable locates a supported OpenSSH client tool. On Windows it also checks the standard System32\\OpenSSH installation directory.

func TestOpenSSHConnection added in v0.2.0

func TestOpenSSHConnection(ctx context.Context, options OpenSSHCommandOptions) error

TestOpenSSHConnection performs a bounded, batch-mode authentication test with OpenSSH. When IdentityFile is empty, OpenSSH's normal local key and agent discovery remains available (including Windows OpenSSH agent integration).

func TrustHostKeys added in v0.2.0

func TrustHostKeys(filename, host, port, keys string) error

TrustHostKeys appends only valid public-key lines whose host token exactly matches the requested host and port. Call this only after an explicit trust decision by the user; fingerprinting alone never changes trust state.

func ValidateHost added in v0.2.0

func ValidateHost(host string) error

ValidateHost validates a host or OpenSSH alias accepted by the local app.

func ValidateIdentityFile added in v0.2.0

func ValidateIdentityFile(identityFile string) error

ValidateIdentityFile validates an optional local key path before it is used in an OpenSSH command. Shell quoting remains the responsibility of the host.

func ValidateProfile added in v0.2.0

func ValidateProfile(profile Profile) error

ValidateProfile checks the same constraints applied to profiles loaded from YAML. Hosts adapting another local profile store should use it before passing the profile to Dial, RunCommand, or SFTP operations.

func ValidateTimeout

func ValidateTimeout(timeout time.Duration) (time.Duration, error)

ValidateTimeout applies the command and transfer timeout defaults and hard maximum.

func ValidateUsername added in v0.2.0

func ValidateUsername(username string) error

ValidateUsername validates an optional OpenSSH username.

Types

type Client

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

Client owns an SSH connection and any local SSH-agent socket used to authenticate.

func Dial

func Dial(ctx context.Context, profile Profile) (*Client, error)

Dial opens a bounded SSH connection and rejects every host key absent from the profile's OpenSSH known_hosts file. It never enables agent forwarding.

func (*Client) Close

func (c *Client) Close() error

Close closes the SSH connection and its optional local agent connection.

func (*Client) SSHClient

func (c *Client) SSHClient() *cryptossh.Client

SSHClient exposes the underlying client for focused protocol adapters such as SFTP.

type CommandResult

type CommandResult struct {
	Stdout   string        `json:"stdout"`
	Stderr   string        `json:"stderr"`
	ExitCode int           `json:"exit_code"`
	Duration time.Duration `json:"duration"`
}

CommandResult contains bounded command output and the remote process exit code.

func RunCommand

func RunCommand(ctx context.Context, profile Profile, command string, timeout time.Duration) (CommandResult, error)

RunCommand executes one remote shell command with an overall timeout. A non-zero remote exit code is returned in the result; connection and execution failures are returned as errors.

type ConfigAlias added in v0.2.0

type ConfigAlias struct {
	Alias string `json:"alias"`
}

ConfigAlias is a non-secret SSH host alias read from a local OpenSSH config.

func ListOpenSSHConfigAliases added in v0.2.0

func ListOpenSSHConfigAliases() ([]ConfigAlias, error)

ListOpenSSHConfigAliases lists concrete Host aliases from the current user's ~/.ssh/config, following bounded Include directives. Wildcard and negated Host patterns are not returned. This lists names only; it does not evaluate every OpenSSH directive or make an alias an authenticated SSH profile.

func ReadOpenSSHConfigAliases added in v0.2.0

func ReadOpenSSHConfigAliases(filename string) ([]ConfigAlias, error)

ReadOpenSSHConfigAliases lists aliases from a specific OpenSSH config file. A missing config file is treated as an empty config, matching OpenSSH's optional per-user configuration behavior.

type GeneratedKey added in v0.2.0

type GeneratedKey struct {
	PrivateKeyPath string `json:"privateKeyPath"`
	PublicKey      string `json:"publicKey"`
}

GeneratedKey describes the paths and public half of a generated Ed25519 key. Private key material is never returned.

func GenerateEd25519Key added in v0.2.0

func GenerateEd25519Key(directory, name string) (GeneratedKey, error)

GenerateEd25519Key creates an unencrypted OpenSSH-format Ed25519 key pair. Existing files are never overwritten; a numeric suffix is chosen instead.

type HostKeyFingerprint added in v0.2.0

type HostKeyFingerprint struct {
	Host        string `json:"host"`
	Port        string `json:"port"`
	Fingerprint string `json:"fingerprint"`
	Keys        string `json:"keys"`
}

HostKeyFingerprint is an untrusted server-key observation. It is not written to known_hosts until TrustHostKeys is explicitly called.

func FingerprintHostKey added in v0.2.0

func FingerprintHostKey(ctx context.Context, host, port string) (HostKeyFingerprint, error)

FingerprintHostKey performs an unauthenticated SSH key exchange to capture the presented host key. It never modifies known_hosts or authenticates a user.

type OpenSSHCommandOptions added in v0.2.0

type OpenSSHCommandOptions struct {
	Host         string
	Username     string
	Port         string
	IdentityFile string
}

OpenSSHCommandOptions contains locally stored values used to prepare an interactive OpenSSH command. It is not intended to be populated from an untrusted remote workflow request.

type Profile

type Profile struct {
	Host           string `yaml:"host"`
	Port           int    `yaml:"port"`
	Username       string `yaml:"username"`
	IdentityFile   string `yaml:"identity_file"`
	KnownHostsFile string `yaml:"known_hosts_file"`
	UseAgent       bool   `yaml:"use_agent"`
}

Profile describes one local SSH identity and its strict host-key trust file. Its fields are loaded from a local YAML file, never from an NFlow request.

Jump to

Keyboard shortcuts

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