credentials/userpass: integrate into auth cli

This commit is contained in:
Mitchell Hashimoto 2015-04-19 15:17:24 -07:00
parent c5cadc026d
commit 0b7e7190b5
2 changed files with 63 additions and 3 deletions

View 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)
}

View file

@ -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
}, },