2023-01-05 04:28:40 -08:00
|
|
|
import { v4 as uuid } from 'uuid';
|
|
|
|
import * as Db from '@/Db';
|
|
|
|
import config from '@/config';
|
|
|
|
import { audit } from '@/audit';
|
|
|
|
import { CREDENTIALS_REPORT } from '@/audit/constants';
|
|
|
|
import { getRiskSection } from './utils';
|
|
|
|
import * as testDb from '../shared/testDb';
|
2023-07-13 01:14:48 -07:00
|
|
|
import { generateNanoId } from '@db/utils/generators';
|
2023-01-05 04:28:40 -08:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2023-01-13 09:12:22 -08:00
|
|
|
await testDb.init();
|
2023-01-05 04:28:40 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
2023-01-13 09:12:22 -08:00
|
|
|
await testDb.truncate(['Workflow', 'Credentials', 'Execution']);
|
2023-01-05 04:28:40 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
2023-01-13 09:12:22 -08:00
|
|
|
await testDb.terminate();
|
2023-01-05 04:28:40 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should report credentials not in any use', async () => {
|
|
|
|
const credentialDetails = {
|
2023-06-20 10:13:18 -07:00
|
|
|
id: generateNanoId(),
|
2023-01-05 04:28:40 -08:00
|
|
|
name: 'My Slack Credential',
|
|
|
|
data: 'U2FsdGVkX18WjITBG4IDqrGB1xE/uzVNjtwDAG3lP7E=',
|
|
|
|
type: 'slackApi',
|
|
|
|
nodesAccess: [{ nodeType: 'n8n-nodes-base.slack', date: '2022-12-21T11:23:00.561Z' }],
|
|
|
|
};
|
|
|
|
|
|
|
|
const workflowDetails = {
|
2023-06-20 10:13:18 -07:00
|
|
|
id: generateNanoId(),
|
2023-01-05 04:28:40 -08:00
|
|
|
name: 'My Test Workflow',
|
|
|
|
active: false,
|
|
|
|
connections: {},
|
|
|
|
nodeTypes: {},
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: uuid(),
|
|
|
|
name: 'My Node',
|
|
|
|
type: 'n8n-nodes-base.slack',
|
|
|
|
typeVersion: 1,
|
|
|
|
position: [0, 0] as [number, number],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
Db.collections.Credentials.save(credentialDetails),
|
|
|
|
Db.collections.Workflow.save(workflowDetails),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const testAudit = await audit(['credentials']);
|
|
|
|
|
|
|
|
const section = getRiskSection(
|
|
|
|
testAudit,
|
|
|
|
CREDENTIALS_REPORT.RISK,
|
|
|
|
CREDENTIALS_REPORT.SECTIONS.CREDS_NOT_IN_ANY_USE,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(section.location).toHaveLength(1);
|
|
|
|
expect(section.location[0]).toMatchObject({
|
2023-06-20 10:13:18 -07:00
|
|
|
id: credentialDetails.id,
|
2023-01-05 04:28:40 -08:00
|
|
|
name: 'My Slack Credential',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should report credentials not in active use', async () => {
|
|
|
|
const credentialDetails = {
|
2023-06-20 10:13:18 -07:00
|
|
|
id: generateNanoId(),
|
2023-01-05 04:28:40 -08:00
|
|
|
name: 'My Slack Credential',
|
|
|
|
data: 'U2FsdGVkX18WjITBG4IDqrGB1xE/uzVNjtwDAG3lP7E=',
|
|
|
|
type: 'slackApi',
|
|
|
|
nodesAccess: [{ nodeType: 'n8n-nodes-base.slack', date: '2022-12-21T11:23:00.561Z' }],
|
|
|
|
};
|
|
|
|
|
|
|
|
const credential = await Db.collections.Credentials.save(credentialDetails);
|
|
|
|
|
|
|
|
const workflowDetails = {
|
2023-06-20 10:13:18 -07:00
|
|
|
id: generateNanoId(),
|
2023-01-05 04:28:40 -08:00
|
|
|
name: 'My Test Workflow',
|
|
|
|
active: false,
|
|
|
|
connections: {},
|
|
|
|
nodeTypes: {},
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: uuid(),
|
|
|
|
name: 'My Node',
|
|
|
|
type: 'n8n-nodes-base.slack',
|
|
|
|
typeVersion: 1,
|
|
|
|
position: [0, 0] as [number, number],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
await Db.collections.Workflow.save(workflowDetails);
|
|
|
|
|
|
|
|
const testAudit = await audit(['credentials']);
|
|
|
|
|
|
|
|
const section = getRiskSection(
|
|
|
|
testAudit,
|
|
|
|
CREDENTIALS_REPORT.RISK,
|
|
|
|
CREDENTIALS_REPORT.SECTIONS.CREDS_NOT_IN_ACTIVE_USE,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(section.location).toHaveLength(1);
|
|
|
|
expect(section.location[0]).toMatchObject({
|
|
|
|
id: credential.id,
|
|
|
|
name: 'My Slack Credential',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should report credential in not recently executed workflow', async () => {
|
|
|
|
const credentialDetails = {
|
2023-06-20 10:13:18 -07:00
|
|
|
id: generateNanoId(),
|
2023-01-05 04:28:40 -08:00
|
|
|
name: 'My Slack Credential',
|
|
|
|
data: 'U2FsdGVkX18WjITBG4IDqrGB1xE/uzVNjtwDAG3lP7E=',
|
|
|
|
type: 'slackApi',
|
|
|
|
nodesAccess: [{ nodeType: 'n8n-nodes-base.slack', date: '2022-12-21T11:23:00.561Z' }],
|
|
|
|
};
|
|
|
|
|
|
|
|
const credential = await Db.collections.Credentials.save(credentialDetails);
|
|
|
|
|
|
|
|
const workflowDetails = {
|
2023-06-20 10:13:18 -07:00
|
|
|
id: generateNanoId(),
|
2023-01-05 04:28:40 -08:00
|
|
|
name: 'My Test Workflow',
|
|
|
|
active: false,
|
|
|
|
connections: {},
|
|
|
|
nodeTypes: {},
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: uuid(),
|
|
|
|
name: 'My Node',
|
|
|
|
type: 'n8n-nodes-base.slack',
|
|
|
|
typeVersion: 1,
|
|
|
|
position: [0, 0] as [number, number],
|
|
|
|
credentials: {
|
|
|
|
slackApi: {
|
|
|
|
id: credential.id,
|
|
|
|
name: credential.name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
const workflow = await Db.collections.Workflow.save(workflowDetails);
|
|
|
|
|
|
|
|
const date = new Date();
|
|
|
|
date.setDate(date.getDate() - config.getEnv('security.audit.daysAbandonedWorkflow') - 1);
|
|
|
|
|
2023-06-20 10:13:18 -07:00
|
|
|
const savedExecution = await Db.collections.Execution.save({
|
2023-01-05 04:28:40 -08:00
|
|
|
finished: true,
|
|
|
|
mode: 'manual',
|
|
|
|
startedAt: date,
|
|
|
|
stoppedAt: date,
|
|
|
|
workflowId: workflow.id,
|
|
|
|
waitTill: null,
|
|
|
|
});
|
2023-06-20 10:13:18 -07:00
|
|
|
await Db.collections.ExecutionData.save({
|
|
|
|
execution: savedExecution,
|
|
|
|
data: '[]',
|
|
|
|
workflowData: workflow,
|
|
|
|
});
|
2023-01-05 04:28:40 -08:00
|
|
|
|
|
|
|
const testAudit = await audit(['credentials']);
|
|
|
|
|
|
|
|
const section = getRiskSection(
|
|
|
|
testAudit,
|
|
|
|
CREDENTIALS_REPORT.RISK,
|
|
|
|
CREDENTIALS_REPORT.SECTIONS.CREDS_NOT_RECENTLY_EXECUTED,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(section.location).toHaveLength(1);
|
|
|
|
expect(section.location[0]).toMatchObject({
|
|
|
|
id: credential.id,
|
|
|
|
name: credential.name,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not report credentials in recently executed workflow', async () => {
|
|
|
|
const credentialDetails = {
|
2023-06-20 10:13:18 -07:00
|
|
|
id: generateNanoId(),
|
2023-01-05 04:28:40 -08:00
|
|
|
name: 'My Slack Credential',
|
|
|
|
data: 'U2FsdGVkX18WjITBG4IDqrGB1xE/uzVNjtwDAG3lP7E=',
|
|
|
|
type: 'slackApi',
|
|
|
|
nodesAccess: [{ nodeType: 'n8n-nodes-base.slack', date: '2022-12-21T11:23:00.561Z' }],
|
|
|
|
};
|
|
|
|
|
|
|
|
const credential = await Db.collections.Credentials.save(credentialDetails);
|
|
|
|
|
|
|
|
const workflowDetails = {
|
2023-06-20 10:13:18 -07:00
|
|
|
id: generateNanoId(),
|
2023-01-05 04:28:40 -08:00
|
|
|
name: 'My Test Workflow',
|
|
|
|
active: true,
|
|
|
|
connections: {},
|
|
|
|
nodeTypes: {},
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: uuid(),
|
|
|
|
name: 'My Node',
|
|
|
|
type: 'n8n-nodes-base.slack',
|
|
|
|
typeVersion: 1,
|
|
|
|
position: [0, 0] as [number, number],
|
|
|
|
credentials: {
|
|
|
|
slackApi: {
|
|
|
|
id: credential.id,
|
|
|
|
name: credential.name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
const workflow = await Db.collections.Workflow.save(workflowDetails);
|
|
|
|
|
|
|
|
const date = new Date();
|
|
|
|
date.setDate(date.getDate() - config.getEnv('security.audit.daysAbandonedWorkflow') + 1);
|
|
|
|
|
2023-06-20 10:13:18 -07:00
|
|
|
const savedExecution = await Db.collections.Execution.save({
|
2023-01-05 04:28:40 -08:00
|
|
|
finished: true,
|
|
|
|
mode: 'manual',
|
|
|
|
startedAt: date,
|
|
|
|
stoppedAt: date,
|
|
|
|
workflowId: workflow.id,
|
|
|
|
waitTill: null,
|
|
|
|
});
|
|
|
|
|
2023-06-20 10:13:18 -07:00
|
|
|
await Db.collections.ExecutionData.save({
|
|
|
|
execution: savedExecution,
|
|
|
|
data: '[]',
|
|
|
|
workflowData: workflow,
|
|
|
|
});
|
|
|
|
|
2023-01-05 04:28:40 -08:00
|
|
|
const testAudit = await audit(['credentials']);
|
|
|
|
|
|
|
|
expect(testAudit).toBeEmptyArray();
|
|
|
|
});
|