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

command/ssh: create and reuse the api client #3909

Merged
merged 3 commits into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion command/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ type BaseCommand struct {

tokenHelper token.TokenHelper

// For testing
client *api.Client
}

Expand Down Expand Up @@ -110,6 +109,8 @@ func (c *BaseCommand) Client() (*api.Client, error) {
client.SetToken(token)
}

c.client = client

return client, nil
}

Expand Down
28 changes: 10 additions & 18 deletions command/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ func (c *SSHCommand) Run(args []string) int {
sshArgs = args[1:]
}

// Set the client in the command
_, err = c.Client()
if err != nil {
c.UI.Error(err.Error())
return 1
}

// Credentials are generated only against a registered role. If user
// does not specify a role with the SSH command, then lookup API is used
// to fetch all the roles with which this IP is associated. If there is
Expand Down Expand Up @@ -331,13 +338,7 @@ func (c *SSHCommand) handleTypeCA(username, ip string, sshArgs []string) int {
return 1
}

client, err := c.Client()
if err != nil {
c.UI.Error(err.Error())
return 1
}

sshClient := client.SSHWithMountPoint(c.flagMountPoint)
sshClient := c.client.SSHWithMountPoint(c.flagMountPoint)

// Attempt to sign the public key
secret, err := sshClient.SignKey(c.flagRole, map[string]interface{}{
Expand Down Expand Up @@ -611,12 +612,7 @@ func (c *SSHCommand) handleTypeDynamic(username, ip string, sshArgs []string) in
// generateCredential generates a credential for the given role and returns the
// decoded secret data.
func (c *SSHCommand) generateCredential(username, ip string) (*api.Secret, *SSHCredentialResp, error) {
client, err := c.Client()
if err != nil {
return nil, nil, err
}

sshClient := client.SSHWithMountPoint(c.flagMountPoint)
sshClient := c.client.SSHWithMountPoint(c.flagMountPoint)

// Attempt to generate the credential.
secret, err := sshClient.Credential(c.flagRole, map[string]interface{}{
Expand Down Expand Up @@ -683,11 +679,7 @@ func (c *SSHCommand) defaultRole(mountPoint, ip string) (string, error) {
data := map[string]interface{}{
"ip": ip,
}
client, err := c.Client()
if err != nil {
return "", err
}
secret, err := client.Logical().Write(mountPoint+"/lookup", data)
secret, err := c.client.Logical().Write(mountPoint+"/lookup", data)
if err != nil {
return "", fmt.Errorf("Error finding roles for IP %q: %q", ip, err)

Expand Down