]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Change: remove credentials from redis url in system status (#6104)
authorshamoon <4887959+shamoon@users.noreply.github.com>
Thu, 14 Mar 2024 15:20:34 +0000 (08:20 -0700)
committerGitHub <noreply@github.com>
Thu, 14 Mar 2024 15:20:34 +0000 (08:20 -0700)
src/documents/tests/test_api_status.py
src/documents/views.py

index 1f8940ea9bf2aad95c584c4d75fbad952af32486..96e639c357649b47e11c86c35ca764f16027d3d0 100644 (file)
@@ -100,6 +100,45 @@ class TestSystemStatus(APITestCase):
         self.assertEqual(response.status_code, status.HTTP_200_OK)
         self.assertEqual(response.data["tasks"]["redis_status"], "OK")
 
+    def test_system_status_redis_no_credentials(self):
+        """
+        GIVEN:
+            - Redis URL with credentials
+        WHEN:
+            - The user requests the system status
+        THEN:
+            - The response contains the redis URL but no credentials
+        """
+        with override_settings(
+            _CHANNELS_REDIS_URL="redis://username:password@localhost:6379",
+        ):
+            self.client.force_login(self.user)
+            response = self.client.get(self.ENDPOINT)
+            self.assertEqual(response.status_code, status.HTTP_200_OK)
+            self.assertEqual(
+                response.data["tasks"]["redis_url"],
+                "redis://localhost:6379",
+            )
+
+    def test_system_status_redis_socket(self):
+        """
+        GIVEN:
+            - Redis URL is socket
+        WHEN:
+            - The user requests the system status
+        THEN:
+            - The response contains the redis URL
+        """
+
+        with override_settings(_CHANNELS_REDIS_URL="unix:///path/to/redis.sock"):
+            self.client.force_login(self.user)
+            response = self.client.get(self.ENDPOINT)
+            self.assertEqual(response.status_code, status.HTTP_200_OK)
+            self.assertEqual(
+                response.data["tasks"]["redis_url"],
+                "unix:///path/to/redis.sock",
+            )
+
     @mock.patch("celery.app.control.Inspect.ping")
     def test_system_status_celery_ping(self, mock_ping):
         """
index 99db0a5d8a281c42b35dbed97c0c0db70f58fce5..5fa0f7eb196c89df33a51fd18df3c431bdd00614 100644 (file)
@@ -12,6 +12,7 @@ from pathlib import Path
 from time import mktime
 from unicodedata import normalize
 from urllib.parse import quote
+from urllib.parse import urlparse
 
 import pathvalidate
 from django.apps import apps
@@ -1617,6 +1618,10 @@ class SystemStatusView(GenericAPIView, PassUserMixin):
         media_stats = os.statvfs(settings.MEDIA_ROOT)
 
         redis_url = settings._CHANNELS_REDIS_URL
+        redis_url_parsed = urlparse(redis_url)
+        redis_constructed_url = f"{redis_url_parsed.scheme}://{redis_url_parsed.path or redis_url_parsed.hostname}"
+        if redis_url_parsed.hostname is not None:
+            redis_constructed_url += f":{redis_url_parsed.port}"
         redis_error = None
         with Redis.from_url(url=redis_url) as client:
             try:
@@ -1728,7 +1733,7 @@ class SystemStatusView(GenericAPIView, PassUserMixin):
                     },
                 },
                 "tasks": {
-                    "redis_url": redis_url,
+                    "redis_url": redis_constructed_url,
                     "redis_status": redis_status,
                     "redis_error": redis_error,
                     "celery_status": celery_active,