netbox icon indicating copy to clipboard operation
netbox copied to clipboard

Support Redis Unix sockets

Open tacerus opened this issue 1 year ago • 1 comments

Deployment Type

Self-hosted

NetBox Version

3.7.5

Python Version

3.11

Steps to Reproduce

Re-open the issue in https://github.com/netbox-community/netbox/issues/4377.

Expected Behavior

Unix sockets should be supported.

Observed Behavior

https://github.com/netbox-community/netbox/pull/15590 was closed.

tacerus avatar May 04 '24 19:05 tacerus

In https://github.com/netbox-community/netbox/pull/15590#issuecomment-2029621652 additional discussion was desired. What are the concerns? No discussion happened in https://github.com/netbox-community/netbox/issues/4377. Can we implement the patch from @evlli in https://github.com/netbox-community/netbox/pull/15590?

I have an alternative patch attached, but the already submitted one seems more flexible and thought through.

Patch
--- a/netbox/netbox/settings.py	2024-05-04 21:35:18.459236034 +0200
+++ b/netbox/netbox/settings.py	2024-05-04 21:35:05.462490445 +0200
@@ -277,6 +277,7 @@
 TASKS_REDIS_SSL = TASKS_REDIS.get('SSL', False)
 TASKS_REDIS_SKIP_TLS_VERIFY = TASKS_REDIS.get('INSECURE_SKIP_TLS_VERIFY', False)
 TASKS_REDIS_CA_CERT_PATH = TASKS_REDIS.get('CA_CERT_PATH', False)
+TASKS_REDIS_USE_UNIX_SOCKET = TASKS_REDIS.get('USE_UNIX_SOCKET', False)
 
 # Caching
 if 'caching' not in REDIS:
@@ -292,13 +293,21 @@
 CACHING_REDIS_SENTINELS = REDIS['caching'].get('SENTINELS', [])
 CACHING_REDIS_SENTINEL_SERVICE = REDIS['caching'].get('SENTINEL_SERVICE', 'default')
 CACHING_REDIS_PROTO = 'rediss' if REDIS['caching'].get('SSL', False) else 'redis'
+CACHING_REDIS_PROTO = 'unix' if REDIS['caching'].get('USE_UNIX_SOCKET', False) else CACHING_REDIS_PROTO
 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)
 
+if CACHING_REDIS_PROTO == 'unix':
+    CACHING_REDIS_PORT=''
+    CACHING_REDIS_DATABASE=f'?db={CACHING_REDIS_DATABASE}'
+else:
+    CACHING_REDIS_PORT=f':{CACHING_REDIS_PORT}'
+    CACHING_REDIS_DATABASE=f'/{CACHING_REDIS_DATABASE}'
+
 CACHES = {
     'default': {
         'BACKEND': 'django_redis.cache.RedisCache',
-        'LOCATION': f'{CACHING_REDIS_PROTO}://{CACHING_REDIS_USERNAME_HOST}:{CACHING_REDIS_PORT}/{CACHING_REDIS_DATABASE}',
+        'LOCATION': f'{CACHING_REDIS_PROTO}://{CACHING_REDIS_USERNAME_HOST}{CACHING_REDIS_PORT}{CACHING_REDIS_DATABASE}',
         'OPTIONS': {
             'CLIENT_CLASS': 'django_redis.client.DefaultClient',
             'PASSWORD': CACHING_REDIS_PASSWORD,
@@ -688,12 +697,18 @@
         },
     }
 else:
-    RQ_PARAMS = {
-        'HOST': TASKS_REDIS_HOST,
-        'PORT': TASKS_REDIS_PORT,
-        'SSL': TASKS_REDIS_SSL,
-        'SSL_CERT_REQS': None if TASKS_REDIS_SKIP_TLS_VERIFY else 'required',
-    }
+    if TASKS_REDIS_USE_UNIX_SOCKET:
+        RQ_PARAMS = {
+            'UNIX_SOCKET_PATH': TASKS_REDIS_HOST,
+        }
+    else:
+        RQ_PARAMS = {
+            'HOST': TASKS_REDIS_HOST,
+            'PORT': TASKS_REDIS_PORT,
+            'SSL': TASKS_REDIS_SSL,
+            'SSL_CERT_REQS': None if TASKS_REDIS_SKIP_TLS_VERIFY else 'required',
+        }
+
 RQ_PARAMS.update({
     'DB': TASKS_REDIS_DATABASE,
     'USERNAME': TASKS_REDIS_USERNAME,

tacerus avatar May 04 '24 19:05 tacerus

Thank you for opening a bug report. It seems that the described functionality is intended behavior. If you meant to open a feature request instead, please close this issue and open a new one using the feature request template. Otherwise, please revise your post above to elaborate on why you believe the observed behavior is flawed.

DanSheps avatar May 06 '24 13:05 DanSheps