Skip to content

Commit

Permalink
Fix ssh keypath defaulting
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
  • Loading branch information
kke committed Sep 7, 2023
1 parent 15b7c99 commit 8f08b6a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
16 changes: 11 additions & 5 deletions ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type PasswordCallback func() (secret string, err error)

var (
authMethodCache = sync.Map{}
defaultKeypaths = []string{"~/.ssh/id_rsa", "~/.ssh/identity", "~/.ssh/id_dsa"}
defaultKeypaths = []string{"~/.ssh/id_rsa", "~/.ssh/identity", "~/.ssh/id_dsa", "~/.ssh/id_ecdsa", "~/.ssh/id_ed25519"}
dummyhostKeyPaths []string
globalOnce sync.Once
knownHostsMU sync.Mutex
Expand Down Expand Up @@ -130,6 +130,16 @@ func (c *SSH) initGlobalDefaults() {
dummyhostKeyPaths = append(dummyhostKeyPaths, expanded)
}
}
for _, defaultKeypath := range defaultKeypaths {
if expanded, err := expandAndValidatePath(defaultKeypath); err == nil {
for _, dummyHostKeyPath := range dummyhostKeyPaths {
if dummyHostKeyPath == expanded {
continue
}
}
dummyhostKeyPaths = append(dummyhostKeyPaths, defaultKeypath)
}
}
}

func findUniq(a, b []string) (string, bool) {
Expand Down Expand Up @@ -163,10 +173,6 @@ func (c *SSH) SetDefaults() {
c.KeyPath = nil

paths := c.keypathsFromConfig()
if len(paths) == 0 {
// no paths found in ssh config either, use defaults
paths = append(paths, defaultKeypaths...)
}

for _, p := range paths {
expanded, err := expandAndValidatePath(p)
Expand Down
11 changes: 11 additions & 0 deletions test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ rig_test_key_from_path() {
RET=$exit_code
}

rig_test_key_from_default_location() {
color_echo "- Testing keypath from default location"
make create-host
mv .ssh/identity .ssh/id_ecdsa
set +e
./rigtest -host 127.0.0.1:$(ssh_port node0) -user root
local exit_code=$?
set -e
RET=$exit_code
}

rig_test_protected_key_from_path() {
color_echo "- Testing regular keypath to encrypted key, two hosts"
make create-host KEY_PASSPHRASE=testPhrase REPLICAS=2
Expand Down

0 comments on commit 8f08b6a

Please sign in to comment.