Skip to content
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 AWS_DEFAULT_PROFILE env variable when resolving profile #8354

Merged
merged 1 commit into from Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/plugins/aws/provider/awsProvider.js
Expand Up @@ -1086,10 +1086,11 @@ class AwsProvider {
const stageUpper = this.getStage() ? this.getStage().toUpperCase() : null;

// add specified credentials, overriding with more specific declarations
const awsDefaultProfile = process.env.AWS_DEFAULT_PROFILE || 'default';
try {
impl.addProfileCredentials(result, 'default');
impl.addProfileCredentials(result, awsDefaultProfile);
} catch (err) {
if (err.message !== 'Profile default does not exist') {
if (err.message !== `Profile ${awsDefaultProfile} does not exist`) {
throw err;
}
}
Expand Down
29 changes: 29 additions & 0 deletions lib/plugins/aws/provider/awsProvider.test.js
Expand Up @@ -1139,6 +1139,35 @@ describe('AwsProvider', () => {
const credentials = newAwsProvider.getCredentials();
expect(credentials.signatureVersion).to.equal('v4');
});

it('should get credentials from process.env.AWS_DEFAULT_PROFILE, not "default"', () => {
process.env.AWS_DEFAULT_PROFILE = 'notDefaultTemporary';
newAwsProvider.options['aws-profile'] = undefined;

serverless.utils.appendFileSync(
relevantEnvironment.AWS_SHARED_CREDENTIALS_FILE,
'[default]\n' +
`aws_access_key_id = ${fakeCredentials.accessKeyId}\n` +
`aws_secret_access_key = ${fakeCredentials.secretAccessKey}\n`
);

const credentials = newAwsProvider.getCredentials();
expect(credentials.credentials.profile).to.equal('notDefaultTemporary');
});

it('should get "default" credentials in lieu of anything else', () => {
newAwsProvider.options['aws-profile'] = undefined;

serverless.utils.appendFileSync(
relevantEnvironment.AWS_SHARED_CREDENTIALS_FILE,
'[default]\n' +
`aws_access_key_id = ${fakeCredentials.accessKeyId}\n` +
`aws_secret_access_key = ${fakeCredentials.secretAccessKey}\n`
);

const credentials = newAwsProvider.getCredentials();
expect(credentials.credentials.profile).to.equal('default');
});
});

describe('values', () => {
Expand Down