From fc7c6efd3b4d2dfbd04e6f199776b610259b7683 Mon Sep 17 00:00:00 2001 From: Brent Baude Date: Fri, 4 Aug 2023 08:26:36 -0500 Subject: [PATCH] Fail if ssh key exists when init'ing a machine, if the ssh key already exists, then we get a somewhat oblique error. here we make it clear what the problem was and early return. Signed-off-by: Brent Baude [NO NEW TESTS NEEDED] Signed-off-by: Brent Baude --- pkg/machine/keys.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/machine/keys.go b/pkg/machine/keys.go index 16561df30b..91c17aa2c6 100644 --- a/pkg/machine/keys.go +++ b/pkg/machine/keys.go @@ -20,6 +20,10 @@ var sshCommand = []string{"ssh-keygen", "-N", "", "-t", "ed25519", "-f"} // CreateSSHKeys makes a priv and pub ssh key for interacting // the a VM. func CreateSSHKeys(writeLocation string) (string, error) { + // If the SSH key already exists, hard fail + if _, err := os.Stat(writeLocation); err == nil { + return "", fmt.Errorf("SSH key already exists: %s", writeLocation) + } if err := os.MkdirAll(filepath.Dir(writeLocation), 0700); err != nil { return "", err }