From 861a902f816b4cf305b4f83482a20bb7047168e8 Mon Sep 17 00:00:00 2001 From: David Dworken Date: Tue, 27 Aug 2019 21:39:32 -0400 Subject: [PATCH] Automatically create ~/.ssh/ prior to attempting to write a key to it Fixes issue 32 where on windows it is likely that users may not have .ssh directory --- src/cmd/kssh/kssh.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/cmd/kssh/kssh.go b/src/cmd/kssh/kssh.go index 14b51a0..0c45006 100644 --- a/src/cmd/kssh/kssh.go +++ b/src/cmd/kssh/kssh.go @@ -291,7 +291,15 @@ func isValidCert(keyPath string) bool { // Provision a new signed SSH key with the given config func provisionNewKey(config kssh.ConfigFile, keyPath string) error { log.Debug("Generating a new SSH key...") - err := sshutils.GenerateNewSSHKey(keyPath, true, false) + + // Make ~/.ssh/ in case it doesn't exist + err := kssh.MakeDotSSH() + if err != nil { + return err + } + + // Generate the key itself and read it + err = sshutils.GenerateNewSSHKey(keyPath, true, false) if err != nil { return fmt.Errorf("Failed to generate a new SSH key: %v", err) } @@ -300,6 +308,7 @@ func provisionNewKey(config kssh.ConfigFile, keyPath string) error { return fmt.Errorf("Failed to read the SSH key from the filesystem: %v", err) } + // Provision the key randomUUID, err := uuid.NewRandom() if err != nil { return fmt.Errorf("Failed to generate a new UUID for the SignatureRequest: %v", err) @@ -315,6 +324,7 @@ func provisionNewKey(config kssh.ConfigFile, keyPath string) error { } log.Debug("Received signature from the CA!") + // Write it to ~/.ssh err = ioutil.WriteFile(shared.KeyPathToCert(keyPath), []byte(resp.SignedKey), 0600) if err != nil { return fmt.Errorf("Failed to write new SSH key to disk: %v", err)