credentials/userpass: integrate into auth cli
This commit is contained in:
parent
c5cadc026d
commit
0b7e7190b5
57
builtin/credential/userpass/cli.go
Normal file
57
builtin/credential/userpass/cli.go
Normal file
|
@ -0,0 +1,57 @@
|
|||
package userpass
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/vault/api"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
type CLIHandler struct{}
|
||||
|
||||
func (h *CLIHandler) Auth(c *api.Client, m map[string]string) (string, error) {
|
||||
var data struct {
|
||||
Username string `mapstructure:"username"`
|
||||
Password string `mapstructure:"password"`
|
||||
Mount string `mapstructure:"mount"`
|
||||
}
|
||||
if err := mapstructure.WeakDecode(m, &data); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if data.Username == "" || data.Password == "" {
|
||||
return "", fmt.Errorf("Both 'username' and 'password' must be specified")
|
||||
}
|
||||
if data.Mount == "" {
|
||||
data.Mount = "userpass"
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("auth/%s/login/%s", data.Mount, data.Username)
|
||||
secret, err := c.Logical().Write(path, map[string]interface{}{
|
||||
"password": data.Password,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if secret == nil {
|
||||
return "", fmt.Errorf("empty response from credential provider")
|
||||
}
|
||||
|
||||
return secret.Auth.ClientToken, nil
|
||||
}
|
||||
|
||||
func (h *CLIHandler) Help() string {
|
||||
help := `
|
||||
The "userpass" credential provider allows you to authenticate with
|
||||
a username and password. To use it, specify the "username" and "password"
|
||||
vars with the "-var" flag.
|
||||
|
||||
Example: vault auth -method=userpass \
|
||||
-var="username=<user>"
|
||||
-var="password=<password>"
|
||||
|
||||
`
|
||||
|
||||
return strings.TrimSpace(help)
|
||||
}
|
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
credAppId "github.com/hashicorp/vault/builtin/credential/app-id"
|
||||
credGitHub "github.com/hashicorp/vault/builtin/credential/github"
|
||||
credUserpass "github.com/hashicorp/vault/builtin/credential/userpass"
|
||||
|
||||
"github.com/hashicorp/vault/builtin/logical/aws"
|
||||
"github.com/hashicorp/vault/builtin/logical/consul"
|
||||
|
@ -49,8 +50,9 @@ func Commands(metaPtr *command.Meta) map[string]cli.CommandFactory {
|
|||
"file": auditFile.Factory,
|
||||
},
|
||||
CredentialBackends: map[string]logical.Factory{
|
||||
"app-id": credAppId.Factory,
|
||||
"github": credGitHub.Factory,
|
||||
"app-id": credAppId.Factory,
|
||||
"github": credGitHub.Factory,
|
||||
"userpass": credUserpass.Factory,
|
||||
},
|
||||
LogicalBackends: map[string]logical.Factory{
|
||||
"aws": aws.Factory,
|
||||
|
@ -71,7 +73,8 @@ func Commands(metaPtr *command.Meta) map[string]cli.CommandFactory {
|
|||
return &command.AuthCommand{
|
||||
Meta: meta,
|
||||
Handlers: map[string]command.AuthHandler{
|
||||
"github": &credGitHub.CLIHandler{},
|
||||
"github": &credGitHub.CLIHandler{},
|
||||
"userpass": &credUserpass.CLIHandler{},
|
||||
},
|
||||
}, nil
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue