Skip to content

Commit

Permalink
Gracefully handle absence fo the ~/.aws/credentials file
Browse files Browse the repository at this point in the history
The JS SDK assumes the file exists when it is passed as an argument, so
this change makes the CDK Toolkit check for file existence before
passing down to the SDK layer. Additionally, the SDK "ini file"
re-implementation failed to check for file existece before attempting to
load file contents.

Fixes #540
  • Loading branch information
RomainMuller committed Aug 10, 2018
1 parent 6413a6e commit 6164bfd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
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)
? (AWS as any).util.ini.parse((AWS as any).util.readFileSync(this.filename))
: {};
}
}
}
}

0 comments on commit 6164bfd

Please sign in to comment.