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

Commit

Permalink
transport: fix ssh override config, fixes #519
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuadros committed Jul 28, 2017
1 parent 11a403e commit 8ddbecf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
13 changes: 8 additions & 5 deletions plumbing/transport/ssh/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,14 @@ func overrideConfig(overrides *ssh.ClientConfig, c *ssh.ClientConfig) {
return
}

vo := reflect.ValueOf(*overrides)
vc := reflect.ValueOf(*c)
for i := 0; i < vc.Type().NumField(); i++ {
vcf := vc.Field(i)
vof := vo.Field(i)
t := reflect.TypeOf(*c)
vc := reflect.ValueOf(c).Elem()
vo := reflect.ValueOf(overrides).Elem()

for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
vcf := vc.FieldByName(f.Name)
vof := vo.FieldByName(f.Name)
if isZeroValue(vcf) {
vcf.Set(vof)
}
Expand Down
32 changes: 32 additions & 0 deletions plumbing/transport/ssh/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,39 @@ package ssh
import (
"testing"

"golang.org/x/crypto/ssh"

. "gopkg.in/check.v1"
)

func Test(t *testing.T) { TestingT(t) }

func (s *SuiteCommon) TestOverrideConfig(c *C) {
config := &ssh.ClientConfig{
User: "foo",
Auth: []ssh.AuthMethod{
ssh.Password("yourpassword"),
},
HostKeyCallback: ssh.FixedHostKey(nil),
}

target := &ssh.ClientConfig{}
overrideConfig(config, target)

c.Assert(target.User, Equals, "foo")
c.Assert(target.Auth, HasLen, 1)
c.Assert(target.HostKeyCallback, NotNil)
}

func (s *SuiteCommon) TestOverrideConfigKeep(c *C) {
config := &ssh.ClientConfig{
User: "foo",
}

target := &ssh.ClientConfig{
User: "bar",
}

overrideConfig(config, target)
c.Assert(target.User, Equals, "bar")
}

0 comments on commit 8ddbecf

Please sign in to comment.