5
0
Fork 0
mirror of https://github.com/hashicorp/vault-action.git synced 2025-11-07 15:16:56 +00:00

add proxy support through global-agent, add proxy tests, update README

This commit is contained in:
Uwe Schumacher 2025-07-04 10:13:48 +02:00
parent 4c06c5ccf5
commit 1c8fc1b2a9
2 changed files with 96 additions and 0 deletions

24
src/proxy.js Normal file
View file

@ -0,0 +1,24 @@
const {bootstrap} = require('global-agent');
const core = require("@actions/core");
const MAJOR_NODEJS_VERSION = parseInt(process.version.slice(1).split('.')[0], 10);
/**
* Will enable global-agent proxy if the necessary
* [global-agent environment variables](https://github.com/gajus/global-agent?tab=readme-ov-file#environment-variables)
* has been set.
* Important note: `global-agent` only works with Node.js v10 and above.
* Node.js versions below v10 are not supported
*
* The bootstrap routine guards against multiple initializations of global-agent
*/
function enableProxy() {
if (MAJOR_NODEJS_VERSION >= 10) {
bootstrap();
} else {
core.debug('proxy configuration does not work with Node.js below v10')
}
}
module.exports = {
enableProxy
}

72
src/proxy.test.js Normal file
View file

@ -0,0 +1,72 @@
const {enableProxy } = require('./proxy')
const got = require('got');
describe('global-agent proxy support works as expected', () => {
it('should fail with a request specific RequestError if got tries to request any-host without proxy', async () => {
const expectedRequestErrorMessage = `getaddrinfo ENOTFOUND any-host`;
try {
await got.get('http://any-host')
throw new Error('Test fail if the got request succeeds against expectation');
} catch(e) {
expect(e).toBeDefined();
expect(e.message).toEqual(expectedRequestErrorMessage);
expect(e.code).toEqual('ENOTFOUND');
}
});
it('should fail with a proxy specific RequestError if got tries to request through the defined proxy', async () => {
const PROXY_HOST = "my-test-proxy-server";
process.env.GLOBAL_AGENT_HTTP_PROXY = `http://${PROXY_HOST}:8000`;
enableProxy();
const expectedRequestErrorMessage = `getaddrinfo ENOTFOUND ${PROXY_HOST}`;
try {
await got.get('http://any-host')
throw new Error('Test fail if the got request succeeds against expectation');
} catch(e) {
expect(e).toBeDefined();
expect(e.message).toEqual(expectedRequestErrorMessage);
expect(e.code).toEqual('ENOTFOUND');
}
});
it('should fail with a request specific RequestError if HTTP_PROXY URL is undefined', async () => {
// set proxy url to undefined at runtime
global.GLOBAL_AGENT.HTTP_PROXY = undefined;
const expectedRequestErrorMessage = `getaddrinfo ENOTFOUND any-host`;
try {
await got.get('http://any-host')
throw new Error('Test fail if the got request succeeds against expectation');
} catch(e) {
expect(e).toBeDefined();
expect(e.message).toEqual(expectedRequestErrorMessage);
expect(e.code).toEqual('ENOTFOUND');
}
});
it('should fail with a request specific RequestError if HTTP_PROXY URL is empty', async () => {
// set proxy url to empty string at runtime
global.GLOBAL_AGENT.HTTP_PROXY = '';
const expectedRequestErrorMessage = `getaddrinfo ENOTFOUND any-host`;
try {
await got.get('http://any-host')
throw new Error('Test fail if the got request succeeds against expectation');
} catch(e) {
expect(e).toBeDefined();
expect(e.message).toEqual(expectedRequestErrorMessage);
expect(e.code).toEqual('ENOTFOUND');
}
});
it('should fail with UNEXPECTED_STATE_ERROR if HTTP_PROXY URL protocol does not start with "http:"', async () => {
// set proxy url to string with blanks at runtime
global.GLOBAL_AGENT.HTTP_PROXY = 'file://';
try {
await got.get('http://any-host')
throw new Error('Test fail if the got request succeeds against expectation');
} catch(e) {
expect(e).toBeDefined();
expect(e.code).toEqual('UNEXPECTED_STATE_ERROR');
}
});
})