From 6ec4ada932cf98f04e2561ec7d8ce2e21598b7de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Fri, 10 Mar 2023 18:29:11 +0100 Subject: [PATCH 1/2] fix(core): Initialize LDAP right before setting up the controllers (no-changelog) --- packages/cli/src/Server.ts | 12 ++++++++---- packages/cli/src/commands/start.ts | 3 --- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/cli/src/Server.ts b/packages/cli/src/Server.ts index 0f89c5ff54fe3..c7d982dc86791 100644 --- a/packages/cli/src/Server.ts +++ b/packages/cli/src/Server.ts @@ -135,7 +135,12 @@ import { licenseController } from './license/license.controller'; import { Push, setupPushServer, setupPushHandler } from '@/push'; import { setupAuthMiddlewares } from './middlewares'; import { initEvents } from './events'; -import { getLdapLoginLabel, isLdapEnabled, isLdapLoginEnabled } from './Ldap/helpers'; +import { + getLdapLoginLabel, + handleLdapInit, + isLdapEnabled, + isLdapLoginEnabled, +} from './Ldap/helpers'; import { AbstractServer } from './AbstractServer'; import { configureMetrics } from './metrics'; import { setupBasicAuth } from './middlewares/basicAuth'; @@ -490,9 +495,8 @@ class Server extends AbstractServer { }), ); - // ---------------------------------------- - // User Management - // ---------------------------------------- + await handleLdapInit(); + this.registerControllers(ignoredEndpoints); this.app.use(`/${this.restEndpoint}/credentials`, credentialsController); diff --git a/packages/cli/src/commands/start.ts b/packages/cli/src/commands/start.ts index bac95bfe3c0b9..3211f48a3d48c 100644 --- a/packages/cli/src/commands/start.ts +++ b/packages/cli/src/commands/start.ts @@ -24,7 +24,6 @@ import * as GenericHelpers from '@/GenericHelpers'; import * as Server from '@/Server'; import { TestWebhooks } from '@/TestWebhooks'; import { getAllInstalledPackages } from '@/CommunityNodes/packageModel'; -import { handleLdapInit } from '@/Ldap/helpers'; import { EDITOR_UI_DIST_DIR, GENERATED_STATIC_DIR } from '@/constants'; import { eventBus } from '@/eventbus'; import { BaseCommand } from './BaseCommand'; @@ -326,8 +325,6 @@ export class Start extends BaseCommand { ); } - await handleLdapInit(); - await Server.start(); // Start to get active workflows and run their triggers From 068de92a879b8c08ee47d7f60172786eeb8067f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Fri, 10 Mar 2023 18:39:16 +0100 Subject: [PATCH 2/2] initLicense should happen much earlier --- packages/cli/src/Server.ts | 16 ---------------- packages/cli/src/commands/BaseCommand.ts | 8 +++++--- packages/cli/src/commands/start.ts | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/packages/cli/src/Server.ts b/packages/cli/src/Server.ts index c7d982dc86791..d8d26e98e1059 100644 --- a/packages/cli/src/Server.ts +++ b/packages/cli/src/Server.ts @@ -130,7 +130,6 @@ import * as WorkflowExecuteAdditionalData from '@/WorkflowExecuteAdditionalData' import { toHttpNodeParameters } from '@/CurlConverterHelper'; import { eventBusRouter } from '@/eventbus/eventBusRoutes'; import { isLogStreamingEnabled } from '@/eventbus/MessageEventBus/MessageEventBusHelper'; -import { getLicense } from '@/License'; import { licenseController } from './license/license.controller'; import { Push, setupPushServer, setupPushHandler } from '@/push'; import { setupAuthMiddlewares } from './middlewares'; @@ -362,20 +361,6 @@ class Server extends AbstractServer { return this.frontendSettings; } - async initLicense(): Promise { - const license = getLicense(); - await license.init(this.frontendSettings.instanceId); - - const activationKey = config.getEnv('license.activationKey'); - if (activationKey) { - try { - await license.activate(activationKey); - } catch (e) { - LoggerProxy.error('Could not activate license', e); - } - } - } - private registerControllers(ignoredEndpoints: Readonly) { const { app, externalHooks, activeWorkflowRunner, nodeTypes } = this; const repositories = Db.collections; @@ -433,7 +418,6 @@ class Server extends AbstractServer { await this.externalHooks.run('frontend.settings', [this.frontendSettings]); - await this.initLicense(); await this.postHog.init(this.frontendSettings.instanceId); const publicApiEndpoint = config.getEnv('publicApi.path'); diff --git a/packages/cli/src/commands/BaseCommand.ts b/packages/cli/src/commands/BaseCommand.ts index 96e19f1d16336..d50846cf34cc4 100644 --- a/packages/cli/src/commands/BaseCommand.ts +++ b/packages/cli/src/commands/BaseCommand.ts @@ -34,6 +34,8 @@ export abstract class BaseCommand extends Command { protected userSettings: IUserSettings; + protected instanceId: string; + async init(): Promise { await initErrorHandling(); @@ -49,9 +51,9 @@ export abstract class BaseCommand extends Command { const credentialTypes = Container.get(CredentialTypes); CredentialsOverwrites(credentialTypes); - const instanceId = this.userSettings.instanceId ?? ''; - await Container.get(PostHogClient).init(instanceId); - await Container.get(InternalHooks).init(instanceId); + this.instanceId = this.userSettings.instanceId ?? ''; + await Container.get(PostHogClient).init(this.instanceId); + await Container.get(InternalHooks).init(this.instanceId); await Db.init().catch(async (error: Error) => this.exitWithCrash('There was an error initializing DB', error), diff --git a/packages/cli/src/commands/start.ts b/packages/cli/src/commands/start.ts index 3211f48a3d48c..1363870a207ba 100644 --- a/packages/cli/src/commands/start.ts +++ b/packages/cli/src/commands/start.ts @@ -28,6 +28,7 @@ import { EDITOR_UI_DIST_DIR, GENERATED_STATIC_DIR } from '@/constants'; import { eventBus } from '@/eventbus'; import { BaseCommand } from './BaseCommand'; import { InternalHooks } from '@/InternalHooks'; +import { getLicense } from '@/License'; // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-var-requires const open = require('open'); @@ -181,11 +182,26 @@ export class Start extends BaseCommand { await Promise.all(files.map(compileFile)); } + async initLicense(): Promise { + const license = getLicense(); + await license.init(this.instanceId); + + const activationKey = config.getEnv('license.activationKey'); + if (activationKey) { + try { + await license.activate(activationKey); + } catch (e) { + LoggerProxy.error('Could not activate license', e as Error); + } + } + } + async init() { await this.initCrashJournal(); await super.init(); this.logger.info('Initializing n8n process'); + await this.initLicense(); await this.initBinaryManager(); await this.initExternalHooks();