1
1
var AWS = require ( '../core' ) ;
2
2
var path = require ( 'path' ) ;
3
+ var SharedIniFile = require ( '../shared_ini' ) ;
3
4
var STS = require ( '../../clients/sts' ) ;
4
5
5
6
/**
6
7
* Represents credentials loaded from shared credentials file
7
- * (defaulting to ~/.aws/credentials).
8
+ * (defaulting to ~/.aws/credentials or defined by the
9
+ * `AWS_SHARED_CREDENTIALS_FILE` environment variable).
8
10
*
9
11
* ## Using the shared credentials file
10
12
*
@@ -39,8 +41,9 @@ AWS.SharedIniFileCredentials = AWS.util.inherit(AWS.Credentials, {
39
41
* @param options [map] a set of options
40
42
* @option options profile [String] (AWS_PROFILE env var or 'default')
41
43
* the name of the profile to load.
42
- * @option options filename [String] ('~/.aws/credentials') the filename
43
- * to use when loading credentials.
44
+ * @option options filename [String] ('~/.aws/credentials' or defined by
45
+ * AWS_SHARED_CREDENTIALS_FILE process env var)
46
+ * the filename to use when loading credentials.
44
47
* @option options disableAssumeRole [Boolean] (false) True to disable
45
48
* support for profiles that assume an IAM role. If true, and an assume
46
49
* role profile is selected, an error is raised.
@@ -51,8 +54,8 @@ AWS.SharedIniFileCredentials = AWS.util.inherit(AWS.Credentials, {
51
54
options = options || { } ;
52
55
53
56
this . filename = options . filename ;
54
- this . profile = options . profile || process . env . AWS_PROFILE || 'default' ;
55
- this . disableAssumeRole = ! ! options . disableAssumeRole ;
57
+ this . profile = options . profile || process . env . AWS_PROFILE || AWS . util . defaultProfile ;
58
+ this . disableAssumeRole = Boolean ( options . disableAssumeRole ) ;
56
59
this . get ( function ( ) { } ) ;
57
60
} ,
58
61
@@ -70,19 +73,35 @@ AWS.SharedIniFileCredentials = AWS.util.inherit(AWS.Credentials, {
70
73
refresh : function refresh ( callback ) {
71
74
if ( ! callback ) callback = function ( err ) { if ( err ) throw err ; } ;
72
75
try {
73
- if ( ! this . filename ) this . loadDefaultFilename ( ) ;
74
- var creds = AWS . util . ini . parse ( AWS . util . readFileSync ( this . filename ) ) ;
75
- var profile = creds [ this . profile ] ;
76
+ var profiles = { } ;
77
+ var i , availableProfiles ;
78
+ if ( process . env [ AWS . util . configOptInEnv ] ) {
79
+ var config = new SharedIniFile ( {
80
+ isConfig : true ,
81
+ filename : process . env [ AWS . util . sharedConfigFileEnv ]
82
+ } ) ;
83
+ for ( i = 0 , availableProfiles = config . getProfiles ( ) ; i < availableProfiles . length ; i ++ ) {
84
+ profiles [ availableProfiles [ i ] ] = config . getProfile ( availableProfiles [ i ] ) ;
85
+ }
86
+ }
87
+ var creds = new SharedIniFile ( {
88
+ filename : this . filename ||
89
+ ( process . env [ AWS . util . configOptInEnv ] && process . env [ AWS . util . sharedCredentialsFileEnv ] )
90
+ } ) ;
91
+ for ( i = 0 , availableProfiles = creds . getProfiles ( ) ; i < availableProfiles . length ; i ++ ) {
92
+ profiles [ availableProfiles [ i ] ] = creds . getProfile ( availableProfiles [ i ] ) ;
93
+ }
94
+ var profile = profiles [ this . profile ] || { } ;
76
95
77
- if ( typeof profile !== 'object' ) {
96
+ if ( Object . keys ( profile ) . length === 0 ) {
78
97
throw AWS . util . error (
79
- new Error ( 'Profile ' + this . profile + ' not found in ' + this . filename ) ,
98
+ new Error ( 'Profile ' + this . profile + ' not found' ) ,
80
99
{ code : 'SharedIniFileCredentialsProviderFailure' }
81
100
) ;
82
101
}
83
102
84
103
if ( profile [ 'role_arn' ] ) {
85
- this . loadRoleProfile ( creds , profile , callback ) ;
104
+ this . loadRoleProfile ( profiles , profile , callback ) ;
86
105
return ;
87
106
}
88
107
@@ -92,8 +111,7 @@ AWS.SharedIniFileCredentials = AWS.util.inherit(AWS.Credentials, {
92
111
93
112
if ( ! this . accessKeyId || ! this . secretAccessKey ) {
94
113
throw AWS . util . error (
95
- new Error ( 'Credentials not set in ' + this . filename +
96
- ' using profile ' + this . profile ) ,
114
+ new Error ( 'Credentials not set for profile ' + this . profile ) ,
97
115
{ code : 'SharedIniFileCredentialsProviderFailure' }
98
116
) ;
99
117
}
@@ -111,8 +129,8 @@ AWS.SharedIniFileCredentials = AWS.util.inherit(AWS.Credentials, {
111
129
if ( this . disableAssumeRole ) {
112
130
throw AWS . util . error (
113
131
new Error ( 'Role assumption profiles are disabled. ' +
114
- 'Failed to load profile ' + this . profile + ' from ' +
115
- this . filename ) ,
132
+ 'Failed to load profile ' + this . profile +
133
+ ' from ' + creds . filename ) ,
116
134
{ code : 'SharedIniFileCredentialsProviderFailure' }
117
135
) ;
118
136
}
@@ -125,8 +143,7 @@ AWS.SharedIniFileCredentials = AWS.util.inherit(AWS.Credentials, {
125
143
126
144
if ( ! sourceProfileName ) {
127
145
throw AWS . util . error (
128
- new Error ( 'source_profile is not set in ' + this . filename +
129
- ' using profile ' + this . profile ) ,
146
+ new Error ( 'source_profile is not set using profile ' + this . profile ) ,
130
147
{ code : 'SharedIniFileCredentialsProviderFailure' }
131
148
) ;
132
149
}
@@ -135,9 +152,8 @@ AWS.SharedIniFileCredentials = AWS.util.inherit(AWS.Credentials, {
135
152
136
153
if ( typeof sourceProfile !== 'object' ) {
137
154
throw AWS . util . error (
138
- new Error ( 'source_profile ' + sourceProfileName + ' set in ' +
139
- this . filename + ' using profile ' + this . profile +
140
- ' does not exist' ) ,
155
+ new Error ( 'source_profile ' + sourceProfileName + ' using profile '
156
+ + this . profile + ' does not exist' ) ,
141
157
{ code : 'SharedIniFileCredentialsProviderFailure' }
142
158
) ;
143
159
}
@@ -153,8 +169,7 @@ AWS.SharedIniFileCredentials = AWS.util.inherit(AWS.Credentials, {
153
169
if ( ! sourceCredentials . accessKeyId || ! sourceCredentials . secretAccessKey ) {
154
170
throw AWS . util . error (
155
171
new Error ( 'Credentials not set in source_profile ' +
156
- sourceProfileName + ' set in ' + this . filename +
157
- ' using profile ' + this . profile ) ,
172
+ sourceProfileName + ' using profile ' + this . profile ) ,
158
173
{ code : 'SharedIniFileCredentialsProviderFailure' }
159
174
) ;
160
175
}
@@ -184,23 +199,5 @@ AWS.SharedIniFileCredentials = AWS.util.inherit(AWS.Credentials, {
184
199
self . expireTime = data . Credentials . Expiration ;
185
200
callback ( ) ;
186
201
} ) ;
187
- } ,
188
-
189
- /**
190
- * @api private
191
- */
192
- loadDefaultFilename : function loadDefaultFilename ( ) {
193
- var env = process . env ;
194
- var home = env . HOME ||
195
- env . USERPROFILE ||
196
- ( env . HOMEPATH ? ( ( env . HOMEDRIVE || 'C:/' ) + env . HOMEPATH ) : null ) ;
197
- if ( ! home ) {
198
- throw AWS . util . error (
199
- new Error ( 'Cannot load credentials, HOME path not set' ) ,
200
- { code : 'SharedIniFileCredentialsProviderFailure' }
201
- ) ;
202
- }
203
-
204
- this . filename = path . join ( home , '.aws' , 'credentials' ) ;
205
202
}
206
203
} ) ;
0 commit comments