Documentation
¶
Overview ¶
Package ssh handles SSH key injection and host key collection for node containers, and the export of the SSH client configuration.
Index ¶
- Constants
- func ExportConfig(ctx context.Context, client *docker.Client, fs afero.Fs, dir, realm string) error
- func ExportFiles() []string
- func GenerateSSHConfig(dir, realm string, hosts []string) string
- func HostKey(stdout string) (string, error)
- func HostKeyFromFile(content string) (string, error)
- func RemoveExport(fs afero.Fs, dir string) error
Constants ¶
const ( // ConfigFile is the ssh_config that a user's ssh includes. ConfigFile = "ssh_config" // KeyFile is the realm's private key, which ConfigFile names. KeyFile = "id_ed25519" // KnownHostsFile holds the nodes' host keys, which ConfigFile names. KnownHostsFile = "known_hosts" )
The files of the SSH config export, in the directory that ExportConfig writes.
const HostKeyFile = "/etc/ssh/ssh_host_ed25519_key.pub"
HostKeyFile is where sshd keeps a node's ed25519 host public key, which the node generates on its first boot, in authorized_keys format with a comment: "ssh-ed25519 AAAA... root@host".
const InjectKeyScript = `mkdir -p /root/.ssh
printf '%s\n' "$1" >> ` + authorizedKeysPath + `
ssh-keyscan -t ed25519 localhost`
InjectKeyScript appends $1, a public key, to root's authorized_keys, creating the directory if needed, and prints the ed25519 host key that sshd serves, as ssh-keyscan reports it: "localhost ssh-ed25519 AAAA...". It has one command per line, for sh -e, which stops at the first that fails, and takes the key as an argument of the shell, not as part of its script. ssh-keyscan asks sshd, which must be running, for the key it serves; HostKey reads it from the output.
Variables ¶
This section is empty.
Functions ¶
func ExportConfig ¶
ExportConfig exports the SSH configuration of realm to dir: it reads the private key and known_hosts from the realm's SSH relay container (mesh.RealmNames), in one docker exec, and writes id_ed25519, known_hosts and ssh_config to dir. Each file is written to a temporary file in dir and renamed into place, in the order of ExportFiles, so that a reader never sees a truncated file or Host blocks before their keys. When the relay container does not exist, the error satisfies docker.IsNotFound and dir is left as it is; RemoveExport then removes the export of a realm without clusters.
The sind CLI runs it after every command that changes the realm's clusters; pkg/cluster does not, so a library caller that keeps an export runs it itself.
func ExportFiles ¶
func ExportFiles() []string
ExportFiles returns the files of the export, in the order ExportConfig replaces them: the key and the host keys before the ssh_config that names their hosts.
func GenerateSSHConfig ¶
GenerateSSHConfig returns the SSH config that lets the user's ssh reach the realm's nodes through its SSH relay container (mesh.RealmNames), with the private key and known_hosts in dir (KeyFile, KnownHostsFile). hosts are the nodes' DNS names; a name in it that is not a node's DNS name in the realm, <node>.<cluster>.<realm>.sind, is left out. For the default realm, it includes hostname canonicalization directives that enable short-name SSH access (e.g. "ssh controller").
func HostKey ¶
HostKey returns the ed25519 host public key in the output of InjectKeyScript, in "ssh-ed25519 AAAA..." format (without the hostname prefix). It skips ssh-keyscan's comment lines.
func HostKeyFromFile ¶
HostKeyFromFile returns the key of HostKeyFile's content, in "ssh-ed25519 AAAA..." format, without the comment.
func RemoveExport ¶
RemoveExport removes the files of the export from dir, and dir itself if that leaves it empty. Files that do not exist are no error. It removes them in the reverse order of ExportFiles, ssh_config first, so that a reader never sees Host blocks whose key or host keys are gone.
Types ¶
This section is empty.