Skip to content

Commit

Permalink
refactor(core): Port nodes config (no-changelog) (#10140)
Browse files Browse the repository at this point in the history
Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <[email protected]>
  • Loading branch information
ivov and netroy authored Jul 23, 2024
1 parent d2a3a4a commit 95b85dd
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 65 deletions.
46 changes: 46 additions & 0 deletions packages/@n8n/config/src/configs/nodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Config, Env, Nested } from '../decorators';

function isStringArray(input: unknown): input is string[] {
return Array.isArray(input) && input.every((item) => typeof item === 'string');
}

class JsonStringArray extends Array<string> {
constructor(str: string) {
super();

let parsed: unknown;

try {
parsed = JSON.parse(str);
} catch {
return [];
}

return isStringArray(parsed) ? parsed : [];
}
}

@Config
class CommunityPackagesConfig {
/** Whether to enable community packages */
@Env('N8N_COMMUNITY_PACKAGES_ENABLED')
enabled: boolean = true;
}

@Config
export class NodesConfig {
/** Node types to load. Includes all if unspecified. @example '["n8n-nodes-base.hackerNews"]' */
@Env('NODES_INCLUDE')
readonly include: JsonStringArray = [];

/** Node types not to load. Excludes none if unspecified. @example '["n8n-nodes-base.hackerNews"]' */
@Env('NODES_EXCLUDE')
readonly exclude: JsonStringArray = [];

/** Node type to use as error trigger */
@Env('NODES_ERROR_TRIGGER_TYPE')
readonly errorTriggerType: string = 'n8n-nodes-base.errorTrigger';

@Nested
readonly communityPackages: CommunityPackagesConfig;
}
3 changes: 3 additions & 0 deletions packages/@n8n/config/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Container, Service } from 'typedi';

// eslint-disable-next-line @typescript-eslint/ban-types
type Class = Function;
type Constructable<T = unknown> = new (rawValue: string) => T;
type PropertyKey = string | symbol;
interface PropertyMetadata {
type: unknown;
Expand Down Expand Up @@ -46,6 +47,8 @@ export const Config: ClassDecorator = (ConfigClass: Class) => {
} else {
value = value === 'true';
}
} else if (type !== String && type !== Object) {
value = new (type as Constructable)(value as string);
}

