From c27326cab8d4bda2155511c7b78aa15380d94d95 Mon Sep 17 00:00:00 2001 From: Marc Velasco Date: Wed, 6 Oct 2021 23:14:46 -0700 Subject: [PATCH 1/4] fix for #12659, rendor absolute path when tilde starts ssh-key path --- pkg/minikube/registry/drvs/ssh/ssh.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/minikube/registry/drvs/ssh/ssh.go b/pkg/minikube/registry/drvs/ssh/ssh.go index 8985143b615c..708790e027a6 100644 --- a/pkg/minikube/registry/drvs/ssh/ssh.go +++ b/pkg/minikube/registry/drvs/ssh/ssh.go @@ -27,6 +27,9 @@ import ( "k8s.io/minikube/pkg/minikube/driver" "k8s.io/minikube/pkg/minikube/localpath" "k8s.io/minikube/pkg/minikube/registry" + + "os/user" + "path/filepath" ) func init() { @@ -63,7 +66,18 @@ func configure(cc config.ClusterConfig, n config.Node) (interface{}, error) { d.IPAddress = cc.SSHIPAddress d.SSHUser = cc.SSHUser - d.SSHKey = cc.SSHKey + + //If ~ is in the front of the path to ssh-key then convert to the absolute path + if len(cc.SSHKey) > 0 && cc.SSHKey[0] == '~' { + usr, err := user.Current() + if err != nil { + return nil, errors.Errorf("Error determining path to ssh key") + } + d.SSHKey = filepath.Join(usr.HomeDir, cc.SSHKey[1:]) + } else { + d.SSHKey = cc.SSHKey + } + d.SSHPort = cc.SSHPort return d, nil From a827379f082cbbb5494bac19c84c5b5000f99b9b Mon Sep 17 00:00:00 2001 From: Marc Velasco Date: Thu, 7 Oct 2021 09:27:25 -0700 Subject: [PATCH 2/4] remove comment before absolute path function to fix lint errors --- pkg/minikube/registry/drvs/ssh/ssh.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/minikube/registry/drvs/ssh/ssh.go b/pkg/minikube/registry/drvs/ssh/ssh.go index 708790e027a6..85989d62a686 100644 --- a/pkg/minikube/registry/drvs/ssh/ssh.go +++ b/pkg/minikube/registry/drvs/ssh/ssh.go @@ -67,7 +67,6 @@ func configure(cc config.ClusterConfig, n config.Node) (interface{}, error) { d.IPAddress = cc.SSHIPAddress d.SSHUser = cc.SSHUser - //If ~ is in the front of the path to ssh-key then convert to the absolute path if len(cc.SSHKey) > 0 && cc.SSHKey[0] == '~' { usr, err := user.Current() if err != nil { From 4f5524f039cc97c54f4214502d3e288bb7335bce Mon Sep 17 00:00:00 2001 From: Marc Velasco Date: Thu, 7 Oct 2021 10:40:13 -0700 Subject: [PATCH 3/4] changes for comments to the PR, use os.UserHomeDir and strings.hasprefix, move package order --- pkg/minikube/registry/drvs/ssh/ssh.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/minikube/registry/drvs/ssh/ssh.go b/pkg/minikube/registry/drvs/ssh/ssh.go index 85989d62a686..967f48a0c966 100644 --- a/pkg/minikube/registry/drvs/ssh/ssh.go +++ b/pkg/minikube/registry/drvs/ssh/ssh.go @@ -18,6 +18,9 @@ package ssh import ( "fmt" + "os" + "path/filepath" + "strings" "github.com/docker/machine/libmachine/drivers" "github.com/pkg/errors" @@ -27,9 +30,6 @@ import ( "k8s.io/minikube/pkg/minikube/driver" "k8s.io/minikube/pkg/minikube/localpath" "k8s.io/minikube/pkg/minikube/registry" - - "os/user" - "path/filepath" ) func init() { @@ -67,12 +67,12 @@ func configure(cc config.ClusterConfig, n config.Node) (interface{}, error) { d.IPAddress = cc.SSHIPAddress d.SSHUser = cc.SSHUser - if len(cc.SSHKey) > 0 && cc.SSHKey[0] == '~' { - usr, err := user.Current() + if strings.HasPrefix(cc.SSHKey, "~") { + dirname, err := os.UserHomeDir() if err != nil { return nil, errors.Errorf("Error determining path to ssh key") } - d.SSHKey = filepath.Join(usr.HomeDir, cc.SSHKey[1:]) + d.SSHKey = filepath.Join(dirname, cc.SSHKey[1:]) } else { d.SSHKey = cc.SSHKey } From a48e875ec1aac525e7216d3f77dc36aad3b8e171 Mon Sep 17 00:00:00 2001 From: Marc Velasco Date: Thu, 7 Oct 2021 11:57:43 -0700 Subject: [PATCH 4/4] return error if unable to get os.UserHomeDir in ssh-key when resolving absolute path --- pkg/minikube/registry/drvs/ssh/ssh.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/minikube/registry/drvs/ssh/ssh.go b/pkg/minikube/registry/drvs/ssh/ssh.go index 967f48a0c966..8308cf90053a 100644 --- a/pkg/minikube/registry/drvs/ssh/ssh.go +++ b/pkg/minikube/registry/drvs/ssh/ssh.go @@ -70,7 +70,7 @@ func configure(cc config.ClusterConfig, n config.Node) (interface{}, error) { if strings.HasPrefix(cc.SSHKey, "~") { dirname, err := os.UserHomeDir() if err != nil { - return nil, errors.Errorf("Error determining path to ssh key") + return nil, errors.Errorf("Error determining path to ssh key: %v", err) } d.SSHKey = filepath.Join(dirname, cc.SSHKey[1:]) } else {