-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add support for loading shared config #1391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for loading shared config #1391
Conversation
Anything holding back a merge here? This functionality would be really useful for compatibility with other SDKs. |
lib/shared_ini.js
Outdated
/** | ||
* @api private | ||
*/ | ||
AWS.SharedIniFile = AWS.util.inherit({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is private, you could treat this as a regular module instead of hanging the class on AWS
. Then you won't have to do the side-effects require
in node_loader.js
.
@@ -111,8 +126,7 @@ AWS.SharedIniFileCredentials = AWS.util.inherit(AWS.Credentials, { | |||
if (this.disableAssumeRole) { | |||
throw AWS.util.error( | |||
new Error('Role assumption profiles are disabled. ' + | |||
'Failed to load profile ' + this.profile + ' from ' + | |||
this.filename), | |||
'Failed to load profile ' + this.profile), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you still get this.filename
from creds.filename
instead? It might still be useful to have the filename in the message, especially now that we will be looking at credentials and config.
lib/node_loader.js
Outdated
return process.env.AWS_REGION || process.env.AMAZON_REGION; | ||
var env = process.env; | ||
var region = env.AWS_REGION || env.AMAZON_REGION; | ||
if (!region && env.AWS_SDK_LOAD_CONFIG) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we're actually supposed to check the credentials file for region, then the config file, similar to the CLI/boto.
@@ -2,9 +2,14 @@ var AWS = require('../core'); | |||
var path = require('path'); | |||
var STS = require('../../clients/sts'); | |||
|
|||
var configOptInEnv = 'AWS_SDK_LOAD_CONFIG'; | |||
var sharedFileEnv = 'AWS_SHARED_CREDENTIALS_FILE'; | |||
var defaultProfile = 'default'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we grab defaultProfile
from AWS.util.defaultProfile
instead?
expect(AWS.util.readFileSync.calls[0].arguments[0]).to.match(/[\/\\]home[\/\\]user[\/\\].aws[\/\\]config/) | ||
expect(AWS.util.readFileSync.calls[1].arguments[0]).to.equal(process.env.AWS_SHARED_CREDENTIALS_FILE) | ||
|
||
it 'loads credentials from ~/.aws/config if AWS_SDK_LOAD_CONFIG is set', -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a test to make sure the credentials from ~/.aws/credentials
is used preferentially over the credentials in ~/.aws/config
if the same profile exists in both files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add a test for that.
One thing I'm not clear on, does the following scenario work:
|
…ILE if AWS_SDK_LOAD_CONFIG is set
…ed in AWS_CONFIG_FILE if AWS_SDK_LOAD_CONFIG is set
…rce profiles defined in ~/.aws/config
3e73060
to
fc4d210
Compare
I had to force push due to a merge conflict in AWS.util. The last commit add the ability to assume a role for a profile defined in |
Codecov Report
@@ Coverage Diff @@
## master #1391 +/- ##
==========================================
- Coverage 95.35% 95.34% -0.02%
==========================================
Files 176 177 +1
Lines 6222 6270 +48
Branches 1278 1293 +15
==========================================
+ Hits 5933 5978 +45
- Misses 289 292 +3
Continue to review full report at Codecov.
|
test/credentials.spec.coffee
Outdated
|
||
it 'prefers credentials from ~/.aws/credentials if AWS_SDK_LOAD_CONFIG is set', -> | ||
process.env.AWS_SDK_LOAD_CONFIG = '1' | ||
mock = ''' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This mock can probably be removed.
test/credentials.spec.coffee
Outdated
</AssumeRoleResult> | ||
</AssumeRoleResponse> | ||
''' | ||
debugger; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra statement :)
test/credentials.spec.coffee
Outdated
aws_access_key_id = akid2 | ||
aws_secret_access_key = secret2 | ||
''' | ||
helpers.mockHttpResponse 200, {}, ''' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this test is actually ensuring that the creds from profile foo
are used instead of default
.
What do you think about spying on AWS.STS
or AWS.Credentials
to get the accessKeyId that was used as the source?
test/credentials.spec.coffee
Outdated
</AssumeRoleResult> | ||
</AssumeRoleResponse> | ||
''' | ||
debugger; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra statement.
Am I to understand that the SDK currently doesn't support ~/.aws at all? Or just doesn't support using assumed roles through the ~/.aws entries, which has only recently been added to PHP and worked a while in Python? |
@eric-tucker |
profiles[availableProfiles[i]] || {}, | ||
creds.getProfile(availableProfiles[i]) | ||
); | ||
for (i = 0, availableProfiles = creds.getProfiles(); i < availableProfiles.length; i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Can we either use var i
here, or declare var i
at the top of the function? Just worried if we someday change i
in the for loop above, then this i
becomes a global.
12e966a
to
7528c83
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not load |
@dotchev |
This appears to be a bug upstream as a result of aws/aws-sdk-js#1391 We now check for the presence of ~/.aws prior to setting this env-var.
Hi, This page is still confusing : https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-region.html#setting-region-config-file. "The SDK for JavaScript automatically searches for a config file when it loads." I understand here that I need to set |
The |
Thanks! |
@chrisradek any update on the version bump? Having this inconsistency for over a year is enough justification for a major release IMHO |
It would have been nice if you could add an example of using this feature. I am having issues of using assume role with JS. |
This seems to be missing support for the mfa_serial option. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread. |
This PR adds support for loading configuration from
~/.aws/config
, including credentials, the default region to use, and role assumption metadata. This is an opt-in feature and will only take effect if theAWS_SDK_LOAD_CONFIG
environment variable is set to something truthy.I also added support for some configuration environment variables that we do not currently support, namely
AWS_SHARED_CREDENTIALS_FILE
andAWS_CONFIG_FILE
, both of which will only be used ifAWS_SDK_LOAD_CONFIG
is set.This should resolve #1196 as well (via
AWS_SHARED_CREDENTIALS_FILE
).Resolves #1296
Resolves #1039
Resolves #993