From 050e8f5b22708252e8a53bb2c443cd701d6bfb89 Mon Sep 17 00:00:00 2001 From: Omar Ajoue Date: Thu, 29 Apr 2021 12:52:19 +0200 Subject: [PATCH] Changes to types so that credentials can be always loaded from DB This first commit changes all return types from the execute functions and calls to get credentials to be async so we can use await. This is a first step as previously credentials were loaded in memory and always available. We will now be loading them from the DB which requires turning the whole call chain async. --- packages/cli/src/CredentialsHelper.ts | 11 +++++-- packages/core/src/NodeExecuteFunctions.ts | 40 +++++++++++------------ packages/core/test/Helpers.ts | 10 +++--- packages/workflow/src/Interfaces.ts | 18 +++++----- 4 files changed, 43 insertions(+), 36 deletions(-) diff --git a/packages/cli/src/CredentialsHelper.ts b/packages/cli/src/CredentialsHelper.ts index 07e9f484f7..42fd136b03 100644 --- a/packages/cli/src/CredentialsHelper.ts +++ b/packages/cli/src/CredentialsHelper.ts @@ -48,7 +48,12 @@ export class CredentialsHelper extends ICredentialsHelper { * @returns {Credentials} * @memberof CredentialsHelper */ - getCredentials(name: string, type: string): Credentials { + async getCredentials(name: string, type: string): Promise { + + const credentialsDb = await Db.collections.Credentials?.find({type}); + + console.log(credentialsDb); + if (!this.workflowCredentials[type]) { throw new Error(`No credentials of type "${type}" exist.`); } @@ -102,8 +107,8 @@ export class CredentialsHelper extends ICredentialsHelper { * @returns {ICredentialDataDecryptedObject} * @memberof CredentialsHelper */ - getDecrypted(name: string, type: string, mode: WorkflowExecuteMode, raw?: boolean, expressionResolveValues?: ICredentialsExpressionResolveValues): ICredentialDataDecryptedObject { - const credentials = this.getCredentials(name, type); + async getDecrypted(name: string, type: string, mode: WorkflowExecuteMode, raw?: boolean, expressionResolveValues?: ICredentialsExpressionResolveValues): Promise { + const credentials = await this.getCredentials(name, type); const decryptedDataOriginal = credentials.getData(this.encryptionKey); diff --git a/packages/core/src/NodeExecuteFunctions.ts b/packages/core/src/NodeExecuteFunctions.ts index e77e1a3d94..0da9f649ab 100644 --- a/packages/core/src/NodeExecuteFunctions.ts +++ b/packages/core/src/NodeExecuteFunctions.ts @@ -137,8 +137,8 @@ export async function prepareBinaryData(binaryData: Buffer, filePath?: string, m * * @returns */ -export function requestOAuth2(this: IAllExecuteFunctions, credentialsType: string, requestOptions: OptionsWithUri | requestPromise.RequestPromiseOptions, node: INode, additionalData: IWorkflowExecuteAdditionalData, oAuth2Options?: IOAuth2Options) { - const credentials = this.getCredentials(credentialsType) as ICredentialDataDecryptedObject; +export async function requestOAuth2(this: IAllExecuteFunctions, credentialsType: string, requestOptions: OptionsWithUri | requestPromise.RequestPromiseOptions, node: INode, additionalData: IWorkflowExecuteAdditionalData, oAuth2Options?: IOAuth2Options) { + const credentials = await this.getCredentials(credentialsType) as ICredentialDataDecryptedObject; if (credentials === undefined) { throw new Error('No credentials got returned!'); @@ -220,8 +220,8 @@ export function requestOAuth2(this: IAllExecuteFunctions, credentialsType: strin * @param {(OptionsWithUrl | requestPromise.RequestPromiseOptions)} requestOptionså * @returns */ -export function requestOAuth1(this: IAllExecuteFunctions, credentialsType: string, requestOptions: OptionsWithUrl | OptionsWithUri | requestPromise.RequestPromiseOptions) { - const credentials = this.getCredentials(credentialsType) as ICredentialDataDecryptedObject; +export async function requestOAuth1(this: IAllExecuteFunctions, credentialsType: string, requestOptions: OptionsWithUrl | OptionsWithUri | requestPromise.RequestPromiseOptions) { + const credentials = await this.getCredentials(credentialsType) as ICredentialDataDecryptedObject; if (credentials === undefined) { throw new Error('No credentials got returned!'); @@ -308,7 +308,7 @@ export function returnJsonArray(jsonData: IDataObject | IDataObject[]): INodeExe * @param {IWorkflowExecuteAdditionalData} additionalData * @returns {(ICredentialDataDecryptedObject | undefined)} */ -export function getCredentials(workflow: Workflow, node: INode, type: string, additionalData: IWorkflowExecuteAdditionalData, mode: WorkflowExecuteMode, runExecutionData?: IRunExecutionData | null, runIndex?: number, connectionInputData?: INodeExecutionData[], itemIndex?: number): ICredentialDataDecryptedObject | undefined { +export async function getCredentials(workflow: Workflow, node: INode, type: string, additionalData: IWorkflowExecuteAdditionalData, mode: WorkflowExecuteMode, runExecutionData?: IRunExecutionData | null, runIndex?: number, connectionInputData?: INodeExecutionData[], itemIndex?: number): Promise { // Get the NodeType as it has the information if the credentials are required const nodeType = workflow.nodeTypes.getByName(node.type); @@ -362,7 +362,7 @@ export function getCredentials(workflow: Workflow, node: INode, type: string, ad const name = node.credentials[type]; - const decryptedDataObject = additionalData.credentialsHelper.getDecrypted(name, type, mode, false, expressionResolveValues); + const decryptedDataObject = await additionalData.credentialsHelper.getDecrypted(name, type, mode, false, expressionResolveValues); return decryptedDataObject; } @@ -546,8 +546,8 @@ export function getExecutePollFunctions(workflow: Workflow, node: INode, additio __emit: (data: INodeExecutionData[][]): void => { throw new Error('Overwrite NodeExecuteFunctions.getExecutePullFunctions.__emit function!'); }, - getCredentials(type: string): ICredentialDataDecryptedObject | undefined { - return getCredentials(workflow, node, type, additionalData, mode); + async getCredentials(type: string): Promise { + return await getCredentials(workflow, node, type, additionalData, mode); }, getMode: (): WorkflowExecuteMode => { return mode; @@ -612,8 +612,8 @@ export function getExecuteTriggerFunctions(workflow: Workflow, node: INode, addi emit: (data: INodeExecutionData[][]): void => { throw new Error('Overwrite NodeExecuteFunctions.getExecuteTriggerFunctions.emit function!'); }, - getCredentials(type: string): ICredentialDataDecryptedObject | undefined { - return getCredentials(workflow, node, type, additionalData, mode); + async getCredentials(type: string): Promise { + return await getCredentials(workflow, node, type, additionalData, mode); }, getNode: () => { return getNode(node); @@ -690,8 +690,8 @@ export function getExecuteFunctions(workflow: Workflow, runExecutionData: IRunEx getContext(type: string): IContextObject { return NodeHelpers.getContext(runExecutionData, type, node); }, - getCredentials(type: string, itemIndex?: number): ICredentialDataDecryptedObject | undefined { - return getCredentials(workflow, node, type, additionalData, mode, runExecutionData, runIndex, connectionInputData, itemIndex); + async getCredentials(type: string, itemIndex?: number): Promise { + return await getCredentials(workflow, node, type, additionalData, mode, runExecutionData, runIndex, connectionInputData, itemIndex); }, getInputData: (inputIndex = 0, inputName = 'main') => { @@ -785,8 +785,8 @@ export function getExecuteSingleFunctions(workflow: Workflow, runExecutionData: getContext(type: string): IContextObject { return NodeHelpers.getContext(runExecutionData, type, node); }, - getCredentials(type: string): ICredentialDataDecryptedObject | undefined { - return getCredentials(workflow, node, type, additionalData, mode, runExecutionData, runIndex, connectionInputData, itemIndex); + async getCredentials(type: string): Promise { + return await getCredentials(workflow, node, type, additionalData, mode, runExecutionData, runIndex, connectionInputData, itemIndex); }, getInputData: (inputIndex = 0, inputName = 'main') => { if (!inputData.hasOwnProperty(inputName)) { @@ -865,8 +865,8 @@ export function getExecuteSingleFunctions(workflow: Workflow, runExecutionData: export function getLoadOptionsFunctions(workflow: Workflow, node: INode, additionalData: IWorkflowExecuteAdditionalData): ILoadOptionsFunctions { return ((workflow: Workflow, node: INode) => { const that = { - getCredentials(type: string): ICredentialDataDecryptedObject | undefined { - return getCredentials(workflow, node, type, additionalData, 'internal'); + async getCredentials(type: string): Promise { + return await getCredentials(workflow, node, type, additionalData, 'internal'); }, getCurrentNodeParameter: (parameterName: string): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | object | undefined => { const nodeParameters = additionalData.currentNodeParameters; @@ -924,8 +924,8 @@ export function getLoadOptionsFunctions(workflow: Workflow, node: INode, additio export function getExecuteHookFunctions(workflow: Workflow, node: INode, additionalData: IWorkflowExecuteAdditionalData, mode: WorkflowExecuteMode, activation: WorkflowActivateMode, isTest?: boolean, webhookData?: IWebhookData): IHookFunctions { return ((workflow: Workflow, node: INode) => { const that = { - getCredentials(type: string): ICredentialDataDecryptedObject | undefined { - return getCredentials(workflow, node, type, additionalData, mode); + async getCredentials(type: string): Promise { + return await getCredentials(workflow, node, type, additionalData, mode); }, getMode: (): WorkflowExecuteMode => { return mode; @@ -1001,8 +1001,8 @@ export function getExecuteWebhookFunctions(workflow: Workflow, node: INode, addi } return additionalData.httpRequest.body; }, - getCredentials(type: string): ICredentialDataDecryptedObject | undefined { - return getCredentials(workflow, node, type, additionalData, mode); + async getCredentials(type: string): Promise { + return await getCredentials(workflow, node, type, additionalData, mode); }, getHeaderData(): object { if (additionalData.httpRequest === undefined) { diff --git a/packages/core/test/Helpers.ts b/packages/core/test/Helpers.ts index 4335e9e1e9..693ca3a880 100644 --- a/packages/core/test/Helpers.ts +++ b/packages/core/test/Helpers.ts @@ -26,12 +26,14 @@ import { export class CredentialsHelper extends ICredentialsHelper { - getDecrypted(name: string, type: string): ICredentialDataDecryptedObject { - return {}; + getDecrypted(name: string, type: string): Promise { + return new Promise(res => res({})); } - getCredentials(name: string, type: string): Credentials { - return new Credentials('', '', [], ''); + getCredentials(name: string, type: string): Promise { + return new Promise(res => { + res(new Credentials('', '', [], '')); + }); } async updateCredentials(name: string, type: string, data: ICredentialDataDecryptedObject): Promise {} diff --git a/packages/workflow/src/Interfaces.ts b/packages/workflow/src/Interfaces.ts index ba78499b5a..9262cff86c 100644 --- a/packages/workflow/src/Interfaces.ts +++ b/packages/workflow/src/Interfaces.ts @@ -102,8 +102,8 @@ export abstract class ICredentialsHelper { this.workflowCredentials = workflowCredentials; } - abstract getCredentials(name: string, type: string): ICredentials; - abstract getDecrypted(name: string, type: string, mode: WorkflowExecuteMode, raw?: boolean, expressionResolveValues?: ICredentialsExpressionResolveValues): ICredentialDataDecryptedObject; + abstract getCredentials(name: string, type: string): Promise; + abstract getDecrypted(name: string, type: string, mode: WorkflowExecuteMode, raw?: boolean, expressionResolveValues?: ICredentialsExpressionResolveValues): Promise; abstract updateCredentials(name: string, type: string, data: ICredentialDataDecryptedObject): Promise; } @@ -212,7 +212,7 @@ export interface IExecuteFunctions { evaluateExpression(expression: string, itemIndex: number): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[]; executeWorkflow(workflowInfo: IExecuteWorkflowInfo, inputData?: INodeExecutionData[]): Promise; // tslint:disable-line:no-any getContext(type: string): IContextObject; - getCredentials(type: string, itemIndex?: number): ICredentialDataDecryptedObject | undefined; + getCredentials(type: string, itemIndex?: number): Promise; getInputData(inputIndex?: number, inputName?: string): INodeExecutionData[]; getMode(): WorkflowExecuteMode; getNode(): INode; @@ -233,7 +233,7 @@ export interface IExecuteSingleFunctions { continueOnFail(): boolean; evaluateExpression(expression: string, itemIndex: number | undefined): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[]; getContext(type: string): IContextObject; - getCredentials(type: string): ICredentialDataDecryptedObject | undefined; + getCredentials(type: string): Promise; getInputData(inputIndex?: number, inputName?: string): INodeExecutionData; getMode(): WorkflowExecuteMode; getNode(): INode; @@ -254,7 +254,7 @@ export interface IExecuteWorkflowInfo { } export interface ILoadOptionsFunctions { - getCredentials(type: string): ICredentialDataDecryptedObject | undefined; + getCredentials(type: string): Promise; getNode(): INode; getNodeParameter(parameterName: string, fallbackValue?: any): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | object; //tslint:disable-line:no-any getCurrentNodeParameter(parameterName: string): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | object | undefined; @@ -267,7 +267,7 @@ export interface ILoadOptionsFunctions { } export interface IHookFunctions { - getCredentials(type: string): ICredentialDataDecryptedObject | undefined; + getCredentials(type: string): Promise; getMode(): WorkflowExecuteMode; getActivationMode(): WorkflowActivateMode; getNode(): INode; @@ -285,7 +285,7 @@ export interface IHookFunctions { export interface IPollFunctions { __emit(data: INodeExecutionData[][]): void; - getCredentials(type: string): ICredentialDataDecryptedObject | undefined; + getCredentials(type: string): Promise; getMode(): WorkflowExecuteMode; getActivationMode(): WorkflowActivateMode; getNode(): INode; @@ -301,7 +301,7 @@ export interface IPollFunctions { export interface ITriggerFunctions { emit(data: INodeExecutionData[][]): void; - getCredentials(type: string): ICredentialDataDecryptedObject | undefined; + getCredentials(type: string): Promise; getMode(): WorkflowExecuteMode; getActivationMode(): WorkflowActivateMode; getNode(): INode; @@ -317,7 +317,7 @@ export interface ITriggerFunctions { export interface IWebhookFunctions { getBodyData(): IDataObject; - getCredentials(type: string): ICredentialDataDecryptedObject | undefined; + getCredentials(type: string): Promise; getHeaderData(): object; getMode(): WorkflowExecuteMode; getNode(): INode;