From 2dff21660b68b237e88b5928429b008a47e2d6f2 Mon Sep 17 00:00:00 2001 From: Ian Bishop <151477169+ianb-mp@users.noreply.github.com> Date: Tue, 21 May 2024 16:41:45 +1000 Subject: [PATCH] knownhosts hostname fix (#206) --- pkg/ssh/hostkey/callbacks.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/ssh/hostkey/callbacks.go b/pkg/ssh/hostkey/callbacks.go index 689cb0b6..aa71c181 100644 --- a/pkg/ssh/hostkey/callbacks.go +++ b/pkg/ssh/hostkey/callbacks.go @@ -98,7 +98,15 @@ func wrapCallback(hkc ssh.HostKeyCallback, path string, permissive, hash bool) s return fmt.Errorf("failed to open ssh known_hosts file %s for writing: %w", path, err) } - knownHostsEntry := knownhosts.Normalize(remote.String()) + knownHostsEntry := "" + if t, ok := remote.(*net.TCPAddr); ok && t.IP.IsUnspecified() { + // fallback to hostname if remote addr is unspecified + // see: https://github.com/k0sproject/k0sctl/issues/717 + knownHostsEntry = knownhosts.Normalize(hostname) + } else { + knownHostsEntry = knownhosts.Normalize(remote.String()) + } + if hash { knownHostsEntry = knownhosts.HashHostname(knownHostsEntry) }