Skip to content

Commit 8c0c1db

Browse files
committed
cli/connhelper/ssh: use stdlib errors and improve errors
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 6ca9766 commit 8c0c1db

2 files changed

Lines changed: 44 additions & 14 deletions

File tree

cli/connhelper/ssh/ssh.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
package ssh
33

44
import (
5+
"errors"
6+
"fmt"
57
"net/url"
6-
7-
"github.com/pkg/errors"
88
)
99

1010
// ParseURL creates a [Spec] from the given ssh URL. It returns an error if
@@ -13,10 +13,25 @@ import (
1313
func ParseURL(daemonURL string) (*Spec, error) {
1414
u, err := url.Parse(daemonURL)
1515
if err != nil {
16-
return nil, err
16+
var urlErr *url.Error
17+
if errors.As(err, &urlErr) {
18+
err = urlErr.Unwrap()
19+
}
20+
return nil, fmt.Errorf("invalid ssh URL: %w", err)
21+
}
22+
s, err := newSpec(u)
23+
if err != nil {
24+
return nil, fmt.Errorf("invalid ssh URL: %w", err)
25+
}
26+
return s, nil
27+
}
28+
29+
func newSpec(u *url.URL) (*Spec, error) {
30+
if u.Scheme == "" {
31+
return nil, errors.New("no scheme provided")
1732
}
1833
if u.Scheme != "ssh" {
19-
return nil, errors.Errorf("expected scheme ssh, got %q", u.Scheme)
34+
return nil, errors.New("incorrect scheme: " + u.Scheme)
2035
}
2136

2237
var sp Spec
@@ -29,17 +44,18 @@ func ParseURL(daemonURL string) (*Spec, error) {
2944
}
3045
sp.Host = u.Hostname()
3146
if sp.Host == "" {
32-
return nil, errors.Errorf("no host specified")
47+
return nil, errors.New("hostname is empty")
3348
}
3449
sp.Port = u.Port()
3550
sp.Path = u.Path
3651
if u.RawQuery != "" {
37-
return nil, errors.Errorf("extra query after the host: %q", u.RawQuery)
52+
return nil, fmt.Errorf("query parameters are not allowed: %q", u.RawQuery)
3853
}
3954
if u.Fragment != "" {
40-
return nil, errors.Errorf("extra fragment after the host: %q", u.Fragment)
55+
return nil, fmt.Errorf("fragments are not allowed: %q", u.Fragment)
4156
}
42-
return &sp, err
57+
58+
return &sp, nil
4359
}
4460

4561
// Spec of SSH URL

cli/connhelper/ssh/ssh_test.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,30 +84,44 @@ func TestParseURL(t *testing.T) {
8484
Path: "/var/run/docker.sock",
8585
},
8686
},
87+
{
88+
doc: "malformed URL",
89+
url: "malformed %%url",
90+
expectedError: `invalid ssh URL: invalid URL escape "%%u"`,
91+
},
92+
{
93+
doc: "URL missing scheme",
94+
url: "no-scheme.example.com",
95+
expectedError: "invalid ssh URL: no scheme provided",
96+
},
8797
{
8898
doc: "invalid URL with password",
8999
url: "ssh://me:passw0rd@example.com",
90-
expectedError: "plain-text password is not supported",
100+
expectedError: "invalid ssh URL: plain-text password is not supported",
91101
},
92102
{
93103
doc: "invalid URL with query parameter",
94-
url: "ssh://example.com?bar",
95-
expectedError: `extra query after the host: "bar"`,
104+
url: "ssh://example.com?foo=bar&bar=baz",
105+
expectedError: `invalid ssh URL: query parameters are not allowed: "foo=bar&bar=baz"`,
96106
},
97107
{
98108
doc: "invalid URL with fragment",
99109
url: "ssh://example.com#bar",
100-
expectedError: `extra fragment after the host: "bar"`,
110+
expectedError: `invalid ssh URL: fragments are not allowed: "bar"`,
101111
},
102112
{
103113
doc: "invalid URL without hostname",
104114
url: "ssh://",
105-
expectedError: "no host specified",
115+
expectedError: "invalid ssh URL: hostname is empty",
116+
},
117+
{
118+
url: "ssh:///no-hostname",
119+
expectedError: "invalid ssh URL: hostname is empty",
106120
},
107121
{
108122
doc: "invalid URL with unsupported scheme",
109123
url: "https://example.com",
110-
expectedError: `expected scheme ssh, got "https"`,
124+
expectedError: `invalid ssh URL: incorrect scheme: https`,
111125
},
112126
}
113127
for _, tc := range testCases {

0 commit comments

Comments
 (0)