Skip to content

BrainAPI is a knowledge graph–powered AI memory layer that transforms unstructured data into structured knowledge, enabling intelligent search, recommendations, and contextual memory for AI agents and applications.

Authenticate Celery Redis connections

Christian98a87202c625
diff --git a/src/workers/app.py b/src/workers/app.py
index e4d33b4..6bfb7fe 100644
--- a/src/workers/app.py
+++ b/src/workers/app.py
@@ -24,6 +24,7 @@ warnings.filterwarnings(
)
from src.workers.kombu_redis_patch import apply_kombu_redis_unblocked_patch
+from src.workers.redis_url import redis_connection_url
apply_kombu_redis_unblocked_patch()
@@ -42,18 +43,23 @@ os.environ.setdefault("GRPC_ENABLE_FORK_SUPPORT", "1")
REDIS_HOST = os.getenv("REDIS_HOST", "localhost")
REDIS_PORT = os.getenv("REDIS_PORT", "6379")
+REDIS_URL = redis_connection_url(
+ REDIS_HOST,
+ REDIS_PORT,
+ os.getenv("REDIS_PASSWORD"),
+)
ingestion_app = Celery(
"ingestion_app",
broker=(
"amqp://kalo:kalo@localhost:5672/"
if os.getenv("CELERY_BACKEND") == "rabbitmq"
- else f"redis://{REDIS_HOST}:{REDIS_PORT}/0"
+ else REDIS_URL
),
backend=(
"rpc://"
if os.getenv("CELERY_BACKEND") == "rabbitmq"
- else f"redis://{REDIS_HOST}:{REDIS_PORT}/0"
+ else REDIS_URL
),
include=["src.workers.tasks.ingestion"],
)
diff --git a/src/workers/redis_url.py b/src/workers/redis_url.py
new file mode 100644
index 0000000..58a4dbe
--- /dev/null
+++ b/src/workers/redis_url.py
@@ -0,0 +1,18 @@
+"""Redis connection URL helpers for worker transports."""
+
+from __future__ import annotations
+
+from urllib.parse import quote
+
+
+def redis_connection_url(
+ host: str,
+ port: str | int,
+ password: str | None,
+ *,
+ database: int = 0,
+) -> str:
+ """Build a Redis URL, percent-encoding credentials when authentication is enabled."""
+
+ auth = f":{quote(password, safe='')}@" if password else ""
+ return f"redis://{auth}{host}:{port}/{database}"
diff --git a/tests/test_production_hardening.py b/tests/test_production_hardening.py
index 44651c0..2ff7f5a 100644
--- a/tests/test_production_hardening.py
+++ b/tests/test_production_hardening.py
@@ -10,6 +10,7 @@ from fastapi.testclient import TestClient
from src.services.api.errors import install_error_handlers
from src.services.api.middlewares.auth import BrainPATMiddleware
from src.services.api.routes.system import system_router
+from src.workers.redis_url import redis_connection_url
def test_malformed_authorization_fails_closed_with_401():
@@ -76,3 +77,11 @@ def test_plugin_failures_are_fatal_by_default_in_production():
clear=False,
):
_enforce_plugin_results({"demo": False})
+
+
+def test_worker_redis_url_propagates_encoded_password():
+ assert (
+ redis_connection_url("redis", 6379, "secret:/@ value")
+ == "redis://:secret%3A%2F%40%20value@redis:6379/0"
+ )
+ assert redis_connection_url("redis", "6379", None) == "redis://redis:6379/0"