diff --git a/package.json b/package.json index 450b79f..23bb556 100644 --- a/package.json +++ b/package.json @@ -50,11 +50,11 @@ "jsonwebtoken": "^9.0.2", "moment": "^2.30.1", "mongodb": "^6.5.0", - "mongoose": "^8.3.0", + "mongoose": "^8.3.2", "pino": "^8.20.0", "pino-pretty": "^11.0.0", "swagger-ui-express": "^5.0.0", - "switcher-client": "^3.2.1", + "switcher-client": "^4.0.2", "validator": "^13.11.0" }, "devDependencies": { diff --git a/src/client/relay/index.js b/src/client/relay/index.js index d96b5b5..02ecc44 100644 --- a/src/client/relay/index.js +++ b/src/client/relay/index.js @@ -4,8 +4,8 @@ import { StrategiesToRelayDataType, RelayMethods } from '../../models/config.js' import { checkHttpsAgent } from '../../external/switcher-api-facade.js'; const agent = async (url) => { - const rejectUnauthorized = !(await checkHttpsAgent(url)); - return new https.Agent({ rejectUnauthorized }); + const response = await checkHttpsAgent(url); + return new https.Agent({ rejectUnauthorized: !(response?.result) }); }; export async function resolveNotification(relay, entry, environment) { diff --git a/src/external/switcher-api-facade.js b/src/external/switcher-api-facade.js index 814dde9..21115b2 100644 --- a/src/external/switcher-api-facade.js +++ b/src/external/switcher-api-facade.js @@ -26,18 +26,19 @@ export const SwitcherKeys = Object.freeze({ ACCOUNT_OUT_NOTIFY: 'ACCOUNT_OUT_NOTIFY', SLACK_INTEGRATION: 'SLACK_INTEGRATION', RATE_LIMIT: 'RATE_LIMIT', - HTTPS_AGENT: 'HTTPS_AGENT' + HTTPS_AGENT: 'HTTPS_AGENT', + SWITCHER_AC_METADATA: 'SWITCHER_AC_METADATA' }); -function switcherFlagResult(flag, message) { - if (!flag) { +function switcherFlagResult(response, message) { + if (!response.result) { throw new FeatureUnavailableError(message); } } async function checkFeature(feature, params) { const switcher = Switcher.factory(); - return switcher.isItOn(feature, params, true); + return switcher.detail().isItOn(feature, params); } export async function checkDomain(req) { @@ -133,14 +134,14 @@ export async function checkMetrics(config) { return true; const { owner } = await getDomainById(config.domain); - const flag = await checkFeature(SwitcherKeys.ELEMENT_CREATION, [ + const response = await checkFeature(SwitcherKeys.ELEMENT_CREATION, [ checkPayload(JSON.stringify({ feature: 'metrics', owner })) ]); - if (!flag) { + if (!response.result) { if (!config.disable_metrics) { config.disable_metrics = new Map(); config.disable_metrics.set(EnvType.DEFAULT, true); @@ -180,9 +181,9 @@ export async function checkSlackIntegration(value) { return; switcherFlagResult( - await checkFeature(SwitcherKeys.SLACK_INTEGRATION, [ - checkValue(value) - ]), 'Slack Integration is not available.'); + await checkFeature(SwitcherKeys.SLACK_INTEGRATION, [ + checkValue(value) + ]), 'Slack Integration is not available.'); } export function notifyAcCreation(adminid) { @@ -205,16 +206,21 @@ export function notifyAcDeletion(adminid) { export async function getRateLimit(key, component) { if (process.env.SWITCHER_API_ENABLE === 'true' && key !== process.env.SWITCHER_API_KEY) { + const fromMetadata = await checkFeature(SwitcherKeys.SWITCHER_AC_METADATA); const domain = await getDomainById(component.domain); - const result = await checkFeature(SwitcherKeys.RATE_LIMIT, [ + const response = await checkFeature(SwitcherKeys.RATE_LIMIT, [ checkValue(String(domain.owner)) ]); - if (result) { - const log = Switcher.getLogger(SwitcherKeys.RATE_LIMIT) - .find(log => log.input[0][1] === String(domain.owner)); + if (response.result) { + if (!fromMetadata.result) { + const log = Switcher.getLogger(SwitcherKeys.RATE_LIMIT) + .find(log => log.input[0][1] === String(domain.owner)); - return JSON.parse(log.response.message).rate_limit; + return JSON.parse(log.response.message).rate_limit; + } + + return response.metadata.rate_limit; } } diff --git a/tests/unit-test/switcher-api-facade.test.js b/tests/unit-test/switcher-api-facade.test.js index 5fffbb0..762b8ac 100644 --- a/tests/unit-test/switcher-api-facade.test.js +++ b/tests/unit-test/switcher-api-facade.test.js @@ -180,19 +180,19 @@ describe('Testing Switcher API Facade', () => { test('UNIT_API_FACADE - Should enable feature - History', async () => { //given Switcher.assume(SwitcherKeys.ELEMENT_CREATION).true(); - const resultFeature = await checkHistory(domainId); + const response = await checkHistory(domainId); //test - expect(resultFeature).toBe(true); + expect(response.result).toBe(true); }); test('UNIT_API_FACADE - Should NOT enable feature - History', async () => { //given Switcher.assume(SwitcherKeys.ELEMENT_CREATION).false(); - const resultFeature = await checkHistory(domainId); + const response = await checkHistory(domainId); //test - expect(resultFeature).toBe(false); + expect(response.result).toBe(false); }); test('UNIT_API_FACADE - Should enable feature - Sign up new Account', async () => { @@ -243,12 +243,22 @@ describe('Testing Switcher API Facade', () => { test('UNIT_API_FACADE - Should read rate limit - 100 Request Per Minute', async () => { const call = async () => { + Switcher.assume(SwitcherKeys.SWITCHER_AC_METADATA).true(); + Switcher.assume(SwitcherKeys.RATE_LIMIT).true().withMetadata({ rate_limit: 100 }); + return getRateLimit(component1Key, component1); + }; + + await expect(call()).resolves.toBe(100); + }); + + test('UNIT_API_FACADE - Should read rate limit - 100 Request Per Minute - using messgae (depracated)', async () => { + const call = async () => { + Switcher.assume(SwitcherKeys.SWITCHER_AC_METADATA).false(); Switcher.assume(SwitcherKeys.RATE_LIMIT).true(); - ExecutionLogger.add( - { message: JSON.stringify({ rate_limit: 100 }) }, - SwitcherKeys.RATE_LIMIT, - [checkValue(domainDocument.owner.toString())] - ); + + ExecutionLogger.add({ message: JSON.stringify({ rate_limit: 100 }) }, SwitcherKeys.RATE_LIMIT, [ + checkValue(domainDocument.owner.toString()) + ]); return getRateLimit(component1Key, component1); }; @@ -258,6 +268,7 @@ describe('Testing Switcher API Facade', () => { test('UNIT_API_FACADE - Should NOT read rate limit - Default Request Per Minute', async () => { const call = async () => { + Switcher.assume(SwitcherKeys.SWITCHER_AC_METADATA).true(); Switcher.assume(SwitcherKeys.RATE_LIMIT).false(); return getRateLimit(component1Key, component1); };