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"
|
credAppId "github.com/hashicorp/vault/builtin/credential/app-id"
|
||||||
credGitHub "github.com/hashicorp/vault/builtin/credential/github"
|
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/aws"
|
||||||
"github.com/hashicorp/vault/builtin/logical/consul"
|
"github.com/hashicorp/vault/builtin/logical/consul"
|
||||||
|
@ -51,6 +52,7 @@ func Commands(metaPtr *command.Meta) map[string]cli.CommandFactory {
|
||||||
CredentialBackends: map[string]logical.Factory{
|
CredentialBackends: map[string]logical.Factory{
|
||||||
"app-id": credAppId.Factory,
|
"app-id": credAppId.Factory,
|
||||||
"github": credGitHub.Factory,
|
"github": credGitHub.Factory,
|
||||||
|
"userpass": credUserpass.Factory,
|
||||||
},
|
},
|
||||||
LogicalBackends: map[string]logical.Factory{
|
LogicalBackends: map[string]logical.Factory{
|
||||||
"aws": aws.Factory,
|
"aws": aws.Factory,
|
||||||
|
@ -72,6 +74,7 @@ func Commands(metaPtr *command.Meta) map[string]cli.CommandFactory {
|
||||||
Meta: meta,
|
Meta: meta,
|
||||||
Handlers: map[string]command.AuthHandler{
|
Handlers: map[string]command.AuthHandler{
|
||||||
"github": &credGitHub.CLIHandler{},
|
"github": &credGitHub.CLIHandler{},
|
||||||
|
"userpass": &credUserpass.CLIHandler{},
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue