-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds support for AWS profiles to the CDK toolkit. At the same time, it overhauls how the AWS SDK is configured. The configuration via environment variables set at just the right time is removed, and we reimplement some parts of the SDK in an AWS CLI-compatible way to get a consistent view on the account ID and region based on the provided configuration. Fixes a bug in the AWS STS call where it would do two default credential lookups (down to one now). Fixes #480.
- Loading branch information
Showing
5 changed files
with
145 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* A reimplementation of JS AWS SDK's SharedIniFile class | ||
* | ||
* We need that class to parse the ~/.aws/config file to determine the correct | ||
* region at runtime, but unfortunately it is private upstream. | ||
*/ | ||
|
||
import AWS = require('aws-sdk'); | ||
import os = require('os'); | ||
import path = require('path'); | ||
|
||
export interface SharedIniFileOptions { | ||
isConfig?: boolean; | ||
filename?: string; | ||
} | ||
|
||
export class SharedIniFile { | ||
private readonly isConfig: boolean; | ||
private readonly filename: string; | ||
private parsedContents?: {[key: string]: {[key: string]: string}}; | ||
|
||
constructor(options?: SharedIniFileOptions) { | ||
options = options || {}; | ||
this.isConfig = options.isConfig === true; | ||
this.filename = options.filename || this.getDefaultFilepath(); | ||
} | ||
|
||
public getProfile(profile: string) { | ||
this.ensureFileLoaded(); | ||
|
||
const profileIndex = profile !== (AWS as any).util.defaultProfile && this.isConfig ? | ||
'profile ' + profile : profile; | ||
|
||
return this.parsedContents![profileIndex]; | ||
} | ||
|
||
private getDefaultFilepath(): string { | ||
return path.join( | ||
os.homedir(), | ||
'.aws', | ||
this.isConfig ? 'config' : 'credentials' | ||
); | ||
} | ||
|
||
private ensureFileLoaded() { | ||
if (!this.parsedContents) { | ||
this.parsedContents = (AWS as any).util.ini.parse( | ||
(AWS as any).util.readFileSync(this.filename) | ||
); | ||
} | ||
} | ||
} |