Skip to content

Commit 56cf525

Browse files
committed
refactor(core): Simplify SecretsHelper setup and move it to core
1 parent 4b11268 commit 56cf525

File tree

18 files changed

+94
-106
lines changed

18 files changed

+94
-106
lines changed

packages/cli/src/__tests__/workflow-execute-additional-data.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ExecutionRepository } from '@n8n/db';
44
import { WorkflowRepository } from '@n8n/db';
55
import { Container } from '@n8n/di';
66
import { mock } from 'jest-mock-extended';
7+
import { SecretsHelper } from 'n8n-core';
78
import type { IWorkflowBase } from 'n8n-workflow';
89
import type {
910
IExecuteWorkflowInfo,
@@ -23,7 +24,6 @@ import {
2324
SubworkflowPolicyChecker,
2425
} from '@/executions/pre-execution-checks';
2526
import { ExternalHooks } from '@/external-hooks';
26-
import { SecretsHelper } from '@/secrets-helpers.ee';
2727
import { UrlService } from '@/services/url.service';
2828
import { WorkflowStatisticsService } from '@/services/workflow-statistics.service';
2929
import { Telemetry } from '@/telemetry';
@@ -306,7 +306,7 @@ describe('WorkflowExecuteAdditionalData', () => {
306306
userId: undefined,
307307
setExecutionStatus: expect.any(Function),
308308
variables: mockVariables,
309-
secretsHelpers: secretsHelper,
309+
secretsHelper,
310310
startRunnerTask: expect.any(Function),
311311
logAiEvent: expect.any(Function),
312312
});

packages/cli/src/commands/base-command.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
ObjectStoreService,
1212
DataDeduplicationService,
1313
ErrorReporter,
14+
SecretsHelper,
1415
} from 'n8n-core';
1516
import { ensureError, sleep, UserError } from 'n8n-workflow';
1617

@@ -278,6 +279,7 @@ export abstract class BaseCommand extends Command {
278279
async initExternalSecrets() {
279280
const secretsManager = Container.get(ExternalSecretsManager);
280281
await secretsManager.init();
282+
Container.get(SecretsHelper).setManager(secretsManager);
281283
}
282284

283285
initWorkflowHistory() {

packages/cli/src/controllers/oauth/__tests__/oauth1-credential.controller.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Container } from '@n8n/di';
66
import Csrf from 'csrf';
77
import type { Response } from 'express';
88
import { captor, mock } from 'jest-mock-extended';
9-
import { Cipher, type InstanceSettings } from 'n8n-core';
9+
import { Cipher, type InstanceSettings, SecretsHelper } from 'n8n-core';
1010
import type { IWorkflowExecuteAdditionalData } from 'n8n-workflow';
1111
import nock from 'nock';
1212

@@ -19,7 +19,6 @@ import { BadRequestError } from '@/errors/response-errors/bad-request.error';
1919
import { NotFoundError } from '@/errors/response-errors/not-found.error';
2020
import { ExternalHooks } from '@/external-hooks';
2121
import type { OAuthRequest } from '@/requests';
22-
import { SecretsHelper } from '@/secrets-helpers.ee';
2322
import * as WorkflowExecuteAdditionalData from '@/workflow-execute-additional-data';
2423
import { mockInstance } from '@test/mocking';
2524

packages/cli/src/controllers/oauth/__tests__/oauth2-credential.controller.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Container } from '@n8n/di';
66
import Csrf from 'csrf';
77
import { type Response } from 'express';
88
import { captor, mock } from 'jest-mock-extended';
9-
import { Cipher, type InstanceSettings } from 'n8n-core';
9+
import { Cipher, type InstanceSettings, SecretsHelper } from 'n8n-core';
1010
import type { IWorkflowExecuteAdditionalData } from 'n8n-workflow';
1111
import nock from 'nock';
1212

@@ -19,7 +19,6 @@ import { BadRequestError } from '@/errors/response-errors/bad-request.error';
1919
import { NotFoundError } from '@/errors/response-errors/not-found.error';
2020
import { ExternalHooks } from '@/external-hooks';
2121
import type { OAuthRequest } from '@/requests';
22-
import { SecretsHelper } from '@/secrets-helpers.ee';
2322
import * as WorkflowExecuteAdditionalData from '@/workflow-execute-additional-data';
2423
import { mockInstance } from '@test/mocking';
2524

packages/cli/src/credentials-helper.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,6 @@ export class CredentialsHelper extends ICredentialsHelper {
327327
return decryptedDataOriginal;
328328
}
329329

