Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

fix: 7317 support redis tls (#7404)

pawkae1e963613314
diff --git a/config/default.yaml b/config/default.yaml
index 9305794231..5757a82c2c 100644
--- a/config/default.yaml
+++ b/config/default.yaml
@@ -95,9 +95,20 @@ redis:
port: 6379
auth: null
db: 0
+ tls: false
+ tls_settings:
+ reject_unauthorized: false
+ ca: '/absolute/path/to/server-certificates/root.crt'
+ cert: '/absolute/path/to/client-certificates/postgresql.crt'
+ key: '/absolute/path/to/client-key/postgresql.key'
sentinel:
enabled: false
enable_tls: false
+ tls_settings:
+ reject_unauthorized: false
+ ca: '/absolute/path/to/server-certificates/root.crt'
+ cert: '/absolute/path/to/client-certificates/postgresql.crt'
+ key: '/absolute/path/to/client-key/postgresql.key'
master_name: ''
password: ''
sentinels:
diff --git a/config/production.yaml.example b/config/production.yaml.example
index c23d1ea9fa..2c3b05b66c 100644
--- a/config/production.yaml.example
+++ b/config/production.yaml.example
@@ -93,9 +93,20 @@ redis:
port: 6379
auth: null
db: 0
+ tls: false
+ tls_settings:
+ reject_unauthorized: false
+ ca: '/absolute/path/to/server-certificates/root.crt'
+ cert: '/absolute/path/to/client-certificates/postgresql.crt'
+ key: '/absolute/path/to/client-key/postgresql.key'
sentinel:
enabled: false
enable_tls: false
+ tls_settings:
+ reject_unauthorized: false
+ ca: '/absolute/path/to/server-certificates/root.crt'
+ cert: '/absolute/path/to/client-certificates/postgresql.crt'
+ key: '/absolute/path/to/client-key/postgresql.key'
master_name: ''
password: ''
sentinels:
diff --git a/server/core/initializers/config.ts b/server/core/initializers/config.ts
index 5fdd298521..f75b3c7823 100644
--- a/server/core/initializers/config.ts
+++ b/server/core/initializers/config.ts
@@ -51,9 +51,42 @@ const CONFIG = {
SOCKET: config.has('redis.socket') ? config.get<string>('redis.socket') : null,
AUTH: config.has('redis.auth') ? config.get<string>('redis.auth') : null,
DB: config.has('redis.db') ? config.get<number>('redis.db') : null,
+ TLS: config.has('redis.tls') ? config.get<boolean>('redis.tls') : false,
+ TLS_SETTINGS: {
+ get REJECT_UNAUTHORIZED () {
+ return config.has('redis.tls_settings.reject_unauthorized')
+ ? config.get<boolean>('redis.tls_settings.reject_unauthorized')
+ : false
+ },
+ get CA() {
+ return config.has('redis.tls_settings.ca') ? config.get<string>('redis.tls_settings.ca') : null
+ },
+ get CERT() {
+ return config.has('redis.tls_settings.cert') ? config.get<string>('redis.tls_settings.cert') : null
+ },
+ get KEY() {
+ return config.has('redis.tls_settings.key') ? config.get<string>('redis.tls_settings.key') : null
+ }
+ },
SENTINEL: {
ENABLED: config.has('redis.sentinel.enabled') ? config.get<boolean>('redis.sentinel.enabled') : false,
ENABLE_TLS: config.has('redis.sentinel.enable_tls') ? config.get<boolean>('redis.sentinel.enable_tls') : false,
+ TLS_SETTINGS: {
+ get REJECT_UNAUTHORIZED () {
+ return config.has('redis.sentinel.tls_settings.reject_unauthorized')
+ ? config.get<boolean>('redis.sentinel.tls_settings.reject_unauthorized')
+ : false
+ },
+ get CA() {
+ return config.has('redis.sentinel.tls_settings.ca') ? config.get<string>('redis.sentinel.tls_settings.ca') : null
+ },
+ get CERT() {
+ return config.has('redis.sentinel.tls_settings.cert') ? config.get<string>('redis.sentinel.tls_settings.cert') : null
+ },
+ get KEY() {
+ return config.has('redis.sentinel.tls_settings.key') ? config.get<string>('redis.sentinel.tls_settings.key') : null
+ }
+ },
SENTINELS: config.has('redis.sentinel.sentinels') ? config.get<{ host: string, port: number }[]>('redis.sentinel.sentinels') : [],
MASTER_NAME: config.has('redis.sentinel.master_name') ? config.get<string>('redis.sentinel.master_name') : null,
PASSWORD: config.has('redis.sentinel.password') ? config.get<string>('redis.sentinel.password') : null
diff --git a/server/core/lib/redis.ts b/server/core/lib/redis.ts
index 937c8e34c0..d722525e5b 100644
--- a/server/core/lib/redis.ts
+++ b/server/core/lib/redis.ts
@@ -1,6 +1,8 @@
import { sha256 } from '@peertube/peertube-node-utils'
import { exists } from '@server/helpers/custom-validators/misc.js'
import { Redis as IoRedis, RedisOptions } from 'ioredis'
+import { readFileSync } from 'node:fs'
+import { ConnectionOptions } from 'node:tls'
import { logger, loggerTagsFactory } from '../helpers/logger.js'
import { generateRandomString } from '../helpers/utils.js'
import { CONFIG } from '../initializers/config.js'
@@ -71,6 +73,21 @@ class Redis {
)
}
+ let sentinelTLS: ConnectionOptions = undefined
+ if (CONFIG.REDIS.SENTINEL.ENABLE_TLS) {
+ sentinelTLS = { rejectUnauthorized: CONFIG.REDIS.SENTINEL.TLS_SETTINGS.REJECT_UNAUTHORIZED }
+
+ if (CONFIG.REDIS.SENTINEL.TLS_SETTINGS.CA) {
+ sentinelTLS.ca = readFileSync(CONFIG.REDIS.SENTINEL.TLS_SETTINGS.CA, { encoding: 'utf8' })
+ }
+ if (CONFIG.REDIS.SENTINEL.TLS_SETTINGS.CERT) {
+ sentinelTLS.cert = readFileSync(CONFIG.REDIS.SENTINEL.TLS_SETTINGS.CERT, { encoding: 'utf8' })
+ }
+ if (CONFIG.REDIS.SENTINEL.TLS_SETTINGS.KEY) {
+ sentinelTLS.key = readFileSync(CONFIG.REDIS.SENTINEL.TLS_SETTINGS.KEY, { encoding: 'utf8' })
+ }
+ }
+
return {
connectionName,
connectTimeout,
@@ -79,6 +96,7 @@ class Redis {
password: CONFIG.REDIS.AUTH,
sentinels: CONFIG.REDIS.SENTINEL.SENTINELS,
name: CONFIG.REDIS.SENTINEL.MASTER_NAME,
+ sentinelTLS,
...options
}
}
@@ -90,6 +108,21 @@ class Redis {
)
}
+ let tls: ConnectionOptions = undefined
+ if (CONFIG.REDIS.TLS) {
+ tls = { rejectUnauthorized: CONFIG.REDIS.TLS_SETTINGS.REJECT_UNAUTHORIZED }
+
+ if (CONFIG.REDIS.TLS_SETTINGS.CA) {
+ tls.ca = readFileSync(CONFIG.REDIS.TLS_SETTINGS.CA, { encoding: 'utf8' })
+ }
+ if (CONFIG.REDIS.TLS_SETTINGS.CERT) {
+ tls.cert = readFileSync(CONFIG.REDIS.TLS_SETTINGS.CERT, { encoding: 'utf8' })
+ }
+ if (CONFIG.REDIS.TLS_SETTINGS.KEY) {
+ tls.key = readFileSync(CONFIG.REDIS.TLS_SETTINGS.KEY, { encoding: 'utf8' })
+ }
+ }
+
return {
connectionName,
connectTimeout,
@@ -99,6 +132,7 @@ class Redis {
port: CONFIG.REDIS.PORT,
path: CONFIG.REDIS.SOCKET,
showFriendlyErrorStack: true,
+ tls,
...options
}
}