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

fix: Remove foreign credentials when copying nodes or duplicating workflow #4880

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
2 changes: 1 addition & 1 deletion packages/editor-ui/src/Interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ export interface IWorkflowDb {
sharedWith?: Array<Partial<IUser>>;
ownedBy?: Partial<IUser>;
versionId: string;
usedCredentials?: Array<Partial<ICredentialsResponse>>;
usedCredentials?: IUsedCredential[];
}

// Identical to cli.Interfaces.ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ export default mixins(showMessage, workflowHelpers, restApi).extend({
try {
let workflowToUpdate: IWorkflowDataUpdate | undefined;
if (currentWorkflowId !== PLACEHOLDER_EMPTY_WORKFLOW_ID) {
const { createdAt, updatedAt, ...workflow } = await this.restApi().getWorkflow(this.data.id);
const { createdAt, updatedAt, usedCredentials, ...workflow } = await this.restApi().getWorkflow(this.data.id);
workflowToUpdate = workflow;

this.removeForeignCredentialsFromWorkflow(workflowToUpdate, this.credentialsStore.allCredentials);
}

const saved = await this.saveAsNewWorkflow({
Expand Down
19 changes: 19 additions & 0 deletions packages/editor-ui/src/mixins/workflowHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import { useTemplatesStore } from '@/stores/templates';
import { useNodeTypesStore } from '@/stores/nodeTypes';
import useWorkflowsEEStore from "@/stores/workflows.ee";
import {useUsersStore} from "@/stores/users";
import {ICredentialMap, ICredentialsResponse, IUsedCredential} from "@/Interface";

let cachedWorkflowKey: string | null = '';
let cachedWorkflow: Workflow | null = null;
Expand Down Expand Up @@ -928,5 +929,23 @@ export const workflowHelpers = mixins(

return true;
},

removeForeignCredentialsFromWorkflow(workflow: IWorkflowData | IWorkflowDataUpdate, usableCredentials: ICredentialsResponse[]): void {
workflow.nodes.forEach((node: INode) => {
if (!node.credentials) {
return;
}

node.credentials = Object.entries(node.credentials)
.reduce<INodeCredentials>((acc, [credentialType, credential]) => {
const isUsableCredential = usableCredentials.some((ownCredential) => `${ownCredential.id}` === `${credential.id}`);
if (credential.id && isUsableCredential) {
acc[credentialType] = node.credentials![credentialType];
}

return acc;
}, {});
});
},
},
});
2 changes: 2 additions & 0 deletions packages/editor-ui/src/stores/workflows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const createEmptyWorkflow = (): IWorkflowDb => ({
tags: [],
pinData: {},
versionId: '',
usedCredentials: [],
});

export const useWorkflowsStore = defineStore(STORES.WORKFLOWS, {
Expand Down Expand Up @@ -269,6 +270,7 @@ export const useWorkflowsStore = defineStore(STORES.WORKFLOWS, {
},

setUsedCredentials(data: IUsedCredential[]) {
this.workflow.usedCredentials = data;
this.usedCredentials = data.reduce<{ [name: string]: IUsedCredential }>((accu, credential) => {
accu[credential.id!] = credential;
return accu;
Expand Down
3 changes: 3 additions & 0 deletions packages/editor-ui/src/views/NodeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,10 @@ export default mixins(
...data,
};

this.removeForeignCredentialsFromWorkflow(workflowToCopy, this.credentialsStore.allCredentials);

const nodeData = JSON.stringify(workflowToCopy, null, 2);

this.copyToClipboard(nodeData);
if (data.nodes.length > 0) {
if (!isCut) {
Expand Down