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.
This commit is contained in:
Omar Ajoue 2021-04-29 12:52:19 +02:00
parent 7f0f8deb6d
commit 050e8f5b22
4 changed files with 43 additions and 36 deletions

View file

@ -48,7 +48,12 @@ export class CredentialsHelper extends ICredentialsHelper {
* @returns {Credentials} * @returns {Credentials}
* @memberof CredentialsHelper * @memberof CredentialsHelper
*/ */
getCredentials(name: string, type: string): Credentials { async getCredentials(name: string, type: string): Promise<Credentials> {
const credentialsDb = await Db.collections.Credentials?.find({type});
console.log(credentialsDb);
if (!this.workflowCredentials[type]) { if (!this.workflowCredentials[type]) {
throw new Error(`No credentials of type "${type}" exist.`); throw new Error(`No credentials of type "${type}" exist.`);
} }
@ -102,8 +107,8 @@ export class CredentialsHelper extends ICredentialsHelper {
* @returns {ICredentialDataDecryptedObject} * @returns {ICredentialDataDecryptedObject}
* @memberof CredentialsHelper * @memberof CredentialsHelper
*/ */
getDecrypted(name: string, type: string, mode: WorkflowExecuteMode, raw?: boolean, expressionResolveValues?: ICredentialsExpressionResolveValues): ICredentialDataDecryptedObject { async getDecrypted(name: string, type: string, mode: WorkflowExecuteMode, raw?: boolean, expressionResolveValues?: ICredentialsExpressionResolveValues): Promise<ICredentialDataDecryptedObject> {
const credentials = this.getCredentials(name, type); const credentials = await this.getCredentials(name, type);
const decryptedDataOriginal = credentials.getData(this.encryptionKey); const decryptedDataOriginal = credentials.getData(this.encryptionKey);

View file

@ -137,8 +137,8 @@ export async function prepareBinaryData(binaryData: Buffer, filePath?: string, m
* *
* @returns * @returns
*/ */
export function requestOAuth2(this: IAllExecuteFunctions, credentialsType: string, requestOptions: OptionsWithUri | requestPromise.RequestPromiseOptions, node: INode, additionalData: IWorkflowExecuteAdditionalData, oAuth2Options?: IOAuth2Options) { export async function requestOAuth2(this: IAllExecuteFunctions, credentialsType: string, requestOptions: OptionsWithUri | requestPromise.RequestPromiseOptions, node: INode, additionalData: IWorkflowExecuteAdditionalData, oAuth2Options?: IOAuth2Options) {
const credentials = this.getCredentials(credentialsType) as ICredentialDataDecryptedObject; const credentials = await this.getCredentials(credentialsType) as ICredentialDataDecryptedObject;
if (credentials === undefined) { if (credentials === undefined) {
throw new Error('No credentials got returned!'); throw new Error('No credentials got returned!');
@ -220,8 +220,8 @@ export function requestOAuth2(this: IAllExecuteFunctions, credentialsType: strin
* @param {(OptionsWithUrl | requestPromise.RequestPromiseOptions)} requestOptionså * @param {(OptionsWithUrl | requestPromise.RequestPromiseOptions)} requestOptionså
* @returns * @returns
*/ */
export function requestOAuth1(this: IAllExecuteFunctions, credentialsType: string, requestOptions: OptionsWithUrl | OptionsWithUri | requestPromise.RequestPromiseOptions) { export async function requestOAuth1(this: IAllExecuteFunctions, credentialsType: string, requestOptions: OptionsWithUrl | OptionsWithUri | requestPromise.RequestPromiseOptions) {
const credentials = this.getCredentials(credentialsType) as ICredentialDataDecryptedObject; const credentials = await this.getCredentials(credentialsType) as ICredentialDataDecryptedObject;
if (credentials === undefined) { if (credentials === undefined) {
throw new Error('No credentials got returned!'); throw new Error('No credentials got returned!');
@ -308,7 +308,7 @@ export function returnJsonArray(jsonData: IDataObject | IDataObject[]): INodeExe
* @param {IWorkflowExecuteAdditionalData} additionalData * @param {IWorkflowExecuteAdditionalData} additionalData
* @returns {(ICredentialDataDecryptedObject | undefined)} * @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<ICredentialDataDecryptedObject | undefined> {
// Get the NodeType as it has the information if the credentials are required // Get the NodeType as it has the information if the credentials are required
const nodeType = workflow.nodeTypes.getByName(node.type); 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 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; return decryptedDataObject;
} }
@ -546,8 +546,8 @@ export function getExecutePollFunctions(workflow: Workflow, node: INode, additio
__emit: (data: INodeExecutionData[][]): void => { __emit: (data: INodeExecutionData[][]): void => {
throw new Error('Overwrite NodeExecuteFunctions.getExecutePullFunctions.__emit function!'); throw new Error('Overwrite NodeExecuteFunctions.getExecutePullFunctions.__emit function!');
}, },
getCredentials(type: string): ICredentialDataDecryptedObject | undefined { async getCredentials(type: string): Promise<ICredentialDataDecryptedObject | undefined> {
return getCredentials(workflow, node, type, additionalData, mode); return await getCredentials(workflow, node, type, additionalData, mode);
}, },
getMode: (): WorkflowExecuteMode => { getMode: (): WorkflowExecuteMode => {
return mode; return mode;
@ -612,8 +612,8 @@ export function getExecuteTriggerFunctions(workflow: Workflow, node: INode, addi
emit: (data: INodeExecutionData[][]): void => { emit: (data: INodeExecutionData[][]): void => {
throw new Error('Overwrite NodeExecuteFunctions.getExecuteTriggerFunctions.emit function!'); throw new Error('Overwrite NodeExecuteFunctions.getExecuteTriggerFunctions.emit function!');
}, },
getCredentials(type: string): ICredentialDataDecryptedObject | undefined { async getCredentials(type: string): Promise<ICredentialDataDecryptedObject | undefined> {
return getCredentials(workflow, node, type, additionalData, mode); return await getCredentials(workflow, node, type, additionalData, mode);
}, },
getNode: () => { getNode: () => {
return getNode(node); return getNode(node);
@ -690,8 +690,8 @@ export function getExecuteFunctions(workflow: Workflow, runExecutionData: IRunEx
getContext(type: string): IContextObject { getContext(type: string): IContextObject {
return NodeHelpers.getContext(runExecutionData, type, node); return NodeHelpers.getContext(runExecutionData, type, node);
}, },
getCredentials(type: string, itemIndex?: number): ICredentialDataDecryptedObject | undefined { async getCredentials(type: string, itemIndex?: number): Promise<ICredentialDataDecryptedObject | undefined> {
return getCredentials(workflow, node, type, additionalData, mode, runExecutionData, runIndex, connectionInputData, itemIndex); return await getCredentials(workflow, node, type, additionalData, mode, runExecutionData, runIndex, connectionInputData, itemIndex);
}, },
getInputData: (inputIndex = 0, inputName = 'main') => { getInputData: (inputIndex = 0, inputName = 'main') => {
@ -785,8 +785,8 @@ export function getExecuteSingleFunctions(workflow: Workflow, runExecutionData:
getContext(type: string): IContextObject { getContext(type: string): IContextObject {
return NodeHelpers.getContext(runExecutionData, type, node); return NodeHelpers.getContext(runExecutionData, type, node);
}, },
getCredentials(type: string): ICredentialDataDecryptedObject | undefined { async getCredentials(type: string): Promise<ICredentialDataDecryptedObject | undefined> {
return getCredentials(workflow, node, type, additionalData, mode, runExecutionData, runIndex, connectionInputData, itemIndex); return await getCredentials(workflow, node, type, additionalData, mode, runExecutionData, runIndex, connectionInputData, itemIndex);
}, },
getInputData: (inputIndex = 0, inputName = 'main') => { getInputData: (inputIndex = 0, inputName = 'main') => {
if (!inputData.hasOwnProperty(inputName)) { if (!inputData.hasOwnProperty(inputName)) {
@ -865,8 +865,8 @@ export function getExecuteSingleFunctions(workflow: Workflow, runExecutionData:
export function getLoadOptionsFunctions(workflow: Workflow, node: INode, additionalData: IWorkflowExecuteAdditionalData): ILoadOptionsFunctions { export function getLoadOptionsFunctions(workflow: Workflow, node: INode, additionalData: IWorkflowExecuteAdditionalData): ILoadOptionsFunctions {
return ((workflow: Workflow, node: INode) => { return ((workflow: Workflow, node: INode) => {
const that = { const that = {
getCredentials(type: string): ICredentialDataDecryptedObject | undefined { async getCredentials(type: string): Promise<ICredentialDataDecryptedObject | undefined> {
return getCredentials(workflow, node, type, additionalData, 'internal'); return await getCredentials(workflow, node, type, additionalData, 'internal');
}, },
getCurrentNodeParameter: (parameterName: string): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | object | undefined => { getCurrentNodeParameter: (parameterName: string): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | object | undefined => {
const nodeParameters = additionalData.currentNodeParameters; 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 { export function getExecuteHookFunctions(workflow: Workflow, node: INode, additionalData: IWorkflowExecuteAdditionalData, mode: WorkflowExecuteMode, activation: WorkflowActivateMode, isTest?: boolean, webhookData?: IWebhookData): IHookFunctions {
return ((workflow: Workflow, node: INode) => { return ((workflow: Workflow, node: INode) => {
const that = { const that = {
getCredentials(type: string): ICredentialDataDecryptedObject | undefined { async getCredentials(type: string): Promise<ICredentialDataDecryptedObject | undefined> {
return getCredentials(workflow, node, type, additionalData, mode); return await getCredentials(workflow, node, type, additionalData, mode);
}, },
getMode: (): WorkflowExecuteMode => { getMode: (): WorkflowExecuteMode => {
return mode; return mode;
@ -1001,8 +1001,8 @@ export function getExecuteWebhookFunctions(workflow: Workflow, node: INode, addi
} }
return additionalData.httpRequest.body; return additionalData.httpRequest.body;
}, },
getCredentials(type: string): ICredentialDataDecryptedObject | undefined { async getCredentials(type: string): Promise<ICredentialDataDecryptedObject | undefined> {
return getCredentials(workflow, node, type, additionalData, mode); return await getCredentials(workflow, node, type, additionalData, mode);
}, },
getHeaderData(): object { getHeaderData(): object {
if (additionalData.httpRequest === undefined) { if (additionalData.httpRequest === undefined) {

View file

@ -26,12 +26,14 @@ import {
export class CredentialsHelper extends ICredentialsHelper { export class CredentialsHelper extends ICredentialsHelper {
getDecrypted(name: string, type: string): ICredentialDataDecryptedObject { getDecrypted(name: string, type: string): Promise<ICredentialDataDecryptedObject> {
return {}; return new Promise(res => res({}));
} }
getCredentials(name: string, type: string): Credentials { getCredentials(name: string, type: string): Promise<Credentials> {
return new Credentials('', '', [], ''); return new Promise(res => {
res(new Credentials('', '', [], ''));
});
} }
async updateCredentials(name: string, type: string, data: ICredentialDataDecryptedObject): Promise<void> {} async updateCredentials(name: string, type: string, data: ICredentialDataDecryptedObject): Promise<void> {}

View file

@ -102,8 +102,8 @@ export abstract class ICredentialsHelper {
this.workflowCredentials = workflowCredentials; this.workflowCredentials = workflowCredentials;
} }
abstract getCredentials(name: string, type: string): ICredentials; abstract getCredentials(name: string, type: string): Promise<ICredentials>;
abstract getDecrypted(name: string, type: string, mode: WorkflowExecuteMode, raw?: boolean, expressionResolveValues?: ICredentialsExpressionResolveValues): ICredentialDataDecryptedObject; abstract getDecrypted(name: string, type: string, mode: WorkflowExecuteMode, raw?: boolean, expressionResolveValues?: ICredentialsExpressionResolveValues): Promise<ICredentialDataDecryptedObject>;
abstract updateCredentials(name: string, type: string, data: ICredentialDataDecryptedObject): Promise<void>; abstract updateCredentials(name: string, type: string, data: ICredentialDataDecryptedObject): Promise<void>;
} }
@ -212,7 +212,7 @@ export interface IExecuteFunctions {
evaluateExpression(expression: string, itemIndex: number): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[]; evaluateExpression(expression: string, itemIndex: number): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[];
executeWorkflow(workflowInfo: IExecuteWorkflowInfo, inputData?: INodeExecutionData[]): Promise<any>; // tslint:disable-line:no-any executeWorkflow(workflowInfo: IExecuteWorkflowInfo, inputData?: INodeExecutionData[]): Promise<any>; // tslint:disable-line:no-any
getContext(type: string): IContextObject; getContext(type: string): IContextObject;
getCredentials(type: string, itemIndex?: number): ICredentialDataDecryptedObject | undefined; getCredentials(type: string, itemIndex?: number): Promise<ICredentialDataDecryptedObject | undefined>;
getInputData(inputIndex?: number, inputName?: string): INodeExecutionData[]; getInputData(inputIndex?: number, inputName?: string): INodeExecutionData[];
getMode(): WorkflowExecuteMode; getMode(): WorkflowExecuteMode;
getNode(): INode; getNode(): INode;
@ -233,7 +233,7 @@ export interface IExecuteSingleFunctions {
continueOnFail(): boolean; continueOnFail(): boolean;
evaluateExpression(expression: string, itemIndex: number | undefined): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[]; evaluateExpression(expression: string, itemIndex: number | undefined): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[];
getContext(type: string): IContextObject; getContext(type: string): IContextObject;
getCredentials(type: string): ICredentialDataDecryptedObject | undefined; getCredentials(type: string): Promise<ICredentialDataDecryptedObject | undefined>;
getInputData(inputIndex?: number, inputName?: string): INodeExecutionData; getInputData(inputIndex?: number, inputName?: string): INodeExecutionData;
getMode(): WorkflowExecuteMode; getMode(): WorkflowExecuteMode;
getNode(): INode; getNode(): INode;
@ -254,7 +254,7 @@ export interface IExecuteWorkflowInfo {
} }
export interface ILoadOptionsFunctions { export interface ILoadOptionsFunctions {
getCredentials(type: string): ICredentialDataDecryptedObject | undefined; getCredentials(type: string): Promise<ICredentialDataDecryptedObject | undefined>;
getNode(): INode; getNode(): INode;
getNodeParameter(parameterName: string, fallbackValue?: any): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | object; //tslint:disable-line:no-any getNodeParameter(parameterName: string, fallbackValue?: any): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | object; //tslint:disable-line:no-any
getCurrentNodeParameter(parameterName: string): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | object | undefined; getCurrentNodeParameter(parameterName: string): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | object | undefined;
@ -267,7 +267,7 @@ export interface ILoadOptionsFunctions {
} }
export interface IHookFunctions { export interface IHookFunctions {
getCredentials(type: string): ICredentialDataDecryptedObject | undefined; getCredentials(type: string): Promise<ICredentialDataDecryptedObject | undefined>;
getMode(): WorkflowExecuteMode; getMode(): WorkflowExecuteMode;
getActivationMode(): WorkflowActivateMode; getActivationMode(): WorkflowActivateMode;
getNode(): INode; getNode(): INode;
@ -285,7 +285,7 @@ export interface IHookFunctions {
export interface IPollFunctions { export interface IPollFunctions {
__emit(data: INodeExecutionData[][]): void; __emit(data: INodeExecutionData[][]): void;
getCredentials(type: string): ICredentialDataDecryptedObject | undefined; getCredentials(type: string): Promise<ICredentialDataDecryptedObject | undefined>;
getMode(): WorkflowExecuteMode; getMode(): WorkflowExecuteMode;
getActivationMode(): WorkflowActivateMode; getActivationMode(): WorkflowActivateMode;
getNode(): INode; getNode(): INode;
@ -301,7 +301,7 @@ export interface IPollFunctions {
export interface ITriggerFunctions { export interface ITriggerFunctions {
emit(data: INodeExecutionData[][]): void; emit(data: INodeExecutionData[][]): void;
getCredentials(type: string): ICredentialDataDecryptedObject | undefined; getCredentials(type: string): Promise<ICredentialDataDecryptedObject | undefined>;
getMode(): WorkflowExecuteMode; getMode(): WorkflowExecuteMode;
getActivationMode(): WorkflowActivateMode; getActivationMode(): WorkflowActivateMode;
getNode(): INode; getNode(): INode;
@ -317,7 +317,7 @@ export interface ITriggerFunctions {
export interface IWebhookFunctions { export interface IWebhookFunctions {
getBodyData(): IDataObject; getBodyData(): IDataObject;
getCredentials(type: string): ICredentialDataDecryptedObject | undefined; getCredentials(type: string): Promise<ICredentialDataDecryptedObject | undefined>;
getHeaderData(): object; getHeaderData(): object;
getMode(): WorkflowExecuteMode; getMode(): WorkflowExecuteMode;
getNode(): INode; getNode(): INode;