Skip to content

Commit

Permalink
Add --user/-u option
Browse files Browse the repository at this point in the history
Signed-off-by: Blaine Gardner <[email protected]>
  • Loading branch information
BlaineEXE committed Nov 11, 2018
1 parent d0deb7e commit e674e51
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
2 changes: 2 additions & 0 deletions cmd/octopus/config/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func init() {
"(ssh) file from which the identity (private key) for public key authentication is read")
OctopusCmd.PersistentFlags().Uint16P("port", "p", 22,
"(ssh) port on which to connect to hosts")
OctopusCmd.PersistentFlags().StringP("user", "u", "root",
"user as which to connect to hosts (corresponds to ssh \"-l\" option)")
OctopusCmd.PersistentFlags().BoolP("verbose", "v", false,
"print additional information about octopus progress")

Expand Down
1 change: 1 addition & 0 deletions cmd/octopus/config/octopus.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TrainOctopus() *octopus.Octopus {

remoteConnector.AddIdentityFile(identityFile)
remoteConnector.Port(uint16(viper.GetInt("port")))
remoteConnector.User(viper.GetString("user"))

return octopus.New(
remoteConnector,
Expand Down
3 changes: 3 additions & 0 deletions internal/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ type Connector interface {
// Port should set the port on which remote connections will be made to hosts.
Port(p uint16) error

// User should set the user on hosts to which connections will be made.
User(u string) error

// Connect should connect to the host with the options that have been previously set and return
// an actor which can be called to perform tasks on the remote host. If an error is reported,
// the actor should not need to have its Close method called.
Expand Down
7 changes: 6 additions & 1 deletion internal/remote/test/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ func (c *MockRemoteConnector) AddIdentityFile(filePath string) error {

// Port is a mock method that is not yet implemented.
func (c *MockRemoteConnector) Port(p uint16) error {
return fmt.Errorf("not implemented")
panic("not implemented")
}

// User is a mock method that is not yet implemented.
func (c *MockRemoteConnector) User(u string) error {
panic("not implemented")
}

// Connect is a mock method that appends each host to HostConnects.
Expand Down
6 changes: 6 additions & 0 deletions internal/ssh/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ func (c *Connector) Port(p uint16) error {
return nil
}

// User sets the user on hosts to which connections will be made. The default user is 'root'.
func (c *Connector) User(u string) error {
c.clientConfig.User = u
return nil
}

var dialHost = ssh.Dial

// Connect connects to the host via ssh with the options that have been previously set and returns
Expand Down
28 changes: 19 additions & 9 deletions internal/ssh/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,19 @@ func TestConnector_Connect(t *testing.T) {
testutil.WriteFile(parsableKeyfile, parsableKey, 0644)

// Test connector ready to go with an identity file added
stubConnector := NewConnector()
stubConnector.AddIdentityFile(parsableKeyfile)
stubConnector := func() *Connector {
c := NewConnector()
c.AddIdentityFile(parsableKeyfile)
return c
}

stubClient := &ssh.Client{} // stub dialer always returns this client

var lastAddrDialed string // for checking that the right address was dialed
failDial := true
dialHost = func(network, addr string, config *ssh.ClientConfig) (*ssh.Client, error) {
lastAddrDialed = addr
// include the user in report of last addr dialed to keep test definitions leaner
lastAddrDialed = config.User + "@" + addr
if failDial {
return nil, fmt.Errorf("test dial error")
}
Expand Down Expand Up @@ -130,16 +134,22 @@ func TestConnector_Connect(t *testing.T) {
failDial bool
wants wants
}{
{"successful connection", stubConnector, "1.1.1.1", false, wants{
addrDialed: "1.1.1.1:22", actor: expectedActor("1.1.1.1"), err: false}},
{"fail to dial", stubConnector, "2.2.2.2", true, wants{
addrDialed: "2.2.2.2:22", actor: nil, err: true}},
{"successful connection", stubConnector(), "1.1.1.1", false, wants{
addrDialed: "root@1.1.1.1:22", actor: expectedActor("1.1.1.1"), err: false}},
{"fail to dial", stubConnector(), "2.2.2.2", true, wants{
addrDialed: "root@2.2.2.2:22", actor: nil, err: true}},
{"connector w/o id file", NewConnector(), "not dialed", false, wants{
addrDialed: "not dialed", actor: nil, err: true}},
// Effectively test Port option
{"connect with a different port",
func() (c *Connector) { c = new(Connector); *c = *stubConnector; c.Port(2222); return }(),
func() *Connector { c := stubConnector(); c.Port(2222); return c }(),
"3.3.3.3", false, wants{
addrDialed: "3.3.3.3:2222", actor: expectedActor("3.3.3.3"), err: false}},
addrDialed: "[email protected]:2222", actor: expectedActor("3.3.3.3"), err: false}},
// Effectively test User option
{"connect with a different user",
func() *Connector { c := stubConnector(); c.User("sam"); return c }(),
"4.4.4.4", false, wants{
addrDialed: "[email protected]:22", actor: expectedActor("4.4.4.4"), err: false}},
}
for _, tt := range tests {
lastAddrDialed = "not dialed"
Expand Down

0 comments on commit e674e51

Please sign in to comment.