2016-05-03 19:10:35 +00:00
|
|
|
package awsutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-05-03 21:00:16 +00:00
|
|
|
"net/http"
|
2016-05-03 19:10:35 +00:00
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
2017-07-31 22:27:16 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/defaults"
|
2016-05-03 19:10:35 +00:00
|
|
|
)
|
|
|
|
|
2016-05-03 21:00:16 +00:00
|
|
|
type CredentialsConfig struct {
|
|
|
|
// The access key if static credentials are being used
|
2016-05-03 19:10:35 +00:00
|
|
|
AccessKey string
|
2016-05-03 21:00:16 +00:00
|
|
|
|
|
|
|
// The secret key if static credentials are being used
|
2016-05-03 19:10:35 +00:00
|
|
|
SecretKey string
|
|
|
|
|
2016-05-03 21:00:16 +00:00
|
|
|
// The session token if it is being used
|
|
|
|
SessionToken string
|
|
|
|
|
|
|
|
// If specified, the region will be provided to the config of the
|
|
|
|
// EC2RoleProvider's client. This may be useful if you want to e.g. reuse
|
|
|
|
// the client elsewhere.
|
|
|
|
Region string
|
|
|
|
|
|
|
|
// The filename for the shared credentials provider, if being used
|
|
|
|
Filename string
|
2016-05-03 19:10:35 +00:00
|
|
|
|
2016-05-03 21:00:16 +00:00
|
|
|
// The profile for the shared credentials provider, if being used
|
|
|
|
Profile string
|
|
|
|
|
|
|
|
// The http.Client to use, or nil for the client to use its default
|
|
|
|
HTTPClient *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CredentialsConfig) GenerateCredentialChain() (*credentials.Credentials, error) {
|
2016-05-03 19:10:35 +00:00
|
|
|
var providers []credentials.Provider
|
|
|
|
|
|
|
|
switch {
|
2016-05-03 21:00:16 +00:00
|
|
|
case c.AccessKey != "" && c.SecretKey != "":
|
2016-05-03 19:10:35 +00:00
|
|
|
// Add the static credential provider
|
|
|
|
providers = append(providers, &credentials.StaticProvider{
|
|
|
|
Value: credentials.Value{
|
2016-05-03 21:00:16 +00:00
|
|
|
AccessKeyID: c.AccessKey,
|
|
|
|
SecretAccessKey: c.SecretKey,
|
|
|
|
SessionToken: c.SessionToken,
|
2016-05-03 19:10:35 +00:00
|
|
|
}})
|
2017-05-02 03:34:10 +00:00
|
|
|
case c.AccessKey == "" && c.SecretKey == "":
|
2016-05-03 19:10:35 +00:00
|
|
|
// Attempt to get credentials from the IAM instance role below
|
|
|
|
|
|
|
|
default: // Have one or the other but not both and not neither
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"static AWS client credentials haven't been properly configured (the access key or secret key were provided but not both)")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the environment credential provider
|
|
|
|
providers = append(providers, &credentials.EnvProvider{})
|
|
|
|
|
2016-05-03 21:00:16 +00:00
|
|
|
// Add the shared credentials provider
|
|
|
|
providers = append(providers, &credentials.SharedCredentialsProvider{
|
|
|
|
Filename: c.Filename,
|
|
|
|
Profile: c.Profile,
|
|
|
|
})
|
|
|
|
|
2017-07-31 22:27:16 +00:00
|
|
|
// Add the remote provider
|
|
|
|
def := defaults.Get()
|
|
|
|
if c.Region != "" {
|
|
|
|
def.Config.Region = aws.String(c.Region)
|
|
|
|
}
|
2017-08-14 16:43:11 +00:00
|
|
|
if c.HTTPClient != nil {
|
|
|
|
def.Config.HTTPClient = c.HTTPClient
|
|
|
|
}
|
2017-07-31 22:27:16 +00:00
|
|
|
|
|
|
|
providers = append(providers, defaults.RemoteCredProvider(*def.Config, def.Handlers))
|
2016-05-03 19:10:35 +00:00
|
|
|
|
2016-05-05 14:31:50 +00:00
|
|
|
// Create the credentials required to access the API.
|
2016-05-03 19:10:35 +00:00
|
|
|
creds := credentials.NewChainCredentials(providers)
|
|
|
|
if creds == nil {
|
2018-02-01 17:51:38 +00:00
|
|
|
return nil, fmt.Errorf("could not compile valid credential providers from static config, environment, shared, or instance metadata")
|
2016-05-03 19:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return creds, nil
|
|
|
|
}
|