2015-05-06 01:54:27 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
pwd "github.com/hashicorp/vault/helper/password"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CLIHandler struct{}
|
|
|
|
|
2017-08-31 20:57:00 +00:00
|
|
|
func (h *CLIHandler) Auth(c *api.Client, m map[string]string) (*api.Secret, error) {
|
2015-05-06 01:54:27 +00:00
|
|
|
mount, ok := m["mount"]
|
|
|
|
if !ok {
|
|
|
|
mount = "ldap"
|
|
|
|
}
|
|
|
|
|
|
|
|
username, ok := m["username"]
|
|
|
|
if !ok {
|
2016-12-02 17:44:13 +00:00
|
|
|
username = usernameFromEnv()
|
|
|
|
if username == "" {
|
2017-08-31 20:57:00 +00:00
|
|
|
return nil, fmt.Errorf("'username' not supplied and neither 'LOGNAME' nor 'USER' env vars set")
|
2016-12-02 17:44:13 +00:00
|
|
|
}
|
2015-05-06 01:54:27 +00:00
|
|
|
}
|
|
|
|
password, ok := m["password"]
|
|
|
|
if !ok {
|
2018-01-18 17:14:19 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "Password (will be hidden): ")
|
2015-05-06 01:54:27 +00:00
|
|
|
var err error
|
|
|
|
password, err = pwd.Read(os.Stdin)
|
2018-01-18 17:14:19 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "\n")
|
2015-05-06 01:54:27 +00:00
|
|
|
if err != nil {
|
2017-08-31 20:57:00 +00:00
|
|
|
return nil, err
|
2015-05-06 01:54:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-27 18:28:09 +00:00
|
|
|
data := map[string]interface{}{
|
2015-05-06 01:54:27 +00:00
|
|
|
"password": password,
|
2015-07-27 18:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mfa_method, ok := m["method"]
|
|
|
|
if ok {
|
|
|
|
data["method"] = mfa_method
|
|
|
|
}
|
|
|
|
mfa_passcode, ok := m["passcode"]
|
|
|
|
if ok {
|
|
|
|
data["passcode"] = mfa_passcode
|
|
|
|
}
|
|
|
|
|
|
|
|
path := fmt.Sprintf("auth/%s/login/%s", mount, username)
|
|
|
|
secret, err := c.Logical().Write(path, data)
|
2015-05-06 01:54:27 +00:00
|
|
|
if err != nil {
|
2017-08-31 20:57:00 +00:00
|
|
|
return nil, err
|
2015-05-06 01:54:27 +00:00
|
|
|
}
|
|
|
|
if secret == nil {
|
2017-08-31 20:57:00 +00:00
|
|
|
return nil, fmt.Errorf("empty response from credential provider")
|
2015-05-06 01:54:27 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 20:57:00 +00:00
|
|
|
return secret, nil
|
2015-05-06 01:54:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CLIHandler) Help() string {
|
|
|
|
help := `
|
2017-09-06 14:02:15 +00:00
|
|
|
Usage: vault login -method=ldap [CONFIG K=V...]
|
2015-05-06 01:54:27 +00:00
|
|
|
|
2017-09-13 01:48:52 +00:00
|
|
|
The LDAP auth method allows users to authenticate using LDAP or
|
2017-09-02 22:50:12 +00:00
|
|
|
Active Directory.
|
2015-07-28 04:12:11 +00:00
|
|
|
|
2017-09-02 22:50:12 +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-05-06 01:54:27 +00:00
|
|
|
|
2017-09-02 22:50:12 +00:00
|
|
|
$ vault read auth/<mount>/mfa_config
|
|
|
|
|
|
|
|
Authenticate as "sally":
|
|
|
|
|
2017-09-06 14:02:15 +00:00
|
|
|
$ vault login -method=ldap username=sally
|
2017-09-02 22:50:12 +00:00
|
|
|
Password (will be hidden):
|
|
|
|
|
|
|
|
Authenticate as "bob":
|
|
|
|
|
2017-09-06 14:02:15 +00:00
|
|
|
$ vault login -method=ldap username=bob password=password
|
2017-09-02 22:50:12 +00:00
|
|
|
|
|
|
|
Configuration:
|
|
|
|
|
|
|
|
method=<string>
|
|
|
|
MFA method.
|
|
|
|
|
|
|
|
passcode=<string>
|
|
|
|
MFA OTP/passcode.
|
|
|
|
|
|
|
|
password=<string>
|
|
|
|
LDAP password to use for authentication. If not provided, the CLI will
|
|
|
|
prompt for this on stdin.
|
|
|
|
|
|
|
|
username=<string>
|
|
|
|
LDAP username to use for authentication.
|
|
|
|
`
|
2015-05-06 01:54:27 +00:00
|
|
|
|
|
|
|
return strings.TrimSpace(help)
|
|
|
|
}
|
2016-12-02 17:44:13 +00:00
|
|
|
|
|
|
|
func usernameFromEnv() string {
|
2017-02-08 05:47:06 +00:00
|
|
|
if logname := os.Getenv("LOGNAME"); logname != "" {
|
2016-12-02 17:44:13 +00:00
|
|
|
return logname
|
|
|
|
}
|
2017-02-08 05:47:06 +00:00
|
|
|
if user := os.Getenv("USER"); user != "" {
|
2016-12-02 17:44:13 +00:00
|
|
|
return user
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|