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

fix: gracefully handle absence fo the ~/.aws/credentials file #541

Merged
merged 2 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/aws-cdk/lib/api/util/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Environment} from '@aws-cdk/cx-api';
import AWS = require('aws-sdk');
import fs = require('fs-extra');
import os = require('os');
import path = require('path');
import { debug } from '../../logging';
Expand Down Expand Up @@ -157,7 +158,7 @@ function makeCLICompatibleCredentialProvider(profile: string | undefined) {
return new AWS.CredentialProviderChain([
() => new AWS.EnvironmentCredentials('AWS'),
() => new AWS.EnvironmentCredentials('AMAZON'),
() => new AWS.SharedIniFileCredentials({ profile, filename }),
...(fs.pathExistsSync(filename) ? [() => new AWS.SharedIniFileCredentials({ profile, filename })] : []),
() => {
// Calling private API
if ((AWS.ECSCredentials.prototype as any).isConfiguredForEcsCredentials()) {
Expand Down Expand Up @@ -200,4 +201,4 @@ function getCLICompatibleDefaultRegion(profile: string | undefined): string | un
}

return region;
}
}
9 changes: 5 additions & 4 deletions packages/aws-cdk/lib/api/util/sdk_ini_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import AWS = require('aws-sdk');
import fs = require('fs-extra');
import os = require('os');
import path = require('path');

Expand Down Expand Up @@ -44,9 +45,9 @@ export class SharedIniFile {

private ensureFileLoaded() {
if (!this.parsedContents) {
this.parsedContents = (AWS as any).util.ini.parse(
(AWS as any).util.readFileSync(this.filename)
);
this.parsedContents = fs.pathExistsSync(this.filename)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this is necessary? I'm positive I tested this WITHOUT an ~/.aws/config file and no crash.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My observations show that a missing ~/.aws/credentials would blow up right this spot.

? (AWS as any).util.ini.parse((AWS as any).util.readFileSync(this.filename))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we save of (AWS as any).util as a variable to make this conditional a bit less verbose?

: {};
}
}
}
}