From 77e29ffdf6507b041f85f048314851e92f682887 Mon Sep 17 00:00:00 2001 From: Thomas S Date: Thu, 29 Dec 2022 00:17:58 +0100 Subject: [PATCH] feat(redis): add compatibility to redis > 6 ACLs system using username --- packages/cli/src/Queue.ts | 25 ++++++++++++++++++++++++- packages/cli/src/commands/start.ts | 4 ++++ packages/cli/src/commands/webhook.ts | 4 ++++ packages/cli/src/config/schema.ts | 6 ++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/Queue.ts b/packages/cli/src/Queue.ts index f68c8c82b2006..ccf4bdad9d420 100644 --- a/packages/cli/src/Queue.ts +++ b/packages/cli/src/Queue.ts @@ -3,6 +3,7 @@ import { IExecuteResponsePromiseData } from 'n8n-workflow'; import config from '@/config'; import * as ActiveExecutions from '@/ActiveExecutions'; import * as WebhookHelpers from '@/WebhookHelpers'; +import { RedisOptions } from 'ioredis'; export type Job = Bull.Job; export type JobQueue = Bull.Queue; @@ -30,7 +31,29 @@ export class Queue { this.activeExecutions = ActiveExecutions.getInstance(); const prefix = config.getEnv('queue.bull.prefix'); - const redisOptions = config.getEnv('queue.bull.redis'); + const redisHost = config.getEnv('queue.bull.redis.host'); + const redisUsername = config.getEnv('queue.bull.redis.username'); + const redisPassword = config.getEnv('queue.bull.redis.password'); + const redisPort = config.getEnv('queue.bull.redis.port'); + const redisDB = config.getEnv('queue.bull.redis.db'); + // retro-compatibility with redis < 6 + // prepare new redis options setting in order to define only set values + const redisOptions: RedisOptions = {}; + if (redisHost) { + redisOptions.host = redisHost; + } + if (redisUsername) { + redisOptions.username = redisUsername; + } + if (redisPassword) { + redisOptions.password = redisPassword; + } + if (redisPort) { + redisOptions.port = redisPort; + } + if (redisDB) { + redisOptions.db = redisDB; + } // Disabling ready check is necessary as it allows worker to // quickly reconnect to Redis if Redis crashes or is unreachable // for some time. With it enabled, worker might take minutes to realize diff --git a/packages/cli/src/commands/start.ts b/packages/cli/src/commands/start.ts index 9d9e084a76ea5..a9287da60be4d 100644 --- a/packages/cli/src/commands/start.ts +++ b/packages/cli/src/commands/start.ts @@ -351,6 +351,7 @@ export class Start extends Command { if (config.getEnv('executions.mode') === 'queue') { const redisHost = config.getEnv('queue.bull.redis.host'); + const redisUsername = config.getEnv('queue.bull.redis.username'); const redisPassword = config.getEnv('queue.bull.redis.password'); const redisPort = config.getEnv('queue.bull.redis.port'); const redisDB = config.getEnv('queue.bull.redis.db'); @@ -384,6 +385,9 @@ export class Start extends Command { if (redisHost) { settings.host = redisHost; } + if (redisUsername) { + settings.username = redisUsername; + } if (redisPassword) { settings.password = redisPassword; } diff --git a/packages/cli/src/commands/webhook.ts b/packages/cli/src/commands/webhook.ts index 25cacb59d6917..dac756666b780 100644 --- a/packages/cli/src/commands/webhook.ts +++ b/packages/cli/src/commands/webhook.ts @@ -160,6 +160,7 @@ export class Webhook extends Command { if (config.getEnv('executions.mode') === 'queue') { const redisHost = config.getEnv('queue.bull.redis.host'); + const redisUsername = config.getEnv('queue.bull.redis.username'); const redisPassword = config.getEnv('queue.bull.redis.password'); const redisPort = config.getEnv('queue.bull.redis.port'); const redisDB = config.getEnv('queue.bull.redis.db'); @@ -193,6 +194,9 @@ export class Webhook extends Command { if (redisHost) { settings.host = redisHost; } + if (redisUsername) { + settings.username = redisUsername; + } if (redisPassword) { settings.password = redisPassword; } diff --git a/packages/cli/src/config/schema.ts b/packages/cli/src/config/schema.ts index 10d2852ad7e5c..f9e6e754de257 100644 --- a/packages/cli/src/config/schema.ts +++ b/packages/cli/src/config/schema.ts @@ -380,6 +380,12 @@ export const schema = { default: 10000, env: 'QUEUE_BULL_REDIS_TIMEOUT_THRESHOLD', }, + username: { + doc: 'Redis Username (needs Redis >= 6)', + format: String, + default: '', + env: 'QUEUE_BULL_REDIS_USERNAME', + }, }, queueRecoveryInterval: { doc: 'If > 0 enables an active polling to the queue that can recover for Redis crashes. Given in seconds; 0 is disabled. May increase Redis traffic significantly.',