Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Knownhosts tilde aware path expansion - fixes #1107 #1115

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions libvirt/uri/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
"log"
"net"
"os"
"path/filepath"
"strings"

"github.com/kevinburke/ssh_config"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"golang.org/x/crypto/ssh/knownhosts"

"github.com/dmacvicar/terraform-provider-libvirt/libvirt/util"
)

const (
Expand Down Expand Up @@ -80,13 +81,7 @@
case "privkey":
for _, keypath := range sshKeyPaths {
log.Printf("[DEBUG] Reading ssh key '%s'", keypath)
path := os.ExpandEnv(keypath)
if strings.HasPrefix(path, "~/") {
home, err := os.UserHomeDir()
if err == nil {
path = filepath.Join(home, path[2:])
}
}
path := util.ExpandEnvExt(keypath)
sshKey, err := os.ReadFile(path)
if err != nil {
log.Printf("[ERROR] Failed to read ssh key '%s': %v", keypath, err)
Expand Down Expand Up @@ -118,13 +113,12 @@
// construct the whole ssh connection, which can consist of multiple hops if using proxy jumps,
// the ssh configuration file is loaded once and passed along to each host connection.
func (u *ConnectionURI) dialSSH() (net.Conn, error) {
var sshcfg* ssh_config.Config = nil
sshConfigFile, err := os.Open(util.ExpandEnvExt(defaultSSHConfigFile))

sshConfigFile, err := os.Open(os.ExpandEnv(defaultSSHConfigFile))
if err != nil {
log.Printf("[WARN] Failed to open ssh config file: %v", err)
} else {
sshcfg, err = ssh_config.Decode(sshConfigFile)

Check failure on line 121 in libvirt/uri/ssh.go

View workflow job for this annotation

GitHub Actions / Lint (1.21.x)

undefined: sshcfg

Check failure on line 121 in libvirt/uri/ssh.go

View workflow job for this annotation

GitHub Actions / Lint (1.21.x)

undefined: sshcfg

Check failure on line 121 in libvirt/uri/ssh.go

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, 1.21)

undefined: sshcfg

Check failure on line 121 in libvirt/uri/ssh.go

View workflow job for this annotation

GitHub Actions / Build (macos-latest, 1.21)

undefined: sshcfg
if err != nil {
log.Printf("[WARN] Failed to parse ssh config file: '%v' - sshconfig will be ignored.", err)
}
Expand All @@ -132,7 +126,7 @@
}

// configuration loaded, build tunnel
sshClient, err := u.dialHost(u.Host, sshcfg, 0)

Check failure on line 129 in libvirt/uri/ssh.go

View workflow job for this annotation

GitHub Actions / Lint (1.21.x)

undefined: sshcfg) (typecheck)

Check failure on line 129 in libvirt/uri/ssh.go

View workflow job for this annotation

GitHub Actions / Lint (1.21.x)

undefined: sshcfg (typecheck)

Check failure on line 129 in libvirt/uri/ssh.go

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, 1.21)

undefined: sshcfg

Check failure on line 129 in libvirt/uri/ssh.go

View workflow job for this annotation

GitHub Actions / Build (macos-latest, 1.21)

undefined: sshcfg
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -221,11 +215,11 @@
ssh.KeyAlgoECDSA521,
}
if !skipVerify {
kh, err := knownhosts.New(os.ExpandEnv(knownHostsPath))
kh, err := knownhosts.New(util.ExpandEnvExt(knownHostsPath))
if err != nil {
return nil, fmt.Errorf("failed to read ssh known hosts: %w", err)
}
log.Printf("[DEBUG] Using known hosts file '%s' for target '%s'", os.ExpandEnv(knownHostsPath), target)
log.Printf("[DEBUG] Using known hosts file '%s' for target '%s'", util.ExpandEnvExt(knownHostsPath), target)

hostKeyCallback = func(hostname string, remote net.Addr, key ssh.PublicKey) error {
err := kh(net.JoinHostPort(hostName, port), remote, key)
Expand Down
27 changes: 27 additions & 0 deletions libvirt/util/expandenv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package util

import (
"os"
"path/filepath"
"strings"
)

var (
memetb marked this conversation as resolved.
Show resolved Hide resolved
userHomeDir = os.UserHomeDir
expandEnv = os.ExpandEnv
)

// ExpandEnvExt expands environment variables and resolves ~ to the home directory
// this is a drop-in replacement for os.ExpandEnv but is additionally '~' aware
func ExpandEnvExt(path string) string {
path = expandEnv(path)
if strings.HasPrefix(path, "~/") {
home, err := userHomeDir()
if err != nil {
return path // return path as-is if unable to resolve home directory
}
// Replace ~ with home directory
path = filepath.Join(home, strings.TrimPrefix(path, "~/"))
}
return path
}
31 changes: 31 additions & 0 deletions libvirt/util/expandenv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package util

import (
"testing"
"strings"
"fmt"

"github.com/stretchr/testify/assert"
)

func TestExpandEnvExt(t *testing.T) {
userHomeDir = func() (string, error) {
return "/home/mock", nil
}
expandEnv = func(s string) string {
return strings.Replace(s, "${HOME}", "/home/mock", 1)
}


assert.Equal(t, "foo/bar/baz", ExpandEnvExt("foo/bar/baz"))
assert.Equal(t, "/home/mock/foo/bar/baz", ExpandEnvExt("~/foo/bar/baz"))
assert.Equal(t, "/home/mock/foo/bar/baz", ExpandEnvExt("${HOME}/foo/bar/baz"))
assert.Equal(t, "~foo/bar/baz", ExpandEnvExt("~foo/bar/baz"))

userHomeDir = func() (string, error) {
return "", fmt.Errorf("some failure")
}

// failure to get home expansion should leave string unchanged
assert.Equal(t, "~/foo/bar/baz", ExpandEnvExt("~/foo/bar/baz"))
}
Loading