diff --git a/packages/cli/src/commands/worker.ts b/packages/cli/src/commands/worker.ts index 432b741eba1f30..a88a6ff6b04339 100644 --- a/packages/cli/src/commands/worker.ts +++ b/packages/cli/src/commands/worker.ts @@ -24,8 +24,8 @@ import { BaseCommand } from './BaseCommand'; import { ExecutionRepository } from '@db/repositories'; import { OwnershipService } from '@/services/ownership.service'; import { generateHostInstanceId } from '@/databases/utils/generators'; -// eslint-disable-next-line import/no-extraneous-dependencies -import { IConfig } from '@oclif/config'; +import type { ICredentialsOverwrite } from '@/Interfaces'; +import { CredentialsOverwrites } from '@/CredentialsOverwrites'; export class Worker extends BaseCommand { static description = '\nStarts a n8n worker'; @@ -46,12 +46,7 @@ export class Worker extends BaseCommand { static jobQueue: JobQueue; - readonly uniqueInstanceId: string; - - constructor(argv: string[], cmdConfig: IConfig) { - super(argv, cmdConfig); - this.uniqueInstanceId = generateHostInstanceId('worker'); - } + readonly uniqueInstanceId = generateHostInstanceId('worker'); /** * Stop n8n in a graceful way. @@ -372,6 +367,45 @@ export class Worker extends BaseCommand { process.exit(1); } }); + + let presetCredentialsLoaded = false; + const endpointPresetCredentials = config.getEnv('credentials.overwrite.endpoint'); + if (endpointPresetCredentials !== '') { + // POST endpoint to set preset credentials + app.post( + `/${endpointPresetCredentials}`, + async (req: express.Request, res: express.Response) => { + if (!presetCredentialsLoaded) { + const body = req.body as ICredentialsOverwrite; + + if (req.contentType !== 'application/json') { + ResponseHelper.sendErrorResponse( + res, + new Error( + 'Body must be a valid JSON, make sure the content-type is application/json', + ), + ); + return; + } + + CredentialsOverwrites().setData(body); + + await this.loadNodesAndCredentials.generateTypesForFrontend(); + + presetCredentialsLoaded = true; + + ResponseHelper.sendSuccessResponse(res, { success: true }, true, 200); + } else { + ResponseHelper.sendErrorResponse( + res, + new Error('Preset credentials can be set once'), + ); + } + }, + ); + } + + await this.externalHooks.run('worker.ready'); } // Make sure that the process does not close