Skip to content

Commit cd66634

Browse files
committed
Fix checking out branches/tags on private repos with SSH
Introduce dependency on github.com/kevinburke/ssh_config (MIT license) in order to parse workspace SSH config and resolve which key should be used when listing branches/tags on a remote repository. Currently, only one IdentityFile per host is supported, compared to Git which by default tries additional keys if connection fails. Signed-off-by: Angel Misevski <[email protected]>
1 parent c4a087e commit cd66634

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/google/go-cmp v0.5.9
1010
github.com/google/gofuzz v1.2.0
1111
github.com/google/uuid v1.1.2
12+
github.com/kevinburke/ssh_config v1.2.0
1213
github.com/onsi/ginkgo/v2 v2.0.0
1314
github.com/onsi/gomega v1.17.0
1415
github.com/openshift/api v0.0.0-20200205133042-34f0ec8dab87

project-clone/internal/global.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,18 @@ import (
2626
gittransport "github.com/go-git/go-git/v5/plumbing/transport"
2727
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
2828
gitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
29+
"github.com/kevinburke/ssh_config"
2930
)
3031

3132
const (
3233
credentialsMountPath = "/.git-credentials/credentials"
34+
sshConfigMountPath = "/etc/ssh/ssh_config"
3335
)
3436

3537
var (
3638
ProjectsRoot string
3739
CloneTmpDir string
3840
tokenAuthMethod map[string]*githttp.BasicAuth
39-
sshAuthMethod *gitssh.PublicKeys
4041
credentialsRegex = regexp.MustCompile(`https://(.+):(.+)@(.+)`)
4142
)
4243

@@ -67,8 +68,22 @@ func GetAuthForHost(repoURLStr string) (gittransport.AuthMethod, error) {
6768
}
6869
switch endpoint.Protocol {
6970
case "ssh":
70-
// TODO
71-
return nil, fmt.Errorf("SSH support not yet implemented")
71+
identityFiles := ssh_config.GetAll(endpoint.Host, "IdentityFile")
72+
if len(identityFiles) == 0 {
73+
log.Printf("No SSH key found for host %s", endpoint.Host)
74+
} else if len(identityFiles) > 1 {
75+
// Probably should try all keys, one by one, in the future
76+
log.Printf("Warning: multiple SSH keys found for host %s. Using first match.", endpoint.Host)
77+
}
78+
user := ssh_config.Get(endpoint.Host, "User")
79+
if user == "" {
80+
user = "git"
81+
}
82+
pubkeys, err := gitssh.NewPublicKeysFromFile(user, identityFiles[0], "")
83+
if err != nil {
84+
return nil, fmt.Errorf("failed to set up SSH: %w", err)
85+
}
86+
return pubkeys, nil
7287
case "http", "https":
7388
authMethod, ok := tokenAuthMethod[endpoint.Host]
7489
if !ok {

0 commit comments

Comments
 (0)