Skip to content

Commit

Permalink
Merge pull request #24936 from vmasutin/fixing-ssh-identity
Browse files Browse the repository at this point in the history
Fixing ~/.ssh/identity handling
  • Loading branch information
openshift-merge-bot[bot] authored Jan 7, 2025
2 parents 564b8f3 + dd76034 commit 6abf83f
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions pkg/bindings/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/blang/semver/v4"
"github.com/containers/common/pkg/ssh"
"github.com/containers/podman/v5/version"
"github.com/containers/storage/pkg/fileutils"
"github.com/kevinburke/ssh_config"
"github.com/sirupsen/logrus"
"golang.org/x/net/proxy"
Expand Down Expand Up @@ -92,9 +93,7 @@ func NewConnection(ctx context.Context, uri string) (context.Context, error) {
// or unix:///run/podman/podman.sock
// 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
)
var err error
if v, found := os.LookupEnv("CONTAINER_HOST"); found && uri == "" {
uri = v
}
Expand Down Expand Up @@ -210,15 +209,27 @@ func sshClient(_url *url.URL, uri string, identity string, machine bool) (Connec

if identity == "" {
if val := cfg.Get(alias, "IdentityFile"); val != "" {
// we get default IdentityFile value (~/.ssh/identity) every time
// checking if we got default
defaultIdentityPath := val == ssh_config.Default("IdentityFile")

identity = strings.Trim(val, "\"")

if strings.HasPrefix(identity, "~/") {
homedir, err := os.UserHomeDir()
if err != nil {
return connection, fmt.Errorf("failed to find home dir: %w", err)
}

identity = filepath.Join(homedir, identity[2:])
}
found = true

// if we have default value but no file exists ignoring identity
if err := fileutils.Exists(identity); err != nil && defaultIdentityPath {
identity = ""
} else {
found = true
}
}
}

Expand Down Expand Up @@ -262,7 +273,8 @@ func sshClient(_url *url.URL, uri string, identity string, machine bool) (Connec
connection.Client = &http.Client{
Transport: &http.Transport{
DialContext: dialContext,
}}
},
}
return connection, nil
}

Expand Down

0 comments on commit 6abf83f

Please sign in to comment.