Skip to content

Commit

Permalink
fix(AWS Credentials): Fail when profile is already configured
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed Oct 4, 2021
1 parent bcb2408 commit f8ad7bc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
9 changes: 3 additions & 6 deletions lib/plugins/aws/configCredentials.js
Expand Up @@ -58,13 +58,10 @@ class AwsConfigCredentials {
if (profiles.has(this.options.profile)) {
// Only update the profile if the overwrite flag was set
if (!this.options.overwrite) {
legacy.log(
`Failed! ~/.aws/credentials already has a "${this.options.profile}" profile. Use the overwrite flag ("-o" or "--overwrite") to force the update`
throw new ServerlessError(
`Profile "${this.options.profile}" is already configured in ~/.aws/credentials. Use the overwrite flag ("-o" or "--overwrite") to force the update.`,
'CREDENTIALS_PROFILE_ALREADY_CONFIGURED'
);
log.notice.skip(
`Profile "${this.options.profile}" is already configured in ~/.aws/credentials. Use the overwrite flag ("-o" or "--overwrite") to force the update.`
);
return null;
}
}
profiles.set(this.options.profile, {
Expand Down
37 changes: 26 additions & 11 deletions test/unit/lib/plugins/aws/configCredentials.test.js
@@ -1,6 +1,6 @@
'use strict';

const expect = require('chai').expect;
const chai = require('chai');
const sandbox = require('sinon');
const { constants } = require('fs');
const fs = require('fs');
Expand All @@ -10,6 +10,10 @@ const os = require('os');
const path = require('path');
const AwsConfigCredentials = require('../../../../../lib/plugins/aws/configCredentials');
const Serverless = require('../../../../../lib/Serverless');
const runServerless = require('../../../../utils/run-serverless');

const { expect } = chai;
chai.use(require('chai-as-promised'));

describe('AwsConfigCredentials', () => {
let awsConfigCredentials;
Expand Down Expand Up @@ -137,16 +141,6 @@ describe('AwsConfigCredentials', () => {
);
});

it('should not update the profile if the overwrite flag is not set', () => {
awsConfigCredentials.options.profile = 'my-profile';
awsConfigCredentials.options.key = 'my-new-profile-key';
awsConfigCredentials.options.secret = 'my-new-profile-secret';

fse.outputFileSync(credentialsFilePath, credentialsFileContent);

return awsConfigCredentials.configureCredentials();
});

it('should update the profile', () => {
awsConfigCredentials.options.profile = 'my-profile';
awsConfigCredentials.options.key = 'my-new-profile-key';
Expand Down Expand Up @@ -245,3 +239,24 @@ describe('AwsConfigCredentials', () => {
}
});
});

describe('test/unit/lib/plugins/aws/configCredentials.test.js', () => {
it('should fail if profile is already set and overwrite is not set', async () => {
const credentialsFilePath = path.join(os.homedir(), '.aws', 'credentials');
const credentialsFileContent = [
'[default]',
'aws_access_key_id = my-old-profile-key',
'aws_secret_access_key = my-old-profile-secret',
].join('\n');

await fse.outputFile(credentialsFilePath, credentialsFileContent);

await expect(
runServerless({
noService: true,
command: 'config credentials',
options: { provider: 'aws', key: 'key', secret: 'secret' },
})
).to.be.eventually.rejected.and.have.property('code', 'CREDENTIALS_PROFILE_ALREADY_CONFIGURED');
});
});

0 comments on commit f8ad7bc

Please sign in to comment.