8 "github.com/go-ini/ini"
10 "github.com/aws/aws-sdk-go/aws/awserr"
13 // SharedCredsProviderName provides a name of SharedCreds provider
14 const SharedCredsProviderName = "SharedCredentialsProvider"
17 // ErrSharedCredentialsHomeNotFound is emitted when the user directory cannot be found.
20 ErrSharedCredentialsHomeNotFound = awserr.New("UserHomeNotFound", "user home directory not found.", nil)
23 // A SharedCredentialsProvider retrieves credentials from the current user's home
24 // directory, and keeps track if those credentials are expired.
26 // Profile ini file example: $HOME/.aws/credentials
27 type SharedCredentialsProvider struct {
28 // Path to the shared credentials file.
30 // If empty will look for "AWS_SHARED_CREDENTIALS_FILE" env variable. If the
31 // env value is empty will default to current user's home directory.
32 // Linux/OSX: "$HOME/.aws/credentials"
33 // Windows: "%USERPROFILE%\.aws\credentials"
36 // AWS Profile to extract credentials from the shared credentials file. If empty
37 // will default to environment variable "AWS_PROFILE" or "default" if
38 // environment variable is also not set.
41 // retrieved states if the credentials have been successfully retrieved.
45 // NewSharedCredentials returns a pointer to a new Credentials object
46 // wrapping the Profile file provider.
47 func NewSharedCredentials(filename, profile string) *Credentials {
48 return NewCredentials(&SharedCredentialsProvider{
54 // Retrieve reads and extracts the shared credentials from the current
55 // users home directory.
56 func (p *SharedCredentialsProvider) Retrieve() (Value, error) {
59 filename, err := p.filename()
61 return Value{ProviderName: SharedCredsProviderName}, err
64 creds, err := loadProfile(filename, p.profile())
66 return Value{ProviderName: SharedCredsProviderName}, err
73 // IsExpired returns if the shared credentials have expired.
74 func (p *SharedCredentialsProvider) IsExpired() bool {
78 // loadProfiles loads from the file pointed to by shared credentials filename for profile.
79 // The credentials retrieved from the profile will be returned or error. Error will be
80 // returned if it fails to read from the file, or the data is invalid.
81 func loadProfile(filename, profile string) (Value, error) {
82 config, err := ini.Load(filename)
84 return Value{ProviderName: SharedCredsProviderName}, awserr.New("SharedCredsLoad", "failed to load shared credentials file", err)
86 iniProfile, err := config.GetSection(profile)
88 return Value{ProviderName: SharedCredsProviderName}, awserr.New("SharedCredsLoad", "failed to get profile", err)
91 id, err := iniProfile.GetKey("aws_access_key_id")
93 return Value{ProviderName: SharedCredsProviderName}, awserr.New("SharedCredsAccessKey",
94 fmt.Sprintf("shared credentials %s in %s did not contain aws_access_key_id", profile, filename),
98 secret, err := iniProfile.GetKey("aws_secret_access_key")
100 return Value{ProviderName: SharedCredsProviderName}, awserr.New("SharedCredsSecret",
101 fmt.Sprintf("shared credentials %s in %s did not contain aws_secret_access_key", profile, filename),
105 // Default to empty string if not found
106 token := iniProfile.Key("aws_session_token")
109 AccessKeyID: id.String(),
110 SecretAccessKey: secret.String(),
111 SessionToken: token.String(),
112 ProviderName: SharedCredsProviderName,
116 // filename returns the filename to use to read AWS shared credentials.
118 // Will return an error if the user's home directory path cannot be found.
119 func (p *SharedCredentialsProvider) filename() (string, error) {
120 if p.Filename == "" {
121 if p.Filename = os.Getenv("AWS_SHARED_CREDENTIALS_FILE"); p.Filename != "" {
122 return p.Filename, nil
125 homeDir := os.Getenv("HOME") // *nix
126 if homeDir == "" { // Windows
127 homeDir = os.Getenv("USERPROFILE")
130 return "", ErrSharedCredentialsHomeNotFound
133 p.Filename = filepath.Join(homeDir, ".aws", "credentials")
136 return p.Filename, nil
139 // profile returns the AWS shared credentials profile. If empty will read
140 // environment variable "AWS_PROFILE". If that is not set profile will
142 func (p *SharedCredentialsProvider) profile() string {
144 p.Profile = os.Getenv("AWS_PROFILE")
147 p.Profile = "default"