Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env-cmdrc-template
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"GOOGLE_SKIP_AUTH": true,

"SWITCHER_API_LOGGER": false,
"SWITCHER_API_LOGGER_LEVEL": "debug",

"SWITCHER_API_ENABLE": false,
"SWITCHER_API_THROTTLE": 5000,
"SWITCHER_API_URL": "http://localhost:3000",
Expand Down
26 changes: 0 additions & 26 deletions .eslintrc.json

This file was deleted.

2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ services:
- BITBUCKET_OAUTH_SECRET=${BITBUCKET_OAUTH_SECRET}
- GOOGLE_RECAPTCHA_SECRET=${GOOGLE_RECAPTCHA_SECRET}
- GOOGLE_SKIP_AUTH=${GOOGLE_SKIP_AUTH}

- SWITCHER_API_LOGGER=${SWITCHER_API_LOGGER}
- SWITCHER_API_LOGGER_LEVEL=${SWITCHER_API_LOGGER_LEVEL}

- SWITCHER_API_ENABLE=${SWITCHER_API_ENABLE}
- SWITCHER_API_URL=${SWITCHER_API_URL}
Expand Down
24 changes: 24 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import js from "@eslint/js";
import globals from "globals";

export default [
{
...js.configs.recommended,
files: ["src/**/*.js"]
},
{
files: ["src/**/*.js"],
rules: {
quotes: ['error', 'single'],
semi: ['error', 'always'],
curly: ['error', 'multi-line'],
},
languageOptions: {
ecmaVersion: 2022,
sourceType: 'module',
globals: {
...globals.node
}
}
}
];
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@
"helmet": "^7.1.0",
"jsonwebtoken": "^9.0.2",
"moment": "^2.30.1",
"mongodb": "^6.6.1",
"mongoose": "^8.3.4",
"pino": "^9.0.0",
"mongodb": "^6.6.2",
"mongoose": "^8.4.0",
"pino": "^9.1.0",
"pino-pretty": "^11.0.0",
"swagger-ui-express": "^5.0.0",
"switcher-client": "^4.1.0",
"validator": "^13.12.0"
},
"devDependencies": {
"env-cmd": "^10.1.0",
"eslint": "^8.57.0",
"eslint": "^9.3.0",
"jest": "^29.7.0",
"jest-sonar-reporter": "^2.0.0",
"node-notifier": "^10.0.1",
"nodemon": "^3.1.0",
"sinon": "^17.0.2",
"sinon": "^18.0.0",
"supertest": "^7.0.0"
},
"overrides": {
Expand Down
5 changes: 5 additions & 0 deletions src/client/configuration-resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getEnvironments } from '../services/environment.js';
import { getSlack } from '../services/slack.js';
import { getDomainById } from '../services/domain.js';
import { getComponents } from '../services/component.js';
import Logger from '../helpers/logger.js';

async function resolveConfigByConfig(domainId, key, configId) {
const config = await getConfigs({
Expand Down Expand Up @@ -85,6 +86,7 @@ export async function resolveFlatConfigStrategy(source, context) {
context.admin, strategies, source.config[0].domain, ActionTypes.READ, RouterTypes.STRATEGY, true);
}
} catch (e) {
Logger.debug('resolveFlatConfigStrategy', e);
return null;
}

Expand All @@ -109,6 +111,7 @@ export async function resolveFlatConfig(source, context) {
context.admin, configs, domainId, ActionTypes.READ, RouterTypes.CONFIG, true);
}
} catch (e) {
Logger.debug('resolveFlatConfig', e);
return null;
}

Expand All @@ -130,6 +133,7 @@ export async function resolveFlatGroupConfig(source, context) {
context.admin, group, group[0].domain, ActionTypes.READ, RouterTypes.GROUP, true);
}
} catch (e) {
Logger.debug('resolveFlatGroupConfig', e);
return null;
}

Expand All @@ -149,6 +153,7 @@ export async function resolveFlatDomain(_source, context) {
context.admin, domain, domain._id, ActionTypes.READ, RouterTypes.DOMAIN, true);
}
} catch (e) {
Logger.debug('resolveFlatDomain', e);
return null;
}

Expand Down
2 changes: 2 additions & 0 deletions src/client/permission-resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { RouterTypes } from '../models/permission.js';
import { getConfigs } from '../services/config.js';
import { getGroupConfigs } from '../services/group-config.js';
import { permissionCache } from '../helpers/cache.js';
import Logger from '../helpers/logger.js';

