|
| 1 | +package ssh_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/ecdsa" |
| 5 | + "crypto/elliptic" |
| 6 | + "crypto/rand" |
| 7 | + "crypto/x509" |
| 8 | + "encoding/pem" |
| 9 | + "fmt" |
| 10 | + "net" |
| 11 | + "testing" |
| 12 | + |
| 13 | + golangssh "golang.org/x/crypto/ssh" |
| 14 | + |
| 15 | + "github.com/rancher/fleet/internal/ssh" |
| 16 | +) |
| 17 | + |
| 18 | +func generateECPrivateKeyPEM(t *testing.T) []byte { |
| 19 | + t.Helper() |
| 20 | + key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 21 | + if err != nil { |
| 22 | + t.Fatalf("generating key: %v", err) |
| 23 | + } |
| 24 | + der, err := x509.MarshalECPrivateKey(key) |
| 25 | + if err != nil { |
| 26 | + t.Fatalf("marshalling key: %v", err) |
| 27 | + } |
| 28 | + return pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: der}) |
| 29 | +} |
| 30 | + |
| 31 | +func generateKnownHostsEntry(t *testing.T, hostname string) (golangssh.PublicKey, string) { |
| 32 | + t.Helper() |
| 33 | + key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 34 | + if err != nil { |
| 35 | + t.Fatalf("generating host key: %v", err) |
| 36 | + } |
| 37 | + sshPubKey, err := golangssh.NewPublicKey(&key.PublicKey) |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("creating SSH public key: %v", err) |
| 40 | + } |
| 41 | + entry := fmt.Sprintf("%s %s", hostname, string(golangssh.MarshalAuthorizedKey(sshPubKey))) |
| 42 | + return sshPubKey, entry |
| 43 | +} |
| 44 | + |
| 45 | +// fakeAddr implements net.Addr for use in HostKeyCallback tests. |
| 46 | +type fakeAddr struct{ s string } |
| 47 | + |
| 48 | +func (a fakeAddr) Network() string { return "tcp" } |
| 49 | +func (a fakeAddr) String() string { return a.s } |
| 50 | + |
| 51 | +func TestNewSSHPublicKeys_NoKnownHosts(t *testing.T) { |
| 52 | + keyPEM := generateECPrivateKeyPEM(t) |
| 53 | + pubKeys, err := ssh.NewSSHPublicKeys("git", keyPEM, nil) |
| 54 | + if err != nil { |
| 55 | + t.Fatalf("unexpected error: %v", err) |
| 56 | + } |
| 57 | + if pubKeys == nil { |
| 58 | + t.Fatal("expected non-nil PublicKeys") |
| 59 | + } |
| 60 | + // With no known hosts, InsecureIgnoreHostKey must accept any host. |
| 61 | + if err := pubKeys.HostKeyCallback("any-host:22", fakeAddr{"any-host:22"}, nil); err != nil { |
| 62 | + t.Fatalf("InsecureIgnoreHostKey should accept any host, got: %v", err) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestNewSSHPublicKeys_WithKnownHosts(t *testing.T) { |
| 67 | + keyPEM := generateECPrivateKeyPEM(t) |
| 68 | + hostPubKey, knownHostsEntry := generateKnownHostsEntry(t, "myhost.example.com") |
| 69 | + |
| 70 | + pubKeys, err := ssh.NewSSHPublicKeys("git", keyPEM, []byte(knownHostsEntry)) |
| 71 | + if err != nil { |
| 72 | + t.Fatalf("unexpected error: %v", err) |
| 73 | + } |
| 74 | + |
| 75 | + // Matching key for the known host must be accepted. |
| 76 | + addr := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 22} |
| 77 | + if err := pubKeys.HostKeyCallback("myhost.example.com:22", addr, hostPubKey); err != nil { |
| 78 | + t.Fatalf("expected callback to accept matching key, got: %v", err) |
| 79 | + } |
| 80 | + |
| 81 | + // An unknown host must be rejected. |
| 82 | + if err := pubKeys.HostKeyCallback("unknown-host:22", addr, hostPubKey); err == nil { |
| 83 | + t.Fatal("expected error for unknown host, got nil") |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestNewSSHPublicKeys_InvalidKnownHosts(t *testing.T) { |
| 88 | + keyPEM := generateECPrivateKeyPEM(t) |
| 89 | + // Malformed known_hosts must be rejected at construction time. |
| 90 | + _, err := ssh.NewSSHPublicKeys("git", keyPEM, []byte("not-valid-known-hosts @@@@")) |
| 91 | + if err == nil { |
| 92 | + t.Fatal("expected error for invalid known_hosts, got nil") |
| 93 | + } |
| 94 | +} |
0 commit comments