7 "github.com/aws/aws-sdk-go/aws/awserr"
8 "github.com/aws/aws-sdk-go/internal/ini"
9 "github.com/aws/aws-sdk-go/internal/shareddefaults"
12 // SharedCredsProviderName provides a name of SharedCreds provider
13 const SharedCredsProviderName = "SharedCredentialsProvider"
16 // ErrSharedCredentialsHomeNotFound is emitted when the user directory cannot be found.
17 ErrSharedCredentialsHomeNotFound = awserr.New("UserHomeNotFound", "user home directory not found.", nil)
20 // A SharedCredentialsProvider retrieves credentials from the current user's home
21 // directory, and keeps track if those credentials are expired.
23 // Profile ini file example: $HOME/.aws/credentials
24 type SharedCredentialsProvider struct {
25 // Path to the shared credentials file.
27 // If empty will look for "AWS_SHARED_CREDENTIALS_FILE" env variable. If the
28 // env value is empty will default to current user's home directory.
29 // Linux/OSX: "$HOME/.aws/credentials"
30 // Windows: "%USERPROFILE%\.aws\credentials"
33 // AWS Profile to extract credentials from the shared credentials file. If empty
34 // will default to environment variable "AWS_PROFILE" or "default" if
35 // environment variable is also not set.
38 // retrieved states if the credentials have been successfully retrieved.
42 // NewSharedCredentials returns a pointer to a new Credentials object
43 // wrapping the Profile file provider.
44 func NewSharedCredentials(filename, profile string) *Credentials {
45 return NewCredentials(&SharedCredentialsProvider{
51 // Retrieve reads and extracts the shared credentials from the current
52 // users home directory.
53 func (p *SharedCredentialsProvider) Retrieve() (Value, error) {
56 filename, err := p.filename()
58 return Value{ProviderName: SharedCredsProviderName}, err
61 creds, err := loadProfile(filename, p.profile())
63 return Value{ProviderName: SharedCredsProviderName}, err
70 // IsExpired returns if the shared credentials have expired.
71 func (p *SharedCredentialsProvider) IsExpired() bool {
75 // loadProfiles loads from the file pointed to by shared credentials filename for profile.
76 // The credentials retrieved from the profile will be returned or error. Error will be
77 // returned if it fails to read from the file, or the data is invalid.
78 func loadProfile(filename, profile string) (Value, error) {
79 config, err := ini.OpenFile(filename)
81 return Value{ProviderName: SharedCredsProviderName}, awserr.New("SharedCredsLoad", "failed to load shared credentials file", err)
84 iniProfile, ok := config.GetSection(profile)
86 return Value{ProviderName: SharedCredsProviderName}, awserr.New("SharedCredsLoad", "failed to get profile", nil)
89 id := iniProfile.String("aws_access_key_id")
91 return Value{ProviderName: SharedCredsProviderName}, awserr.New("SharedCredsAccessKey",
92 fmt.Sprintf("shared credentials %s in %s did not contain aws_access_key_id", profile, filename),
96 secret := iniProfile.String("aws_secret_access_key")
98 return Value{ProviderName: SharedCredsProviderName}, awserr.New("SharedCredsSecret",
99 fmt.Sprintf("shared credentials %s in %s did not contain aws_secret_access_key", profile, filename),
103 // Default to empty string if not found
104 token := iniProfile.String("aws_session_token")
108 SecretAccessKey: secret,
110 ProviderName: SharedCredsProviderName,
114 // filename returns the filename to use to read AWS shared credentials.
116 // Will return an error if the user's home directory path cannot be found.
117 func (p *SharedCredentialsProvider) filename() (string, error) {
118 if len(p.Filename) != 0 {
119 return p.Filename, nil
122 if p.Filename = os.Getenv("AWS_SHARED_CREDENTIALS_FILE"); len(p.Filename) != 0 {
123 return p.Filename, nil
126 if home := shareddefaults.UserHomeDir(); len(home) == 0 {
127 // Backwards compatibility of home directly not found error being returned.
128 // This error is too verbose, failure when opening the file would of been
129 // a better error to return.
130 return "", ErrSharedCredentialsHomeNotFound
133 p.Filename = shareddefaults.SharedCredentialsFilename()
135 return p.Filename, nil
138 // profile returns the AWS shared credentials profile. If empty will read
139 // environment variable "AWS_PROFILE". If that is not set profile will
141 func (p *SharedCredentialsProvider) profile() string {
143 p.Profile = os.Getenv("AWS_PROFILE")
146 p.Profile = "default"