Skip to content

Commit

Permalink
fix: check whether the default keyfile exists
Browse files Browse the repository at this point in the history
  • Loading branch information
vimiix committed Jun 19, 2024
1 parent 6cbe140 commit ac2b8ce
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions ssx/entry/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/skeema/knownhosts"
"golang.org/x/crypto/ssh"

"github.com/vimiix/ssx/internal/file"
"github.com/vimiix/ssx/internal/lg"
"github.com/vimiix/ssx/internal/terminal"
"github.com/vimiix/ssx/internal/utils"
Expand All @@ -26,8 +27,8 @@ const (
SourceSSXStore = "ssx_store"
)

const (
defaultIdentityFile = "~/.ssh/id_rsa"
var (
defaultIdentityFile = utils.ExpandHomeDir("~/.ssh/id_rsa")
defaultUser = "root"
defaultPort = "22"
)
Expand Down Expand Up @@ -180,8 +181,8 @@ func (e *Entry) Tidy() error {
if len(e.Port) <= 0 {
e.Port = defaultPort
}
if e.KeyPath == "" {
e.KeyPath = utils.ExpandHomeDir(defaultIdentityFile)
if e.KeyPath == "" && file.IsExist(defaultIdentityFile) {
e.KeyPath = defaultIdentityFile
}
if e.Proxy != nil {
e.Proxy.tidy()
Expand Down Expand Up @@ -262,9 +263,14 @@ func (e *Entry) privateKeyAuthMethods(ctx context.Context) ([]ssh.AuthMethod, er
}
var methods []ssh.AuthMethod
for _, f := range keyfiles {
if !file.IsExist(f) {
lg.Debug("keyfile %s not found, skip", f)
continue
}
auth, err := e.keyfileAuth(ctx, f)
if err != nil {
return nil, err
lg.Debug("skip use keyfile: %s", f)
continue
}
if auth != nil {
methods = append(methods, auth)
Expand Down

0 comments on commit ac2b8ce

Please sign in to comment.