Skip to content

Commit

Permalink
Fix check for encrypted private keys using PassphraseMissingError
Browse files Browse the repository at this point in the history
Signed-off-by: Hans Rakers <[email protected]>
  • Loading branch information
hrak authored and alexellis committed Jul 20, 2020
1 parent 02cd632 commit b946a46
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func loadPublickey(path string) (ssh.AuthMethod, func() error, error) {

signer, err := ssh.ParsePrivateKey(key)
if err != nil {
if err.Error() != "ssh: cannot decode encrypted private keys" {
if _, ok := err.(*ssh.PassphraseMissingError); !ok {
return nil, noopCloseFunc, fmt.Errorf("unable to parse private key: %s", err.Error())
}

Expand Down
6 changes: 4 additions & 2 deletions cmd/install_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package cmd

import (
"errors"
"io/ioutil"
"os"
"regexp"
"strings"
"testing"

"github.com/alexellis/k3sup/pkg/helm"
"golang.org/x/crypto/ssh"
)

// To regenerate:
Expand Down Expand Up @@ -45,7 +47,7 @@ kfFJfrUjElq6Bx9oPPxc2vD40gqnYL57A+Y+X+A0kL4fO7pfh2VxOw==
`

func Test_loadPublickeyEncrypted(t *testing.T) {
want := "parse private key with passphrase failed: x509: decryption password incorrect"
want := &ssh.PassphraseMissingError{}

tmpfile, err := ioutil.TempFile("", "key")
if err != nil {
Expand All @@ -60,7 +62,7 @@ func Test_loadPublickeyEncrypted(t *testing.T) {

tmpfile.Close()
_, _, err = loadPublickey(fileName)
if err.Error() != want {
if errors.Is(err, want) {
t.Fatalf("want: %q, but got: %q", want, err.Error())
}
}
Expand Down

0 comments on commit b946a46

Please sign in to comment.