-
Notifications
You must be signed in to change notification settings - Fork 4k
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
feat(aws-cdk): add support for HTTPS_PROXY #666
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,50 +19,59 @@ import { SharedIniFile } from './sdk_ini_file'; | |
* to the requested account. | ||
*/ | ||
export class SDK { | ||
private readonly userAgent: string; | ||
private readonly defaultAwsAccount: DefaultAWSAccount; | ||
private readonly credentialsCache: CredentialsCache; | ||
private readonly defaultClientArgs: any = {}; | ||
|
||
constructor(private readonly profile: string | undefined) { | ||
// Find the package.json from the main toolkit | ||
const pkg = (require.main as any).require('../package.json'); | ||
this.userAgent = `${pkg.name}/${pkg.version}`; | ||
|
||
const defaultCredentialProvider = makeCLICompatibleCredentialProvider(profile); | ||
|
||
this.defaultAwsAccount = new DefaultAWSAccount(defaultCredentialProvider); | ||
this.credentialsCache = new CredentialsCache(this.defaultAwsAccount, defaultCredentialProvider); | ||
|
||
// Find the package.json from the main toolkit | ||
const pkg = (require.main as any).require('../package.json'); | ||
this.defaultClientArgs.userAgent = `${pkg.name}/${pkg.version}`; | ||
|
||
// https://aws.amazon.com/blogs/developer/using-the-aws-sdk-for-javascript-from-behind-a-proxy/ | ||
const proxyAddress = httpsProxyAddress(); | ||
if (proxyAddress) { | ||
debug('Using proxy server: %s', proxyAddress); | ||
this.defaultClientArgs.httpOptions = { | ||
agent: require('proxy-agent')(proxyAddress) | ||
}; | ||
} | ||
} | ||
|
||
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), | ||
customUserAgent: this.userAgent | ||
...this.defaultClientArgs | ||
}); | ||
} | ||
|
||
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), | ||
customUserAgent: this.userAgent | ||
...this.defaultClientArgs | ||
}); | ||
} | ||
|
||
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), | ||
customUserAgent: this.userAgent | ||
...this.defaultClientArgs | ||
}); | ||
} | ||
|
||
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), | ||
customUserAgent: this.userAgent | ||
...this.defaultClientArgs | ||
}); | ||
} | ||
|
||
|
@@ -109,7 +118,11 @@ class CredentialsCache { | |
const defaultAccount = await this.defaultAwsAccount.get(); | ||
if (!awsAccountId || awsAccountId === defaultAccount) { | ||
debug(`Using default AWS SDK credentials for account ${awsAccountId}`); | ||
return this.defaultCredentialProvider; | ||
|
||
// CredentialProviderChain extends Credentials, but that is a lie. | ||
// https://github.com/aws/aws-sdk-js/issues/2235 | ||
// Call resolve() instead. | ||
return (await this.defaultCredentialProvider).resolvePromise(); | ||
eladb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
const triedSources: CredentialProviderSource[] = []; | ||
|
@@ -122,7 +135,14 @@ class CredentialsCache { | |
triedSources.push(source); | ||
if (!(await source.canProvideCredentials(awsAccountId))) { continue; } | ||
debug(`Using ${source.name} credentials for account ${awsAccountId}`); | ||
return await source.getProvider(awsAccountId, mode); | ||
const providerOrCreds = await source.getProvider(awsAccountId, mode); | ||
|
||
// Backwards compatibility: if the plugin returns a ProviderChain, resolve that chain. | ||
// Otherwise it must have returned credentials. | ||
if ((providerOrCreds as any).resolvePromise) { | ||
return await (providerOrCreds as any).resolvePromise(); | ||
} | ||
return providerOrCreds; | ||
} | ||
const sourceNames = ['default credentials'].concat(triedSources.map(s => s.name)).join(', '); | ||
throw new Error(`Need to perform AWS calls for account ${awsAccountId}, but no credentials found. Tried: ${sourceNames}.`); | ||
|
@@ -256,3 +276,16 @@ async function getCLICompatibleDefaultRegion(profile: string | undefined): Promi | |
|
||
return region; | ||
} | ||
|
||
/** | ||
* Find and return the configured HTTPS proxy address | ||
*/ | ||
function httpsProxyAddress(): string | undefined { | ||
if (process.env.http_proxy) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn’t know process.env is case insensitive There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops. Brain fart. I meant to type https_proxy and HTTPS_PROXY but typed something else :/ |
||
return process.env.http_proxy; | ||
} | ||
if (process.env.https_proxy) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think https should have precedence There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes no, the http_ one should never have been there. |
||
return process.env.https_proxy; | ||
} | ||
return undefined; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a one line note in “cdk —help” about this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose we can make it a real command-line argument as well.