From e90fae47aa5f31286475cf021ef160d8cf408454 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Sun, 12 May 2024 19:12:32 -0700 Subject: [PATCH 1/3] chore: fix Sonar lcov --- sonar-project.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sonar-project.properties b/sonar-project.properties index 8983c27..59ca1ad 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -5,7 +5,7 @@ sonar.projectVersion=1.0.1 sonar.links.homepage=https://github.com/switcherapi/switcher-resolver-node sonar.testExecutionReportPaths=test-report.xml -sonar.javascript.lcov.reportPaths=coverage/lcov.info +sonar.javascript.lcov.reportPaths=./coverage/lcov.info # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. sonar.sources=src From a08fbcfb1ec4516d3decdbf3e93ae98dedb792b8 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Sun, 12 May 2024 19:20:08 -0700 Subject: [PATCH 2/3] added new route --- src/app.js | 2 + src/routers/server-api.js | 105 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 src/routers/server-api.js diff --git a/src/app.js b/src/app.js index 4fba4f3..19776f0 100644 --- a/src/app.js +++ b/src/app.js @@ -9,6 +9,7 @@ import './db/mongoose.js'; import mongoose from 'mongoose'; import swaggerDocument from './api-docs/swagger-document.js'; import clientApiRouter from './routers/client-api.js'; +import serverApiRouter from './routers/server-api.js'; import schema from './client/schema.js'; import { appAuth, resourcesAuth } from './middleware/auth.js'; import { clientLimiter, defaultLimiter } from './middleware/limiter.js'; @@ -28,6 +29,7 @@ app.disable('x-powered-by'); * API Routes */ app.use(clientApiRouter); +app.use(serverApiRouter); /** * GraphQL Routes diff --git a/src/routers/server-api.js b/src/routers/server-api.js new file mode 100644 index 0000000..ac5ae41 --- /dev/null +++ b/src/routers/server-api.js @@ -0,0 +1,105 @@ +import express from 'express'; +import { body, check, query, header } from 'express-validator'; +import jwt from 'jsonwebtoken'; +import { checkConfig, checkConfigComponent, validate } from '../middleware/validators.js'; +import { appAuth, appGenerateCredentials } from '../middleware/auth.js'; +import { resolveCriteria, checkDomain } from '../client/resolvers.js'; +import { getConfigs } from '../services/config.js'; +import { clientLimiter } from '../middleware/limiter.js'; +import { StrategiesType } from '../models/config-strategy.js'; + +const router = new express.Router(); + +// GET /check?key=KEY +// GET /check?key=KEY&showReason=true +// GET /check?key=KEY&showStrategy=true +// GET /check?key=KEY&bypassMetric=true +router.post('/api/criteria', appAuth, clientLimiter, [ + query('key').isLength({ min: 1 }).withMessage('Key is required'), + body('entry').isArray().optional().withMessage('Entry must be an array'), + body('entry.*.input').isString(), + body('entry.*.strategy').isString() + .custom(value => Object.values(StrategiesType).includes(value)).withMessage('Invalid strategy type') +], validate, checkConfig, checkConfigComponent, async (req, res) => { + try { + const environment = req.environment; + const domain = req.domain; + const entry = req.body.entry; + + const context = { domain, entry, environment, bypassMetric: req.query.bypassMetric, component: req.component }; + + const response = await resolveCriteria(req.config, context, 'values description strategy operation activated -_id'); + + delete response.domain; + delete response.group; + + if ((req.query.showReason || 'false') === 'false') { + delete response.reason; + } + + if ((req.query.showStrategy || 'false') === 'false') { + delete response.strategies; + } + + res.send(response); + } catch (e) { + res.status(500).send({ error: e.message }); + } +}); + +router.get('/api//criteria/snapshot_check/:version', appAuth, clientLimiter, [ + check('version', 'Wrong value for domain version').isNumeric() +], validate, async (req, res) => { + try { + const domain = await checkDomain(req.domain); + const version = req.params.version; + + if (domain.lastUpdate > version) { + res.send({ status: false }); + } else { + res.send({ status: true }); + } + } catch (e) { + res.status(500).send({ error: e.message }); + } +}); + +router.post('/api//criteria/switchers_check', appAuth, clientLimiter, [ + check('switchers', 'Switcher Key is required').isArray().isLength({ min: 1 }) +], validate, async (req, res) => { + try { + const configsFound = await getConfigs({ domain: req.domain, components: req.componentId }); + const configs = configsFound.map(config => config.key); + res.send({ not_found: req.body.switchers.filter(switcher => !configs.includes(String(switcher))) }); + } catch (e) { + res.status(500).send({ error: e.message }); + } +}); + +router.post('/api//criteria2/switchers_check', appAuth, clientLimiter, [ + check('switchers', 'Switcher Key is required').isArray().isLength({ min: 1 }) +], validate, async (req, res) => { + try { + const configsFound = await getConfigs({ domain: req.domain, components: req.componentId }); + const configs = configsFound.map(config => config.key); + res.send({ not_found: req.body.switchers.filter(switcher => !configs.includes(String(switcher))) }); + } catch (e) { + res.status(500).send({ error: e.message }); + } +}); + +router.post('/api//criteria/auth', [ + header('switcher-api-key').isString().withMessage('API Key header is required'), + body('domain').isString().withMessage('Domain is required'), + body('component').isString().withMessage('Component is required'), + body('environment').isString().withMessage('Environment is required') +], validate, appGenerateCredentials, clientLimiter, async (req, res) => { + try { + const { exp } = jwt.decode(req.token); + res.send({ token: req.token, exp }); + } catch (e) { + res.status(400).send({ error: e.message }); + } +}); + +export default router; \ No newline at end of file From 4534245cd8312650ecc6cc4b09375e8aed47b1ff Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Sun, 12 May 2024 19:22:56 -0700 Subject: [PATCH 3/3] chore: fix Sonar lcov --- sonar-project.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sonar-project.properties b/sonar-project.properties index 59ca1ad..8983c27 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -5,7 +5,7 @@ sonar.projectVersion=1.0.1 sonar.links.homepage=https://github.com/switcherapi/switcher-resolver-node sonar.testExecutionReportPaths=test-report.xml -sonar.javascript.lcov.reportPaths=./coverage/lcov.info +sonar.javascript.lcov.reportPaths=coverage/lcov.info # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. sonar.sources=src