import { Collection } from 'mongodb'; import Redis from 'ioredis'; const UserSWGProfileObject = require('../__mocks__/json/userSWGProfile.json'); const WebRuleObject = require('../__mocks__/json/webRule.json'); const TpProfileObject = require('../__mocks__/json/tpProfile.json'); const BypassRuleObject = require('../__mocks__/json/bypassRule.json'); const InternalResourcesBypassRuleObject = require('../__mocks__/json/internalResourcesBypassRule.json'); const SwgStatusObject = require('../__mocks__/json/swgStatus.json'); const GroupObject = require('../__mocks__/json/group.json'); const SwgRootCertificateObject = require('../__mocks__/json/swgRootCertificate.json'); const RecordMessageObject = require('../__mocks__/json/recordMessage.json'); const messageAttributesObject = require('../__mocks__/json/messageAttributes.json'); const urlCategoriesObject = require('../__mocks__/json/urlCategories.json'); const appCtrlApplicationsObject = require('../__mocks__/json/appCtrlApplications.json'); const WebRuleWithAppCtrlObject = require('../__mocks__/json/webRuleWithApplicationControl.json'); import { IUpsertUserSWGProfileOptions } from '../types/IUpsertUserSWGProfileOptions'; import { ICreateGroupOptions } from '../types/ICreatGroupOptions'; export async function createUserSWGProfile( collection: Collection, { userId = 'user', tenantId = 'tenant', enabled = false, swgStatus = 'active', webRuleDefaultAction = 'allow', bypassRules = [], webRules = [], groupsOfUser = [], threatPreventionStatus = 'default', threatPreventionProfile = 'tpProfile', }: IUpsertUserSWGProfileOptions, ): Promise { const userSWGProfile = { ...UserSWGProfileObject }; userSWGProfile.userId = userId; userSWGProfile.tenantId = tenantId; userSWGProfile._id = `${tenantId}-${userId}`; userSWGProfile.groupsOfUser = groupsOfUser; userSWGProfile.enabled = enabled; userSWGProfile.swgStatus = swgStatus; userSWGProfile.webRuleDefaultAction = webRuleDefaultAction; userSWGProfile.bypassRules = bypassRules; userSWGProfile.webRules = webRules; userSWGProfile.threatPreventionStatus = threatPreventionStatus; userSWGProfile.threatPreventionProfile = threatPreventionProfile; await collection.insertOne(userSWGProfile); } export async function updateUserSWGProfile( collection: Collection, { userId, tenantId, enabled, swgStatus, webRuleDefaultAction, bypassRules, webRules, groupsOfUser, threatPreventionStatus, threatPreventionProfile, }: IUpsertUserSWGProfileOptions, ): Promise { const updateFields: Partial = {}; if (enabled !== undefined) updateFields.enabled = enabled; if (swgStatus !== undefined) updateFields.swgStatus = swgStatus; if (webRuleDefaultAction !== undefined) updateFields.webRuleDefaultAction = webRuleDefaultAction; if (bypassRules !== undefined) updateFields.bypassRules = bypassRules; if (webRules !== undefined) updateFields.webRules = webRules; if (groupsOfUser !== undefined) updateFields.groupsOfUser = groupsOfUser; if (threatPreventionStatus !== undefined) updateFields.threatPreventionStatus = threatPreventionStatus; if (threatPreventionProfile !== undefined) updateFields.threatPreventionProfile = threatPreventionProfile; await collection.updateOne({ _id: `${tenantId}-${userId}` }, { $set: updateFields }); } export async function createTpProfile( collection: Collection, { tenantId = 'tenant', tpProfileId = 'profileId' }, ): Promise { const tpProfile = { ...TpProfileObject }; tpProfile.tenantId = tenantId; tpProfile._id = tpProfileId; await collection.insertOne(tpProfile); } export async function createWebRule( collection: Collection, { tenantId = 'tenant', webRulesId = 'webId' }, withAppCtrl = false, ): Promise { const webRule = !withAppCtrl ? { ...WebRuleObject } : { ...WebRuleWithAppCtrlObject }; webRule.tenantId = tenantId; webRule.objectId = webRulesId; webRule._id = webRulesId; await collection.insertOne(webRule); } export async function createBypassRule( collection: Collection, { tenantId = 'tenant', bypassRulesId = 'bypassRulesId' }, ): Promise { const bypassRule = { ...BypassRuleObject }; bypassRule.tenantId = tenantId; bypassRule.objectId = bypassRulesId; bypassRule._id = bypassRulesId; await collection.insertOne(bypassRule); } export async function createInternalResourcesBypassRule(collection: Collection): Promise { const internalResourcesBypassRule = { ...InternalResourcesBypassRuleObject }; await collection.insertOne(internalResourcesBypassRule); } export async function createSwgStatus( collection: Collection, { tenantId = 'tenant', status = 'active', webRuleDefaultAction = 'allow' }, ): Promise { const swgStatus = { ...SwgStatusObject }; swgStatus.tenantId = tenantId; swgStatus.swgStatus = status; swgStatus.webRuleDefaultAction = webRuleDefaultAction; await collection.insertOne(swgStatus); } export async function createGroup( collection: Collection, { tenantId = 'tenant', users = [], id = 'groupId' }: ICreateGroupOptions, ): Promise { const group = { ...GroupObject }; group._id = id; group.tenantId = tenantId; group.users = users; await collection.insertOne(group); } export async function createSwgRootCertificate( collection: Collection, { tenantId = 'tenant', id = 'SwgRootCertificateId', active = true }, ): Promise { const swgRootCertificate = { ...SwgRootCertificateObject }; swgRootCertificate.tenantId = tenantId; swgRootCertificate._id = id; swgRootCertificate.active = active; await collection.insertOne(swgRootCertificate); } export function getRecordMessage({ messageId = 'messageId', body = {}, ApproximateReceiveCount = '1', eventTypeValue = 'eventType', }) { const recordMessage = { ...RecordMessageObject }; const messageAttributes = { ...messageAttributesObject }; messageAttributes.eventType = { stringValue: eventTypeValue, stringListValues: [], binaryListValues: [], dataType: 'String', }; recordMessage.messageAttributes = messageAttributes; recordMessage.attributes.ApproximateReceiveCount = ApproximateReceiveCount; recordMessage.messageId = messageId; recordMessage.body = JSON.stringify(body); return recordMessage; } export async function connectToRedis(): Promise { const redisUrl = process.env.REDIS_URL; const redisClient = new Redis(`${redisUrl}/?db=0`, { retryStrategy: function () { return 5000; //5 seconds return delay }, }); return redisClient; } export async function urlCategories(collection: Collection) { await collection.insertMany(urlCategoriesObject); } export async function appCtrlApplications(collection: Collection) { await collection.insertMany(appCtrlApplicationsObject); }