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

transport/ssh: allow passing SSH options #423

Merged
merged 2 commits into from
Jul 5, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions plumbing/transport/ssh/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ssh

import (
"fmt"
"reflect"

"gopkg.in/src-d/go-git.v4/plumbing/transport"
"gopkg.in/src-d/go-git.v4/plumbing/transport/internal/common"
Expand All @@ -11,7 +12,12 @@ import (
)

// DefaultClient is the default SSH client.
var DefaultClient = common.NewClient(&runner{})
var DefaultClient = NewClient(nil)

// NewClient creates a new SSH client with an optional *ssh.ClientConfig.
func NewClient(config *ssh.ClientConfig) transport.Transport {
return common.NewClient(&runner{config: config})
}

// DefaultAuthBuilder is the function used to create a default AuthMethod, when
// the user doesn't provide any.
Expand All @@ -21,10 +27,12 @@ var DefaultAuthBuilder = func(user string) (AuthMethod, error) {

const DefaultPort = 22

type runner struct{}
type runner struct {
config *ssh.ClientConfig
}

func (r *runner) Command(cmd string, ep transport.Endpoint, auth transport.AuthMethod) (common.Command, error) {
c := &command{command: cmd, endpoint: ep}
c := &command{command: cmd, endpoint: ep, config: r.config}
if auth != nil {
c.setAuth(auth)
}
Expand All @@ -42,6 +50,7 @@ type command struct {
endpoint transport.Endpoint
client *ssh.Client
auth AuthMethod
config *ssh.ClientConfig
}

func (c *command) setAuth(auth transport.AuthMethod) error {
Expand Down Expand Up @@ -95,6 +104,8 @@ func (c *command) connect() error {
return err
}

overrideConfig(c.config, config)

c.client, err = ssh.Dial("tcp", c.getHostWithPort(), config)
if err != nil {
return err
Expand Down Expand Up @@ -129,3 +140,25 @@ func (c *command) setAuthFromEndpoint() error {
func endpointToCommand(cmd string, ep transport.Endpoint) string {
return fmt.Sprintf("%s '%s'", cmd, ep.Path())
}

func overrideConfig(overrides *ssh.ClientConfig, c *ssh.ClientConfig) {
if overrides == nil {
return
}

vo := reflect.ValueOf(*overrides)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For do the copy should be enough something like:

var config *ssh.ClientConfig
*config = *overrides

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we don't want to override values set for the session, such as HostKeyCallback.

vc := reflect.ValueOf(*c)
for i := 0; i < vc.Type().NumField(); i++ {
vcf := vc.Field(i)
vof := vo.Field(i)
if isZeroValue(vcf) {
vcf.Set(vof)
}
}

*c = vc.Interface().(ssh.ClientConfig)
}

func isZeroValue(v reflect.Value) bool {
return reflect.DeepEqual(v.Interface(), reflect.Zero(v.Type()).Interface())
}