From fcb96e759e6a25daef040271de3cf58ca032da89 Mon Sep 17 00:00:00 2001 From: hoang Date: Thu, 12 Nov 2026 10:10:26 +0700 Subject: [PATCH] Require authentication for Proxy APIs We have implemented changes to require authentication for all Prometheus Proxy APIs to enhance security. Promgen users will now need to authenticate to access these APIs. --- promgen/proxy.py | 21 ++++++++++++++------- promgen/static/js/promgen.vue.js | 7 ++++++- promgen/templates/base.html | 2 +- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/promgen/proxy.py b/promgen/proxy.py index 95a162968..e81b1e7f4 100644 --- a/promgen/proxy.py +++ b/promgen/proxy.py @@ -7,9 +7,9 @@ from http import HTTPStatus import requests +from django.contrib.auth.mixins import LoginRequiredMixin from django.http import HttpResponse, JsonResponse from django.utils.translation import gettext as _ -from django.views.generic import View from django.views.generic.base import TemplateView from requests.exceptions import HTTPError from rest_framework import permissions @@ -21,7 +21,8 @@ logger = logging.getLogger(__name__) -class PrometheusProxy(View): +class PrometheusProxy(APIView): + permission_classes = [permissions.IsAuthenticated] proxy_headers = {"HTTP_REFERER": "Referer"} @property @@ -53,7 +54,7 @@ def proxy(self, request): yield from concurrent.futures.as_completed(futures) -class ProxyGraph(TemplateView): +class ProxyGraph(LoginRequiredMixin, TemplateView): template_name = "promgen/graph.html" def get_context_data(self, **kwargs): @@ -156,7 +157,9 @@ def get(self, request): ) -class ProxyAlerts(View): +class ProxyAlerts(APIView): + permission_classes = [permissions.IsAuthenticated] + def get(self, request): try: url = util.join_url(util.setting("alertmanager:url"), "/api/v2/alerts") @@ -266,7 +269,9 @@ def get_queryset_for_matcher(matcher, klass): return affected_projects, affected_services -class ProxySilences(View): +class ProxySilences(APIView): + permission_classes = [permissions.IsAuthenticated] + def get(self, request): try: url = util.join_url(util.setting("alertmanager:url"), "/api/v2/silences") @@ -407,7 +412,7 @@ def get_uneditable_for_user(affected_projects, affected_services, user): class ProxySilencesV2(APIView): - permission_classes = [permissions.AllowAny] + permission_classes = [permissions.IsAuthenticated] def post(self, request, *args, **kwargs): serializer = serializers.SilenceSerializer( @@ -503,7 +508,9 @@ def post(self, request, *args, **kwargs): ) -class ProxyDeleteSilence(View): +class ProxyDeleteSilence(APIView): + permission_classes = [permissions.IsAuthenticated] + def delete(self, request, silence_id): url = util.join_url(util.setting("alertmanager:url"), f"/api/v2/silence/{silence_id}") # First, check if the silence exists diff --git a/promgen/static/js/promgen.vue.js b/promgen/static/js/promgen.vue.js index 64fb5774b..e978ef4ef 100644 --- a/promgen/static/js/promgen.vue.js +++ b/promgen/static/js/promgen.vue.js @@ -86,7 +86,12 @@ const app = Vue.createApp({ tgt.classList.toggle('collapse'); }, expireSilence(id) { - fetch(`/proxy/v1/silences/${id}`, { method: 'DELETE' }) + fetch(`/proxy/v1/silences/${id}`, { + method: 'DELETE', + headers: { + "X-CSRFToken": document.querySelector('input[name="csrf_token"]').value, + }, + }) .then(response => { if (response.ok) { location.reload(); diff --git a/promgen/templates/base.html b/promgen/templates/base.html index cbeddc204..8baedd682 100644 --- a/promgen/templates/base.html +++ b/promgen/templates/base.html @@ -82,7 +82,7 @@ crossorigin="anonymous" >