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 <bbaude@redhat.com>

[NO NEW TESTS NEEDED]

Signed-off-by: Brent Baude <bbaude@redhat.com>
This commit is contained in:
Brent Baude
2023-08-04 08:26:36 -05:00
parent 84dec22349
commit fc7c6efd3b

View File

@ -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
}