Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): reimplement blocking workflow updates on interim changes #4446

Merged
merged 17 commits into from
Oct 31, 2022
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
28 changes: 28 additions & 0 deletions packages/cli/src/databases/entities/WorkflowEntity.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import crypto from 'crypto';
import { Length } from 'class-validator';

import type {
Expand All @@ -10,6 +11,9 @@ import type {
} from 'n8n-workflow';

import {
AfterLoad,
AfterUpdate,
AfterInsert,
Column,
Entity,
Index,
Expand Down Expand Up @@ -84,6 +88,30 @@ export class WorkflowEntity extends AbstractEntity implements IWorkflowDb {
transformer: sqlite.jsonColumn,
})
pinData: ISimplifiedPinData;

/**
* Hash of editable workflow state.
*/
hash: string;

@AfterLoad()
@AfterUpdate()
@AfterInsert()
setHash(): void {
const { name, active, nodes, connections, settings, staticData, pinData } = this;

const state = JSON.stringify({
name,
active,
nodes,
connections,
settings,
staticData,
pinData,
});
Comment on lines +103 to +111
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should simplify this to use only nodes and connections. Maybe pinData.

This reduces the chance of having issues with unnecessary warnings such as name changes or activation status.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! As discussed, any editable parts of the workflow that we exclude from the hash will cause changes to those parts to be overwritable, which we likely do not want, so keeping it strict for now.


this.hash = crypto.createHash('md5').update(state).digest('hex');
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/requests.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export declare namespace WorkflowRequest {
settings: IWorkflowSettings;
active: boolean;
tags: string[];
hash: string;
}>;

type Create = AuthenticatedRequest<{}, {}, RequestBody>;
Expand All @@ -56,7 +57,7 @@ export declare namespace WorkflowRequest {

type Delete = Get;

type Update = AuthenticatedRequest<{ id: string }, {}, RequestBody>;
type Update = AuthenticatedRequest<{ id: string }, {}, RequestBody, { forceSave?: string }>;

type NewName = AuthenticatedRequest<{}, {}, {}, { name?: string }>;

Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/workflows/workflows.controller.ee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ EEWorkflowController.patch(
'/:id(\\d+)',
ResponseHelper.send(async (req: WorkflowRequest.Update) => {
const { id: workflowId } = req.params;
const forceSave = req.query.forceSave === 'true';

const updateData = new WorkflowEntity();
const { tags, ...rest } = req.body;
Expand All @@ -193,6 +194,7 @@ EEWorkflowController.patch(
updateData,
workflowId,
tags,
forceSave,
);

const { id, ...remainder } = updatedWorkflow;
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/workflows/workflows.services.ee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,27 +121,28 @@ export class EEWorkflowsService extends WorkflowsService {
workflow: WorkflowEntity,
workflowId: string,
tags?: string[],
forceSave?: boolean,
): Promise<WorkflowEntity> {
const previousVersion = await EEWorkflowsService.get({ id: parseInt(workflowId, 10) });
if (!previousVersion) {
throw new ResponseHelper.ResponseError('Workflow not found', undefined, 404);
}
const allCredentials = await EECredentials.getAll(user);
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
workflow = WorkflowHelpers.validateWorkflowCredentialUsage(
workflow,
previousVersion,
allCredentials,
);
} catch (error) {
console.log(error);
throw new ResponseHelper.ResponseError(
'Invalid workflow credentials - make sure you have access to all credentials and try again.',
undefined,
400,
);
}

return super.updateWorkflow(user, workflow, workflowId, tags);
return super.updateWorkflow(user, workflow, workflowId, tags, forceSave);
}
}
13 changes: 12 additions & 1 deletion packages/cli/src/workflows/workflows.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class WorkflowsService {
workflow: WorkflowEntity,
workflowId: string,
tags?: string[],
forceSave?: boolean,
): Promise<WorkflowEntity> {
const shared = await Db.collections.SharedWorkflow.findOne({
relations: ['workflow'],
Expand All @@ -74,6 +75,14 @@ export class WorkflowsService {
);
}

if (!forceSave && workflow.hash !== shared.workflow.hash) {
throw new ResponseHelper.ResponseError(
`Workflow ID ${workflowId} cannot be saved because it was changed by another user.`,
undefined,
400,
);
}

// check credentials for old format
await WorkflowHelpers.replaceInvalidCredentials(workflow);

Expand Down Expand Up @@ -118,7 +127,9 @@ export class WorkflowsService {
await validateEntity(workflow);
}

await Db.collections.Workflow.update(workflowId, workflow);
const { hash, ...rest } = workflow;

await Db.collections.Workflow.update(workflowId, rest);

if (tags && !config.getEnv('workflowTagsDisabled')) {
const tablePrefix = config.getEnv('database.tablePrefix');
Expand Down
17 changes: 7 additions & 10 deletions packages/cli/test/integration/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -706,27 +706,24 @@ export const emptyPackage = () => {
// workflow
// ----------------------------------

export function makeWorkflow({
withPinData,
withCredential,
}: {
export function makeWorkflow(options?: {
withPinData: boolean;
withCredential?: { id: string; name: string };
}) {
const workflow = new WorkflowEntity();

const node: INode = {
id: uuid(),
name: 'Spotify',
type: 'n8n-nodes-base.spotify',
parameters: { resource: 'track', operation: 'get', id: '123' },
name: 'Cron',
type: 'n8n-nodes-base.cron',
parameters: {},
typeVersion: 1,
position: [740, 240],
};

if (withCredential) {
if (options?.withCredential) {
node.credentials = {
spotifyApi: withCredential,
spotifyApi: options.withCredential,
};
}

Expand All @@ -735,7 +732,7 @@ export function makeWorkflow({
workflow.connections = {};
workflow.nodes = [node];

if (withPinData) {
if (options?.withPinData) {
workflow.pinData = MOCK_PINDATA;
}

Expand Down
Loading