Skip to content

Commit 3198d67

Browse files
committed
fix for bastion host key checking
the assignment of BastionHostKey was incorrect, causing it to use the primary HostKey.
1 parent 73c225d commit 3198d67

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

internal/communicator/ssh/communicator_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,19 @@ func newMockLineServer(t *testing.T, signer ssh.Signer, pubKey string) string {
103103
}
104104
t.Log("Accepted channel")
105105

106+
go func() {
107+
buf := make([]byte, 64)
108+
n, _ := channel.Read(buf)
109+
if n > 0 {
110+
// this unusual test server ends up here when we're trying
111+
// to handshake through a bastion instance. It's the only
112+
// test that uses this path, and only if the test wasn't
113+
// working, so just close the channel and let it fail.
114+
t.Logf("unexpected test server read: %q, closing channel\n", buf[:n])
115+
channel.Close()
116+
}
117+
}()
118+
106119
go func(in <-chan *ssh.Request) {
107120
defer channel.Close()
108121
for req := range in {
@@ -901,3 +914,41 @@ func acceptPublicKey(keystr string) func(ssh.ConnMetadata, ssh.PublicKey) (*ssh.
901914
return nil, fmt.Errorf("public key rejected")
902915
}
903916
}
917+
918+
func TestBastionHostKey(t *testing.T) {
919+
bastionAddr := newMockLineServer(t, nil, testClientPublicKey)
920+
bastionHost, p, _ := net.SplitHostPort(bastionAddr)
921+
bastionPort, _ := strconv.Atoi(p)
922+
923+
// there doesn't need to be a real end server, we should abort before
924+
// initiating the second connection because BastionHostKey is wrong for
925+
// testServerPrivateKey
926+
connInfo := &connectionInfo{
927+
User: "none",
928+
Password: "none",
929+
Host: "127.0.0.1",
930+
Port: uint16(9999),
931+
Timeout: "1s",
932+
933+
BastionUser: "user",
934+
BastionPassword: "pass",
935+
BastionHost: bastionHost,
936+
BastionHostKey: testClientPublicKey,
937+
BastionPort: uint16(bastionPort),
938+
}
939+
940+
cfg, err := prepareSSHConfig(connInfo)
941+
if err != nil {
942+
t.Fatal(err)
943+
}
944+
945+
c := &Communicator{
946+
connInfo: connInfo,
947+
config: cfg,
948+
}
949+
950+
_, err = c.newSession()
951+
if err == nil || !strings.Contains(err.Error(), "Error connecting to bastion: ssh: handshake failed: knownhosts: key mismatch") {
952+
t.Fatalf("expected host key mismatch, got error:%v", err)
953+
}
954+
}

internal/communicator/ssh/provisioner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ func prepareSSHConfig(connInfo *connectionInfo) (*sshConfig, error) {
297297
host: bastionHost,
298298
privateKey: connInfo.BastionPrivateKey,
299299
password: connInfo.BastionPassword,
300-
hostKey: connInfo.HostKey,
300+
hostKey: connInfo.BastionHostKey,
301301
certificate: connInfo.BastionCertificate,
302302
sshAgent: sshAgent,
303303
})

0 commit comments

Comments
 (0)