330-
await additionalData?.secretsHelpers?.waitForInit();
331-
332330
return await this.applyDefaultsAndOverwrites(
333331
additionalData,
334332
decryptedDataOriginal,

packages/cli/src/eventbus/message-event-bus-destination/message-event-bus-destination-webhook.ee.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Container } from '@n8n/di';
44
import axios from 'axios';
55
import type { AxiosRequestConfig, Method } from 'axios';
66
import { Agent as HTTPSAgent } from 'https';
7+
import { SecretsHelper } from 'n8n-core';
78
import { jsonParse, MessageEventBusDestinationTypeNames } from 'n8n-workflow';
89
import type {
910
MessageEventBusDestinationOptions,
@@ -14,7 +15,6 @@ import type {
1415
} from 'n8n-workflow';
1516

1617
import { CredentialsHelper } from '@/credentials-helper';
17-
import { SecretsHelper } from '@/secrets-helpers.ee';
1818

1919
import { MessageEventBusDestination } from './message-event-bus-destination.ee';
2020
import { eventMessageGenericDestinationTestEvent } from '../event-message-classes/event-message-generic';
@@ -103,7 +103,7 @@ export class MessageEventBusDestinationWebhook
103103
if (foundCredential) {
104104
const credentialsDecrypted = await this.credentialsHelper?.getDecrypted(
105105
{
106-
secretsHelpers: Container.get(SecretsHelper),
106+
secretsHelper: Container.get(SecretsHelper),
107107
} as unknown as IWorkflowExecuteAdditionalData,
108108
foundCredential[1],
109109
foundCredential[0],

packages/cli/src/external-secrets.ee/external-secrets-manager.ee.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ export class ExternalSecretsManager {
211211
return provider in this.providers;
212212
}
213213

214-
getProviderNames(): string[] | undefined {
214+
getProviderNames(): string[] {
215215
return Object.keys(this.providers);
216216
}
217217

packages/cli/src/scaling/__tests__/job-processor.service.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { IExecutionResponse } from '@n8n/db';
33
import type { ExecutionRepository } from '@n8n/db';
44
import { mock } from 'jest-mock-extended';
55
import type { WorkflowExecute as ActualWorkflowExecute } from 'n8n-core';
6+
import { SecretsHelper } from 'n8n-core';
67
import { mockInstance } from 'n8n-core/test/utils';
78
import type { IPinData, ITaskData, IWorkflowExecuteAdditionalData } from 'n8n-workflow';
89
import { Workflow, type IRunExecutionData, type WorkflowExecuteMode } from 'n8n-workflow';
@@ -11,7 +12,6 @@ import { CredentialsHelper } from '@/credentials-helper';
1112
import { VariablesService } from '@/environments.ee/variables/variables.service.ee';
1213
import { ExternalHooks } from '@/external-hooks';
1314
import type { ManualExecutionService } from '@/manual-execution.service';
14-
import { SecretsHelper } from '@/secrets-helpers.ee';
1515
import { WorkflowStatisticsService } from '@/services/workflow-statistics.service';
1616
import * as WorkflowExecuteAdditionalData from '@/workflow-execute-additional-data';
1717
import { WorkflowStaticDataService } from '@/workflows/workflow-static-data.service';

packages/cli/src/secrets-helpers.ee.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

packages/cli/src/workflow-execute-additional-data.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Logger } from '@n8n/backend-common';
77
import { GlobalConfig } from '@n8n/config';
88
import { ExecutionRepository, WorkflowRepository } from '@n8n/db';
99
import { Container } from '@n8n/di';
10-
import { WorkflowExecute } from 'n8n-core';
10+
import { SecretsHelper, WorkflowExecute } from 'n8n-core';
1111
import { UnexpectedError, Workflow } from 'n8n-workflow';
1212
import type {
1313
IDataObject,
@@ -46,7 +46,6 @@ import {
4646
import type { UpdateExecutionPayload } from '@/interfaces';
4747
import { NodeTypes } from '@/node-types';
4848
import { Push } from '@/push';
49-
import { SecretsHelper } from '@/secrets-helpers.ee';
5049
import { UrlService } from '@/services/url.service';
5150
import { TaskRequester } from '@/task-runners/task-managers/task-requester';
5251
import { findSubworkflowStart } from '@/utils';
@@ -388,7 +387,7 @@ export async function getBase(
388387
userId,
389388
setExecutionStatus,
390389
variables,
391-
secretsHelpers: Container.get(SecretsHelper),
390+
secretsHelper: Container.get(SecretsHelper),
392391
async startRunnerTask(
393392
additionalData: IWorkflowExecuteAdditionalData,
394393
jobType: string,

packages/cli/test/integration/active-workflow-manager.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { WebhookEntity } from '@n8n/db';
22
import { WorkflowRepository } from '@n8n/db';
33
import { Container } from '@n8n/di';
44
import { mock } from 'jest-mock-extended';
5-
import { InstanceSettings } from 'n8n-core';
5+
import { InstanceSettings, SecretsHelper } from 'n8n-core';
66
import { FormTrigger } from 'n8n-nodes-base/nodes/Form/FormTrigger.node';
77
import { ScheduleTrigger } from 'n8n-nodes-base/nodes/Schedule/ScheduleTrigger.node';
88
import { NodeApiError, Workflow } from 'n8n-workflow';
@@ -19,7 +19,6 @@ import { ExecutionService } from '@/executions/execution.service';
1919
import { ExternalHooks } from '@/external-hooks';
2020
import { NodeTypes } from '@/node-types';
2121
import { Push } from '@/push';
22-
import { SecretsHelper } from '@/secrets-helpers.ee';
2322
import * as WebhookHelpers from '@/webhooks/webhook-helpers';
2423
import { WebhookService } from '@/webhooks/webhook.service';
2524
import * as AdditionalData from '@/workflow-execute-additional-data';

packages/core/src/execution-engine/__tests__/execution-lifecycle-hooks.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type {
1313

1414
import type {
1515
ExecutionLifecycleHookName,
16-
ExecutionLifecyleHookHandlers,
16+
ExecutionLifecycleHookHandlers,
1717
} from '../execution-lifecycle-hooks';
1818
import { ExecutionLifecycleHooks } from '../execution-lifecycle-hooks';
1919

@@ -46,12 +46,14 @@ describe('ExecutionLifecycleHooks', () => {
4646
describe('addHandler()', () => {
4747
const hooksHandlers =
4848
mock<{
49-
[K in keyof ExecutionLifecyleHookHandlers]: ExecutionLifecyleHookHandlers[K][number];
49+
[K in keyof ExecutionLifecycleHookHandlers]: ExecutionLifecycleHookHandlers[K][number];
5050
}>();
5151

5252
const testCases: Array<{
5353
hook: ExecutionLifecycleHookName;
54-
args: Parameters<ExecutionLifecyleHookHandlers[keyof ExecutionLifecyleHookHandlers][number]>;
54+
args: Parameters<
55+
ExecutionLifecycleHookHandlers[keyof ExecutionLifecycleHookHandlers][number]
56+
>;
5557
}> = [
5658
{ hook: 'nodeExecuteBefore', args: ['testNode', mock<ITaskStartedData>()] },
5759
{

packages/core/src/execution-engine/execution-lifecycle-hooks.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {
1111
WorkflowExecuteMode,
1212
} from 'n8n-workflow';
1313

14-
export type ExecutionLifecyleHookHandlers = {
14+
export type ExecutionLifecycleHookHandlers = {
1515
nodeExecuteBefore: Array<
1616
(
1717
this: ExecutionLifecycleHooks,
@@ -56,7 +56,7 @@ s */
5656
>;
5757
};
5858

59-
export type ExecutionLifecycleHookName = keyof ExecutionLifecyleHookHandlers;
59+
export type ExecutionLifecycleHookName = keyof ExecutionLifecycleHookHandlers;
6060

6161
/**
6262
* Contains hooks that trigger at specific events in an execution's lifecycle. Every hook has an array of callbacks to run.
@@ -77,7 +77,7 @@ export type ExecutionLifecycleHookName = keyof ExecutionLifecyleHookHandlers;
7777
* ```
7878
*/
7979
export class ExecutionLifecycleHooks {
80-
readonly handlers: ExecutionLifecyleHookHandlers = {
80+
readonly handlers: ExecutionLifecycleHookHandlers = {
8181
nodeExecuteAfter: [],
8282
nodeExecuteBefore: [],
8383
nodeFetchedData: [],
@@ -92,18 +92,18 @@ export class ExecutionLifecycleHooks {
9292
readonly workflowData: IWorkflowBase,
9393
) {}
9494

95-
addHandler<Hook extends keyof ExecutionLifecyleHookHandlers>(
95+
addHandler<Hook extends keyof ExecutionLifecycleHookHandlers>(
9696
hookName: Hook,
97-
...handlers: Array<ExecutionLifecyleHookHandlers[Hook][number]>
97+
...handlers: Array<ExecutionLifecycleHookHandlers[Hook][number]>
9898
): void {
9999
// @ts-expect-error FIX THIS
100100
this.handlers[hookName].push(...handlers);
101101
}
102102

103103
async runHook<
104-
Hook extends keyof ExecutionLifecyleHookHandlers,
104+
Hook extends keyof ExecutionLifecycleHookHandlers,
105105
Params extends unknown[] = Parameters<
106-
Exclude<ExecutionLifecyleHookHandlers[Hook], undefined>[number]
106+
Exclude<ExecutionLifecycleHookHandlers[Hook], undefined>[number]
107107
>,
108108
>(hookName: Hook, parameters: Params) {
109109
const hooks = this.handlers[hookName];
@@ -116,9 +116,3 @@ export class ExecutionLifecycleHooks {
116116
}
117117
}
118118
}
119-
120-
declare module 'n8n-workflow' {
121-
interface IWorkflowExecuteAdditionalData {
122-
hooks?: ExecutionLifecycleHooks;
123-
}
124-
}

packages/core/src/execution-engine/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
import type { ExecutionLifecycleHooks } from './execution-lifecycle-hooks';
2+
import type { SecretsHelper } from './secrets-helper.ee';
3+
4+
declare module 'n8n-workflow' {
5+
interface IWorkflowExecuteAdditionalData {
6+
hooks?: ExecutionLifecycleHooks;
7+
secretsHelper: SecretsHelper;
8+
}
9+
}
10+
111
export * from './active-workflows';
212
export * from './interfaces';
313
export * from './routing-node';
@@ -6,3 +16,4 @@ export * from './partial-execution-utils';
616
export * from './node-execution-context/utils/execution-metadata';
717
export * from './workflow-execute';
818
export { ExecutionLifecycleHooks } from './execution-lifecycle-hooks';
19+
export { SecretsHelper } from './secrets-helper.ee';

packages/core/src/execution-engine/node-execution-context/utils/__tests__/get-additional-keys.test.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
import { mock } from 'jest-mock-extended';
22
import { LoggerProxy } from 'n8n-workflow';
3-
import type {
4-
IDataObject,
5-
IRunExecutionData,
6-
IWorkflowExecuteAdditionalData,
7-
SecretsHelpersBase,
8-
} from 'n8n-workflow';
3+
import type { IDataObject, IRunExecutionData, IWorkflowExecuteAdditionalData } from 'n8n-workflow';
94

105
import { PLACEHOLDER_EMPTY_EXECUTION_ID } from '@/constants';
6+
import type { SecretsHelper } from '@/execution-engine/secrets-helper.ee';
117

128
import { getAdditionalKeys } from '../get-additional-keys';
139

1410
describe('getAdditionalKeys', () => {
15-
const secretsHelpers = mock<SecretsHelpersBase>();
11+
const secretsHelper = mock<SecretsHelper>();
1612
const additionalData = mock<IWorkflowExecuteAdditionalData>({
1713
executionId: '123',
1814
webhookWaitingBaseUrl: 'https://webhook.test',
1915
formWaitingBaseUrl: 'https://form.test',
2016
variables: { testVar: 'value' },
21-
secretsHelpers,
17+
secretsHelper,
2218
});
2319

2420
const runExecutionData = mock<IRunExecutionData>({
@@ -30,11 +26,11 @@ describe('getAdditionalKeys', () => {
3026

3127
beforeAll(() => {
3228
LoggerProxy.init(mock());
33-
secretsHelpers.hasProvider.mockReturnValue(true);
34-
secretsHelpers.hasSecret.mockReturnValue(true);
35-
secretsHelpers.getSecret.mockReturnValue('secret-value');
36-
secretsHelpers.listSecrets.mockReturnValue(['secret1']);
37-
secretsHelpers.listProviders.mockReturnValue(['provider1']);
29+
secretsHelper.hasProvider.mockReturnValue(true);
30+
secretsHelper.hasSecret.mockReturnValue(true);
31+
secretsHelper.getSecret.mockReturnValue('secret-value');
32+
secretsHelper.listSecrets.mockReturnValue(['secret1']);
33+
secretsHelper.listProviders.mockReturnValue(['provider1']);
3834
});
3935

4036
it('should use placeholder execution ID when none provided', () => {

0 commit comments

Comments
 (0)