Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
support alternate git ssh ports (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
khrisrichardson authored and knative-prow-robot committed Jan 4, 2019
1 parent d2a0ec1 commit 987eb29
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
36 changes: 33 additions & 3 deletions pkg/credentials/gitcreds/creds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ func TestSSHFlagHandling(t *testing.T) {
expectedSSHConfig := fmt.Sprintf(`Host github.com
HostName github.com
IdentityFile %s
Port 22
`, filepath.Join(os.Getenv("HOME"), ".ssh", "id_foo"))
if string(b) != expectedSSHConfig {
t.Errorf("got: %v, wanted: %v", string(b), expectedSSHConfig)
Expand All @@ -243,7 +244,7 @@ func TestSSHFlagHandling(t *testing.T) {
}
}

func TestSSHFlagHandlingTwice(t *testing.T) {
func TestSSHFlagHandlingThrice(t *testing.T) {
credentials.VolumePath, _ = ioutil.TempDir("", "")
fooDir := credentials.VolumeName("foo")
if err := os.MkdirAll(fooDir, os.ModePerm); err != nil {
Expand All @@ -265,12 +266,23 @@ func TestSSHFlagHandlingTwice(t *testing.T) {
if err := ioutil.WriteFile(filepath.Join(barDir, "known_hosts"), []byte("ssh-rsa bbbb"), 0777); err != nil {
t.Fatalf("ioutil.WriteFile(known_hosts) = %v", err)
}
bazDir := credentials.VolumeName("baz")
if err := os.MkdirAll(bazDir, os.ModePerm); err != nil {
t.Fatalf("os.MkdirAll(%s) = %v", bazDir, err)
}
if err := ioutil.WriteFile(filepath.Join(bazDir, corev1.SSHAuthPrivateKey), []byte("derp"), 0777); err != nil {
t.Fatalf("ioutil.WriteFile(ssh-privatekey) = %v", err)
}
if err := ioutil.WriteFile(filepath.Join(bazDir, "known_hosts"), []byte("ssh-rsa cccc"), 0777); err != nil {
t.Fatalf("ioutil.WriteFile(known_hosts) = %v", err)
}

fs := flag.NewFlagSet("test", flag.ContinueOnError)
flags(fs)
err := fs.Parse([]string{
"-ssh-git=foo=github.com",
"-ssh-git=bar=gitlab.com",
"-ssh-git=baz=gitlab.example.com:2222",
})
if err != nil {
t.Fatalf("flag.CommandLine.Parse() = %v", err)
Expand All @@ -289,11 +301,18 @@ func TestSSHFlagHandlingTwice(t *testing.T) {
expectedSSHConfig := fmt.Sprintf(`Host github.com
HostName github.com
IdentityFile %s
Port 22
Host gitlab.com
HostName gitlab.com
IdentityFile %s
Port 22
Host gitlab.example.com
HostName gitlab.example.com
IdentityFile %s
Port 2222
`, filepath.Join(os.Getenv("HOME"), ".ssh", "id_foo"),
filepath.Join(os.Getenv("HOME"), ".ssh", "id_bar"))
filepath.Join(os.Getenv("HOME"), ".ssh", "id_bar"),
filepath.Join(os.Getenv("HOME"), ".ssh", "id_baz"))
if string(b) != expectedSSHConfig {
t.Errorf("got: %v, wanted: %v", string(b), expectedSSHConfig)
}
Expand All @@ -303,7 +322,8 @@ Host gitlab.com
t.Fatalf("ioutil.ReadFile(.ssh/known_hosts) = %v", err)
}
expectedSSHKnownHosts := `ssh-rsa aaaa
ssh-rsa bbbb`
ssh-rsa bbbb
ssh-rsa cccc`
if string(b) != expectedSSHKnownHosts {
t.Errorf("got: %v, wanted: %v", string(b), expectedSSHKnownHosts)
}
Expand All @@ -327,6 +347,16 @@ ssh-rsa bbbb`
if string(b) != expectedIDBar {
t.Errorf("got: %v, wanted: %v", string(b), expectedIDBar)
}

b, err = ioutil.ReadFile(filepath.Join(credentials.VolumePath, ".ssh", "id_baz"))
if err != nil {
t.Fatalf("ioutil.ReadFile(.ssh/id_baz) = %v", err)
}

expectedIDBaz := `derp`
if string(b) != expectedIDBaz {
t.Errorf("got: %v, wanted: %v", string(b), expectedIDBaz)
}
}

func TestSSHFlagHandlingMissingFiles(t *testing.T) {
Expand Down
11 changes: 10 additions & 1 deletion pkg/credentials/gitcreds/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -85,16 +86,24 @@ func (dc *sshGitConfig) Write() error {
// 2. Compute its part of "~/.ssh/config"
// 3. Compute its part of "~/.ssh/known_hosts"
var configEntries []string
var defaultPort = "22"
var knownHosts []string
for _, k := range dc.order {
var host, port string
var err error
if host, port, err = net.SplitHostPort(k); err != nil {
host = k
port = defaultPort
}
v := dc.entries[k]
if err := v.Write(sshDir); err != nil {
return err
}
configEntries = append(configEntries, fmt.Sprintf(`Host %s
HostName %s
IdentityFile %s
`, k, k, v.path(sshDir)))
Port %s
`, host, host, v.path(sshDir), port))

knownHosts = append(knownHosts, v.knownHosts)
}
Expand Down

0 comments on commit 987eb29

Please sign in to comment.