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}
* @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]) {
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<ICredentialDataDecryptedObject> {
const credentials = await this.getCredentials(name, type);
const decryptedDataOriginal = credentials.getData(this.encryptionKey);

View file

@ -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<ICredentialDataDecryptedObject | undefined> {
// 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<ICredentialDataDecryptedObject | undefined> {
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<ICredentialDataDecryptedObject | undefined> {
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<ICredentialDataDecryptedObject | undefined> {
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<ICredentialDataDecryptedObject | undefined> {
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<ICredentialDataDecryptedObject | undefined> {
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<ICredentialDataDecryptedObject | undefined> {
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<ICredentialDataDecryptedObject | undefined> {
return await getCredentials(workflow, node, type, additionalData, mode);
},
getHeaderData(): object {
if (additionalData.httpRequest === undefined) {

View file

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

View file

@ -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<ICredentials>;
abstract getDecrypted(name: string, type: string, mode: WorkflowExecuteMode, raw?: boolean, expressionResolveValues?: ICredentialsExpressionResolveValues): Promise<ICredentialDataDecryptedObject>;
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[];
executeWorkflow(workflowInfo: IExecuteWorkflowInfo, inputData?: INodeExecutionData[]): Promise<any>; // tslint:disable-line:no-any
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[];
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<ICredentialDataDecryptedObject | undefined>;
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<ICredentialDataDecryptedObject | undefined>;
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<ICredentialDataDecryptedObject | undefined>;
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<ICredentialDataDecryptedObject | undefined>;
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<ICredentialDataDecryptedObject | undefined>;
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<ICredentialDataDecryptedObject | undefined>;
getHeaderData(): object;
getMode(): WorkflowExecuteMode;
getNode(): INode;