Skip to content

Commit 8ddbecf

Browse files
committed
transport: fix ssh override config, fixes #519
1 parent 11a403e commit 8ddbecf

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

plumbing/transport/ssh/common.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,14 @@ func overrideConfig(overrides *ssh.ClientConfig, c *ssh.ClientConfig) {
146146
return
147147
}
148148

149-
vo := reflect.ValueOf(*overrides)
150-
vc := reflect.ValueOf(*c)
151-
for i := 0; i < vc.Type().NumField(); i++ {
152-
vcf := vc.Field(i)
153-
vof := vo.Field(i)
149+
t := reflect.TypeOf(*c)
150+
vc := reflect.ValueOf(c).Elem()
151+
vo := reflect.ValueOf(overrides).Elem()
152+
153+
for i := 0; i < t.NumField(); i++ {
154+
f := t.Field(i)
155+
vcf := vc.FieldByName(f.Name)
156+
vof := vo.FieldByName(f.Name)
154157
if isZeroValue(vcf) {
155158
vcf.Set(vof)
156159
}

plumbing/transport/ssh/common_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,39 @@ package ssh
33
import (
44
"testing"
55

6+
"golang.org/x/crypto/ssh"
7+
68
. "gopkg.in/check.v1"
79
)
810

911
func Test(t *testing.T) { TestingT(t) }
12+
13+
func (s *SuiteCommon) TestOverrideConfig(c *C) {
14+
config := &ssh.ClientConfig{
15+
User: "foo",
16+
Auth: []ssh.AuthMethod{
17+
ssh.Password("yourpassword"),
18+
},
19+
HostKeyCallback: ssh.FixedHostKey(nil),
20+
}
21+
22+
target := &ssh.ClientConfig{}
23+
overrideConfig(config, target)
24+
25+
c.Assert(target.User, Equals, "foo")
26+
c.Assert(target.Auth, HasLen, 1)
27+
c.Assert(target.HostKeyCallback, NotNil)
28+
}
29+
30+
func (s *SuiteCommon) TestOverrideConfigKeep(c *C) {
31+
config := &ssh.ClientConfig{
32+
User: "foo",
33+
}
34+
35+
target := &ssh.ClientConfig{
36+
User: "bar",
37+
}
38+
39+
overrideConfig(config, target)
40+
c.Assert(target.User, Equals, "bar")
41+
}

0 commit comments

Comments
 (0)