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

Add ability to configure proxy with env vars #188

Merged
merged 2 commits into from
Oct 16, 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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ const client = jwksClient({
For more information, see [the NodeJS request library `agentOptions`
documentation](https://github.com/request/request#using-optionsagentoptions).

### Proxy configuration

There are two ways to configure the usage of a proxy:
- Provide the ```proxy``` option when initialiting the client as shown above
- Provide the ```HTTP_PROXY```, ```HTTPS_PROXY``` and ```NO_PROXY``` environment variables

## Running Tests

```
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"jsonwebtoken": "^8.5.1",
"limiter": "^1.1.5",
"lru-memoizer": "^2.1.2",
"ms": "^2.1.2"
"ms": "^2.1.2",
"proxy-from-env": "^1.1.0"
},
"devDependencies": {
"@types/chai": "^4.2.11",
Expand Down
11 changes: 6 additions & 5 deletions src/wrappers/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import url from 'url';
import httpProxyAgent from 'http-proxy-agent';
import httpsProxyAgent from 'https-proxy-agent';
import { request } from 'axios';
import { getProxyForUrl } from 'proxy-from-env';

export default function(options, cb) {
const requestOptions = {
Expand All @@ -12,19 +13,19 @@ export default function(options, cb) {
timeout: options.timeout
};

if (options.proxy || options.agentOptions || options.strictSSL != undefined) {
const proxyUrl = options.proxy || getProxyForUrl(options.uri);
if (proxyUrl || options.agentOptions || options.strictSSL != undefined) {
const agentOptions = {
...(options.strictSSL != undefined) && { rejectUnauthorized: options.strictSSL },
...(options.headers && { headers: options.headers }),
...options.agentOptions
};

if (options.proxy) {
if (proxyUrl) {
// Axios proxy workaround: https://github.com/axios/axios/issues/2072
const proxy = url.parse(options.proxy);

const proxyOptions = url.parse(proxyUrl);
requestOptions.proxy = false; //proxyParsed
const proxyAgentOptions = { ...agentOptions, ...proxy };
const proxyAgentOptions = { ...agentOptions, ...proxyOptions };
requestOptions.httpAgent = new httpProxyAgent(proxyAgentOptions);
requestOptions.httpsAgent = new httpsProxyAgent(proxyAgentOptions);
} else {
Expand Down
22 changes: 22 additions & 0 deletions tests/request.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,28 @@ describe('Request wrapper tests', () => {
done(err);
});
});

const expectAgent = (agent, host, port, protocol) => {
expect(agent.proxy.host).to.equal(host);
expect(agent.proxy.port).to.equal(port);
expect(agent.proxy.protocol).to.equal(protocol);
};

it('should pass the "proxy" option', (done) => {
request({ uri, proxy: 'http://dummy-proxy.org:123' }, (err, options) => {
expectAgent(options.httpsAgent, 'dummy-proxy.org', 123, 'http:');
done(err);
});
});

it('should read the proxy config from the environment', (done) => {
process.env.HTTPS_PROXY = 'http://another-dummy-proxy.org:456';
request({ uri }, (err, options) => {
expectAgent(options.httpsAgent, 'another-dummy-proxy.org', 456, 'http:');
process.env.HTTPS_PROXY = undefined;
done(err);
});
});
});
});
});