Skip to content

Commit

Permalink
Fixed failing unit tests related to git creds
Browse files Browse the repository at this point in the history
Signed-off-by: Anand Francis Joseph <anjoseph@redhat.com>
  • Loading branch information
anandf committed Oct 29, 2023
1 parent 3bb20be commit e53e475
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
6 changes: 4 additions & 2 deletions util/git/creds.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ func (c SSHCreds) Environ() (io.Closer, []string, error) {
args = append(args, "-o", "StrictHostKeyChecking=yes", "-o", fmt.Sprintf("UserKnownHostsFile=%s", knownHostsFile))
}
// Handle SSH socks5 proxy settings
proxyEnv := []string{}
if c.proxy != "" {
parsedProxyURL, err := url.Parse(c.proxy)
if err != nil {
Expand All @@ -316,13 +317,14 @@ func (c SSHCreds) Environ() (io.Closer, []string, error) {
parsedProxyURL.Hostname(),
parsedProxyURL.Port()))
if parsedProxyURL.User != nil {
env = append(env, fmt.Sprintf("SOCKS5_USER=%s", parsedProxyURL.User.Username()))
proxyEnv = append(proxyEnv, fmt.Sprintf("SOCKS5_USER=%s", parsedProxyURL.User.Username()))
if socks5_passwd, isPasswdSet := parsedProxyURL.User.Password(); isPasswdSet {
env = append(env, fmt.Sprintf("SOCKS5_PASSWD=%s", socks5_passwd))
proxyEnv = append(proxyEnv, fmt.Sprintf("SOCKS5_PASSWD=%s", socks5_passwd))
}
}
}
env = append(env, []string{fmt.Sprintf("GIT_SSH_COMMAND=%s", strings.Join(args, " "))}...)
env = append(env, proxyEnv...)
return sshPrivateKeyFile(file.Name()), env, nil
}

Expand Down
8 changes: 5 additions & 3 deletions util/git/creds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func Test_SSHCreds_Environ_WithProxy(t *testing.T) {
hostsPath := cert.GetSSHKnownHostsDataPath()
assert.Contains(t, env[1], fmt.Sprintf("-o UserKnownHostsFile=%s", hostsPath))
}
assert.Contains(t, env[1], "-o ProxyCommand='ncat --proxy-type socks5 --proxy 127.0.0.1:1080 --proxy-auth '' %h %p'")
assert.Contains(t, env[1], "-o ProxyCommand='connect-proxy -S 127.0.0.1:1080 -5 %h %p'")

envRegex := regexp.MustCompile("-i ([^ ]+)")
assert.Regexp(t, envRegex, env[1])
Expand All @@ -275,11 +275,13 @@ func Test_SSHCreds_Environ_WithProxyUserNamePassword(t *testing.T) {
creds := NewSSHCreds("sshPrivateKey", caFile, insecureIgnoreHostKey, &NoopCredsStore{}, "socks5://user:password@127.0.0.1:1080")
closer, env, err := creds.Environ()
require.NoError(t, err)
require.Len(t, env, 2)
require.Len(t, env, 4)

assert.Equal(t, fmt.Sprintf("GIT_SSL_CAINFO=%s/caFile", tempDir), env[0], "CAINFO env var must be set")

assert.True(t, strings.HasPrefix(env[1], "GIT_SSH_COMMAND="))
assert.Equal(t, "SOCKS5_USER=user", env[2], "SOCKS5 user env var must be set")
assert.Equal(t, "SOCKS5_PASSWD=password", env[3], "SOCKS5 password env var must be set")

if insecureIgnoreHostKey {
assert.Contains(t, env[1], "-o StrictHostKeyChecking=no")
Expand All @@ -289,7 +291,7 @@ func Test_SSHCreds_Environ_WithProxyUserNamePassword(t *testing.T) {
hostsPath := cert.GetSSHKnownHostsDataPath()
assert.Contains(t, env[1], fmt.Sprintf("-o UserKnownHostsFile=%s", hostsPath))
}
assert.Contains(t, env[1], "-o ProxyCommand='ncat --proxy-type socks5 --proxy 127.0.0.1:1080 --proxy-auth 'user:password' %h %p'")
assert.Contains(t, env[1], "-o ProxyCommand='connect-proxy -S 127.0.0.1:1080 -5 %h %p'")

envRegex := regexp.MustCompile("-i ([^ ]+)")
assert.Regexp(t, envRegex, env[1])
Expand Down

0 comments on commit e53e475

Please sign in to comment.