From 6de9111bf6345e8e78b0545fa7295505a8dc75e3 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Thu, 22 Jun 2023 23:40:26 -0700 Subject: [PATCH 1/2] Relay: added possibility to accept self-signed cert --- src/client/relay/index.js | 11 +++++-- src/external/switcher-api-facade.js | 10 +++++- tests/relay.test.js | 2 +- tests/unit-test/client/relay.test.js | 46 ++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 tests/unit-test/client/relay.test.js diff --git a/src/client/relay/index.js b/src/client/relay/index.js index 0a97f9b..5629d6c 100644 --- a/src/client/relay/index.js +++ b/src/client/relay/index.js @@ -1,5 +1,12 @@ import axios from 'axios'; +import https from 'https'; import { StrategiesToRelayDataType, RelayMethods } from '../../models/config'; +import { checkHttpsAgent } from '../../external/switcher-api-facade'; + +const agent = async (url) => { + const rejectUnauthorized = !(await checkHttpsAgent(url)); + return new https.Agent({ rejectUnauthorized }); +}; export function resolveNotification(relay, entry, environment) { const url = relay.endpoint[environment]; @@ -41,7 +48,7 @@ export async function resolveVerification(relay, environment) { async function post(url, data, headers) { try { - return await axios.post(url, data, headers); + return await axios.post(url, data, { httpsAgent: agent(url) }, headers); } catch (error) { throw new Error(`Failed to reach ${url} via POST`); } @@ -49,7 +56,7 @@ async function post(url, data, headers) { async function get(url, data, headers) { try { - return await axios.get(`${url}${data}`, headers); + return await axios.get(`${url}${data}`, { httpsAgent: agent(url) }, headers); } catch (error) { throw new Error(`Failed to reach ${url} via GET`); } diff --git a/src/external/switcher-api-facade.js b/src/external/switcher-api-facade.js index 361dc98..91ca5cf 100644 --- a/src/external/switcher-api-facade.js +++ b/src/external/switcher-api-facade.js @@ -26,7 +26,8 @@ export const SwitcherKeys = Object.freeze({ ACCOUNT_OUT_NOTIFY: 'ACCOUNT_OUT_NOTIFY', SLACK_INTEGRATION: 'SLACK_INTEGRATION', SLACK_UPDATE: 'SLACK_UPDATE', - RATE_LIMIT: 'RATE_LIMIT' + RATE_LIMIT: 'RATE_LIMIT', + HTTPS_AGENT: 'HTTPS_AGENT' }); function switcherFlagResult(flag, message) { @@ -220,4 +221,11 @@ export async function getRateLimit(key, component) { } return parseInt(process.env.MAX_REQUEST_PER_MINUTE || DEFAULT_RATE_LIMIT); +} + +export async function checkHttpsAgent(value) { + if (process.env.SWITCHER_API_ENABLE != 'true') + return; + + return await checkFeature(SwitcherKeys.HTTPS_AGENT, [checkValue(value)]); } \ No newline at end of file diff --git a/tests/relay.test.js b/tests/relay.test.js index 48797f3..f5de809 100644 --- a/tests/relay.test.js +++ b/tests/relay.test.js @@ -20,7 +20,7 @@ import { adminMasterAccountId } from './fixtures/db_api'; import { StrategiesType, ConfigStrategy } from '../src/models/config-strategy'; const changeStrategy = async (strategyId, newOperation, status, environment) => { - const strategy = await ConfigStrategy.findById(strategyId).exec() + const strategy = await ConfigStrategy.findById(strategyId).exec(); strategy.operation = newOperation ? newOperation : strategy.operation; strategy.activated.set(environment, status !== undefined ? status : strategy.activated.get(environment)); strategy.updatedBy = adminMasterAccountId; diff --git a/tests/unit-test/client/relay.test.js b/tests/unit-test/client/relay.test.js new file mode 100644 index 0000000..69d122a --- /dev/null +++ b/tests/unit-test/client/relay.test.js @@ -0,0 +1,46 @@ +import { resolveValidation } from '../../../src/client/relay'; +import sinon from 'sinon'; +import axios from 'axios'; +import { RelayMethods } from '../../../src/models/config'; +import { StrategiesType } from '../../../src/models/config-strategy'; +import { EnvType } from '../../../src/models/environment'; +import { Switcher } from 'switcher-client'; + +describe('Testing Client Relay', () => { + + let axiosStub; + + beforeAll(() => { + process.env.SWITCHER_API_ENABLE = true; + }); + + test('CLIENT_RELAY_SUITE - Should resolve validation', async () => { + // Mock + axiosStub = sinon.stub(axios, 'get'); + + // Given + const mockRelayService = { data: { result: true, reason: 'Success' } }; + axiosStub.returns(Promise.resolve(mockRelayService)); + Switcher.assume('HTTPS_AGENT').true(); + + const relay = { + endpoint: { + default: 'https://localhost' + }, + method: RelayMethods.GET, + auth_prefix: 'Bearer', + auth_token: { + default: 'token' + } + }; + + const entry = [{ + strategy: StrategiesType.VALUE, + input: 'test' + }]; + + const res = await resolveValidation(relay, entry, EnvType.DEFAULT); + expect(res.result).toBe(true); + }); + +}); \ No newline at end of file From dd192962ff2e998f6d477fdc35e0d34d87fbdb1e Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Thu, 22 Jun 2023 23:46:20 -0700 Subject: [PATCH 2/2] Fixed code smells --- tests/slack.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/slack.test.js b/tests/slack.test.js index 3bf5949..cb71258 100644 --- a/tests/slack.test.js +++ b/tests/slack.test.js @@ -21,7 +21,7 @@ import { import { TicketValidationType } from '../src/models/slack_ticket'; afterAll(async () => { - await Slack.deleteMany(); + await Slack.deleteMany().exec(); await new Promise(resolve => setTimeout(resolve, 1000)); await mongoose.disconnect(); }); @@ -840,7 +840,7 @@ describe('Slack Route - Process Ticket', () => { test('SLACK_SUITE - Should reset installation tickets', async () => { //verify that - let slackInstallation = await Slack.findOne({ team_id: slack.team_id }); + let slackInstallation = await Slack.findOne({ team_id: slack.team_id }).exec(); expect(slackInstallation.tickets.length).toBeGreaterThan(0); //test @@ -851,7 +851,7 @@ describe('Slack Route - Process Ticket', () => { team_id: slack.team_id }).expect(200); - slackInstallation = await Slack.findOne({ team_id: slack.team_id }); + slackInstallation = await Slack.findOne({ team_id: slack.team_id }).exec(); expect(slackInstallation.tickets.length).toEqual(0); });