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

Allow use of assumed roles behind a proxy. #898

Merged
merged 2 commits into from
Oct 12, 2018
Merged
Changes from all commits
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
29 changes: 13 additions & 16 deletions packages/aws-cdk/lib/api/util/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export interface SDKOptions {
export class SDK {
private readonly defaultAwsAccount: DefaultAWSAccount;
private readonly credentialsCache: CredentialsCache;
private readonly defaultClientArgs: any = {};
private readonly profile?: string;

constructor(options: SDKOptions) {
Expand All @@ -58,52 +57,50 @@ export class SDK {

// Find the package.json from the main toolkit
const pkg = (require.main as any).require('../package.json');
this.defaultClientArgs.userAgent = `${pkg.name}/${pkg.version}`;
AWS.config.update({
customUserAgent: `${pkg.name}/${pkg.version}`
});

// https://aws.amazon.com/blogs/developer/using-the-aws-sdk-for-javascript-from-behind-a-proxy/
if (options.proxyAddress === undefined) {
options.proxyAddress = httpsProxyFromEnvironment();
}
if (options.proxyAddress) { // Ignore empty string on purpose
debug('Using proxy server: %s', options.proxyAddress);
this.defaultClientArgs.httpOptions = {
agent: require('proxy-agent')(options.proxyAddress)
};
AWS.config.update({
httpOptions: { agent: require('proxy-agent')(options.proxyAddress) }
});
}

this.defaultAwsAccount = new DefaultAWSAccount(defaultCredentialProvider, this.defaultClientArgs);
this.defaultAwsAccount = new DefaultAWSAccount(defaultCredentialProvider);
this.credentialsCache = new CredentialsCache(this.defaultAwsAccount, defaultCredentialProvider);
}

public async cloudFormation(environment: Environment, mode: Mode): Promise<AWS.CloudFormation> {
return new AWS.CloudFormation({
region: environment.region,
credentials: await this.credentialsCache.get(environment.account, mode),
...this.defaultClientArgs
credentials: await this.credentialsCache.get(environment.account, mode)
});
}

public async ec2(awsAccountId: string | undefined, region: string | undefined, mode: Mode): Promise<AWS.EC2> {
return new AWS.EC2({
region,
credentials: await this.credentialsCache.get(awsAccountId, mode),
...this.defaultClientArgs
credentials: await this.credentialsCache.get(awsAccountId, mode)
});
}

public async ssm(awsAccountId: string | undefined, region: string | undefined, mode: Mode): Promise<AWS.SSM> {
return new AWS.SSM({
region,
credentials: await this.credentialsCache.get(awsAccountId, mode),
...this.defaultClientArgs
credentials: await this.credentialsCache.get(awsAccountId, mode)
});
}

public async s3(environment: Environment, mode: Mode): Promise<AWS.S3> {
return new AWS.S3({
region: environment.region,
credentials: await this.credentialsCache.get(environment.account, mode),
...this.defaultClientArgs
credentials: await this.credentialsCache.get(environment.account, mode)
});
}

Expand Down Expand Up @@ -195,7 +192,7 @@ class DefaultAWSAccount {
private defaultAccountId?: string = undefined;
private readonly accountCache = new AccountAccessKeyCache();

constructor(private readonly defaultCredentialsProvider: Promise<AWS.CredentialProviderChain>, private readonly defaultClientArgs: any) {
constructor(private readonly defaultCredentialsProvider: Promise<AWS.CredentialProviderChain>) {
}

/**
Expand Down Expand Up @@ -223,7 +220,7 @@ class DefaultAWSAccount {
const accountId = await this.accountCache.fetch(creds.accessKeyId, async () => {
// if we don't have one, resolve from STS and store in cache.
debug('Looking up default account ID from STS');
const result = await new AWS.STS({ credentials: creds, ...this.defaultClientArgs }).getCallerIdentity().promise();
const result = await new AWS.STS({ credentials: creds }).getCallerIdentity().promise();
const aid = result.Account;
if (!aid) {
debug('STS didn\'t return an account ID');
Expand Down