Skip to content

Commit

Permalink
ssh: avoid setting flags through hostname
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
  • Loading branch information
tonistiigi authored and Tibor Vass committed May 28, 2020
1 parent 263bb53 commit d30970e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cli/connhelper/connhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func GetConnectionHelper(daemonURL string) (*ConnectionHelper, error) {
}
return &ConnectionHelper{
Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
return commandconn.New(ctx, "ssh", append(sp.Args(), []string{"--", "docker", "system", "dial-stdio"}...)...)
return commandconn.New(ctx, "ssh", sp.Args("docker", "system", "dial-stdio")...)
},
Host: "http://docker",
}, nil
Expand Down
7 changes: 4 additions & 3 deletions cli/connhelper/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,16 @@ type Spec struct {
Port string
}

// Args returns args except "ssh" itself and "-- ..."
func (sp *Spec) Args() []string {
// Args returns args except "ssh" itself combined with optional additional command args
func (sp *Spec) Args(add ...string) []string {
var args []string
if sp.User != "" {
args = append(args, "-l", sp.User)
}
if sp.Port != "" {
args = append(args, "-p", sp.Port)
}
args = append(args, sp.Host)
args = append(args, "--", sp.Host)
args = append(args, add...)
return args
}
20 changes: 11 additions & 9 deletions cli/connhelper/ssh/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ func TestParseURL(t *testing.T) {
{
url: "ssh://foo",
expectedArgs: []string{
"foo",
"--", "foo",
},
},
{
url: "ssh://me@foo:10022",
expectedArgs: []string{
"-l", "me",
"-p", "10022",
"foo",
"--", "foo",
},
},
{
Expand Down Expand Up @@ -53,12 +53,14 @@ func TestParseURL(t *testing.T) {
},
}
for _, tc := range testCases {
sp, err := ParseURL(tc.url)
if tc.expectedError == "" {
assert.NilError(t, err)
assert.Check(t, is.DeepEqual(tc.expectedArgs, sp.Args()))
} else {
assert.ErrorContains(t, err, tc.expectedError)
}
t.Run(tc.url, func(t *testing.T) {
sp, err := ParseURL(tc.url)
if tc.expectedError == "" {
assert.NilError(t, err)
assert.Check(t, is.DeepEqual(tc.expectedArgs, sp.Args()))
} else {
assert.ErrorContains(t, err, tc.expectedError)
}
})
}
}

0 comments on commit d30970e

Please sign in to comment.