🎨 Centralize error throwing for encryption keys and credentials (#3105)
* Centralized error throwing for encryption key * Unifying the error message used by cli and core packages * Improvements to error messages to make it more DRY * Removed unnecessary throw * Throwing error when credential does not exist to simplify node behavior (#3112) Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
export const BINARY_ENCODING = 'base64';
|
||||
export const CUSTOM_EXTENSION_ENV = 'N8N_CUSTOM_EXTENSIONS';
|
||||
export const ENCRYPTION_KEY_ENV_OVERWRITE = 'N8N_ENCRYPTION_KEY';
|
||||
@@ -9,3 +10,7 @@ export const PLACEHOLDER_EMPTY_EXECUTION_ID = '__UNKOWN__';
|
||||
export const PLACEHOLDER_EMPTY_WORKFLOW_ID = '__EMPTY__';
|
||||
export const TUNNEL_SUBDOMAIN_ENV = 'N8N_TUNNEL_SUBDOMAIN';
|
||||
export const WAIT_TIME_UNLIMITED = '3000-01-01T00:00:00.000Z';
|
||||
|
||||
export const RESPONSE_ERROR_MESSAGES = {
|
||||
NO_ENCRYPTION_KEY: 'Encryption key is missing or was not set',
|
||||
};
|
||||
|
||||
@@ -874,13 +874,7 @@ export async function requestOAuth2(
|
||||
oAuth2Options?: IOAuth2Options,
|
||||
isN8nRequest = false,
|
||||
) {
|
||||
const credentials = (await this.getCredentials(
|
||||
credentialsType,
|
||||
)) as ICredentialDataDecryptedObject;
|
||||
|
||||
if (credentials === undefined) {
|
||||
throw new Error('No credentials were returned!');
|
||||
}
|
||||
const credentials = await this.getCredentials(credentialsType);
|
||||
|
||||
if (credentials.oauthTokenData === undefined) {
|
||||
throw new Error('OAuth credentials not connected!');
|
||||
@@ -997,9 +991,7 @@ export async function requestOAuth1(
|
||||
| IHttpRequestOptions,
|
||||
isN8nRequest = false,
|
||||
) {
|
||||
const credentials = (await this.getCredentials(
|
||||
credentialsType,
|
||||
)) as ICredentialDataDecryptedObject;
|
||||
const credentials = await this.getCredentials(credentialsType);
|
||||
|
||||
if (credentials === undefined) {
|
||||
throw new Error('No credentials were returned!');
|
||||
@@ -1269,7 +1261,7 @@ export async function getCredentials(
|
||||
runIndex?: number,
|
||||
connectionInputData?: INodeExecutionData[],
|
||||
itemIndex?: number,
|
||||
): Promise<ICredentialDataDecryptedObject | undefined> {
|
||||
): Promise<ICredentialDataDecryptedObject> {
|
||||
// Get the NodeType as it has the information if the credentials are required
|
||||
const nodeType = workflow.nodeTypes.getByNameAndVersion(node.type, node.typeVersion);
|
||||
if (nodeType === undefined) {
|
||||
@@ -1309,8 +1301,8 @@ export async function getCredentials(
|
||||
node.parameters,
|
||||
)
|
||||
) {
|
||||
// Credentials should not be displayed so return undefined even if they would be defined
|
||||
return undefined;
|
||||
// Credentials should not be displayed even if they would be defined
|
||||
throw new NodeOperationError(node, 'Credentials not found');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1327,15 +1319,15 @@ export async function getCredentials(
|
||||
throw new NodeOperationError(node, `Node does not have any credentials set for "${type}"!`);
|
||||
}
|
||||
} else {
|
||||
// Credentials are not required so resolve with undefined
|
||||
return undefined;
|
||||
// Credentials are not required
|
||||
throw new NodeOperationError(node, 'Node does not require credentials');
|
||||
}
|
||||
}
|
||||
|
||||
if (fullAccess && (!node.credentials || !node.credentials[type])) {
|
||||
// Make sure that fullAccess nodes still behave like before that if they
|
||||
// request access to credentials that are currently not set it returns undefined
|
||||
return undefined;
|
||||
throw new NodeOperationError(node, 'Credentials not found');
|
||||
}
|
||||
|
||||
let expressionResolveValues: ICredentialsExpressionResolveValues | undefined;
|
||||
@@ -1605,7 +1597,7 @@ export function getExecutePollFunctions(
|
||||
__emit: (data: INodeExecutionData[][]): void => {
|
||||
throw new Error('Overwrite NodeExecuteFunctions.getExecutePullFunctions.__emit function!');
|
||||
},
|
||||
async getCredentials(type: string): Promise<ICredentialDataDecryptedObject | undefined> {
|
||||
async getCredentials(type: string): Promise<ICredentialDataDecryptedObject> {
|
||||
return getCredentials(workflow, node, type, additionalData, mode);
|
||||
},
|
||||
getMode: (): WorkflowExecuteMode => {
|
||||
@@ -1759,7 +1751,7 @@ export function getExecuteTriggerFunctions(
|
||||
emitError: (error: Error): void => {
|
||||
throw new Error('Overwrite NodeExecuteFunctions.getExecuteTriggerFunctions.emit function!');
|
||||
},
|
||||
async getCredentials(type: string): Promise<ICredentialDataDecryptedObject | undefined> {
|
||||
async getCredentials(type: string): Promise<ICredentialDataDecryptedObject> {
|
||||
return getCredentials(workflow, node, type, additionalData, mode);
|
||||
},
|
||||
getNode: () => {
|
||||
@@ -1949,7 +1941,7 @@ export function getExecuteFunctions(
|
||||
async getCredentials(
|
||||
type: string,
|
||||
itemIndex?: number,
|
||||
): Promise<ICredentialDataDecryptedObject | undefined> {
|
||||
): Promise<ICredentialDataDecryptedObject> {
|
||||
return getCredentials(
|
||||
workflow,
|
||||
node,
|
||||
@@ -2193,7 +2185,7 @@ export function getExecuteSingleFunctions(
|
||||
getContext(type: string): IContextObject {
|
||||
return NodeHelpers.getContext(runExecutionData, type, node);
|
||||
},
|
||||
async getCredentials(type: string): Promise<ICredentialDataDecryptedObject | undefined> {
|
||||
async getCredentials(type: string): Promise<ICredentialDataDecryptedObject> {
|
||||
return getCredentials(
|
||||
workflow,
|
||||
node,
|
||||
@@ -2389,7 +2381,7 @@ export function getLoadOptionsFunctions(
|
||||
): ILoadOptionsFunctions {
|
||||
return ((workflow: Workflow, node: INode, path: string) => {
|
||||
const that = {
|
||||
async getCredentials(type: string): Promise<ICredentialDataDecryptedObject | undefined> {
|
||||
async getCredentials(type: string): Promise<ICredentialDataDecryptedObject> {
|
||||
return getCredentials(workflow, node, type, additionalData, 'internal');
|
||||
},
|
||||
getCurrentNodeParameter: (
|
||||
@@ -2533,7 +2525,7 @@ export function getExecuteHookFunctions(
|
||||
): IHookFunctions {
|
||||
return ((workflow: Workflow, node: INode) => {
|
||||
const that = {
|
||||
async getCredentials(type: string): Promise<ICredentialDataDecryptedObject | undefined> {
|
||||
async getCredentials(type: string): Promise<ICredentialDataDecryptedObject> {
|
||||
return getCredentials(workflow, node, type, additionalData, mode);
|
||||
},
|
||||
getMode: (): WorkflowExecuteMode => {
|
||||
@@ -2692,7 +2684,7 @@ export function getExecuteWebhookFunctions(
|
||||
}
|
||||
return additionalData.httpRequest.body;
|
||||
},
|
||||
async getCredentials(type: string): Promise<ICredentialDataDecryptedObject | undefined> {
|
||||
async getCredentials(type: string): Promise<ICredentialDataDecryptedObject> {
|
||||
return getCredentials(workflow, node, type, additionalData, mode);
|
||||
},
|
||||
getHeaderData(): object {
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
ENCRYPTION_KEY_ENV_OVERWRITE,
|
||||
EXTENSIONS_SUBDIRECTORY,
|
||||
IUserSettings,
|
||||
RESPONSE_ERROR_MESSAGES,
|
||||
USER_FOLDER_ENV_OVERWRITE,
|
||||
USER_SETTINGS_FILE_NAME,
|
||||
USER_SETTINGS_SUBFOLDER,
|
||||
@@ -73,19 +74,15 @@ export async function prepareUserSettings(): Promise<IUserSettings> {
|
||||
* @returns
|
||||
*/
|
||||
|
||||
export async function getEncryptionKey(): Promise<string | undefined> {
|
||||
export async function getEncryptionKey(): Promise<string> {
|
||||
if (process.env[ENCRYPTION_KEY_ENV_OVERWRITE] !== undefined) {
|
||||
return process.env[ENCRYPTION_KEY_ENV_OVERWRITE];
|
||||
return process.env[ENCRYPTION_KEY_ENV_OVERWRITE] as string;
|
||||
}
|
||||
|
||||
const userSettings = await getUserSettings();
|
||||
|
||||
if (userSettings === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (userSettings.encryptionKey === undefined) {
|
||||
return undefined;
|
||||
if (userSettings === undefined || userSettings.encryptionKey === undefined) {
|
||||
throw new Error(RESPONSE_ERROR_MESSAGES.NO_ENCRYPTION_KEY);
|
||||
}
|
||||
|
||||
return userSettings.encryptionKey;
|
||||
|
||||
Reference in New Issue
Block a user