if (value !== undefined) {
Expand Down
4 changes: 4 additions & 0 deletions packages/@n8n/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { PublicApiConfig } from './configs/public-api';
import { ExternalSecretsConfig } from './configs/external-secrets';
import { TemplatesConfig } from './configs/templates';
import { EventBusConfig } from './configs/event-bus';
import { NodesConfig } from './configs/nodes';

@Config
class UserManagementConfig {
Expand Down Expand Up @@ -39,4 +40,7 @@ export class GlobalConfig {

@Nested
eventBus: EventBusConfig;

@Nested
readonly nodes: NodesConfig;
}
17 changes: 15 additions & 2 deletions packages/@n8n/config/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('GlobalConfig', () => {
process.env = originalEnv;
});

const defaultConfig = {
const defaultConfig: GlobalConfig = {
database: {
logging: {
enabled: false,
Expand Down Expand Up @@ -100,6 +100,14 @@ describe('GlobalConfig', () => {
preferGet: false,
updateInterval: 300,
},
nodes: {
communityPackages: {
enabled: true,
},
errorTriggerType: 'n8n-nodes-base.errorTrigger',
include: [],
exclude: [],
},
publicApi: {
disabled: false,
path: 'api',
Expand Down Expand Up @@ -128,6 +136,7 @@ describe('GlobalConfig', () => {
DB_POSTGRESDB_HOST: 'some-host',
DB_POSTGRESDB_USER: 'n8n',
DB_TABLE_PREFIX: 'test_',
NODES_INCLUDE: '["n8n-nodes-base.hackerNews"]',
};
const config = Container.get(GlobalConfig);
expect(config).toEqual({
Expand All @@ -144,11 +153,15 @@ describe('GlobalConfig', () => {
tablePrefix: 'test_',
type: 'sqlite',
},
nodes: {
...defaultConfig.nodes,
include: ['n8n-nodes-base.hackerNews'],
},
});
expect(mockFs.readFileSync).not.toHaveBeenCalled();
});

it('should use values from env variables when defined and convert them to the correct type', () => {
it('should read values from files using _FILE env variables', () => {
const passwordFile = '/path/to/postgres/password';
process.env = {
DB_POSTGRESDB_PASSWORD_FILE: passwordFile,
Expand Down
7 changes: 4 additions & 3 deletions packages/cli/src/LoadNodesAndCredentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import type {
} from 'n8n-workflow';
import { ApplicationError, ErrorReporterProxy as ErrorReporter } from 'n8n-workflow';

import config from '@/config';
import {
CUSTOM_API_CALL_KEY,
CUSTOM_API_CALL_NAME,
Expand All @@ -28,6 +27,7 @@ import {
inE2ETests,
} from '@/constants';
import { Logger } from '@/Logger';
import { GlobalConfig } from '@n8n/config';

interface LoadedNodesAndCredentials {
nodes: INodeTypeData;
Expand All @@ -44,15 +44,16 @@ export class LoadNodesAndCredentials {

loaders: Record<string, DirectoryLoader> = {};

excludeNodes = config.getEnv('nodes.exclude');
excludeNodes = this.globalConfig.nodes.exclude;

includeNodes = config.getEnv('nodes.include');
includeNodes = this.globalConfig.nodes.include;

private postProcessors: Array<() => Promise<void>> = [];

constructor(
private readonly logger: Logger,
private readonly instanceSettings: InstanceSettings,
private readonly globalConfig: GlobalConfig,
) {}

async init() {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class Server extends AbstractServer {
await Container.get(LdapService).init();
}

if (config.getEnv('nodes.communityPackages.enabled')) {
if (this.globalConfig.nodes.communityPackages.enabled) {
await import('@/controllers/communityPackages.controller');
}

Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/WorkflowExecuteAdditionalData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ import { UrlService } from './services/url.service';
import { WorkflowExecutionService } from './workflows/workflowExecution.service';
import { MessageEventBus } from '@/eventbus/MessageEventBus/MessageEventBus';
import { EventService } from './eventbus/event.service';

const ERROR_TRIGGER_TYPE = config.getEnv('nodes.errorTriggerType');
import { GlobalConfig } from '@n8n/config';

export function objectToError(errorObject: unknown, workflow: Workflow): Error {
// TODO: Expand with other error types
Expand Down Expand Up @@ -176,6 +175,7 @@ export function executeErrorWorkflow(
};
}

const { errorTriggerType } = Container.get(GlobalConfig).nodes;
// Run the error workflow
// To avoid an infinite loop do not run the error workflow again if the error-workflow itself failed and it is its own error-workflow.
const { errorWorkflow } = workflowData.settings ?? {};
Expand Down Expand Up @@ -220,7 +220,7 @@ export function executeErrorWorkflow(
} else if (
mode !== 'error' &&
workflowId !== undefined &&
workflowData.nodes.some((node) => node.type === ERROR_TRIGGER_TYPE)
workflowData.nodes.some((node) => node.type === errorTriggerType)
) {
logger.verbose('Start internal error workflow', { executionId, workflowId });
void Container.get(OwnershipService)
Expand Down
5 changes: 2 additions & 3 deletions packages/cli/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,15 @@ export class Start extends BaseCommand {
config.set(setting.key, jsonParse(setting.value, { fallbackValue: setting.value }));
});

const areCommunityPackagesEnabled = config.getEnv('nodes.communityPackages.enabled');
const globalConfig = Container.get(GlobalConfig);

if (areCommunityPackagesEnabled) {
if (globalConfig.nodes.communityPackages.enabled) {
const { CommunityPackagesService } = await import('@/services/communityPackages.service');
await Container.get(CommunityPackagesService).setMissingPackages({
reinstallMissingPackages: flags.reinstallMissingPackages,
});
}

const globalConfig = Container.get(GlobalConfig);
const { type: dbType } = globalConfig.database;
if (dbType === 'sqlite') {
const shouldRunVacuum = globalConfig.database.sqlite.executeVacuumOnStartup;
Expand Down
40 changes: 1 addition & 39 deletions packages/cli/src/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,9 @@ import path from 'path';
import convict from 'convict';
import { Container } from 'typedi';
import { InstanceSettings } from 'n8n-core';
import { LOG_LEVELS, jsonParse } from 'n8n-workflow';
import { LOG_LEVELS } from 'n8n-workflow';
import { ensureStringArray } from './utils';

convict.addFormat({
name: 'json-string-array',
coerce: (rawStr: string) =>
jsonParse<string[]>(rawStr, {
errorMessage: `Expected this value "${rawStr}" to be valid JSON`,
}),
validate: ensureStringArray,
});

convict.addFormat({
name: 'comma-separated-list',
coerce: (rawStr: string) => rawStr.split(','),
Expand Down Expand Up @@ -615,35 +606,6 @@ export const schema = {
env: 'EXTERNAL_HOOK_FILES',
},

nodes: {
include: {
doc: 'Nodes to load',
format: 'json-string-array',
default: undefined,
env: 'NODES_INCLUDE',
},
exclude: {
doc: 'Nodes not to load',
format: 'json-string-array',
default: undefined,
env: 'NODES_EXCLUDE',
},
errorTriggerType: {
doc: 'Node Type to use as Error Trigger',
format: String,
default: 'n8n-nodes-base.errorTrigger',
env: 'NODES_ERROR_TRIGGER_TYPE',
},
communityPackages: {
enabled: {
doc: 'Allows you to disable the usage of community packages for nodes',
format: Boolean,
default: true,
env: 'N8N_COMMUNITY_PACKAGES_ENABLED',
},
},
},

logs: {
level: {
doc: 'Log output level',
Expand Down
2 changes: 0 additions & 2 deletions packages/cli/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ type ToReturnType<T extends ConfigOptionPath> = T extends NumericPath
type ExceptionPaths = {
'queue.bull.redis': RedisOptions;
binaryDataManager: BinaryData.Config;
'nodes.exclude': string[] | undefined;
'nodes.include': string[] | undefined;
'userManagement.isInstanceOwnerSetUp': boolean;
'ui.banners.dismissed': string[] | undefined;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,17 @@ export class InstanceRiskReporter implements RiskReporter {
const settings: Record<string, unknown> = {};

settings.features = {
communityPackagesEnabled: config.getEnv('nodes.communityPackages.enabled'),
communityPackagesEnabled: this.globalConfig.nodes.communityPackages.enabled,
versionNotificationsEnabled: this.globalConfig.versionNotifications.enabled,
templatesEnabled: this.globalConfig.templates.enabled,
publicApiEnabled: isApiEnabled(),
};

const { exclude, include } = this.globalConfig.nodes;

settings.nodes = {
nodesExclude: config.getEnv('nodes.exclude') ?? 'none',
nodesInclude: config.getEnv('nodes.include') ?? 'none',
nodesExclude: exclude.length === 0 ? 'none' : exclude.join(', '),
nodesInclude: include.length === 0 ? 'none' : include.join(', '),
};

settings.telemetry = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as path from 'path';
import glob from 'fast-glob';
import { Service } from 'typedi';
import config from '@/config';
import { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials';
import { getNodeTypes } from '@/security-audit/utils';
import {
Expand All @@ -14,12 +13,14 @@ import {
import type { WorkflowEntity } from '@db/entities/WorkflowEntity';
import type { Risk, RiskReporter } from '@/security-audit/types';
import { CommunityPackagesService } from '@/services/communityPackages.service';
import { GlobalConfig } from '@n8n/config';

@Service()
export class NodesRiskReporter implements RiskReporter {
constructor(
private readonly loadNodesAndCredentials: LoadNodesAndCredentials,
private readonly communityPackagesService: CommunityPackagesService,
private readonly globalConfig: GlobalConfig,
) {}

async report(workflows: WorkflowEntity[]) {
Expand Down Expand Up @@ -85,7 +86,7 @@ export class NodesRiskReporter implements RiskReporter {
}

private async getCommunityNodeDetails() {
if (!config.getEnv('nodes.communityPackages.enabled')) return [];
if (!this.globalConfig.nodes.communityPackages.enabled) return [];

const installedPackages = await this.communityPackagesService.getAllInstalledPackages();

Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/services/frontend.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class FrontendService {

this.initSettings();

if (config.getEnv('nodes.communityPackages.enabled')) {
if (this.globalConfig.nodes.communityPackages.enabled) {
void import('@/services/communityPackages.service').then(({ CommunityPackagesService }) => {
this.communityPackagesService = Container.get(CommunityPackagesService);
});
Expand Down Expand Up @@ -154,7 +154,7 @@ export class FrontendService {
latestVersion: 1,
path: this.globalConfig.publicApi.path,
swaggerUi: {
enabled: !Container.get(GlobalConfig).publicApi.swaggerUiDisabled,
enabled: !this.globalConfig.publicApi.swaggerUiDisabled,
},
},
workflowTagsDisabled: config.getEnv('workflowTagsDisabled'),
Expand All @@ -166,7 +166,7 @@ export class FrontendService {
},
executionMode: config.getEnv('executions.mode'),
pushBackend: config.getEnv('push.backend'),
communityNodesEnabled: config.getEnv('nodes.communityPackages.enabled'),
communityNodesEnabled: this.globalConfig.nodes.communityPackages.enabled,
deployment: {
type: config.getEnv('deployment.type'),
},
Expand Down
Loading

0 comments on commit 95b85dd

Please sign in to comment.