Repository: mozilla/bugbug

Issue:
Bugbug HTTP service failing to connect to Redis
Heroku switched our Redis instance to SSL, and they are using self-signed certificates so we fail to validate them.

> redis.exceptions.ConnectionError: Error 1 connecting to [ec2-44-194-67-42.compute-1.amazonaws.com:32269](http://ec2-44-194-67-42.compute-1.amazonaws.com:32269/). [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:997).


Discussion:

Resolving patch:
diff --git a/http_service/bugbug_http/app.py b/http_service/bugbug_http/app.py
--- a/http_service/bugbug_http/app.py
+++ b/http_service/bugbug_http/app.py
@@ -10,6 +10,7 @@
 from dataclasses import dataclass, field
 from datetime import datetime, timedelta
 from typing import Any, Callable, Sequence
+from urllib.parse import urlparse
 
 import orjson
 import zstandard
@@ -63,8 +64,15 @@
 )
 
 application = Flask(__name__)
-redis_url = os.environ.get("REDIS_URL", "redis://localhost/0")
-redis_conn = Redis.from_url(redis_url)
+url = urlparse(os.environ.get("REDIS_URL", "redis://localhost/0"))
+assert url.hostname is not None
+redis_conn = Redis(
+    host=url.hostname,
+    port=url.port if url.port is not None else 6379,
+    password=url.password,
+    ssl=True,
+    ssl_cert_reqs=None,
+)
 
 # Kill jobs which don't finish within 12 minutes.
 JOB_TIMEOUT = 12 * 60
diff --git a/http_service/bugbug_http/models.py b/http_service/bugbug_http/models.py
--- a/http_service/bugbug_http/models.py
+++ b/http_service/bugbug_http/models.py
@@ -8,6 +8,7 @@
 from datetime import timedelta
 from functools import lru_cache
 from typing import Sequence
+from urllib.parse import urlparse
 
 import orjson
 import requests
@@ -39,7 +40,15 @@
 ]
 
 DEFAULT_EXPIRATION_TTL = 7 * 24 * 3600  # A week
-redis = Redis.from_url(os.environ.get("REDIS_URL", "redis://localhost/0"))
+url = urlparse(os.environ.get("REDIS_URL", "redis://localhost/0"))
+assert url.hostname is not None
+redis = Redis(
+    host=url.hostname,
+    port=url.port if url.port is not None else 6379,
+    password=url.password,
+    ssl=True,
+    ssl_cert_reqs=None,
+)
 
 MODEL_CACHE: ReadthroughTTLCache[str, Model] = ReadthroughTTLCache(
     timedelta(hours=1), lambda m: Model.load(f"{m}model")
diff --git a/http_service/bugbug_http/worker.py b/http_service/bugbug_http/worker.py
--- a/http_service/bugbug_http/worker.py
+++ b/http_service/bugbug_http/worker.py
@@ -6,6 +6,7 @@
 
 import os
 import sys
+from urllib.parse import urlparse
 
 from redis import Redis
 from rq import Connection, Worker
@@ -24,8 +25,15 @@ def main():
 
     # Provide queue names to listen to as arguments to this script,
     # similar to rq worker
-    redis_url = os.environ.get("REDIS_URL", "redis://localhost/0")
-    redis_conn = Redis.from_url(redis_url)
+    url = urlparse(os.environ.get("REDIS_URL", "redis://localhost/0"))
+    assert url.hostname is not None
+    redis_conn = Redis(
+        host=url.hostname,
+        port=url.port if url.port is not None else 6379,
+        password=url.password,
+        ssl=True,
+        ssl_cert_reqs=None,
+    )
     with Connection(connection=redis_conn):
         qs = sys.argv[1:] or ["default"]
 


Test patch: