2015-04-19 22:17:24 +00:00
|
|
|
package userpass
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-07-28 04:11:35 +00:00
|
|
|
"os"
|
2015-12-08 04:32:49 +00:00
|
|
|
"strings"
|
2015-04-19 22:17:24 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/vault/api"
|
2015-07-28 04:11:35 +00:00
|
|
|
pwd "github.com/hashicorp/vault/helper/password"
|
2015-12-08 04:32:49 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2015-04-19 22:17:24 +00:00
|
|
|
)
|
|
|
|
|
2017-02-04 12:47:09 +00:00
|
|
|
type CLIHandler struct {
|
|
|
|
DefaultMount string
|
|
|
|
}
|
2015-04-19 22:17:24 +00:00
|
|
|
|
2017-08-31 20:57:00 +00:00
|
|
|
func (h *CLIHandler) Auth(c *api.Client, m map[string]string) (*api.Secret, error) {
|
2015-04-19 22:17:24 +00:00
|
|
|
var data struct {
|
|
|
|
Username string `mapstructure:"username"`
|
|
|
|
Password string `mapstructure:"password"`
|
|
|
|
Mount string `mapstructure:"mount"`
|
2015-07-28 04:11:35 +00:00
|
|
|
Method string `mapstructure:"method"`
|
|
|
|
Passcode string `mapstructure:"passcode"`
|
2015-04-19 22:17:24 +00:00
|
|
|
}
|
|
|
|
if err := mapstructure.WeakDecode(m, &data); err != nil {
|
2017-08-31 20:57:00 +00:00
|
|
|
return nil, err
|
2015-04-19 22:17:24 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 04:11:35 +00:00
|
|
|
if data.Username == "" {
|
2017-08-31 20:57:00 +00:00
|
|
|
return nil, fmt.Errorf("'username' must be specified")
|
2015-07-28 04:11:35 +00:00
|
|
|
}
|
|
|
|
if data.Password == "" {
|
|
|
|
fmt.Printf("Password (will be hidden): ")
|
2015-07-28 18:55:46 +00:00
|
|
|
password, err := pwd.Read(os.Stdin)
|
2015-07-28 04:11:35 +00:00
|
|
|
fmt.Println()
|
|
|
|
if err != nil {
|
2017-08-31 20:57:00 +00:00
|
|
|
return nil, err
|
2015-07-28 04:11:35 +00:00
|
|
|
}
|
2015-07-28 18:55:46 +00:00
|
|
|
data.Password = password
|
2015-04-19 22:17:24 +00:00
|
|
|
}
|
|
|
|
if data.Mount == "" {
|
2017-02-04 12:47:09 +00:00
|
|
|
data.Mount = h.DefaultMount
|
2015-04-19 22:17:24 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 04:11:35 +00:00
|
|
|
options := map[string]interface{}{
|
2015-04-19 22:17:24 +00:00
|
|
|
"password": data.Password,
|
2015-07-28 04:11:35 +00:00
|
|
|
}
|
|
|
|
if data.Method != "" {
|
|
|
|
options["method"] = data.Method
|
|
|
|
}
|
|
|
|
if data.Passcode != "" {
|
|
|
|
options["passcode"] = data.Passcode
|
|
|
|
}
|
|
|
|
|
|
|
|
path := fmt.Sprintf("auth/%s/login/%s", data.Mount, data.Username)
|
|
|
|
secret, err := c.Logical().Write(path, options)
|
2015-04-19 22:17:24 +00:00
|
|
|
if err != nil {
|
2017-08-31 20:57:00 +00:00
|
|
|
return nil, err
|
2015-04-19 22:17:24 +00:00
|
|
|
}
|
|
|
|
if secret == nil {
|
2017-08-31 20:57:00 +00:00
|
|
|
return nil, fmt.Errorf("empty response from credential provider")
|
2015-04-19 22:17:24 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 20:57:00 +00:00
|
|
|
return secret, nil
|
2015-04-19 22:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CLIHandler) Help() string {
|
|
|
|
help := `
|
2017-09-06 14:02:15 +00:00
|
|
|
Usage: vault login -method=userpass [CONFIG K=V...]
|
2015-07-28 04:11:35 +00:00
|
|
|
|
2017-09-13 01:48:52 +00:00
|
|
|
The userpass auth method allows users to authenticate using Vault's
|
2017-09-06 14:02:15 +00:00
|
|
|
internal user database.
|
2015-04-19 22:17:24 +00:00
|
|
|
|
2017-09-02 22:50:35 +00:00
|
|
|
If MFA is enabled, a "method" and/or "passcode" may be required depending on
|
2017-09-06 14:02:15 +00:00
|
|
|
the MFA method. To check which MFA is in use, run:
|
2015-04-19 22:17:24 +00:00
|
|
|
|
2017-09-02 22:50:35 +00:00
|
|
|
$ vault read auth/<mount>/mfa_config
|
|
|
|
|
|
|
|
Authenticate as "sally":
|
|
|
|
|
2017-09-06 14:02:15 +00:00
|
|
|
$ vault login -method=userpass username=sally
|
2017-09-02 22:50:35 +00:00
|
|
|
Password (will be hidden):
|
|
|
|
|
|
|
|
Authenticate as "bob":
|
|
|
|
|
2017-09-06 14:02:15 +00:00
|
|
|
$ vault login -method=userpass username=bob password=password
|
2017-09-02 22:50:35 +00:00
|
|
|
|
|
|
|
Configuration:
|
|
|
|
|
|
|
|
method=<string>
|
|
|
|
MFA method.
|
|
|
|
|
|
|
|
passcode=<string>
|
|
|
|
MFA OTP/passcode.
|
|
|
|
|
|
|
|
password=<string>
|
2017-09-06 14:02:15 +00:00
|
|
|
Password to use for authentication. If not provided, the CLI will prompt
|
|
|
|
for this on stdin.
|
2017-09-02 22:50:35 +00:00
|
|
|
|
|
|
|
username=<string>
|
|
|
|
Username to use for authentication.
|
|
|
|
`
|
2015-04-19 22:17:24 +00:00
|
|
|
|
|
|
|
return strings.TrimSpace(help)
|
|
|
|
}
|