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
- Variables
- func DefaultKeyDirectory() string
- func DefaultKnownHostsPath() string
- func DefaultProfilesPath() string
- func LoadProfiles(filename string) (map[string]Profile, error)
- func NormalizeHostPort(host, port string) (string, string, error)
- func PrepareOpenSSHArgs(options OpenSSHCommandOptions) ([]string, error)
- func PrepareOpenSSHTestArgs(options OpenSSHCommandOptions) ([]string, error)
- func ResolveLocalPath(value string) (string, error)
- func ResolveOpenSSHExecutable(tool string) (string, error)
- func TestOpenSSHConnection(ctx context.Context, options OpenSSHCommandOptions) error
- func TrustHostKeys(filename, host, port, keys string) error
- func ValidateHost(host string) error
- func ValidateIdentityFile(identityFile string) error
- func ValidateProfile(profile Profile) error
- func ValidateTimeout(timeout time.Duration) (time.Duration, error)
- func ValidateUsername(username string) error
- type Client
- type CommandResult
- type ConfigAlias
- type GeneratedKey
- type HostKeyFingerprint
- type OpenSSHCommandOptions
- type Profile
Constants ¶
const ( DefaultCommandTimeout = 30 * time.Second MaxOperationTimeout = 10 * time.Minute MaxCommandOutputBytes = 1 << 20 )
Variables ¶
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.
var ErrOutputLimit = errors.New("ssh command output exceeded the configured byte limit")
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 ¶
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
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 ResolveLocalPath ¶ added in v0.2.0
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
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
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
ValidateHost validates a host or OpenSSH alias accepted by the local app.
func ValidateIdentityFile ¶ added in v0.2.0
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
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 ¶
ValidateTimeout applies the command and transfer timeout defaults and hard maximum.
func ValidateUsername ¶ added in v0.2.0
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 ¶
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.
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
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.