mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-14 00:24:07 -08:00
cd08c8e4c6
Based on #7065 | Story: https://linear.app/n8n/issue/PAY-771 n8n on filesystem mode marks binary data to delete on manual execution deletion, on unsaved execution completion, and on every execution pruning cycle. We later prune binary data in a separate cycle via these marker files, based on the configured TTL. In the context of introducing an S3 client to manage binary data, the filesystem mode's mark-and-prune setup is too tightly coupled to the general binary data management client interface. This PR... - Ensures the deletion of an execution causes the deletion of any binary data associated to it. This does away with the need for binary data TTL and simplifies the filesystem mode's mark-and-prune setup. - Refactors all execution deletions (including pruning) to cause soft deletions, hard-deletes soft-deleted executions based on the existing pruning config, and adjusts execution endpoints to filter out soft-deleted executions. This reduces DB load, and keeps binary data around long enough for users to access it when building workflows with unsaved executions. - Moves all execution pruning work from an execution lifecycle hook to `execution.repository.ts`. This keeps related logic in a single place. - Removes all marking logic from the binary data manager. This simplifies the interface that the S3 client will meet. - Adds basic sanity-check tests to pruning logic and execution deletion. Out of scope: - Improving existing pruning logic. - Improving existing execution repository logic. - Adjusting dir structure for filesystem mode. --------- Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
194 lines
4.3 KiB
TypeScript
194 lines
4.3 KiB
TypeScript
import { v4 as uuid } from 'uuid';
|
|
import * as Db from '@/Db';
|
|
import { audit } from '@/audit';
|
|
import {
|
|
DATABASE_REPORT,
|
|
SQL_NODE_TYPES,
|
|
SQL_NODE_TYPES_WITH_QUERY_PARAMS,
|
|
} from '@/audit/constants';
|
|
import { getRiskSection, saveManualTriggerWorkflow } from './utils';
|
|
import * as testDb from '../shared/testDb';
|
|
import { generateNanoId } from '@db/utils/generators';
|
|
|
|
import { LoggerProxy } from 'n8n-workflow';
|
|
import { getLogger } from '@/Logger';
|
|
|
|
LoggerProxy.init(getLogger());
|
|
|
|
beforeAll(async () => {
|
|
await testDb.init();
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
await testDb.truncate(['Workflow']);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await testDb.terminate();
|
|
});
|
|
|
|
test('should report expressions in queries', async () => {
|
|
const map = [...SQL_NODE_TYPES].reduce<{ [nodeType: string]: string }>((acc, cur) => {
|
|
return (acc[cur] = uuid()), acc;
|
|
}, {});
|
|
|
|
const promises = Object.entries(map).map(async ([nodeType, nodeId]) => {
|
|
const details = {
|
|
id: generateNanoId(),
|
|
name: 'My Test Workflow',
|
|
active: false,
|
|
connections: {},
|
|
nodeTypes: {},
|
|
nodes: [
|
|
{
|
|
id: nodeId,
|
|
name: 'My Node',
|
|
type: nodeType,
|
|
parameters: {
|
|
operation: 'executeQuery',
|
|
query: '=SELECT * FROM {{ $json.table }}',
|
|
additionalFields: {},
|
|
},
|
|
typeVersion: 1,
|
|
position: [0, 0] as [number, number],
|
|
},
|
|
],
|
|
};
|
|
|
|
return Db.collections.Workflow.save(details);
|
|
});
|
|
|
|
await Promise.all(promises);
|
|
|
|
const testAudit = await audit(['database']);
|
|
|
|
const section = getRiskSection(
|
|
testAudit,
|
|
DATABASE_REPORT.RISK,
|
|
DATABASE_REPORT.SECTIONS.EXPRESSIONS_IN_QUERIES,
|
|
);
|
|
|
|
expect(section.location).toHaveLength(SQL_NODE_TYPES.size);
|
|
|
|
for (const loc of section.location) {
|
|
if (loc.kind === 'node') {
|
|
expect(loc.nodeId).toBe(map[loc.nodeType]);
|
|
}
|
|
}
|
|
});
|
|
|
|
test('should report expressions in query params', async () => {
|
|
const map = [...SQL_NODE_TYPES_WITH_QUERY_PARAMS].reduce<{ [nodeType: string]: string }>(
|
|
(acc, cur) => {
|
|
return (acc[cur] = uuid()), acc;
|
|
},
|
|
{},
|
|
);
|
|
|
|
const promises = Object.entries(map).map(async ([nodeType, nodeId]) => {
|
|
const details = {
|
|
id: generateNanoId(),
|
|
name: 'My Test Workflow',
|
|
active: false,
|
|
connections: {},
|
|
nodeTypes: {},
|
|
nodes: [
|
|
{
|
|
id: nodeId,
|
|
name: 'My Node',
|
|
type: nodeType,
|
|
parameters: {
|
|
operation: 'executeQuery',
|
|
query: 'SELECT * FROM users WHERE id = $1;',
|
|
additionalFields: {
|
|
queryParams: '={{ $json.userId }}',
|
|
},
|
|
},
|
|
typeVersion: 1,
|
|
position: [0, 0] as [number, number],
|
|
},
|
|
],
|
|
};
|
|
|
|
return Db.collections.Workflow.save(details);
|
|
});
|
|
|
|
await Promise.all(promises);
|
|
|
|
const testAudit = await audit(['database']);
|
|
|
|
const section = getRiskSection(
|
|
testAudit,
|
|
DATABASE_REPORT.RISK,
|
|
DATABASE_REPORT.SECTIONS.EXPRESSIONS_IN_QUERY_PARAMS,
|
|
);
|
|
|
|
expect(section.location).toHaveLength(SQL_NODE_TYPES_WITH_QUERY_PARAMS.size);
|
|
|
|
for (const loc of section.location) {
|
|
if (loc.kind === 'node') {
|
|
expect(loc.nodeId).toBe(map[loc.nodeType]);
|
|
}
|
|
}
|
|
});
|
|
|
|
test('should report unused query params', async () => {
|
|
const map = [...SQL_NODE_TYPES_WITH_QUERY_PARAMS].reduce<{ [nodeType: string]: string }>(
|
|
(acc, cur) => {
|
|
return (acc[cur] = uuid()), acc;
|
|
},
|
|
{},
|
|
);
|
|
|
|
const promises = Object.entries(map).map(async ([nodeType, nodeId]) => {
|
|
const details = {
|
|
id: generateNanoId(),
|
|
name: 'My Test Workflow',
|
|
active: false,
|
|
connections: {},
|
|
nodeTypes: {},
|
|
nodes: [
|
|
{
|
|
id: nodeId,
|
|
name: 'My Node',
|
|
type: nodeType,
|
|
parameters: {
|
|
operation: 'executeQuery',
|
|
query: 'SELECT * FROM users WHERE id = 123;',
|
|
},
|
|
typeVersion: 1,
|
|
position: [0, 0] as [number, number],
|
|
},
|
|
],
|
|
};
|
|
|
|
return Db.collections.Workflow.save(details);
|
|
});
|
|
|
|
await Promise.all(promises);
|
|
|
|
const testAudit = await audit(['database']);
|
|
|
|
const section = getRiskSection(
|
|
testAudit,
|
|
DATABASE_REPORT.RISK,
|
|
DATABASE_REPORT.SECTIONS.UNUSED_QUERY_PARAMS,
|
|
);
|
|
|
|
expect(section.location).toHaveLength(SQL_NODE_TYPES_WITH_QUERY_PARAMS.size);
|
|
|
|
for (const loc of section.location) {
|
|
if (loc.kind === 'node') {
|
|
expect(loc.nodeId).toBe(map[loc.nodeType]);
|
|
}
|
|
}
|
|
});
|
|
|
|
test('should not report non-database node', async () => {
|
|
await saveManualTriggerWorkflow();
|
|
|
|
const testAudit = await audit(['database']);
|
|
|
|
expect(testAudit).toBeEmptyArray();
|
|
});
|