ssh

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: Apache-2.0 Imports: 19 Imported by: 0 modules, 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 is the ssh consumer of a secret (ADR-28, ADR-35): ssh_exec, a built-in tool that runs one command on a server with an SSH key the model names by its alias and never sees, and the workspace's trusted host keys (KnownHosts).

A call goes through four doors, each its own and in this order:

  1. The command is parsed into words (sshgate.Parse); what a shell would interpret is refused, and what leaves is every word single-quoted (Argv.Wire), so the login shell on the server runs exactly those words. The words are scanned for a stored secret before they leave.
  2. The permission tree decides "<user>@<host> <command>" (sshgate.Kind): the user is the secret's login there (Keys.Login), never the model's choice.
  3. The host's key: a host with a trusted key must present exactly that key, and a changed one is refused until the owner removes the old one; a host nobody trusted yet is asked about once, its key read by a connection that ends before anything is authenticated (sshgate.HostKeyKind), and kept in the workspace's ssh-hosts.json when approved. A host key is never trusted blindly.
  4. The secret: the tree decides "<alias>@<host>" (secret.UseKind) and the key is resolved host-side (Keys.Key); the leak scanner learns it, its lines and its passphrase.

The model gets the exit code, stdout and stderr, each cut at Limits.MaxOutput with a marker saying how much was not shown, and a command that runs past its timeout is killed.

Index

Constants

View Source
const (
	DefaultTimeout    = time.Minute
	DefaultMaxTimeout = 10 * time.Minute
	DefaultMaxOutput  = 64 << 10
)

The limits a Limits field left at zero takes.

View Source
const ExecTool = "ssh_exec"

ExecTool is the name of the tool that runs a command on a host.

Variables

View Source
var ErrHostKeyChanged = errors.New("ssh: the host's key is not the one trusted for it")

ErrHostKeyChanged is returned when a host presents a key other than the one trusted for it. It is never trusted on the spot: the owner removes the old key first (Forget), and the next connection asks about the new one.

View Source
var ErrNoRequest = fmt.Errorf("an SSH key logs in with %s and rides no request: %w", ExecTool, secret.ErrWrongMaterial)

ErrNoRequest is an SSH key's answer to a request that would carry it in a header: it logs in with ssh_exec and rides no request.

Functions

func CheckKey

func CheckKey(key []byte) error

CheckKey reports whether key is a private key a command could log in with, unencrypted: what a command beside the daemon checks before putting one into nocturn's own vault.

func CheckStored

func CheckStored(user string, hosts []string, key []byte) ([]string, error)

CheckStored checks an SSH key a person puts into a store with the hosts it logs in to as user: the key unencrypted (CheckKey), each host an endpoint. It returns the hosts as addresses.

func FromSource

func FromSource(source func() (secret.Source, error), it secret.Item) func(context.Context) (Key, error)

FromSource resolves an SSH key item host-side: its key, and its passphrase field (secret.SSHPassphraseField) when the item has one. source is asked at the moment of use, so a locked store fails then, not when the node is built.

func Tool

func Tool(cfg Config) (agentkit.Tool, error)

Tool is ssh_exec over cfg.

Types

type Config

type Config struct {
	Keys  Keys
	Known HostKeys
	// Scanner learns the key and its passphrase, so output that echoes either is redacted, and scans
	// the command before it leaves: a command is model output sent to a third party.
	Scanner *secret.Scanner
	Limits  Limits
	// Dial connects to "host:port"; nil is a net.Dialer.
	Dial func(ctx context.Context, network, address string) (net.Conn, error)
	Log  *slog.Logger
}

Config is what the tool needs.

type HostKey

type HostKey struct {
	// Host is the host with its port when that is not 22 ("fw.example", "fw.example:2222").
	Host string `json:"host"`
	// Type is the key's algorithm ("ssh-ed25519"), Key its wire form in base64, Fingerprint its
	// SHA256 fingerprint as ssh-keygen prints it ("SHA256:…").
	Type        string `json:"type"`
	Key         string `json:"key"`
	Fingerprint string `json:"fingerprint"`
	// Added is when the owner trusted it, By the holder whose call asked.
	Added time.Time `json:"added"`
	By    string    `json:"by,omitempty"`
}

HostKey is one host's trusted key, as the workspace keeps it.

func HostKeyOf

func HostKeyOf(address string, key xssh.PublicKey) HostKey

HostKeyOf describes a host's key for address.

func (HostKey) PublicKey

