2015-03-20 16:59:48 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2015-08-06 16:26:41 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2015-10-30 22:22:48 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
2015-08-06 16:26:41 +00:00
|
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
2015-12-08 04:32:49 +00:00
|
|
|
"github.com/aws/aws-sdk-go/service/sts"
|
2015-10-22 18:37:12 +00:00
|
|
|
"github.com/hashicorp/go-cleanhttp"
|
2016-05-03 19:10:35 +00:00
|
|
|
"github.com/hashicorp/vault/helper/awsutil"
|
2015-03-20 16:59:48 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
)
|
|
|
|
|
2015-12-08 04:32:49 +00:00
|
|
|
func getRootConfig(s logical.Storage) (*aws.Config, error) {
|
2016-05-03 21:00:16 +00:00
|
|
|
credsConfig := &awsutil.CredentialsConfig{}
|
2016-05-03 19:10:35 +00:00
|
|
|
|
2015-04-19 05:21:31 +00:00
|
|
|
entry, err := s.Get("config/root")
|
2015-03-20 16:59:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-03 19:10:35 +00:00
|
|
|
if entry != nil {
|
|
|
|
var config rootConfig
|
|
|
|
if err := entry.DecodeJSON(&config); err != nil {
|
|
|
|
return nil, fmt.Errorf("error reading root configuration: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-05-03 21:00:16 +00:00
|
|
|
credsConfig.AccessKey = config.AccessKey
|
|
|
|
credsConfig.SecretKey = config.SecretKey
|
|
|
|
credsConfig.Region = config.Region
|
2015-03-20 16:59:48 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 21:00:16 +00:00
|
|
|
if credsConfig.Region == "" {
|
|
|
|
credsConfig.Region = "us-east-1"
|
2016-05-03 19:25:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 21:00:16 +00:00
|
|
|
credsConfig.HTTPClient = cleanhttp.DefaultClient()
|
|
|
|
|
|
|
|
creds, err := credsConfig.GenerateCredentialChain()
|
2016-05-03 19:10:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-03-20 16:59:48 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 04:32:49 +00:00
|
|
|
return &aws.Config{
|
2015-08-06 16:37:08 +00:00
|
|
|
Credentials: creds,
|
2016-05-03 21:00:16 +00:00
|
|
|
Region: aws.String(credsConfig.Region),
|
2015-10-22 18:37:12 +00:00
|
|
|
HTTPClient: cleanhttp.DefaultClient(),
|
2015-12-08 04:32:49 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2015-10-30 22:22:48 +00:00
|
|
|
|
2015-12-08 04:32:49 +00:00
|
|
|
func clientIAM(s logical.Storage) (*iam.IAM, error) {
|
|
|
|
awsConfig, _ := getRootConfig(s)
|
2015-10-30 22:22:48 +00:00
|
|
|
return iam.New(session.New(awsConfig)), nil
|
2015-03-20 16:59:48 +00:00
|
|
|
}
|
2015-12-08 04:32:49 +00:00
|
|
|
|
|
|
|
func clientSTS(s logical.Storage) (*sts.STS, error) {
|
|
|
|
awsConfig, _ := getRootConfig(s)
|
|
|
|
return sts.New(session.New(awsConfig)), nil
|
|
|
|
}
|