Skip to content

Commit

Permalink
Merge pull request #23847 from afbjorklund/ssh-config-main
Browse files Browse the repository at this point in the history
Add support for ssh_config for connection
  • Loading branch information
openshift-merge-bot[bot] authored Oct 30, 2024
2 parents c8238a9 + b455f94 commit 5751154
Show file tree
Hide file tree
Showing 18 changed files with 1,775 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ require (
github.com/hashicorp/go-multierror v1.1.1
github.com/hugelgupf/p9 v0.3.1-0.20230822151754-54f5c5530921
github.com/json-iterator/go v1.1.12
github.com/kevinburke/ssh_config v1.2.0
github.com/klauspost/pgzip v1.2.6
github.com/linuxkit/virtsock v0.0.0-20220523201153-1a23e78aa7a2
github.com/mattn/go-shellwords v1.0.12
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ github.com/josharian/native v1.1.0 h1:uuaP0hAbW7Y4l0ZRQ6C9zfb7Mg1mbFKry/xzDAfmtL
github.com/josharian/native v1.1.0/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
Expand Down
63 changes: 62 additions & 1 deletion pkg/bindings/connection.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bindings

import (
"bytes"
"context"
"errors"
"fmt"
Expand All @@ -9,13 +10,15 @@ import (
"net/http"
"net/url"
"os"
"os/user"
"strconv"
"strings"
"time"

"github.com/blang/semver/v4"
"github.com/containers/common/pkg/ssh"
"github.com/containers/podman/v5/version"
"github.com/kevinburke/ssh_config"
"github.com/sirupsen/logrus"
"golang.org/x/net/proxy"
)
Expand Down Expand Up @@ -149,23 +152,81 @@ func sshClient(_url *url.URL, uri string, identity string, machine bool) (Connec
connection := Connection{
URI: _url,
}
userinfo := _url.User
if _url.User == nil {
u, err := user.Current()
if err != nil {
return connection, fmt.Errorf("current user could not be determined: %w", err)
}
userinfo = url.User(u.Username)
}
port := 22
if _url.Port() != "" {
port, err = strconv.Atoi(_url.Port())
if err != nil {
return connection, err
}
}
// ssh_config
alias := _url.Hostname()
cfg := ssh_config.DefaultUserSettings
found := false
if val := cfg.Get(alias, "User"); val != "" {
userinfo = url.User(val)
found = true
}
if val := cfg.Get(alias, "Hostname"); val != "" {
uri = val
found = true
}
if val := cfg.Get(alias, "Port"); val != "" {
if val != ssh_config.Default("Port") {
port, err = strconv.Atoi(val)
if err != nil {
return connection, fmt.Errorf("port is not an int: %s: %w", val, err)
}
found = true
}
}
if val := cfg.Get(alias, "IdentityFile"); val != "" {
if val != ssh_config.Default("IdentityFile") {
identity = strings.Trim(val, "\"")
found = true
}
}
if found {
logrus.Debugf("ssh_config alias found: %s", alias)
logrus.Debugf(" User: %s", userinfo.Username())
logrus.Debugf(" Hostname: %s", uri)
logrus.Debugf(" Port: %d", port)
logrus.Debugf(" IdentityFile: %q", identity)
}
conn, err := ssh.Dial(&ssh.ConnectionDialOptions{
Host: uri,
Identity: identity,
User: _url.User,
User: userinfo,
Port: port,
InsecureIsMachineConnection: machine,
}, ssh.GolangMode)
if err != nil {
return connection, newConnectError(err)
}
if _url.Path == "" {
session, err := conn.NewSession()
if err != nil {
return connection, err
}
defer session.Close()

var b bytes.Buffer
session.Stdout = &b
if err := session.Run(
"podman info --format '{{.Host.RemoteSocket.Path}}'"); err != nil {
return connection, err
}
val := strings.TrimSuffix(b.String(), "\n")
_url.Path = val
}
dialContext := func(ctx context.Context, _, _ string) (net.Conn, error) {
return ssh.DialNet(conn, "unix", _url)
}
Expand Down
1 change: 1 addition & 0 deletions vendor/github.com/kevinburke/ssh_config/.gitattributes

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
1 change: 1 addition & 0 deletions vendor/github.com/kevinburke/ssh_config/.mailmap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions vendor/github.com/kevinburke/ssh_config/AUTHORS.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vendor/github.com/kevinburke/ssh_config/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions vendor/github.com/kevinburke/ssh_config/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions vendor/github.com/kevinburke/ssh_config/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions vendor/github.com/kevinburke/ssh_config/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

1 comment on commit 5751154

@packit-as-a-service
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

podman-next COPR build failed. @containers/packit-build please check.

Please sign in to comment.