func (h HostKey) PublicKey() (xssh.PublicKey, error)

PublicKey parses the key back.

type HostKeys

type HostKeys interface {
	Lookup(address string) (HostKey, bool, error)
	Trust(HostKey) error
}

HostKeys is where the host keys a person trusted are kept: KnownHosts, a workspace's ssh-hosts.json, is one. Lookup answers the key trusted for an address; Trust keeps one, refusing (ErrHostKeyChanged) a different key for a host that has one.

type Key

type Key struct {
	PrivateKey []byte
	Passphrase []byte
}

Key is the material a secret holds for SSH: a private key in OpenSSH or PEM form, and the passphrase it is encrypted with, when it is.

type Keys

type Keys interface {
	// Login is the user the secret logs in as at the endpoint's address: the one its destination
	// there names, or, when it names several, the one the endpoint asks for. It reads metadata and
	// asks nobody: an alias that is no SSH key, an address that is not one of its destinations, or a
	// secret the tree does not offer the caller is an error the model may read, before anybody is
	// asked about the command. It resolves no value.
	Login(ctx context.Context, alias string, at sshgate.Endpoint) (string, error)
	// Key asks the permission tree whether the call may use the secret at the address (the secret
	// Kind, "<alias>@<address>") and returns how to resolve it, host-side. The tool resolves it only
	// once the host has proven its trusted key, so a host that fails to never makes the key leave
	// the store.
	Key(ctx context.Context, alias, address string) (func(context.Context) (Key, error), error)
}

Keys is where a command's key comes from: the workspace's secret nodes, by alias. The model names the alias; the destinations and the login are the secret's, and the value never leaves the host.

type KnownHosts

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

KnownHosts is a workspace's trusted host keys, one per host, in one file outside the mount: what known_hosts is to OpenSSH, written only after the owner approved a key. Every call reads the file again, so a command beside the daemon and the daemon itself see each other's changes; a file that cannot be read trusts nothing and is not written over.

func OpenKnownHosts

func OpenKnownHosts(path string) *KnownHosts

OpenKnownHosts is the store at path; the file need not exist yet.

func (*KnownHosts) Forget

func (k *KnownHosts) Forget(address string) (bool, error)

Forget removes the key trusted for address, reporting whether there was one.

func (*KnownHosts) List

func (k *KnownHosts) List() ([]HostKey, error)

List returns the trusted keys, sorted by host.

func (*KnownHosts) Lookup

func (k *KnownHosts) Lookup(address string) (HostKey, bool, error)

Lookup returns the key trusted for address, if any.

func (*KnownHosts) Trust

func (k *KnownHosts) Trust(h HostKey) error

Trust keeps h as its host's key. The same key again is no change; a different key for a host that has one is ErrHostKeyChanged: trusting a new key takes the old one's removal first.

type Limits

type Limits struct {
	Timeout    time.Duration
	MaxTimeout time.Duration
	MaxOutput  int
}

Limits bound one command: how long it runs when the call names no timeout, the longest a call may name, and how many bytes of stdout and of stderr each come back. A zero field is its default.

type Logins

type Logins map[string][]string

Logins is where an SSH key logs in: by address (sshgate.Endpoint.Address), the users its destinations name there, "" where one named none.

func (Logins) Add

func (l Logins) Add(user, host string) (address string, ok bool)

Add makes user@host a login of the key and returns the host's address. A host that is no endpoint is left out (ok false).

func (Logins) AddDestinations

func (l Logins) AddDestinations(dests []secret.Destination) []string

AddDestinations adds every destination of an SSH key item that is one: an ssh:// URI or one without a scheme (an SSH key's host field). It returns the addresses, in order.

func (Logins) Pick

func (l Logins) Pick(alias string, at sshgate.Endpoint) (string, error)

Pick is the user the key alias logs in as at the endpoint: the one its destination there names, or, of several, the one the endpoint asks for. An address that is no destination, or one where the key names no login, is an error the model may read.

type Result

type Result struct {
	ExitCode int    `json:"exitCode"`
	Signal   string `json:"signal,omitempty"`
	Stdout   string `json:"stdout"`
	Stderr   string `json:"stderr"`
	TimedOut bool   `json:"timedOut,omitempty"`
}

Result is what a command gives back.

Directories

Path Synopsis
Package sshtest is an SSH server in the test process, for the tests of what runs commands over SSH.
Package sshtest is an SSH server in the test process, for the tests of what runs commands over SSH.

Jump to

Keyboard shortcuts

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