fix: Apply credential overwrites recursively (#5072)

This ensures that overwrites defined for a parent credential type also applies to all credentials extending it.
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2023-01-04 18:16:48 +01:00
committed by GitHub
parent f1184ccab5
commit 5d746c4a83
11 changed files with 73 additions and 18 deletions

View File

@@ -8,7 +8,9 @@ import type {
import { RESPONSE_ERROR_MESSAGES } from './constants';
class CredentialTypesClass implements ICredentialTypes {
constructor(private nodesAndCredentials: INodesAndCredentials) {}
constructor(private nodesAndCredentials: INodesAndCredentials) {
nodesAndCredentials.credentialTypes = this;
}
recognizes(type: string) {
return type in this.knownCredentials || type in this.loadedCredentials;
@@ -22,6 +24,22 @@ class CredentialTypesClass implements ICredentialTypes {
return this.knownCredentials[type]?.nodesToTestWith ?? [];
}
/**
* Returns all parent types of the given credential type
*/
getParentTypes(typeName: string): string[] {
const credentialType = this.getByName(typeName);
if (credentialType?.extends === undefined) return [];
const types: string[] = [];
credentialType.extends.forEach((type: string) => {
types.push(type);
types.push(...this.getParentTypes(type));
});
return types;
}
private getCredential(type: string): LoadedClass<ICredentialType> {
const loadedCredentials = this.loadedCredentials;
if (type in loadedCredentials) {

View File

@@ -247,18 +247,7 @@ export class CredentialsHelper extends ICredentialsHelper {
* Returns all parent types of the given credential type
*/
getParentTypes(typeName: string): string[] {
const credentialType = this.credentialTypes.getByName(typeName);
if (credentialType === undefined || credentialType.extends === undefined) {
return [];
}
let types: string[] = [];
credentialType.extends.forEach((type: string) => {
types = [...types, type, ...this.getParentTypes(type)];
});
return types;
return this.credentialTypes.getParentTypes(typeName);
}
/**

View File

@@ -86,8 +86,13 @@ class CredentialsOverwritesClass {
return overwrites;
}
private get(type: string): ICredentialDataDecryptedObject | undefined {
return this.overwriteData[type];
private get(name: string): ICredentialDataDecryptedObject | undefined {
const parentTypes = this.credentialTypes.getParentTypes(name);
return [name, ...parentTypes]
.reverse()
.map((type) => this.overwriteData[type])
.filter((type) => !!type)
.reduce((acc, current) => Object.assign(acc, current), {});
}
getAll(): ICredentialsOverwrite {

View File

@@ -1,3 +1,4 @@
import uniq from 'lodash.uniq';
import {
CUSTOM_EXTENSION_ENV,
UserSettings,
@@ -8,6 +9,7 @@ import {
Types,
} from 'n8n-core';
import type {
ICredentialTypes,
ILogger,
INodesAndCredentials,
KnownNodesAndCredentials,
@@ -46,6 +48,8 @@ export class LoadNodesAndCredentialsClass implements INodesAndCredentials {
includeNodes = config.getEnv('nodes.include');
credentialTypes: ICredentialTypes;
logger: ILogger;
async init() {
@@ -68,8 +72,21 @@ export class LoadNodesAndCredentialsClass implements INodesAndCredentials {
async generateTypesForFrontend() {
const credentialsOverwrites = CredentialsOverwrites().getAll();
for (const credential of this.types.credentials) {
const overwrittenProperties = [];
this.credentialTypes
.getParentTypes(credential.name)
.reverse()
.map((name) => credentialsOverwrites[name])
.forEach((overwrite) => {
if (overwrite) overwrittenProperties.push(...Object.keys(overwrite));
});
if (credential.name in credentialsOverwrites) {
credential.__overwrittenProperties = Object.keys(credentialsOverwrites[credential.name]);
overwrittenProperties.push(...Object.keys(credentialsOverwrites[credential.name]));
}
if (overwrittenProperties.length) {
credential.__overwrittenProperties = uniq(overwrittenProperties);
}
}