2017-04-24 19:15:50 +00:00
|
|
|
package awsauth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2020-01-13 22:56:41 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2021-07-21 00:42:00 +00:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/awsutil"
|
2017-04-24 19:15:50 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
2017-07-18 12:34:48 +00:00
|
|
|
mount, ok := m["mount"]
|
|
|
|
if !ok {
|
|
|
|
mount = "aws"
|
|
|
|
}
|
|
|
|
|
|
|
|
role, ok := m["role"]
|
|
|
|
if !ok {
|
|
|
|
role = ""
|
2017-04-24 19:15:50 +00:00
|
|
|
}
|
|
|
|
|
2017-07-18 12:34:48 +00:00
|
|
|
headerValue, ok := m["header_value"]
|
|
|
|
if !ok {
|
|
|
|
headerValue = ""
|
|
|
|
}
|
|
|
|
|
2020-09-28 21:06:49 +00:00
|
|
|
logVal, ok := m["log_level"]
|
|
|
|
if !ok {
|
|
|
|
logVal = "info"
|
|
|
|
}
|
|
|
|
level := hclog.LevelFromString(logVal)
|
|
|
|
if level == hclog.NoLevel {
|
|
|
|
return nil, fmt.Errorf("failed to parse 'log_level' value: %q", logVal)
|
|
|
|
}
|
|
|
|
hlogger := hclog.Default()
|
|
|
|
hlogger.SetLevel(level)
|
|
|
|
|
2021-05-26 17:30:46 +00:00
|
|
|
creds, err := awsutil.RetrieveCreds(m["aws_access_key_id"], m["aws_secret_access_key"], m["aws_security_token"], hlogger)
|
2018-09-12 20:30:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-14 18:55:10 +00:00
|
|
|
region := m["region"]
|
|
|
|
if region == "" {
|
|
|
|
region = awsutil.DefaultRegion
|
|
|
|
}
|
2021-05-26 17:30:46 +00:00
|
|
|
|
|
|
|
loginData, err := awsutil.GenerateLoginData(creds, headerValue, region, hlogger)
|
2017-07-18 12:34:48 +00:00
|
|
|
if err != nil {
|
2017-08-31 20:57:00 +00:00
|
|
|
return nil, err
|
2017-07-18 12:34:48 +00:00
|
|
|
}
|
|
|
|
if loginData == nil {
|
2017-08-31 20:57:00 +00:00
|
|
|
return nil, fmt.Errorf("got nil response from GenerateLoginData")
|
2017-07-18 12:34:48 +00:00
|
|
|
}
|
|
|
|
loginData["role"] = role
|
2017-04-24 19:15:50 +00:00
|
|
|
path := fmt.Sprintf("auth/%s/login", mount)
|
2017-07-18 12:34:48 +00:00
|
|
|
secret, err := c.Logical().Write(path, loginData)
|
2017-04-24 19:15:50 +00:00
|
|
|
if err != nil {
|
2017-08-31 20:57:00 +00:00
|
|
|
return nil, err
|
2017-04-24 19:15:50 +00:00
|
|
|
}
|
|
|
|
if secret == nil {
|
2017-08-31 20:57:00 +00:00
|
|
|
return nil, fmt.Errorf("empty response from credential provider")
|
2017-04-24 19:15:50 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 20:57:00 +00:00
|
|
|
return secret, nil
|
2017-04-24 19:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CLIHandler) Help() string {
|
|
|
|
help := `
|
2017-09-06 14:02:15 +00:00
|
|
|
Usage: vault login -method=aws [CONFIG K=V...]
|
2017-09-02 22:49:45 +00:00
|
|
|
|
2017-09-13 01:48:52 +00:00
|
|
|
The AWS auth method allows users to authenticate with AWS IAM
|
2017-09-02 22:49:45 +00:00
|
|
|
credentials. The AWS IAM credentials may be specified in a number of ways,
|
|
|
|
listed in order of precedence below:
|
|
|
|
|
|
|
|
1. Explicitly via the command line (not recommended)
|
|
|
|
|
|
|
|
2. Via the standard AWS environment variables (AWS_ACCESS_KEY, etc.)
|
|
|
|
|
|
|
|
3. Via the ~/.aws/credentials file
|
|
|
|
|
|
|
|
4. Via EC2 instance profile
|
|
|
|
|
|
|
|
Authenticate using locally stored credentials:
|
|
|
|
|
2017-09-06 14:02:15 +00:00
|
|
|
$ vault login -method=aws
|
2017-09-02 22:49:45 +00:00
|
|
|
|
|
|
|
Authenticate by passing keys:
|
|
|
|
|
2017-09-06 14:02:15 +00:00
|
|
|
$ vault login -method=aws aws_access_key_id=... aws_secret_access_key=...
|
2017-09-02 22:49:45 +00:00
|
|
|
|
|
|
|
Configuration:
|
|
|
|
|
|
|
|
aws_access_key_id=<string>
|
|
|
|
Explicit AWS access key ID
|
|
|
|
|
|
|
|
aws_secret_access_key=<string>
|
|
|
|
Explicit AWS secret access key
|
|
|
|
|
|
|
|
aws_security_token=<string>
|
|
|
|
Explicit AWS security token for temporary credentials
|
|
|
|
|
|
|
|
header_value=<string>
|
|
|
|
Value for the x-vault-aws-iam-server-id header in requests
|
|
|
|
|
|
|
|
mount=<string>
|
2017-09-06 14:02:15 +00:00
|
|
|
Path where the AWS credential method is mounted. This is usually provided
|
|
|
|
via the -path flag in the "vault login" command, but it can be specified
|
|
|
|
here as well. If specified here, it takes precedence over the value for
|
|
|
|
-path. The default value is "aws".
|
2017-09-02 22:49:45 +00:00
|
|
|
|
|
|
|
role=<string>
|
|
|
|
Name of the role to request a token against
|
2020-09-28 21:06:49 +00:00
|
|
|
|
|
|
|
log_level=<string>
|
|
|
|
Set logging level during AWS credential acquisition. Valid levels are
|
|
|
|
trace, debug, info, warn, error. Defaults to info.
|
2017-09-02 22:49:45 +00:00
|
|
|
`
|
2017-04-24 19:15:50 +00:00
|
|
|
|
|
|
|
return strings.TrimSpace(help)
|
|
|
|
}
|