Add some target fields and move some things around (#3879)
In preparation for putting proxy code in the api package this creates some necessary fields in the target output and it pre-moves credential parsing and proxy handshake code to their new places. This will make the diff for the actual proxy code move quite a bit smaller. Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
This commit is contained in:
1 parent
82b3f202c0
commit
fb4a5a98dc
27 files changed
+733
-444
No files matched your search
@@ -9,6 +9,7 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
apiproxy "github.com/hashicorp/boundary/api/proxy"
|
||||
"github.com/hashicorp/boundary/internal/cmd/base"
|
||||
"github.com/hashicorp/boundary/internal/target"
|
||||
"github.com/posener/complete"
|
||||
@@ -54,7 +55,7 @@ func (s *sshFlags) defaultExec() string {
|
||||
return strings.ToLower(s.flagSshStyle)
|
||||
}
|
||||
|
||||
func (s *sshFlags) buildArgs(c *Command, port, ip, _ string, creds credentials) (args, envs []string, retCreds credentials, retErr error) {
|
||||
func (s *sshFlags) buildArgs(c *Command, port, ip, _ string, creds apiproxy.Credentials) (args, envs []string, retCreds apiproxy.Credentials, retErr error) {
|
||||
var username string
|
||||
retCreds = creds
|
||||
|
||||
@@ -89,20 +90,20 @@ func (s *sshFlags) buildArgs(c *Command, port, ip, _ string, creds credentials)
|
||||
|
||||
case "sshpass":
|
||||
if !tryConsume {
|
||||
return nil, nil, credentials{}, errors.New("Credentials must be consumed when using sshpass")
|
||||
return nil, nil, apiproxy.Credentials{}, errors.New("Credentials must be consumed when using sshpass")
|
||||
}
|
||||
var password string
|
||||
if len(retCreds.usernamePassword) > 0 {
|
||||
if len(retCreds.UsernamePassword) > 0 {
|
||||
// For now just grab the first username password credential brokered
|
||||
// Mark credential as consumed so that it is not printed to user
|
||||
retCreds.usernamePassword[0].consumed = true
|
||||
retCreds.UsernamePassword[0].Consumed = true
|
||||
|
||||
username = retCreds.usernamePassword[0].Username
|
||||
password = retCreds.usernamePassword[0].Password
|
||||
username = retCreds.UsernamePassword[0].Username
|
||||
password = retCreds.UsernamePassword[0].Password
|
||||
}
|
||||
|
||||
if password == "" {
|
||||
return nil, nil, credentials{}, errors.New("Password is required when using sshpass")
|
||||
return nil, nil, apiproxy.Credentials{}, errors.New("Password is required when using sshpass")
|
||||
}
|
||||
|
||||
// sshpass requires that the password is passed as env-var "SSHPASS"
|
||||
@@ -130,24 +131,24 @@ func (s *sshFlags) buildArgs(c *Command, port, ip, _ string, creds credentials)
|
||||
// Do nothing
|
||||
|
||||
// If we want to consume check if we have a private key available first
|
||||
case len(creds.sshPrivateKey) > 0:
|
||||
case len(creds.SshPrivateKey) > 0:
|
||||
// For now just grab the first ssh private key credential brokered
|
||||
cred := retCreds.sshPrivateKey[0]
|
||||
cred := retCreds.SshPrivateKey[0]
|
||||
|
||||
username = cred.Username
|
||||
privateKey := cred.PrivateKey
|
||||
cred.consumed = true
|
||||
cred.Consumed = true
|
||||
if cred.Passphrase != "" {
|
||||
// Don't re-print everything, but print the passphrase they'll need
|
||||
cred.consumed = false
|
||||
delete(cred.raw.Credential, "username")
|
||||
delete(cred.raw.Credential, "private_key")
|
||||
cred.Consumed = false
|
||||
delete(cred.Raw.Credential, "username")
|
||||
delete(cred.Raw.Credential, "private_key")
|
||||
}
|
||||
retCreds.sshPrivateKey[0] = cred
|
||||
retCreds.SshPrivateKey[0] = cred
|
||||
|
||||
pkFile, err := os.CreateTemp("", "*")
|
||||
if err != nil {
|
||||
return nil, nil, credentials{}, fmt.Errorf("Error saving ssh private key to tmp file: %w", err)
|
||||
return nil, nil, apiproxy.Credentials{}, fmt.Errorf("Error saving ssh private key to tmp file: %w", err)
|
||||
}
|
||||
c.cleanupFuncs = append(c.cleanupFuncs, func() error {
|
||||
if err := os.Remove(pkFile.Name()); err != nil {
|
||||
@@ -163,15 +164,15 @@ func (s *sshFlags) buildArgs(c *Command, port, ip, _ string, creds credentials)
|
||||
}
|
||||
_, err = pkFile.WriteString(privateKey)
|
||||
if err != nil {
|
||||
return nil, nil, credentials{}, fmt.Errorf("Error writing private key file to %s: %w", pkFile.Name(), err)
|
||||
return nil, nil, apiproxy.Credentials{}, fmt.Errorf("Error writing private key file to %s: %w", pkFile.Name(), err)
|
||||
}
|
||||
if err := pkFile.Close(); err != nil {
|
||||
return nil, nil, credentials{}, fmt.Errorf("Error closing private key file after writing to %s: %w", pkFile.Name(), err)
|
||||
return nil, nil, apiproxy.Credentials{}, fmt.Errorf("Error closing private key file after writing to %s: %w", pkFile.Name(), err)
|
||||
}
|
||||
args = append(args, "-i", pkFile.Name())
|
||||
|
||||
// Next check if we have a username password credential
|
||||
case len(creds.usernamePassword) > 0:
|
||||
case len(creds.UsernamePassword) > 0:
|
||||
// We cannot use the password of the credential outside of sshpass, but we
|
||||
// can use the username.
|
||||
// N.B. Do not mark credential as consumed, as user will still need enter
|
||||
@@ -180,7 +181,7 @@ func (s *sshFlags) buildArgs(c *Command, port, ip, _ string, creds credentials)
|
||||
if c.flagUsername == "" {
|
||||
// If the user did not actively provide a username flag set the
|
||||
// username to that of the first credential we got.
|
||||
username = retCreds.usernamePassword[0].Username
|
||||
username = retCreds.UsernamePassword[0].Username
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user