Skip to content

Commit

Permalink
Merge pull request #23893 from afbjorklund/ssh-config-refactor
Browse files Browse the repository at this point in the history
refactor: add sshClient function
  • Loading branch information
openshift-merge-bot[bot] authored Sep 10, 2024
2 parents c38c197 + 837755e commit b1efc50
Showing 1 changed file with 38 additions and 23 deletions.
61 changes: 38 additions & 23 deletions pkg/bindings/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func NewConnection(ctx context.Context, uri string) (context.Context, error) {
// A valid URI connection should be scheme://
// For example tcp://localhost:<port>
// or unix:///run/podman/podman.sock
// or ssh://<user>@<host>[:port]/run/podman/podman.sock?secure=True
// or ssh://<user>@<host>[:port]/run/podman/podman.sock
func NewConnectionWithIdentity(ctx context.Context, uri string, identity string, machine bool) (context.Context, error) {
var (
err error
Expand All @@ -108,30 +108,11 @@ func NewConnectionWithIdentity(ctx context.Context, uri string, identity string,
var connection Connection
switch _url.Scheme {
case "ssh":
port := 22
if _url.Port() != "" {
port, err = strconv.Atoi(_url.Port())
if err != nil {
return nil, err
}
}
conn, err := ssh.Dial(&ssh.ConnectionDialOptions{
Host: uri,
Identity: identity,
User: _url.User,
Port: port,
InsecureIsMachineConnection: machine,
}, "golang")
conn, err := sshClient(_url, uri, identity, machine)
if err != nil {
return nil, newConnectError(err)
return nil, err
}
connection = Connection{URI: _url}
connection.Client = &http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
return ssh.DialNet(conn, "unix", _url)
},
}}
connection = conn
case "unix":
if !strings.HasPrefix(uri, "unix:///") {
// autofix unix://path_element vs unix:///path_element
Expand Down Expand Up @@ -161,6 +142,40 @@ func NewConnectionWithIdentity(ctx context.Context, uri string, identity string,
return ctx, nil
}

func sshClient(_url *url.URL, uri string, identity string, machine bool) (Connection, error) {
var (
err error
)
connection := Connection{
URI: _url,
}
port := 22
if _url.Port() != "" {
port, err = strconv.Atoi(_url.Port())
if err != nil {
return connection, err
}
}
conn, err := ssh.Dial(&ssh.ConnectionDialOptions{
Host: uri,
Identity: identity,
User: _url.User,
Port: port,
InsecureIsMachineConnection: machine,
}, ssh.GolangMode)
if err != nil {
return connection, newConnectError(err)
}
dialContext := func(ctx context.Context, _, _ string) (net.Conn, error) {
return ssh.DialNet(conn, "unix", _url)
}
connection.Client = &http.Client{
Transport: &http.Transport{
DialContext: dialContext,
}}
return connection, nil
}

func tcpClient(_url *url.URL) (Connection, error) {
connection := Connection{
URI: _url,
Expand Down

1 comment on commit b1efc50

@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.