vcom/internal/ssh/conn/prompter.go
vrubelroman 94002e286f feat(ssh): add conn dialer with bounded handshake, host keys and auth chain (2B.2)
The whole handshake (TCP, kex, auth, SFTP init) runs under a deadline
that ctx cancellation cuts short, and a late client is closed (N-P1-3).
HostKeyAlgorithms come from known_hosts (N-P1-4); unknown and changed
keys go to a Prompter (N-P2-14). Auth tries identity files, then agent
keys over one shared agent connection, then default keys, password and
keyboard-interactive through the Prompter (N-P2-1, N-P2-2).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 23:33:26 +03:00

55 lines
2 KiB
Go

// Package conn owns SSH connections: dialing with a bounded handshake,
// host-key verification, authentication, keepalive, reconnect, remote exec
// and a reference-counted pool. Nothing outside the Pool closes a
// connection.
package conn
import (
"context"
"crypto/sha256"
"encoding/base64"
"golang.org/x/crypto/ssh"
"vcom/internal/ssh/sshconfig"
)
// Endpoint is the resolved host to dial.
type Endpoint = sshconfig.Endpoint
// HostIdentity identifies a server host key.
type HostIdentity struct {
KeyType string // e.g. "ssh-ed25519"
FingerprintSHA256 string // "SHA256:..." as printed by ssh-keygen -l
}
// IdentityOf returns the identity of a public key.
func IdentityOf(k ssh.PublicKey) HostIdentity {
sum := sha256.Sum256(k.Marshal())
return HostIdentity{
KeyType: k.Type(),
FingerprintSHA256: "SHA256:" + base64.RawStdEncoding.EncodeToString(sum[:]),
}
}
// HostKeyDecision is the answer to an unknown or changed host key.
type HostKeyDecision uint8
const (
Reject HostKeyDecision = iota // refuse to connect
AcceptOnce // trust for this process only
AcceptAndSave // trust and append to known_hosts
)
// Prompter asks the user. The UI implements it via jobs.Ask; every method
// must return promptly when ctx is cancelled. A nil Prompter rejects unknown
// host keys and offers no interactive authentication.
type Prompter interface {
// HostKey is called for a key that is not in known_hosts. changed is
// true when known_hosts has a different key of the same type (possible
// MITM); known lists the keys known_hosts has for the host.
HostKey(ctx context.Context, ep Endpoint, id HostIdentity, known []HostIdentity, changed bool) (HostKeyDecision, error)
Password(ctx context.Context, ep Endpoint) (string, error)
KeyboardInteractive(ctx context.Context, ep Endpoint, instruction string, questions []string, echo []bool) ([]string, error)
Passphrase(ctx context.Context, keyPath string) ([]byte, error)
}