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
25 changes: 12 additions & 13 deletions src/aggregator/permission-resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,23 @@ export async function resolvePermission(args, admin) {

let elements = await getElements(args.domain, args.parent, args.router);

let result = [];
for (const element of elements) {
result.push({
id: element._id,
name: element.name || element.key,
permissions: []
});

for (const action_perm of args.actions) {
const result = await Promise.all(elements.map(async (element) => {
const permissions = await Promise.all(args.actions.map(async (action_perm) => {
try {
await verifyOwnership(admin, element, args.domain, action_perm, args.router, false, args.environment);
result.at(-1).permissions.push({ action: action_perm.toString(), result: 'ok' });
return { action: action_perm.toString(), result: 'ok' };
} catch (e) {
Logger.debug('resolvePermission', e);
result.at(-1).permissions.push({ action: action_perm.toString(), result: 'nok' });
return { action: action_perm.toString(), result: 'nok' };
}
}
}
}));

return {
id: element._id,
name: element.name || element.key,
permissions
};
}));

if (result.length) {
permissionCache.set(cacheKey, result);
Expand Down
17 changes: 5 additions & 12 deletions src/aggregator/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,12 @@
async function resolveComponentsFirst(source, context, groups) {
if (context._component) {
const component = await Component.findOne({ domain: source._id, name: context._component }).exec();
const validGroups = [];

context._component = component?._id;
for (const group of groups) {
let configsLength = await Config.find({
domain: source._id, group: group._id, components: context._component
}).countDocuments().exec();

if (configsLength) {
validGroups.push(group);
}
}
return validGroups;
const configsLengths = await Promise.all(groups.map(group => Config.find({

Check failure on line 169 in src/aggregator/resolvers.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

NoSQL Injection via unsanitized user input

See more on https://sonarcloud.io/project/issues?id=switcherapi_switcher-api&issues=AaEJKpSrdJ77iHnxokOJ&open=AaEJKpSrdJ77iHnxokOJ&pullRequest=588
Comment thread
sonarqubecloud[bot] marked this conversation as resolved.
Dismissed
domain: source._id, group: group._id, components: context._component
}).countDocuments().exec()));

return groups.filter((_, index) => configsLengths[index]);
}
return groups;
}
19 changes: 4 additions & 15 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,10 @@ export async function verifyOwnership(admin, element, domainId, actions, routerT
}

const actionsArray = Array.isArray(actions) ? actions : [actions];
let hasPermission = [];
let allowedElement;

// Verify each team permission
for (const team of teams) {
if (cascade) {
allowedElement = await verifyPermissionsCascade(team, element, actionsArray, routerType, environment);
} else {
allowedElement = await verifyPermissions(team, element, actionsArray, routerType, environment);
}

if (allowedElement) {
hasPermission.push(allowedElement);
}
}
let hasPermission = (await Promise.all(teams.map((team) => cascade
? verifyPermissionsCascade(team, element, actionsArray, routerType, environment)
: verifyPermissions(team, element, actionsArray, routerType, environment)
))).filter(Boolean);

if (!hasPermission.length) {
throw new PermissionError('Action forbidden');
Expand Down
2 changes: 1 addition & 1 deletion src/models/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ adminSchema.pre('deleteOne', { document: true, query: false }, async function ()
for (const team of teams) {
let indexMmeber = team.members.indexOf(this._id);
team.members.splice(indexMmeber, 1);
await team.save();
}

await Promise.all(teams.map(team => team.save()));
notifyAcDeletion(this._id);
});

Expand Down
6 changes: 3 additions & 3 deletions src/models/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ componentSchema.pre('validate', async function () {

componentSchema.pre('deleteOne', { document: true, query: false }, async function () {
const configsToRemoveFrom = await Config.find({ components: { $in: [this._id] } }).exec();
for (const config of configsToRemoveFrom) {
await Promise.all(configsToRemoveFrom.map(async (config) => {
const indexValue = config.components.indexOf(this._id);
config.components.splice(indexValue, 1);
config.save();
}
await config.save();
}));
});

const Component = mongoose.model('Component', componentSchema);
Expand Down
2 changes: 1 addition & 1 deletion src/models/config-strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ configStrategySchema.pre('save', async function () {
}

const operations = StrategyRequirementDefinition.find(element => element.strategy === strategy).operations;
const foundOperation = operations.find((element) => element === operationStrategy);
const foundOperation = operations.includes(operationStrategy);

// Verify strategy operation requirements
if (!foundOperation) {
Expand Down
4 changes: 1 addition & 3 deletions src/models/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,7 @@ configSchema.virtual('configStrategy', {
configSchema.pre('deleteOne', { document: true, query: false }, async function () {
const strategies = await ConfigStrategy.find({ config: this._id }).exec();
if (strategies) {
for (const strategy of strategies) {
await Promise.resolve(strategy.deleteOne());
}
await Promise.all(strategies.map(strategy => strategy.deleteOne()));
}

await History.deleteMany({ domainId: this.domain, elementId: this._id }).exec();
Expand Down
8 changes: 2 additions & 6 deletions src/models/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,12 @@ domainSchema.pre('deleteOne', { document: true, query: false }, async function (
const groups = await GroupConfig.find({ domain: this._id }).exec();

if (groups) {
for (const group of groups) {
await Promise.resolve(group.deleteOne());
}
await Promise.all(groups.map(group => group.deleteOne()));
}

const teams = await Team.find({ domain: this._id }).exec();
if (teams) {
for (const team of teams) {
await Promise.resolve(team.deleteOne());
}
await Promise.all(teams.map(team => team.deleteOne()));
}

await Promise.all([
Expand Down
4 changes: 1 addition & 3 deletions src/models/group-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ groupConfigSchema.pre('deleteOne', { document: true, query: false }, async funct
const configs = await Config.find({ group: this._id }).exec();

if (configs) {
for (const config of configs) {
await Promise.resolve(config.deleteOne());
}
await Promise.all(configs.map(config => config.deleteOne()));
}

await History.deleteMany({ domainId: this.domain, elementId: this._id }).exec();
Expand Down
3 changes: 2 additions & 1 deletion src/models/team.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ teamSchema.pre('deleteOne', { document: true, query: false }, async function ()
for (const member of membersToRemve) {
const indexValue = member.teams.indexOf(this._id);
member.teams.splice(indexValue, 1);
member.save();
}

await Promise.all(membersToRemve.map((member) => member.save()));
});

export const Team = mongoose.model('Team', teamSchema);
9 changes: 4 additions & 5 deletions src/routers/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,16 @@ router.post('/admin/collaboration/permission', auth, [
return res.send(permissionCache.get(cacheKey));
}

let result = [];
for (const action_perm of req.body.action) {
const result = await Promise.all(req.body.action.map(async action_perm => {
try {
await verifyOwnership(req.admin, element, req.body.domain, action_perm,
req.body.router, false, req.body.environment);
result.push({ action: action_perm, result: 'ok' });
return { action: action_perm, result: 'ok' };
} catch (e) {
Logger.debug('resolvePermission', e);
result.push({ action : action_perm, result: 'nok' });
return { action : action_perm, result: 'nok' };
}
}
}));

if (result.length) {
permissionCache.set(cacheKey, result);
Expand Down
5 changes: 3 additions & 2 deletions src/services/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,14 @@ export async function leaveDomain(domainid, admin) {
for (const admin_team of teams) {
let indexMmeber = admin_team.members.indexOf(admin.id);
admin_team.members.splice(indexMmeber, 1);
await admin_team.save();

let indexTeam = admin.teams.indexOf(admin_team._id);
admin.teams.splice(indexTeam, 1);
await saveAdmin(admin);
}

await Promise.all(teams.map((admin_team) => admin_team.save()));
await saveAdmin(admin);

return admin;
}

Expand Down
22 changes: 11 additions & 11 deletions src/services/config-strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ async function verifyStrategyValueInput(strategyId, value) {
}

function validateValues(values) {
const invalidValue = values.find(value => value.length > 128);
if (invalidValue) {
const hasInvalidValue = values.some(value => value.length > 128);
if (hasInvalidValue) {
throw new BadRequestError('Value cannot be longer than 128 characters');
}
}
Expand All @@ -44,7 +44,7 @@ export async function getStrategies(where, lean = false) {
}

export async function populateAdmin(configStrategies) {
for (const configStrategy of configStrategies) {
for await (const configStrategy of configStrategies) {
await configStrategy.populate({ path: 'admin', select: 'name' });
}
}
Expand All @@ -70,7 +70,7 @@ export async function createStrategy(args, admin, status = true) {
RouterTypes.STRATEGY, false, environment.name);

await configStrategy.save();
updateDomainVersion(configStrategy.domain);
await updateDomainVersion(configStrategy.domain);

return configStrategy;
}
Expand All @@ -81,7 +81,7 @@ export async function deleteStrategy(id, admin) {
RouterTypes.STRATEGY, false, configStrategy.activated.keys().next().value);

await configStrategy.deleteOne();
updateDomainVersion(configStrategy.domain);
await updateDomainVersion(configStrategy.domain);

// resets permission cache
permissionCache.permissionReset(configStrategy.domain, ActionTypes.ALL, RouterTypes.STRATEGY, configStrategy.name);
Expand All @@ -98,7 +98,7 @@ export async function updateStrategy(id, args, admin) {
const updates = Object.keys(args);
updates.forEach((update) => configStrategy[update] = args[update]);
await configStrategy.save();
updateDomainVersion(configStrategy.domain);
await updateDomainVersion(configStrategy.domain);

return configStrategy;
}
Expand All @@ -107,7 +107,7 @@ export async function addVal(id, args, admin) {
let configStrategy = await verifyStrategyValueInput(id, args.value);

const value = args.value.trim();
const foundExistingOne = configStrategy.values.find((element) => element === value);
const foundExistingOne = configStrategy.values.includes(value);

validateValues([value]);
if (foundExistingOne) {
Expand All @@ -120,7 +120,7 @@ export async function addVal(id, args, admin) {

configStrategy.values.push(value);
await configStrategy.save();
updateDomainVersion(configStrategy.domain);
await updateDomainVersion(configStrategy.domain);

return configStrategy;
}
Expand Down Expand Up @@ -154,7 +154,7 @@ export async function updateVal(id, args, admin) {
configStrategy.values.splice(indexOldValue, 1);
configStrategy.values.push(newvalue);
await configStrategy.save();
updateDomainVersion(configStrategy.domain);
await updateDomainVersion(configStrategy.domain);

return configStrategy;
}
Expand All @@ -175,7 +175,7 @@ export async function removeVal(id, args, admin) {

configStrategy.values.splice(indexValue, 1);
await configStrategy.save();
updateDomainVersion(configStrategy.domain);
await updateDomainVersion(configStrategy.domain);

return configStrategy;
}
Expand All @@ -199,7 +199,7 @@ export async function updateStatusEnv(id, args, admin) {

configStrategy.activated.set(updates[0], args[updates[0]]);
await configStrategy.save();
updateDomainVersion(configStrategy.domain);
await updateDomainVersion(configStrategy.domain);

return configStrategy;
}
2 changes: 1 addition & 1 deletion src/services/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export async function createDomain(args, admin) {
owner: admin._id
});

environment.save();
await environment.save();
return saveDomain(domain);
}

Expand Down
6 changes: 3 additions & 3 deletions src/services/gitops/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export async function pushChanges(domainId, environment, changes) {
Logger.debug(`Changes: ${JSON.stringify(changes)}`);

let domain = await getDomainById(domainId);
for (const change of changes) {
await CHANGE_PROCESSES[change.action](domain, change, environment);
}
await Promise.all(changes.map(change =>
CHANGE_PROCESSES[change.action](domain, change, environment)
));

domain = await updateDomainVersion(domainId);
return {
Expand Down
8 changes: 3 additions & 5 deletions src/services/gitops/push-deleted.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ async function processComponentDeleted(domain, change) {
const components = await getComponents({ domain: domain._id, name: { $in: content } });
const componentIds = components.map(component => component._id);

for (const id of componentIds) {
if (config.components.includes(id)) {
await removeComponent(config._id, { component: id }, admin);
}
}
await Promise.all(componentIds
.filter(id => config.components.includes(id))
.map(id => removeComponent(config._id, { component: id }, admin)));
}
12 changes: 5 additions & 7 deletions src/services/gitops/push-new.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async function processNewGroup(domain, change, environment) {
}, admin);

if (content.config?.length) {
for (const config of content.config) {
for await (const config of content.config) {
await processNewConfig(domain, {
path: [group.name],
content: config
Expand Down Expand Up @@ -81,7 +81,7 @@ async function processNewConfig(domain, change, environment) {
const config = await createConfig(newConfig, admin);

if (content.strategies?.length) {
for (const strategy of content.strategies) {
for await(const strategy of content.strategies) {
await processNewStrategy(domain, {
path: [group.name, config.key],
content: strategy
Expand Down Expand Up @@ -117,11 +117,9 @@ async function processNewComponent(domain, change) {
const components = await getComponents({ domain: domain._id, name: { $in: content } });
const componentIds = components.map(component => component._id);

for (const id of componentIds) {
if (!config.components.includes(id)) {
await addComponent(config._id, { component: id }, admin);
}
}
await Promise.all(componentIds
.filter(id => !config.components.includes(id))
.map(id => addComponent(config._id, { component: id }, admin)));
}

function getNewValue(newValue, defaultValue) {
Expand Down
Loading
Loading