Skip to content

Commit

Permalink
Use https proxy agent (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
EHitchcockIAG authored Oct 11, 2022
1 parent 0ae8044 commit acb114f
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 34 deletions.
155 changes: 123 additions & 32 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
"@slack/web-api": "^6.7.2",
"axios": "^0.27.2",
"flat": "^5.0.2",
"markup-js": "^1.5.21"
"https-proxy-agent": "^5.0.1",
"markup-js": "^1.5.21",
"whatwg-url": "^11.0.0"
},
"devDependencies": {
"@vercel/ncc": "^0.34.0",
Expand Down
20 changes: 19 additions & 1 deletion src/slack-send.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const axios = require('axios');
const { promises: fs } = require('fs');
const path = require('path');
const markup = require('markup-js');
const HttpsProxyAgent = require('https-proxy-agent');
const { parseURL } = require('whatwg-url');

const SLACK_WEBHOOK_TYPES = {
WORKFLOW_TRIGGER: 'WORKFLOW_TRIGGER',
Expand Down Expand Up @@ -106,8 +108,24 @@ module.exports = async function slackSend(core) {
payload = flatPayload;
}

const axiosOpts = {};
try {
await axios.post(webhookUrl, payload);
if (parseURL(webhookUrl).scheme === 'https') {
const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy || '';
if (httpsProxy && parseURL(httpsProxy).scheme === 'http') {
const httpsProxyAgent = new HttpsProxyAgent(httpsProxy);
axiosOpts.httpsAgent = httpsProxyAgent;

// Use configured tunnel above instead of default axios proxy setup from env vars
axiosOpts.proxy = false;
}
}
} catch (err) {
console.log('failed to configure https proxy agent for http proxy. Using default axios configuration');
}

try {
await axios.post(webhookUrl, payload, axiosOpts);
} catch (err) {
console.log('axios post failed, double check the payload being sent includes the keys Slack expects');
console.log(payload);
Expand Down
27 changes: 27 additions & 0 deletions test/slack-send-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,33 @@ describe('slack-send', () => {
await slackSend(fakeCore);
assert(AxiosMock.post.calledWith('https://someurl', payload));
});
describe('proxy config', () => {
beforeEach(() => {
delete process.env.HTTPS_PROXY;
});
it('should use https proxy agent when proxy uses HTTP', async () => {
process.env.HTTPS_PROXY = 'http://test.proxy:8080/';
await slackSend(fakeCore);
assert(AxiosMock.post.calledWith('https://someurl', payload, sinon.match.has('httpsAgent').and(sinon.match.has('proxy'))));
});
it('should use default axios config when no proxy set', async () => {
await slackSend(fakeCore);
assert(AxiosMock.post.calledWithExactly('https://someurl', payload, {}));
});
it('should use default axios config when proxy uses HTTPS', async () => {
process.env.HTTPS_PROXY = 'https://test.proxy:8080/';
await slackSend(fakeCore);
assert(AxiosMock.post.calledWithExactly('https://someurl', payload, {}));
});
it('should use default axios config when proxy URL is invalid', async () => {
process.env.HTTPS_PROXY = 'invalid string';
const consoleSpy = sinon.spy(console, 'log');
await slackSend(fakeCore);

assert(consoleSpy.calledWith('failed to configure https proxy agent for http proxy. Using default axios configuration'));
assert(AxiosMock.post.calledWithExactly('https://someurl', payload, {}));
});
});
});
describe('sad path', () => {
it('should set an error if the POST to the webhook fails without a response', async () => {
Expand Down

0 comments on commit acb114f

Please sign in to comment.