Update target credential library service, API, SDK, CLI (#1343)
This commit is contained in:
1 parent
83c65d0d4b
commit
86c9a90554
27 files changed
+1474
-1127
No files matched your search
@@ -1,6 +1,10 @@
|
||||
package connect
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/cmd/base"
|
||||
@@ -28,7 +32,15 @@ func postgresOptions(c *Command, set *base.FlagSets) {
|
||||
Target: &c.flagUsername,
|
||||
EnvVar: "BOUNDARY_CONNECT_USERNAME",
|
||||
Completion: complete.PredictNothing,
|
||||
Usage: `Specifies the username to pass through to the client`,
|
||||
Usage: `Specifies the username to pass through to the client. May be overridden by credentials sourced from a credential store.`,
|
||||
})
|
||||
|
||||
f.StringVar(&base.StringVar{
|
||||
Name: "dbname",
|
||||
Target: &c.flagDbname,
|
||||
EnvVar: "BOUNDARY_CONNECT_DBNAME",
|
||||
Completion: complete.PredictNothing,
|
||||
Usage: `Specifies the database name to pass through to the client.`,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -40,14 +52,71 @@ func (p *postgresFlags) defaultExec() string {
|
||||
return strings.ToLower(p.flagPostgresStyle)
|
||||
}
|
||||
|
||||
func (p *postgresFlags) buildArgs(c *Command, port, ip, addr string) []string {
|
||||
var args []string
|
||||
type postgresCredentials struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func (p *postgresFlags) buildArgs(c *Command, port, ip, addr string) (args, envs []string, retErr error) {
|
||||
var creds postgresCredentials
|
||||
if c.sessionAuthz != nil && len(c.sessionAuthz.Credentials) > 0 {
|
||||
for _, cred := range c.sessionAuthz.Credentials {
|
||||
// TODO: Could allow switching on library ID or name
|
||||
switch cred.CredentialLibrary.Type {
|
||||
case "vault":
|
||||
// Attempt unmarshaling into creds
|
||||
if err := json.Unmarshal(cred.Secret, &creds); err != nil {
|
||||
return nil, nil, fmt.Errorf("Error unmarshaling Vault secret: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if creds.Username != "" && creds.Password != "" {
|
||||
// In the future we can look for other types if we support other
|
||||
// authentication mechanisms
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch p.flagPostgresStyle {
|
||||
case "psql":
|
||||
args = append(args, "-p", port, "-h", ip)
|
||||
if c.flagUsername != "" {
|
||||
|
||||
if c.flagDbname != "" {
|
||||
args = append(args, "-d", c.flagDbname)
|
||||
}
|
||||
|
||||
switch {
|
||||
case creds.Username != "":
|
||||
args = append(args, "-U", creds.Username)
|
||||
case c.flagUsername != "":
|
||||
args = append(args, "-U", c.flagUsername)
|
||||
}
|
||||
|
||||
if creds.Password != "" {
|
||||
passfile, err := ioutil.TempFile("", "*")
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("Error saving postgres password to tmp file: %w", err)
|
||||
}
|
||||
c.cleanupFuncs = append(c.cleanupFuncs, func() error {
|
||||
if err := os.Remove(passfile.Name()); err != nil {
|
||||
return fmt.Errorf("Error removing temporary password file; consider removing %s manually: %w", passfile.Name(), err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
_, err = passfile.WriteString(fmt.Sprintf("*:*:*:*:%s", creds.Password))
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("Error writing password file to %s: %w", passfile.Name(), err)
|
||||
}
|
||||
if err := passfile.Close(); err != nil {
|
||||
return nil, nil, fmt.Errorf("Error closing password file after writing to %s: %w", passfile.Name(), err)
|
||||
}
|
||||
envs = append(envs, fmt.Sprintf("PGPASSFILE=%s", passfile.Name()))
|
||||
|
||||
if c.flagDbname == "" {
|
||||
c.UI.Warn("Credentials are being brokered but no -dbname parameter provided. psql may misinterpret another parameter as the database name.")
|
||||
}
|
||||
}
|
||||
}
|
||||
return args
|
||||
return
|
||||
}
|
||||
Reference in new issue
Block a user