From 92900fd887f11487ab019d9fcac90e87dc10f59a Mon Sep 17 00:00:00 2001 From: Minijackson Date: Tue, 21 May 2024 20:17:42 +0200 Subject: [PATCH 1/2] Fixes #15962: support Redis Unix sockets --- docs/configuration/required-parameters.md | 19 +++++++++++++++++++ netbox/netbox/settings.py | 9 ++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/docs/configuration/required-parameters.md b/docs/configuration/required-parameters.md index bda3659952..3a286c5870 100644 --- a/docs/configuration/required-parameters.md +++ b/docs/configuration/required-parameters.md @@ -103,6 +103,25 @@ REDIS = { It is highly recommended to keep the task and cache databases separate. Using the same database number on the same Redis instance for both may result in queued background tasks being lost during cache flushing events. +### Using Redis with UNIX sockets + +Redis may be configured by using the `URL` configuration setting, +instead of specifying `HOST`, `PORT`, and so on. +This can be used to specify a UNIX socket connection. + +For example: + +```python +REDIS = { + "tasks": { + "URL": "unix:///run/redis-netbox/redis.sock?db=0" + }, + "caching": { + "URL": "unix:///run/redis-netbox/redis.sock?db=1" + }, +} +``` + ### Using Redis Sentinel If you are using [Redis Sentinel](https://redis.io/topics/sentinel) for high-availability purposes, there is minimal diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index 38df165510..1551b0722f 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -241,6 +241,7 @@ def _setting(name, default=None): TASKS_REDIS = REDIS['tasks'] TASKS_REDIS_HOST = TASKS_REDIS.get('HOST', 'localhost') TASKS_REDIS_PORT = TASKS_REDIS.get('PORT', 6379) +TASKS_REDIS_URL = TASKS_REDIS.get('URL') TASKS_REDIS_SENTINELS = TASKS_REDIS.get('SENTINELS', []) TASKS_REDIS_USING_SENTINEL = all([ isinstance(TASKS_REDIS_SENTINELS, (list, tuple)), @@ -269,7 +270,7 @@ def _setting(name, default=None): CACHING_REDIS_PROTO = 'rediss' if REDIS['caching'].get('SSL', False) else 'redis' CACHING_REDIS_SKIP_TLS_VERIFY = REDIS['caching'].get('INSECURE_SKIP_TLS_VERIFY', False) CACHING_REDIS_CA_CERT_PATH = REDIS['caching'].get('CA_CERT_PATH', False) -CACHING_REDIS_URL = f'{CACHING_REDIS_PROTO}://{CACHING_REDIS_USERNAME_HOST}:{CACHING_REDIS_PORT}/{CACHING_REDIS_DATABASE}' +CACHING_REDIS_URL = REDIS['caching'].get('URL', f'{CACHING_REDIS_PROTO}://{CACHING_REDIS_USERNAME_HOST}:{CACHING_REDIS_PORT}/{CACHING_REDIS_DATABASE}') # Configure Django's default cache to use Redis CACHES = { @@ -669,6 +670,12 @@ def _setting(name, default=None): 'socket_connect_timeout': TASKS_REDIS_SENTINEL_TIMEOUT }, } +elif TASKS_REDIS_URL: + RQ_PARAMS = { + 'URL': TASKS_REDIS_URL, + 'SSL': TASKS_REDIS_SSL, + 'SSL_CERT_REQS': None if TASKS_REDIS_SKIP_TLS_VERIFY else 'required', + } else: RQ_PARAMS = { 'HOST': TASKS_REDIS_HOST, From 48db48c8d467a15eb502fb5573844edfa985cd64 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Tue, 21 May 2024 16:36:24 -0400 Subject: [PATCH 2/2] Clean up language & remove obsolete note --- docs/configuration/required-parameters.md | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/docs/configuration/required-parameters.md b/docs/configuration/required-parameters.md index 3a286c5870..90eb8c0cfc 100644 --- a/docs/configuration/required-parameters.md +++ b/docs/configuration/required-parameters.md @@ -94,30 +94,21 @@ REDIS = { } ``` -!!! note - If you are upgrading from a NetBox release older than v2.7.0, please note that the Redis connection configuration - settings have changed. Manual modification to bring the `REDIS` section inline with the above specification is - necessary - !!! warning It is highly recommended to keep the task and cache databases separate. Using the same database number on the same Redis instance for both may result in queued background tasks being lost during cache flushing events. -### Using Redis with UNIX sockets - -Redis may be configured by using the `URL` configuration setting, -instead of specifying `HOST`, `PORT`, and so on. -This can be used to specify a UNIX socket connection. +### UNIX Socket Support -For example: +Redis may alternatively be configured by specifying a complete URL instead of individual components. This approach supports the use of a UNIX socket connection. For example: ```python REDIS = { - "tasks": { - "URL": "unix:///run/redis-netbox/redis.sock?db=0" + 'tasks': { + 'URL': 'unix:///run/redis-netbox/redis.sock?db=0' }, - "caching": { - "URL": "unix:///run/redis-netbox/redis.sock?db=1" + 'caching': { + 'URL': 'unix:///run/redis-netbox/redis.sock?db=1' }, } ```