@@ -3,12 +3,13 @@ var path = require('path');
3
3
var STS = require ( '../../clients/sts' ) ;
4
4
5
5
var configOptInEnv = 'AWS_SDK_LOAD_CONFIG' ;
6
+ var sharedFileEnv = 'AWS_SHARED_CREDENTIALS_FILE' ;
6
7
var defaultProfile = 'default' ;
7
8
8
9
/**
9
10
* Represents credentials loaded from shared credentials file
10
11
* (defaulting to ~/.aws/credentials or defined by the
11
- * `AWS_CREDENTIAL_PROFILES_FILE ` environment variable).
12
+ * `AWS_SHARED_CREDENTIALS_FILE ` environment variable).
12
13
*
13
14
* ## Using the shared credentials file
14
15
*
@@ -44,7 +45,7 @@ AWS.SharedIniFileCredentials = AWS.util.inherit(AWS.Credentials, {
44
45
* @option options profile [String] (AWS_PROFILE env var or 'default')
45
46
* the name of the profile to load.
46
47
* @option options filename [String] ('~/.aws/credentials' or defined by
47
- * AWS_CREDENTIAL_PROFILES_FILE process env var)
48
+ * AWS_SHARED_CREDENTIALS_FILE process env var)
48
49
* the filename to use when loading credentials.
49
50
* @option options disableAssumeRole [Boolean] (false) True to disable
50
51
* support for profiles that assume an IAM role. If true, and an assume
@@ -75,21 +76,23 @@ AWS.SharedIniFileCredentials = AWS.util.inherit(AWS.Credentials, {
75
76
refresh : function refresh ( callback ) {
76
77
if ( ! callback ) callback = function ( err ) { if ( err ) throw err ; } ;
77
78
try {
78
- if ( ! this . filename ) this . loadDefaultFilename ( ) ;
79
79
var profile = { } ;
80
80
if ( process . env [ configOptInEnv ] ) {
81
- var configProfileName = this . profile === defaultProfile ?
82
- 'default' : 'profile ' + this . profile ;
83
- var config = AWS . util . ini
84
- . parse ( AWS . util . readFileSync ( this . configFilename ) ) ;
85
- profile = AWS . util . merge ( profile , config [ configProfileName ] ) ;
81
+ var config = new AWS . SharedIniFile ( {
82
+ isConfig : true ,
83
+ filename : process . env . AWS_CONFIG_FILE
84
+ } ) ;
85
+ profile = AWS . util . merge ( profile , config . getProfile ( this . profile ) ) ;
86
86
}
87
- var creds = AWS . util . ini . parse ( AWS . util . readFileSync ( this . filename ) ) ;
88
- profile = AWS . util . merge ( profile , creds [ this . profile ] ) ;
87
+ var creds = new AWS . SharedIniFile ( {
88
+ filename : this . filename ||
89
+ ( process . env [ configOptInEnv ] && process . env [ sharedFileEnv ] )
90
+ } ) ;
91
+ profile = AWS . util . merge ( profile , creds . getProfile ( this . profile ) ) ;
89
92
90
93
if ( Object . keys ( profile ) . length === 0 ) {
91
94
throw AWS . util . error (
92
- new Error ( 'Profile ' + this . profile + ' not found in ' + this . filename ) ,
95
+ new Error ( 'Profile ' + this . profile + ' not found' ) ,
93
96
{ code : 'SharedIniFileCredentialsProviderFailure' }
94
97
) ;
95
98
}
@@ -105,8 +108,7 @@ AWS.SharedIniFileCredentials = AWS.util.inherit(AWS.Credentials, {
105
108
106
109
if ( ! this . accessKeyId || ! this . secretAccessKey ) {
107
110
throw AWS . util . error (
108
- new Error ( 'Credentials not set in ' + this . filename +
109
- ' using profile ' + this . profile ) ,
111
+ new Error ( 'Credentials not set for profile ' + this . profile ) ,
110
112
{ code : 'SharedIniFileCredentialsProviderFailure' }
111
113
) ;
112
114
}
@@ -124,8 +126,7 @@ AWS.SharedIniFileCredentials = AWS.util.inherit(AWS.Credentials, {
124
126
if ( this . disableAssumeRole ) {
125
127
throw AWS . util . error (
126
128
new Error ( 'Role assumption profiles are disabled. ' +
127
- 'Failed to load profile ' + this . profile + ' from ' +
128
- this . filename ) ,
129
+ 'Failed to load profile ' + this . profile ) ,
129
130
{ code : 'SharedIniFileCredentialsProviderFailure' }
130
131
) ;
131
132
}
@@ -138,19 +139,17 @@ AWS.SharedIniFileCredentials = AWS.util.inherit(AWS.Credentials, {
138
139
139
140
if ( ! sourceProfileName ) {
140
141
throw AWS . util . error (
141
- new Error ( 'source_profile is not set in ' + this . filename +
142
- ' using profile ' + this . profile ) ,
142
+ new Error ( 'source_profile is not set using profile ' + this . profile ) ,
143
143
{ code : 'SharedIniFileCredentialsProviderFailure' }
144
144
) ;
145
145
}
146
146
147
- var sourceProfile = creds [ sourceProfileName ] ;
147
+ var sourceProfile = creds . getProfile ( sourceProfileName ) ;
148
148
149
149
if ( typeof sourceProfile !== 'object' ) {
150
150
throw AWS . util . error (
151
- new Error ( 'source_profile ' + sourceProfileName + ' set in ' +
152
- this . filename + ' using profile ' + this . profile +
153
- ' does not exist' ) ,
151
+ new Error ( 'source_profile ' + sourceProfileName + ' using profile '
152
+ + this . profile + ' does not exist' ) ,
154
153
{ code : 'SharedIniFileCredentialsProviderFailure' }
155
154
) ;
156
155
}
@@ -166,8 +165,7 @@ AWS.SharedIniFileCredentials = AWS.util.inherit(AWS.Credentials, {
166
165
if ( ! sourceCredentials . accessKeyId || ! sourceCredentials . secretAccessKey ) {
167
166
throw AWS . util . error (
168
167
new Error ( 'Credentials not set in source_profile ' +
169
- sourceProfileName + ' set in ' + this . filename +
170
- ' using profile ' + this . profile ) ,
168
+ sourceProfileName + ' using profile ' + this . profile ) ,
171
169
{ code : 'SharedIniFileCredentialsProviderFailure' }
172
170
) ;
173
171
}
@@ -197,30 +195,5 @@ AWS.SharedIniFileCredentials = AWS.util.inherit(AWS.Credentials, {
197
195
self . expireTime = data . Credentials . Expiration ;
198
196
callback ( ) ;
199
197
} ) ;
200
- } ,
201
-
202
- /**
203
- * @api private
204
- */
205
- loadDefaultFilename : function loadDefaultFilename ( ) {
206
- var env = process . env ;
207
-
208
- var home = env . HOME ||
209
- env . USERPROFILE ||
210
- ( env . HOMEPATH ? ( ( env . HOMEDRIVE || 'C:/' ) + env . HOMEPATH ) : null ) ;
211
- if ( ! home ) {
212
- throw AWS . util . error (
213
- new Error ( 'Cannot load credentials, HOME path not set' ) ,
214
- { code : 'SharedIniFileCredentialsProviderFailure' }
215
- ) ;
216
- }
217
-
218
- this . filename = path . join ( home , '.aws' , 'credentials' ) ;
219
-
220
- if ( env [ configOptInEnv ] ) {
221
- this . filename = env . AWS_SHARED_CREDENTIALS_FILE || this . filename ;
222
- this . configFilename = env . AWS_CONFIG_FILE ||
223
- path . join ( home , '.aws' , 'config' ) ;
224
- }
225
198
}
226
199
} ) ;
0 commit comments