2017-04-24 19:15:50 +00:00
|
|
|
package awsauth
|
2016-04-06 00:42:26 +00:00
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2016-04-06 00:42:26 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2017-02-01 19:16:03 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
|
2016-04-06 00:42:26 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2016-09-23 16:47:35 +00:00
|
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
2017-06-07 14:27:11 +00:00
|
|
|
"github.com/aws/aws-sdk-go/service/sts"
|
2018-04-05 15:49:21 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2016-04-06 00:42:26 +00:00
|
|
|
"github.com/hashicorp/go-cleanhttp"
|
2016-05-05 14:40:49 +00:00
|
|
|
"github.com/hashicorp/vault/helper/awsutil"
|
2016-04-06 00:42:26 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
)
|
|
|
|
|
2017-04-24 19:15:50 +00:00
|
|
|
// getRawClientConfig creates a aws-sdk-go config, which is used to create client
|
2016-04-07 18:13:19 +00:00
|
|
|
// that can interact with AWS API. This builds credentials in the following
|
|
|
|
// order of preference:
|
|
|
|
//
|
|
|
|
// * Static credentials from 'config/client'
|
|
|
|
// * Environment variables
|
|
|
|
// * Instance metadata role
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *backend) getRawClientConfig(ctx context.Context, s logical.Storage, region, clientType string) (*aws.Config, error) {
|
2016-05-05 14:40:49 +00:00
|
|
|
credsConfig := &awsutil.CredentialsConfig{
|
|
|
|
Region: region,
|
|
|
|
}
|
|
|
|
|
2016-04-06 00:42:26 +00:00
|
|
|
// Read the configured secret key and access key
|
2018-01-19 06:44:44 +00:00
|
|
|
config, err := b.nonLockedClientConfigEntry(ctx, s)
|
2016-04-06 00:42:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-07 18:13:19 +00:00
|
|
|
|
2016-05-03 16:14:07 +00:00
|
|
|
endpoint := aws.String("")
|
2018-02-16 16:11:17 +00:00
|
|
|
var maxRetries int = aws.UseServiceDefaultRetries
|
2016-04-07 18:13:19 +00:00
|
|
|
if config != nil {
|
2016-05-03 16:14:07 +00:00
|
|
|
// Override the default endpoint with the configured endpoint.
|
2017-04-24 19:15:50 +00:00
|
|
|
switch {
|
|
|
|
case clientType == "ec2" && config.Endpoint != "":
|
2016-05-03 16:14:07 +00:00
|
|
|
endpoint = aws.String(config.Endpoint)
|
2017-04-24 19:15:50 +00:00
|
|
|
case clientType == "iam" && config.IAMEndpoint != "":
|
|
|
|
endpoint = aws.String(config.IAMEndpoint)
|
|
|
|
case clientType == "sts" && config.STSEndpoint != "":
|
|
|
|
endpoint = aws.String(config.STSEndpoint)
|
2016-05-03 16:14:07 +00:00
|
|
|
}
|
|
|
|
|
2016-05-05 14:40:49 +00:00
|
|
|
credsConfig.AccessKey = config.AccessKey
|
|
|
|
credsConfig.SecretKey = config.SecretKey
|
2018-02-16 16:11:17 +00:00
|
|
|
maxRetries = config.MaxRetries
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
2016-05-05 14:40:49 +00:00
|
|
|
credsConfig.HTTPClient = cleanhttp.DefaultClient()
|
2016-04-07 18:13:19 +00:00
|
|
|
|
2016-05-05 14:40:49 +00:00
|
|
|
creds, err := credsConfig.GenerateCredentialChain()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-07 18:13:19 +00:00
|
|
|
if creds == nil {
|
2017-02-01 19:16:03 +00:00
|
|
|
return nil, fmt.Errorf("could not compile valid credential providers from static config, environment, shared, or instance metadata")
|
2016-04-07 18:13:19 +00:00
|
|
|
}
|
2016-04-06 00:42:26 +00:00
|
|
|
|
|
|
|
// Create a config that can be used to make the API calls.
|
2016-05-03 16:14:07 +00:00
|
|
|
return &aws.Config{
|
2016-04-06 00:42:26 +00:00
|
|
|
Credentials: creds,
|
2016-04-07 18:13:19 +00:00
|
|
|
Region: aws.String(region),
|
2016-04-06 00:42:26 +00:00
|
|
|
HTTPClient: cleanhttp.DefaultClient(),
|
2016-05-03 16:14:07 +00:00
|
|
|
Endpoint: endpoint,
|
2018-02-16 16:11:17 +00:00
|
|
|
MaxRetries: aws.Int(maxRetries),
|
2016-05-03 16:14:07 +00:00
|
|
|
}, nil
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-24 19:15:50 +00:00
|
|
|
// getClientConfig returns an aws-sdk-go config, with optionally assumed credentials
|
2018-02-01 17:51:38 +00:00
|
|
|
// It uses getRawClientConfig to obtain config for the runtime environment, and if
|
2017-04-24 19:15:50 +00:00
|
|
|
// stsRole is a non-empty string, it will use AssumeRole to obtain a set of assumed
|
|
|
|
// credentials. The credentials will expire after 15 minutes but will auto-refresh.
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *backend) getClientConfig(ctx context.Context, s logical.Storage, region, stsRole, accountID, clientType string) (*aws.Config, error) {
|
2017-04-24 19:15:50 +00:00
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
config, err := b.getRawClientConfig(ctx, s, region, clientType)
|
2017-02-01 19:16:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if config == nil {
|
|
|
|
return nil, fmt.Errorf("could not compile valid credentials through the default provider chain")
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
stsConfig, err := b.getRawClientConfig(ctx, s, region, "sts")
|
2017-06-07 14:27:11 +00:00
|
|
|
if stsConfig == nil {
|
|
|
|
return nil, fmt.Errorf("could not configure STS client")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-24 19:15:50 +00:00
|
|
|
if stsRole != "" {
|
2017-06-07 14:27:11 +00:00
|
|
|
assumedCredentials := stscreds.NewCredentials(session.New(stsConfig), stsRole)
|
2017-04-24 19:15:50 +00:00
|
|
|
// Test that we actually have permissions to assume the role
|
|
|
|
if _, err = assumedCredentials.Get(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.Credentials = assumedCredentials
|
2017-06-07 14:27:11 +00:00
|
|
|
} else {
|
|
|
|
if b.defaultAWSAccountID == "" {
|
|
|
|
client := sts.New(session.New(stsConfig))
|
|
|
|
if client == nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("could not obtain sts client: {{err}}", err)
|
2017-06-07 14:27:11 +00:00
|
|
|
}
|
|
|
|
inputParams := &sts.GetCallerIdentityInput{}
|
|
|
|
identity, err := client.GetCallerIdentity(inputParams)
|
|
|
|
if err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("unable to fetch current caller: {{err}}", err)
|
2017-06-07 14:27:11 +00:00
|
|
|
}
|
|
|
|
if identity == nil {
|
|
|
|
return nil, fmt.Errorf("got nil result from GetCallerIdentity")
|
|
|
|
}
|
|
|
|
b.defaultAWSAccountID = *identity.Account
|
|
|
|
}
|
|
|
|
if b.defaultAWSAccountID != accountID {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, fmt.Errorf("unable to fetch client for account ID %q -- default client is for account %q", accountID, b.defaultAWSAccountID)
|
2017-06-07 14:27:11 +00:00
|
|
|
}
|
2017-04-24 19:15:50 +00:00
|
|
|
}
|
2017-02-01 19:16:03 +00:00
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2016-04-14 04:11:17 +00:00
|
|
|
// flushCachedEC2Clients deletes all the cached ec2 client objects from the backend.
|
2016-04-28 00:01:39 +00:00
|
|
|
// If the client credentials configuration is deleted or updated in the backend, all
|
2016-09-23 16:47:35 +00:00
|
|
|
// the cached EC2 client objects will be flushed. Config mutex lock should be
|
|
|
|
// acquired for write operation before calling this method.
|
2016-04-14 04:11:17 +00:00
|
|
|
func (b *backend) flushCachedEC2Clients() {
|
2016-09-23 16:47:35 +00:00
|
|
|
// deleting items in map during iteration is safe
|
2016-04-14 04:11:17 +00:00
|
|
|
for region, _ := range b.EC2ClientsMap {
|
|
|
|
delete(b.EC2ClientsMap, region)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-23 16:47:35 +00:00
|
|
|
// flushCachedIAMClients deletes all the cached iam client objects from the
|
|
|
|
// backend. If the client credentials configuration is deleted or updated in
|
|
|
|
// the backend, all the cached IAM client objects will be flushed. Config mutex
|
|
|
|
// lock should be acquired for write operation before calling this method.
|
|
|
|
func (b *backend) flushCachedIAMClients() {
|
|
|
|
// deleting items in map during iteration is safe
|
|
|
|
for region, _ := range b.IAMClientsMap {
|
|
|
|
delete(b.IAMClientsMap, region)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-30 21:51:48 +00:00
|
|
|
// Gets an entry out of the user ID cache
|
|
|
|
func (b *backend) getCachedUserId(userId string) string {
|
|
|
|
if userId == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if entry, ok := b.iamUserIdToArnCache.Get(userId); ok {
|
|
|
|
b.iamUserIdToArnCache.SetDefault(userId, entry)
|
|
|
|
return entry.(string)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets an entry in the user ID cache
|
|
|
|
func (b *backend) setCachedUserId(userId, arn string) {
|
|
|
|
if userId != "" {
|
|
|
|
b.iamUserIdToArnCache.SetDefault(userId, arn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *backend) stsRoleForAccount(ctx context.Context, s logical.Storage, accountID string) (string, error) {
|
2017-06-07 14:27:11 +00:00
|
|
|
// Check if an STS configuration exists for the AWS account
|
2018-01-19 06:44:44 +00:00
|
|
|
sts, err := b.lockedAwsStsEntry(ctx, s, accountID)
|
2017-06-07 14:27:11 +00:00
|
|
|
if err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return "", errwrap.Wrapf(fmt.Sprintf("error fetching STS config for account ID %q: {{err}}", accountID), err)
|
2017-06-07 14:27:11 +00:00
|
|
|
}
|
|
|
|
// An empty STS role signifies the master account
|
|
|
|
if sts != nil {
|
|
|
|
return sts.StsRole, nil
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2016-09-23 16:47:35 +00:00
|
|
|
// clientEC2 creates a client to interact with AWS EC2 API
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *backend) clientEC2(ctx context.Context, s logical.Storage, region, accountID string) (*ec2.EC2, error) {
|
|
|
|
stsRole, err := b.stsRoleForAccount(ctx, s, accountID)
|
2017-06-07 14:27:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-28 00:01:39 +00:00
|
|
|
b.configMutex.RLock()
|
2017-02-01 19:16:03 +00:00
|
|
|
if b.EC2ClientsMap[region] != nil && b.EC2ClientsMap[region][stsRole] != nil {
|
2016-04-28 00:01:39 +00:00
|
|
|
defer b.configMutex.RUnlock()
|
2016-09-23 16:47:35 +00:00
|
|
|
// If the client object was already created, return it
|
2017-02-01 19:16:03 +00:00
|
|
|
return b.EC2ClientsMap[region][stsRole], nil
|
2016-04-07 18:13:19 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 16:47:35 +00:00
|
|
|
// Release the read lock and acquire the write lock
|
2016-04-28 00:01:39 +00:00
|
|
|
b.configMutex.RUnlock()
|
2016-04-07 18:13:19 +00:00
|
|
|
b.configMutex.Lock()
|
|
|
|
defer b.configMutex.Unlock()
|
|
|
|
|
2016-09-23 16:47:35 +00:00
|
|
|
// If the client gets created while switching the locks, return it
|
2017-02-01 19:16:03 +00:00
|
|
|
if b.EC2ClientsMap[region] != nil && b.EC2ClientsMap[region][stsRole] != nil {
|
|
|
|
return b.EC2ClientsMap[region][stsRole], nil
|
2016-04-28 00:01:39 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 16:47:35 +00:00
|
|
|
// Create an AWS config object using a chain of providers
|
2017-02-01 19:16:03 +00:00
|
|
|
var awsConfig *aws.Config
|
2018-01-19 06:44:44 +00:00
|
|
|
awsConfig, err = b.getClientConfig(ctx, s, region, stsRole, accountID, "ec2")
|
2017-02-01 19:16:03 +00:00
|
|
|
|
2016-04-06 00:42:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-07 18:13:19 +00:00
|
|
|
|
2017-02-01 19:16:03 +00:00
|
|
|
if awsConfig == nil {
|
|
|
|
return nil, fmt.Errorf("could not retrieve valid assumed credentials")
|
|
|
|
}
|
|
|
|
|
2016-09-23 16:47:35 +00:00
|
|
|
// Create a new EC2 client object, cache it and return the same
|
2017-02-01 19:16:03 +00:00
|
|
|
client := ec2.New(session.New(awsConfig))
|
|
|
|
if client == nil {
|
|
|
|
return nil, fmt.Errorf("could not obtain ec2 client")
|
|
|
|
}
|
|
|
|
if _, ok := b.EC2ClientsMap[region]; !ok {
|
|
|
|
b.EC2ClientsMap[region] = map[string]*ec2.EC2{stsRole: client}
|
|
|
|
} else {
|
|
|
|
b.EC2ClientsMap[region][stsRole] = client
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.EC2ClientsMap[region][stsRole], nil
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
2016-09-23 16:47:35 +00:00
|
|
|
|
|
|
|
// clientIAM creates a client to interact with AWS IAM API
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *backend) clientIAM(ctx context.Context, s logical.Storage, region, accountID string) (*iam.IAM, error) {
|
|
|
|
stsRole, err := b.stsRoleForAccount(ctx, s, accountID)
|
2017-06-07 14:27:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-23 16:47:35 +00:00
|
|
|
b.configMutex.RLock()
|
2017-02-01 19:16:03 +00:00
|
|
|
if b.IAMClientsMap[region] != nil && b.IAMClientsMap[region][stsRole] != nil {
|
2016-09-23 16:47:35 +00:00
|
|
|
defer b.configMutex.RUnlock()
|
|
|
|
// If the client object was already created, return it
|
2017-02-01 19:16:03 +00:00
|
|
|
return b.IAMClientsMap[region][stsRole], nil
|
2016-09-23 16:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Release the read lock and acquire the write lock
|
|
|
|
b.configMutex.RUnlock()
|
|
|
|
b.configMutex.Lock()
|
|
|
|
defer b.configMutex.Unlock()
|
|
|
|
|
|
|
|
// If the client gets created while switching the locks, return it
|
2017-02-01 19:16:03 +00:00
|
|
|
if b.IAMClientsMap[region] != nil && b.IAMClientsMap[region][stsRole] != nil {
|
|
|
|
return b.IAMClientsMap[region][stsRole], nil
|
2016-09-23 16:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create an AWS config object using a chain of providers
|
2017-02-01 19:16:03 +00:00
|
|
|
var awsConfig *aws.Config
|
2018-01-19 06:44:44 +00:00
|
|
|
awsConfig, err = b.getClientConfig(ctx, s, region, stsRole, accountID, "iam")
|
2017-02-01 19:16:03 +00:00
|
|
|
|
2016-09-23 16:47:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-01 19:16:03 +00:00
|
|
|
if awsConfig == nil {
|
|
|
|
return nil, fmt.Errorf("could not retrieve valid assumed credentials")
|
|
|
|
}
|
|
|
|
|
2016-09-23 16:47:35 +00:00
|
|
|
// Create a new IAM client object, cache it and return the same
|
2017-02-01 19:16:03 +00:00
|
|
|
client := iam.New(session.New(awsConfig))
|
|
|
|
if client == nil {
|
|
|
|
return nil, fmt.Errorf("could not obtain iam client")
|
|
|
|
}
|
|
|
|
if _, ok := b.IAMClientsMap[region]; !ok {
|
|
|
|
b.IAMClientsMap[region] = map[string]*iam.IAM{stsRole: client}
|
|
|
|
} else {
|
|
|
|
b.IAMClientsMap[region][stsRole] = client
|
|
|
|
}
|
|
|
|
return b.IAMClientsMap[region][stsRole], nil
|
2016-09-23 16:47:35 +00:00
|
|
|
}
|