Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a --ssh-agent-forwarding flag #88

Merged
merged 2 commits into from
Jun 24, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ const (
ArgSSHKeys = "ssh-keys"
// ArgsSSHPort is a ssh argument.
ArgsSSHPort = "ssh-port"
// ArgsSSHAgentForwarding is a ssh argument.
ArgsSSHAgentForwarding = "ssh-agent-forwarding"
// ArgUserData is a user data argument.
ArgUserData = "user-data"
// ArgUserDataFile is a user data file location argument.
Expand Down
8 changes: 4 additions & 4 deletions commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,15 @@ func withTestClient(t *testing.T, tFn testFn) {
}

type TestConfig struct {
SSHFn func(user, host, keyPath string, port int) runner.Runner
SSHFn func(user, host, keyPath string, port int, agentForwarding bool) runner.Runner
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm thinking this won't be the last option for SSH, so shouldn't we pass this option in a more forward-thinking way?

type SSHOptions map[string]interface{}

func(user, host, keyPath string, port int, options SSHOptions) runner.Runner {}

This would allow you do something like:

opts[doctl.ArgsSSHAgentForwarding] = true

Not sure what this means long term, but it would solve the immediate issue of longer function signatures.

v *viper.Viper
}

var _ doctl.Config = &TestConfig{}

func NewTestConfig() *TestConfig {
return &TestConfig{
SSHFn: func(u, h, kp string, p int) runner.Runner {
SSHFn: func(u, h, kp string, p int, a bool) runner.Runner {
return &doctl.MockRunner{}
},
v: viper.New(),
Expand All @@ -217,8 +217,8 @@ func (c *TestConfig) GetGodoClient(trace bool) (*godo.Client, error) {
return &godo.Client{}, nil
}

func (c *TestConfig) SSH(user, host, keyPath string, port int) runner.Runner {
return c.SSHFn(user, host, keyPath, port)
func (c *TestConfig) SSH(user, host, keyPath string, port int, agentForwarding bool) runner.Runner {
return c.SSHFn(user, host, keyPath, port, agentForwarding)
}

func (c *TestConfig) Set(ns, key string, val interface{}) {
Expand Down
10 changes: 8 additions & 2 deletions commands/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func SSH(parent *Command) *Command {
AddStringFlag(cmdSSH, doctl.ArgSSHUser, "root", "ssh user")
AddStringFlag(cmdSSH, doctl.ArgsSSHKeyPath, path, "path to private ssh key")
AddIntFlag(cmdSSH, doctl.ArgsSSHPort, 22, "port sshd is running on")
AddBoolFlag(cmdSSH, doctl.ArgsSSHAgentForwarding, false, "enable ssh agent forwarding")

return cmdSSH
}
Expand Down Expand Up @@ -72,6 +73,11 @@ func RunSSH(c *CmdConfig) error {
return err
}

agentForwarding, err := c.Doit.GetBool(c.NS, doctl.ArgsSSHAgentForwarding)
if err != nil {
return err
}

var droplet *do.Droplet

ds := c.Droplets()
Expand All @@ -93,7 +99,7 @@ func RunSSH(c *CmdConfig) error {

shi := extractHostInfo(dropletID)

if (shi.user != "") {
if shi.user != "" {
user = shi.user
}

Expand Down Expand Up @@ -131,7 +137,7 @@ func RunSSH(c *CmdConfig) error {
return errors.New("could not find droplet address")
}

runner := c.Doit.SSH(user, ip, keyPath, port)
runner := c.Doit.SSH(user, ip, keyPath, port, agentForwarding)
return runner.Run()
}

Expand Down
25 changes: 23 additions & 2 deletions commands/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestSSH_CustomPort(t *testing.T) {
rm.On("Run").Return(nil)

tc := config.Doit.(*TestConfig)
tc.SSHFn = func(user, host, keyPath string, port int) runner.Runner {
tc.SSHFn = func(user, host, keyPath string, port int, agentForwarding bool) runner.Runner {
assert.Equal(t, 2222, port)
return rm
}
Expand All @@ -104,7 +104,7 @@ func TestSSH_CustomUser(t *testing.T) {
rm.On("Run").Return(nil)

tc := config.Doit.(*TestConfig)
tc.SSHFn = func(user, host, keyPath string, port int) runner.Runner {
tc.SSHFn = func(user, host, keyPath string, port int, agentForwarding bool) runner.Runner {
assert.Equal(t, "foobar", user)
return rm
}
Expand All @@ -119,6 +119,27 @@ func TestSSH_CustomUser(t *testing.T) {
})
}

func TestSSH_AgentForwarding(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
rm := &mocks.Runner{}
rm.On("Run").Return(nil)

tc := config.Doit.(*TestConfig)
tc.SSHFn = func(user, host, keyPath string, port int, agentForwarding bool) runner.Runner {
assert.Equal(t, true, agentForwarding)
return rm
}

tm.droplets.On("List").Return(testDropletList, nil)

config.Doit.Set(config.NS, doctl.ArgsSSHAgentForwarding, true)
config.Args = append(config.Args, testDroplet.Name)

err := RunSSH(config)
assert.NoError(t, err)
})
}

func Test_extractHostInfo(t *testing.T) {
cases := []struct {
s string
Expand Down
14 changes: 7 additions & 7 deletions doit.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (glv *GithubLatestVersioner) LatestVersion() (string, error) {
// Config is an interface that represent doit's config.
type Config interface {
GetGodoClient(trace bool) (*godo.Client, error)
SSH(user, host, keyPath string, port int) runner.Runner
SSH(user, host, keyPath string, port int, agentForwarding bool) runner.Runner
Set(ns, key string, val interface{})
GetString(ns, key string) (string, error)
GetBool(ns, key string) (bool, error)
Expand Down Expand Up @@ -205,14 +205,14 @@ func (c *LiveConfig) GetGodoClient(trace bool) (*godo.Client, error) {
}

// SSH creates a ssh connection to a host.
func (c *LiveConfig) SSH(user, host, keyPath string, port int) runner.Runner {
func (c *LiveConfig) SSH(user, host, keyPath string, port int, agentForwarding bool) runner.Runner {
return &ssh.Runner{
User: user,
Host: host,
KeyPath: keyPath,
Port: port,
User: user,
Host: host,
KeyPath: keyPath,
Port: port,
AgentForwarding: agentForwarding,
}

}

// Set sets a config key.
Expand Down
9 changes: 5 additions & 4 deletions pkg/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ func sshConnect(user string, host string, method ssh.AuthMethod) error {

// Runner runs ssh commands.
type Runner struct {
User string
Host string
KeyPath string
Port int
User string
Host string
KeyPath string
Port int
AgentForwarding bool
}

var _ runner.Runner = &Runner{}
Expand Down
4 changes: 4 additions & 0 deletions pkg/ssh/ssh_external.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func runExternalSSH(r *Runner) error {
args = append(args, "-p", strconv.Itoa(r.Port))
}

if r.AgentForwarding {
args = append(args, "-A")
}

args = append(args, sshHost)

cmd := exec.Command("ssh", args...)
Expand Down