Skip to content

Commit 8c9e884

Browse files
committed
Handle errors when resolving ssh aliases to hostnames
1 parent 0921311 commit 8c9e884

File tree

2 files changed

+48
-23
lines changed

2 files changed

+48
-23
lines changed

pkg/ssh/ssh.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ func (t *Translator) resolve(hostname string) (string, error) {
9595
}
9696
}
9797

98-
_ = sshCmd.Wait()
98+
err = sshCmd.Wait()
99+
if err != nil || resolvedHost == "" {
100+
// handle failures by returning the original hostname unchanged
101+
resolvedHost = hostname
102+
}
99103

100104
if t.cacheMap == nil {
101105
t.cacheMap = map[string]string{}

pkg/ssh/ssh_test.go

+43-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ssh
22

33
import (
4+
"errors"
45
"fmt"
56
"net/url"
67
"os"
@@ -85,7 +86,13 @@ func TestHelperProcess(t *testing.T) {
8586
return
8687
}
8788
if err := func(args []string) error {
88-
fmt.Fprint(os.Stdout, "hostname github.com\n")
89+
if len(args) < 3 || args[2] == "error" {
90+
return errors.New("fatal")
91+
}
92+
if args[2] == "empty.io" {
93+
return nil
94+
}
95+
fmt.Fprintf(os.Stdout, "hostname %s\n", args[2])
8996
return nil
9097
}(os.Args[3:]); err != nil {
9198
fmt.Fprintln(os.Stderr, err)
@@ -111,32 +118,46 @@ func TestTranslator_caching(t *testing.T) {
111118
},
112119
}
113120

114-
u1, err := url.Parse("ssh://github1.com/owner/repo.git")
115-
if err != nil {
116-
t.Fatalf("error parsing URL: %v", err)
117-
}
118-
if res := tr.Translate(u1); res.Host != "github.com" {
119-
t.Errorf("expected github.com, got: %q", res.Host)
120-
}
121-
if res := tr.Translate(u1); res.Host != "github.com" {
122-
t.Errorf("expected github.com, got: %q", res.Host)
123-
}
124-
125-
u2, err := url.Parse("ssh://github2.com/owner/repo.git")
126-
if err != nil {
127-
t.Fatalf("error parsing URL: %v", err)
128-
}
129-
if res := tr.Translate(u2); res.Host != "github.com" {
130-
t.Errorf("expected github.com, got: %q", res.Host)
121+
tests := []struct {
122+
input string
123+
result string
124+
}{
125+
{
126+
input: "ssh://github1.com/owner/repo.git",
127+
result: "github1.com",
128+
},
129+
{
130+
input: "ssh://github2.com/owner/repo.git",
131+
result: "github2.com",
132+
},
133+
{
134+
input: "ssh://empty.io/owner/repo.git",
135+
result: "empty.io",
136+
},
137+
{
138+
input: "ssh://error/owner/repo.git",
139+
result: "error",
140+
},
131141
}
132-
if res := tr.Translate(u2); res.Host != "github.com" {
133-
t.Errorf("expected github.com, got: %q", res.Host)
142+
for _, tt := range tests {
143+
t.Run(tt.input, func(t *testing.T) {
144+
u, err := url.Parse(tt.input)
145+
if err != nil {
146+
t.Fatalf("error parsing URL: %v", err)
147+
}
148+
if res := tr.Translate(u); res.Host != tt.result {
149+
t.Errorf("expected github.com, got: %q", res.Host)
150+
}
151+
if res := tr.Translate(u); res.Host != tt.result {
152+
t.Errorf("expected github.com, got: %q (second call)", res.Host)
153+
}
154+
})
134155
}
135156

136157
if countLookPath != 1 {
137158
t.Errorf("expected lookPath to happen 1 time; actual: %d", countLookPath)
138159
}
139-
if countNewCommand != 2 {
140-
t.Errorf("expected ssh command to shell out 2 times; actual: %d", countNewCommand)
160+
if countNewCommand != len(tests) {
161+
t.Errorf("expected ssh command to shell out %d times; actual: %d", len(tests), countNewCommand)
141162
}
142163
}

0 commit comments

Comments
 (0)