export async function resolvePermission(args, admin) {
const cacheKey = permissionCache.permissionKey(admin._id, args.domain, args.parent,
Expand All @@ -27,6 +28,7 @@ export async function resolvePermission(args, admin) {
await verifyOwnership(admin, element, args.domain, action_perm, args.router, false, args.environment);
result[result.length - 1].permissions.push({ action: action_perm.toString(), result: 'ok' });
} catch (e) {
Logger.debug('resolvePermission', e);
result[result.length - 1].permissions.push({ action: action_perm.toString(), result: 'nok' });
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/client/relay/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import axios from 'axios';
import https from 'https';
import { StrategiesToRelayDataType, RelayMethods } from '../../models/config.js';
import { checkHttpsAgent } from '../../external/switcher-api-facade.js';
import Logger from '../../helpers/logger.js';

const agent = async (url) => {
const response = await checkHttpsAgent(url);
Expand Down Expand Up @@ -53,6 +54,7 @@ async function post(url, data, headers) {
try {
return await axios.post(url, data, { httpsAgent: await agent(url), headers });
} catch (error) {
Logger.debug('post', error);
throw new Error(`Failed to reach ${url} via POST`);
}
}
Expand All @@ -61,6 +63,7 @@ async function get(url, data, headers) {
try {
return await axios.get(`${url}${data}`, { httpsAgent: await agent(url), headers });
} catch (error) {
Logger.debug('get', error);
throw new Error(`Failed to reach ${url} via GET`);
}
}
Expand Down
19 changes: 11 additions & 8 deletions src/client/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export async function resolveConfigStrategy(source, _id, strategy, operation, ac
strategies = await verifyOwnership(context.admin, strategies, parentConfig.domain, ActionTypes.READ, RouterTypes.STRATEGY);
}
} catch (e) {
Logger.debug('resolveConfigStrategy', e);
return null;
}

Expand All @@ -60,19 +61,20 @@ export async function resolveConfig(source, _id, key, activated, context) {
if (_id) { args._id = _id; }
if (key) { args.key = key; }
if (context._component) { args.components = context._component; }

let configs = await Config.find({ group: source._id, ...args }).lean().exec();

if (activated !== undefined) {
configs = configs.filter(config => config.activated[context.environment] === activated);
}

try {
if (context.admin) {
let parentGroup = await GroupConfig.findById(source._id).exec();
configs = await verifyOwnership(context.admin, configs, parentGroup.domain, ActionTypes.READ, RouterTypes.CONFIG, true);
}
} catch (e) {
Logger.debug('resolveConfig', e);
return null;
}

Expand All @@ -96,6 +98,7 @@ export async function resolveGroupConfig(source, _id, name, activated, context)
groups = await verifyOwnership(context.admin, groups, source._id, ActionTypes.READ, RouterTypes.GROUP, true);
}
} catch (e) {
Logger.debug('resolveGroupConfig', e);
return null;
}

Expand All @@ -119,17 +122,16 @@ export async function resolveDomain(_id, name, activated, context) {
}

let domain = await Domain.findOne({ ...args }).lean().exec();
if (activated !== undefined) {
if (domain.activated[context.environment] !== activated) {
return null;
}
if (activated !== undefined && domain.activated[context.environment] !== activated) {
return null;
}

try {
if (context.admin) {
domain = await verifyOwnership(context.admin, domain, domain._id, ActionTypes.READ, RouterTypes.DOMAIN, true);
}
} catch (e) {
Logger.debug('resolveDomain', e);
return null;
}

Expand Down Expand Up @@ -222,8 +224,9 @@ function checkFlags(config, group, domain, environment) {
async function checkStrategy(entry, strategies, environment) {
if (strategies) {
for (const strategy of strategies) {
if (!strategy.activated[environment])
if (!strategy.activated[environment]) {
continue;
}

await checkStrategyInput(entry, strategy);
}
Expand Down
6 changes: 4 additions & 2 deletions src/external/google-recaptcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import axios from 'axios';
export const url = 'https://www.google.com/recaptcha/api/siteverify';

export async function validate_token(token, remoteAddress) {
if (!process.env.GOOGLE_RECAPTCHA_SECRET || process.env.GOOGLE_SKIP_AUTH == 'true')
if (!process.env.GOOGLE_RECAPTCHA_SECRET || process.env.GOOGLE_SKIP_AUTH == 'true') {
return;
}

if (token === null || token === undefined)
if (token === null || token === undefined) {
throw new GoogleRecaptchaError('Token is empty or invalid');
}

const response = await axios.post(
`${url}?secret=${process.env.GOOGLE_RECAPTCHA_SECRET}&response=${token}&remoteip=${remoteAddress}`, null,
Expand Down
5 changes: 4 additions & 1 deletion src/external/oauth-bitbucket.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios from 'axios';
import Logger from '../helpers/logger.js';

export const bitBucketAccessTokenUrl = 'https://bitbucket.org/site/oauth2/access_token';
export const bitBucketAPIUserUrl = 'https://api.bitbucket.org/2.0/user';
Expand All @@ -22,6 +23,7 @@ export async function getBitBucketToken(code) {

return response.data.access_token;
} catch (error) {
Logger.debug('getBitBucketToken', error);
throw new BitBucketAuthError('Failed to get Bitbucket access token');
}
}
Expand All @@ -42,7 +44,8 @@ export async function getBitBucketUserInfo(token) {
avatar: response.data?.links?.avatar?.href
};
} catch (error) {
throw new BitBucketAuthError('Failed to get Bitbucket user info');
Logger.debug('getBitBucketUserInfo', error);
throw new BitBucketAuthError('Failed to get Bitbucket user info');
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/external/oauth-git.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios from 'axios';
import Logger from '../helpers/logger.js';

export const githubAccessTokenUrl = 'https://github.com/login/oauth/access_token';
export const githubAPIUserUrl = 'https://api.github.com/user';
Expand All @@ -15,6 +16,7 @@ export async function getGitToken(code) {

return response.data.access_token;
} catch (error) {
Logger.debug('getGitToken', error);
throw new GitAuthError('Failed to get GitHub access token');
}
}
Expand All @@ -35,7 +37,8 @@ export async function getGitUserInfo(token) {
avatar: response.data.avatar_url
};
} catch (error) {
throw new GitAuthError('Failed to get GitHub user info');
Logger.debug('getGitUserInfo', error);
throw new GitAuthError('Failed to get GitHub user info');
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/external/sendgrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ export function sendAccountRecoveryCode(email, name, code) {
}

function sendMail(email, name, code, template_id) {
if (!process.env.SENDGRID_API_KEY)
if (!process.env.SENDGRID_API_KEY) {
return;
}

axios.post(sendGridApiUrl, {
from: { email: process.env.SENDGRID_MAIL_FROM },
Expand Down
Loading