2022-11-24 09:32:00 -08:00
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
|
|
/* eslint-disable @typescript-eslint/naming-convention */
|
|
|
|
import { Router } from 'express';
|
2023-03-31 04:51:38 -07:00
|
|
|
import type { Request } from 'express';
|
2022-11-24 09:32:00 -08:00
|
|
|
import bodyParser from 'body-parser';
|
|
|
|
import { v4 as uuid } from 'uuid';
|
2023-04-12 01:59:14 -07:00
|
|
|
import { Container } from 'typedi';
|
2022-11-24 09:32:00 -08:00
|
|
|
import config from '@/config';
|
|
|
|
import * as Db from '@/Db';
|
2023-01-27 05:56:56 -08:00
|
|
|
import type { Role } from '@db/entities/Role';
|
2023-04-12 01:59:14 -07:00
|
|
|
import { RoleRepository } from '@db/repositories';
|
2022-11-24 09:32:00 -08:00
|
|
|
import { hashPassword } from '@/UserManagement/UserManagementHelper';
|
2023-03-30 00:59:04 -07:00
|
|
|
import { eventBus } from '@/eventbus/MessageEventBus/MessageEventBus';
|
2023-03-31 04:51:38 -07:00
|
|
|
import { License } from '../License';
|
2023-04-03 00:49:55 -07:00
|
|
|
import { LICENSE_FEATURES } from '@/constants';
|
2022-11-24 09:32:00 -08:00
|
|
|
|
|
|
|
if (process.env.E2E_TESTS !== 'true') {
|
|
|
|
console.error('E2E endpoints only allowed during E2E tests');
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2023-03-31 04:51:38 -07:00
|
|
|
const enabledFeatures = {
|
2023-04-03 00:49:55 -07:00
|
|
|
[LICENSE_FEATURES.SHARING]: true, //default to true here instead of setting it in config/index.ts for e2e
|
|
|
|
[LICENSE_FEATURES.LDAP]: false,
|
|
|
|
[LICENSE_FEATURES.SAML]: false,
|
|
|
|
[LICENSE_FEATURES.LOG_STREAMING]: false,
|
|
|
|
[LICENSE_FEATURES.ADVANCED_EXECUTION_FILTERS]: false,
|
2023-04-18 04:29:26 -07:00
|
|
|
[LICENSE_FEATURES.VERSION_CONTROL]: false,
|
2023-03-31 04:51:38 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
type Feature = keyof typeof enabledFeatures;
|
|
|
|
|
|
|
|
Container.get(License).isFeatureEnabled = (feature: Feature) => enabledFeatures[feature] ?? false;
|
|
|
|
|
2023-05-03 05:06:06 -07:00
|
|
|
const tablesNotToTruncate = ['sqlite_sequence'];
|
2022-11-24 09:32:00 -08:00
|
|
|
|
|
|
|
const truncateAll = async () => {
|
2023-04-12 01:59:14 -07:00
|
|
|
const connection = Db.getConnection();
|
2023-05-03 05:06:06 -07:00
|
|
|
const allTables: Array<{ name: string }> = await connection.query(
|
|
|
|
"SELECT name FROM sqlite_master WHERE type='table';",
|
|
|
|
);
|
|
|
|
|
|
|
|
// Disable foreign key constraint checks
|
|
|
|
await connection.query('PRAGMA foreign_keys = OFF;');
|
|
|
|
|
|
|
|
for (const { name: table } of allTables) {
|
|
|
|
try {
|
|
|
|
if (tablesNotToTruncate.includes(table)) continue;
|
|
|
|
await connection.query(
|
|
|
|
`DELETE FROM ${table}; DELETE FROM sqlite_sequence WHERE name=${table};`,
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
console.warn('Dropping Table for E2E Reset error: ', error);
|
|
|
|
}
|
2022-11-24 09:32:00 -08:00
|
|
|
}
|
2023-05-03 05:06:06 -07:00
|
|
|
|
|
|
|
// Re-enable foreign key constraint checks
|
|
|
|
await connection.query('PRAGMA foreign_keys = ON;');
|
2022-11-24 09:32:00 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
const setupUserManagement = async () => {
|
2023-04-12 01:59:14 -07:00
|
|
|
const connection = Db.getConnection();
|
2022-11-24 09:32:00 -08:00
|
|
|
await connection.query('INSERT INTO role (name, scope) VALUES ("owner", "global");');
|
|
|
|
const instanceOwnerRole = (await connection.query(
|
|
|
|
'SELECT last_insert_rowid() as insertId',
|
|
|
|
)) as Array<{ insertId: number }>;
|
|
|
|
|
|
|
|
const roles: Array<[Role['name'], Role['scope']]> = [
|
|
|
|
['member', 'global'],
|
|
|
|
['owner', 'workflow'],
|
|
|
|
['owner', 'credential'],
|
|
|
|
['user', 'credential'],
|
|
|
|
['editor', 'workflow'],
|
|
|
|
];
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
roles.map(async ([name, scope]) =>
|
|
|
|
connection.query(`INSERT INTO role (name, scope) VALUES ("${name}", "${scope}");`),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
await connection.query(
|
|
|
|
`INSERT INTO user (id, globalRoleId) values ("${uuid()}", ${instanceOwnerRole[0].insertId})`,
|
|
|
|
);
|
|
|
|
await connection.query(
|
2022-12-29 03:20:43 -08:00
|
|
|
"INSERT INTO \"settings\" (key, value, loadOnStartup) values ('userManagement.isInstanceOwnerSetUp', 'false', true), ('userManagement.skipInstanceOwnerSetup', 'false', true)",
|
2022-11-24 09:32:00 -08:00
|
|
|
);
|
2023-01-04 00:47:48 -08:00
|
|
|
|
|
|
|
config.set('userManagement.isInstanceOwnerSetUp', false);
|
|
|
|
};
|
|
|
|
|
|
|
|
const resetLogStreaming = async () => {
|
2023-04-03 00:49:55 -07:00
|
|
|
enabledFeatures[LICENSE_FEATURES.LOG_STREAMING] = false;
|
2023-01-04 00:47:48 -08:00
|
|
|
for (const id in eventBus.destinations) {
|
|
|
|
await eventBus.removeDestination(id);
|
|
|
|
}
|
2022-11-24 09:32:00 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
export const e2eController = Router();
|
|
|
|
|
|
|
|
e2eController.post('/db/reset', async (req, res) => {
|
2023-01-04 00:47:48 -08:00
|
|
|
await resetLogStreaming();
|
2022-11-24 09:32:00 -08:00
|
|
|
await truncateAll();
|
|
|
|
await setupUserManagement();
|
|
|
|
|
|
|
|
res.writeHead(204).end();
|
|
|
|
});
|
|
|
|
|
|
|
|
e2eController.post('/db/setup-owner', bodyParser.json(), async (req, res) => {
|
|
|
|
if (config.get('userManagement.isInstanceOwnerSetUp')) {
|
|
|
|
res.writeHead(500).send({ error: 'Owner already setup' });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-12 01:59:14 -07:00
|
|
|
const globalRole = await Container.get(RoleRepository).findGlobalOwnerRoleOrFail();
|
2022-11-24 09:32:00 -08:00
|
|
|
|
2023-01-13 09:12:22 -08:00
|
|
|
const owner = await Db.collections.User.findOneByOrFail({ globalRoleId: globalRole.id });
|
2022-11-24 09:32:00 -08:00
|
|
|
|
|
|
|
await Db.collections.User.update(owner.id, {
|
|
|
|
email: req.body.email,
|
|
|
|
password: await hashPassword(req.body.password),
|
|
|
|
firstName: req.body.firstName,
|
|
|
|
lastName: req.body.lastName,
|
|
|
|
});
|
|
|
|
|
|
|
|
await Db.collections.Settings.update(
|
|
|
|
{ key: 'userManagement.isInstanceOwnerSetUp' },
|
|
|
|
{ value: 'true' },
|
|
|
|
);
|
|
|
|
|
|
|
|
config.set('userManagement.isInstanceOwnerSetUp', true);
|
|
|
|
|
|
|
|
res.writeHead(204).end();
|
|
|
|
});
|
2023-01-04 00:47:48 -08:00
|
|
|
|
2023-05-03 05:06:06 -07:00
|
|
|
e2eController.patch(
|
|
|
|
'/feature/:feature',
|
|
|
|
bodyParser.json(),
|
|
|
|
async (req: Request<{ feature: Feature }>, res) => {
|
|
|
|
const { feature } = req.params;
|
|
|
|
const { enabled } = req.body;
|
|
|
|
|
|
|
|
enabledFeatures[feature] = enabled === undefined || enabled === true;
|
|
|
|
res.writeHead(204).end();
|
|
|
|
},
|
|
|
|
);
|