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(cli): proxy support is broken #5803

Merged
merged 2 commits into from
Jan 15, 2020
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
48 changes: 32 additions & 16 deletions packages/aws-cdk/lib/api/util/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ export class SDK implements ISDK {
return environment;
}

private async configureSDKHttpOptions(options: SDKOptions) {
private configureSDKHttpOptions(options: SDKOptions) {
const config: {[k: string]: any} = {};
const httpOptions: {[k: string]: any} = {};
config.httpOptions = {};

let userAgent = options.userAgent;
if (userAgent == null) {
Expand All @@ -207,19 +207,30 @@ export class SDK implements ISDK {
}
config.customUserAgent = userAgent;

// https://aws.amazon.com/blogs/developer/using-the-aws-sdk-for-javascript-from-behind-a-proxy/
options.proxyAddress = options.proxyAddress || httpsProxyFromEnvironment();
options.caBundlePath = options.caBundlePath || caBundlePathFromEnvironment();
const proxyAddress = options.proxyAddress || httpsProxyFromEnvironment();
const caBundlePath = options.caBundlePath || caBundlePathFromEnvironment();

if (options.proxyAddress) { // Ignore empty string on purpose
debug('Using proxy server: %s', options.proxyAddress);
httpOptions.proxy = options.proxyAddress;
if (proxyAddress && caBundlePath) {
throw new Error(`Cannot specify Proxy (${proxyAddress}) and CA Bundle (${caBundlePath}) at the same time`);
// Maybe it's possible after all, but I've been staring at
Copy link
Contributor

Choose a reason for hiding this comment

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

If I understand correctly, there is a use case for using a proxy and a CA bundle, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably.

  1. We didn't have CA certs before so not adding the feature is not a regression from that respect.

  2. This PR is fixing the regression of proxy support working at all. Adding support for CAs while using the ProxyAgent can be a feature request that we deal with a later date. Have you seen the source code linked? I really don't want to add more risk by trying to figure out what is happening there that at this time.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fair enough. so maybe we can improve the error message to read:

At the moment, it is impossible to use a proxy with a custom CA bundle (see issue https://github...).

// https://github.com/TooTallNate/node-proxy-agent/blob/master/index.js#L79
// a while now trying to figure out what to pass in so that the underlying Agent
// object will get the 'ca' argument. It's not trivial and I don't want to risk it.
}
if (options.caBundlePath) {
debug('Using ca bundle path: %s', options.caBundlePath);
httpOptions.agent = new https.Agent({ca: await readIfPossible(options.caBundlePath)});

if (proxyAddress) { // Ignore empty string on purpose
// https://aws.amazon.com/blogs/developer/using-the-aws-sdk-for-javascript-from-behind-a-proxy/
debug('Using proxy server: %s', proxyAddress);
// eslint-disable-next-line @typescript-eslint/no-require-imports
const ProxyAgent: any = require('proxy-agent');
config.httpOptions.agent = new ProxyAgent(proxyAddress);
}
if (caBundlePath) {
debug('Using CA bundle path: %s', caBundlePath);
config.httpOptions.agent = new https.Agent({
ca: readIfPossible(caBundlePath)
});
}
config.httpOptions = httpOptions;

AWS.config.update(config);
}
Expand Down Expand Up @@ -512,7 +523,7 @@ async function hasEc2Credentials() {
['/sys/devices/virtual/dmi/id/sys_vendor', /ec2/i],
];
for (const [file, re] of files) {
if (matchesRegex(re, await readIfPossible(file))) {
if (matchesRegex(re, readIfPossible(file))) {
instance = true;
break;
}
Expand All @@ -532,10 +543,15 @@ async function setConfigVariable() {
}
}

async function readIfPossible(filename: string): Promise<string | undefined> {
/**
* Read a file if it exists, or return undefined
*
* Not async because it is used in the constructor
*/
function readIfPossible(filename: string): string | undefined {
try {
if (!await fs.pathExists(filename)) { return undefined; }
return fs.readFile(filename, { encoding: 'utf-8' });
if (!fs.pathExistsSync(filename)) { return undefined; }
return fs.readFileSync(filename, { encoding: 'utf-8' });
} catch (e) {
debug(e);
return undefined;
Expand Down
1 change: 1 addition & 0 deletions packages/aws-cdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"json-diff": "^0.5.4",
"minimatch": ">=3.0",
"promptly": "^3.0.3",
"proxy-agent": "^3.1.1",
"request": "^2.88.0",
"semver": "^7.1.1",
"source-map-support": "^0.5.16",
Expand Down
Loading