Skip to content

Commit

Permalink
added healthcheck middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
lme-nca committed Feb 7, 2024
1 parent 9ddc6e3 commit 39f90d7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
23 changes: 23 additions & 0 deletions dojo/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from threading import local
from django.db import models
from django.urls import reverse
from django.http import HttpResponse

Check notice on line 9 in dojo/middleware.py

View workflow job for this annotation

GitHub Actions / flake8-your-pr

dojo/middleware.py#L9

'django.http.HttpResponse' imported but unused (F401)


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -164,3 +165,25 @@ def __init__(self, get_response):
def __call__(self, request):
request.META.update(settings.ADDITIONAL_HEADERS)
return self.get_response(request)


class HealthCheckMiddleware:
"""
Middleware that will allow for a healthcheck to return UP without the caller being in the
DJANGO ALLOWED_HOSTS list. Needed for AWS ALB healthchecks and improves general k8 healthchecks
"""

def __init__(self, get_response):

self.get_response = get_response

def __call__(self, request):
response = self.get_response(request)
if request.META['PATH_INFO'] == '/health':
response.status_code = 200
response.data = {'health': 'UP.'}
# you need to change private attribute `_is_render`
# to call render second time
response._is_rendered = False
response.render()
return response

Check notice on line 189 in dojo/middleware.py

View workflow job for this annotation

GitHub Actions / flake8-your-pr

dojo/middleware.py#L189

no newline at end of file (W292)
1 change: 1 addition & 0 deletions dojo/settings/settings.dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,7 @@ def generate_url(scheme, double_slashes, user, password, host, port, path, param
'django.middleware.common.CommonMiddleware',
'dojo.middleware.APITrailingSlashMiddleware',
'dojo.middleware.DojoSytemSettingsMiddleware',
'dojo.middleware.HealthCheckMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.security.SecurityMiddleware',
Expand Down
2 changes: 2 additions & 0 deletions helm/defectdojo/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ django:
# Depending on the size and complexity of your scans, you might want to increase the default ingress timeouts if you see repeated 504 Gateway Timeouts
# nginx.ingress.kubernetes.io/proxy-read-timeout: "1800"
# nginx.ingress.kubernetes.io/proxy-send-timeout: "1800"
# specific for AWS deployments Defectdojo has the /health endpoint for ALB healthchecks
# alb.ingress.kubernetes.io/healthcheck-path: /health
nginx:
tls:
enabled: false
Expand Down

0 comments on commit 39f90d7

Please sign in to comment.