Skip to content

Commit

Permalink
Patch: apply health_check commit without the commit
Browse files Browse the repository at this point in the history
  • Loading branch information
celine-m-s committed Mar 18, 2024
1 parent 81a35ad commit 11ad516
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@
MIDDLEWARE = [
# Generate request Id
"django_datadog_logger.middleware.request_id.RequestIdMiddleware",
# Itou health check for Clever Cloud, don’t require requests to match ALLOWED_HOSTS
"itou.www.middleware.public_health_check",
# Django stack
"django.middleware.gzip.GZipMiddleware",
"django.middleware.security.SecurityMiddleware",
Expand Down
36 changes: 36 additions & 0 deletions itou/www/middleware.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import sentry_sdk
from django.core.cache import cache
from django.core.files.storage import default_storage
from django.db import connection
from django.http.response import HttpResponse, HttpResponseServerError
from django.utils.cache import add_never_cache_headers


Expand All @@ -9,3 +14,34 @@ def middleware(request):
return response

return middleware


def public_health_check(get_response):
def middleware(request):
"""
Bypass ALLOWED_HOSTS checks
CleverCloud probes access this path through IP directly, we don’t want to serve
a 400 BadRequest because the request Host header is not in the ALLOWED_HOSTS.
"""
if request.path == "/check-health":
try:
with connection.cursor() as c:
c.execute("SELECT 'check-database-connection'")
cache.get("check-cache-connection")
default_storage.exists("check-s3-file-access.txt")
body = b"Healthy\n"
return HttpResponse(
body,
content_type="text/plain",
charset="utf-8",
# CommonMiddleware is later in the middleware chain, and it checks ALLOWED_HOSTS.
headers={
"Content-Length": str(len(body)),
},
)
except Exception as e:
sentry_sdk.capture_exception(e)
return HttpResponseServerError()
return get_response(request)

return middleware
21 changes: 21 additions & 0 deletions tests/www/test_check_health.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class TestCheckHealth:
def test_get(self, client):
response = client.get("/check-health")
assert response.status_code == 200
assert response.charset == "utf-8"
assert response["Content-Type"] == "text/plain"
assert response["Content-Length"] == "8"
assert response.content.decode() == "Healthy\n"

def test_get_as_clever(self, client):
response = client.get(
"/check-health",
# CleverCloud probes connect directly through the IP, their HOST is not in the ALLOWED_HOSTS.
HTTP_HOST="10.2.2.2",
)
assert response.status_code == 200

def test_get_with_error(self, client, mocker):
mocker.patch("itou.www.middleware.connection.cursor", side_effect=Exception("Boom!"))
response = client.get("/check-health")
assert response.status_code == 500

0 comments on commit 11ad516

Please sign in to comment.