2019-11-08 11:23:51 +00:00
|
|
|
"""Metrics view"""
|
|
|
|
from base64 import b64encode
|
2019-11-08 11:24:02 +00:00
|
|
|
|
2019-11-08 11:23:51 +00:00
|
|
|
from django.conf import settings
|
2020-01-17 09:55:11 +00:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2019-11-08 11:23:51 +00:00
|
|
|
from django.views import View
|
|
|
|
from django_prometheus.exports import ExportToDjangoView
|
|
|
|
|
2019-11-08 11:24:02 +00:00
|
|
|
|
2019-11-08 11:23:51 +00:00
|
|
|
class MetricsView(View):
|
|
|
|
"""Wrapper around ExportToDjangoView, using http-basic auth"""
|
|
|
|
|
|
|
|
def get(self, request: HttpRequest) -> HttpResponse:
|
|
|
|
"""Check for HTTP-Basic auth"""
|
2019-12-31 11:51:16 +00:00
|
|
|
auth_header = request.META.get("HTTP_AUTHORIZATION", "")
|
2020-01-02 12:41:49 +00:00
|
|
|
auth_type, _, credentials = auth_header.partition(" ")
|
|
|
|
credentials = f"monitor:{settings.SECRET_KEY}"
|
|
|
|
expected = b64encode(str.encode(credentials)).decode()
|
2019-11-08 11:23:51 +00:00
|
|
|
|
2020-01-02 12:41:49 +00:00
|
|
|
if auth_type != "Basic" or credentials != expected:
|
2020-01-17 09:55:11 +00:00
|
|
|
response = HttpResponse(status=401)
|
|
|
|
response['WWW-Authenticate'] = 'Basic realm="passbook-monitoring"'
|
|
|
|
return response
|
2019-11-08 11:23:51 +00:00
|
|
|
|
|
|
|
return ExportToDjangoView(request)
|