Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions docs/conf/prometheus.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,21 @@ In this case, Promgne's pull mode can be used by trigging a small script running
or any other job framework. This only requires the server where Prometheus is running,
to be able to access Promgen over HTTP.

.. important::

Create a Promgen user as a service account with the appropriate permissions, then
generate an API token to access the API endpoints.

.. code-block:: bash

#!/bin/sh
set -e
# Download all the targets from Promgen to a temporary file
curl http://promgen/api/v1/targets --output /etc/prometheus/targets.tmp
# Optionally you could download from a specific service or project
# curl http://promgen/service/123/targets -o /etc/prometheus/targets.tmp
# curl http://promgen/project/456/targets -o /etc/prometheus/targets.tmp
# Permission required: "promgen.read_config"
curl -H "Authorization: Token $PROMGEN_TOKEN" http://promgen/api/v1/targets --output /etc/prometheus/targets.tmp
# Optionally you could download from a specific service or project if you have at least VIEWER permissions on that service or project.
# curl -H "Authorization: Token $PROMGEN_TOKEN" http://promgen/service/123/targets -o /etc/prometheus/targets.tmp
# curl -H "Authorization: Token $PROMGEN_TOKEN" http://promgen/project/456/targets -o /etc/prometheus/targets.tmp
# Move our file to make it more atomic
mv /etc/prometheus/targets.tmp /etc/prometheus/targets.json
# Tell Prometheus to reload
Expand Down
3 changes: 3 additions & 0 deletions docs/user/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Prometheus nodes. See the `Prometheus API <https://prometheus.io/docs/prometheus
Promgen API
===========

Promgen provides APIs to manage the configuration of Prometheus. They require authentication and the
appropriate permissions to access them.

.. http:post:: /api/v1/alerts

.. http:get:: /api/v1/targets
Expand Down
22 changes: 22 additions & 0 deletions promgen/migrations/0055_alter_site_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 4.2.26 on 2026-11-12 03:51

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("promgen", "0054_customlabel_is_added_to_metric"),
]

operations = [
migrations.AlterModelOptions(
name="site",
options={
"permissions": [
("read_config", "Can read Prometheus config"),
("reload_config", "Can reload Prometheus config"),
("import_config", "Can import Prometheus config"),
]
},
),
]
5 changes: 5 additions & 0 deletions promgen/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ def get_absolute_url(self):

class Meta:
proxy = True
permissions = [
("read_config", "Can read Prometheus config"),
("reload_config", "Can reload Prometheus config"),
("import_config", "Can import Prometheus config"),
]


class Group(Group):
Expand Down
96 changes: 95 additions & 1 deletion promgen/rest.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
# Copyright (c) 2019 LINE Corporation
# These sources are released under the terms of the MIT license: see LICENSE
import json
from itertools import chain

from django.contrib.contenttypes.models import ContentType
from django.http import HttpResponse
from requests.exceptions import HTTPError
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.views import APIView

from promgen import filters, models, permissions, prometheus, renderers, serializers, tasks, util
from promgen import (
filters,
models,
permissions,
prometheus,
renderers,
serializers,
signals,
tasks,
util,
)
from promgen.permissions import PromgenModelPermissions


Expand Down Expand Up @@ -219,3 +231,85 @@ def retrieve(self, request, id):
hosts_data = serializers.HostSerializer(hosts, many=True).data
farm_detail = {**farm_data, "hosts": hosts_data}
return Response(farm_detail)


class ApiConfig(APIView):
permission_classes = [PromgenModelPermissions]
any_perm = True

def get_permissions(self):
self.permissions_required = {
"GET": ["promgen.read_config"],
"POST": ["promgen.import_config"],
}.get(self.request.method, [])
return super().get_permissions()

def get(self, request):
return HttpResponse(prometheus.render_config(), content_type="application/json")

def post(self, request, *args, **kwargs):
try:
body = json.loads(request.body.decode("utf-8"))

prometheus.import_config(body, self.request.user, **kwargs)
except Exception as e:
return HttpResponse(e, status=400)

return HttpResponse("Success", status=202)


class ApiQueue(APIView):
permission_classes = [PromgenModelPermissions]
permissions_required = ["promgen.reload_config"]
any_perm = True

def post(self, request):
signals.trigger_write_config.send(request)
signals.trigger_write_rules.send(request)
signals.trigger_write_urls.send(request)
return HttpResponse("OK", status=202)


class _ExportRules(APIView):
permission_classes = [PromgenModelPermissions]
permissions_required = ["promgen.read_config"]
any_perm = True

def format(self, rules=None, name="promgen"):
content = prometheus.render_rules(rules)
response = HttpResponse(content)
response["Content-Type"] = "application/x-yaml"
response["Content-Disposition"] = "attachment; filename=%s.rule.yml" % name
return response


class RulesConfig(_ExportRules):
def get(self, request):
return self.format()


class RuleExport(_ExportRules):
def get(self, request, content_type, object_id):
ct = ContentType.objects.get(
app_label="promgen", model=content_type
).get_object_for_this_type(pk=object_id)
rules = models.Rule.objects.filter(obj=ct)
return self.format(rules)


class URLConfig(APIView):
permission_classes = [PromgenModelPermissions]

def get_permissions(self):
self.permissions_required = {
"GET": ["promgen.read_config"],
"POST": ["promgen.reload_config"],
}.get(self.request.method, [])
return super().get_permissions()

def get(self, request):
return HttpResponse(prometheus.render_urls(), content_type="application/json")

def post(self, request):
tasks.write_urls()
return HttpResponse("OK", status=202)
52 changes: 51 additions & 1 deletion promgen/tests/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,66 @@
from django.contrib.auth.models import Permission, User
from django.db.models.signals import post_delete, post_save
from django.test import override_settings
from django.urls import reverse
from django.urls import resolve, reverse
from guardian.models import UserObjectPermission
from guardian.shortcuts import assign_perm, remove_perm
from requests import Response
from rest_framework.test import APIRequestFactory, force_authenticate

from promgen import models, plugins, rest, settings, signals, tests
from promgen.notification.email import NotificationEmail


class RestAPITest(tests.PromgenTest):
@override_settings(PROMGEN=tests.SETTINGS)
def test_legacy_api_permissions(self):
cases = (
("get", "/api/v1/config", None, 403),
("get", "/api/v1/config", "read_config", 200),
("post", "/api/v1/config", None, 403),
("post", "/api/v1/config", "read_config", 403),
("post", "/api/v1/config", "import_config", 202),
("get", "/api/v1/targets", None, 403),
("get", "/api/v1/targets", "read_config", 200),
("post", "/api/v1/targets", None, 403),
("post", "/api/v1/targets", "read_config", 403),
("post", "/api/v1/targets", "import_config", 202),
("get", "/api/v1/rules", None, 403),
("get", "/api/v1/rules", "read_config", 200),
("get", "/api/v1/urls", None, 403),
("get", "/api/v1/urls", "read_config", 200),
("post", "/api/v1/urls", None, 403),
("post", "/api/v1/urls", "read_config", 403),
("post", "/api/v1/urls", "reload_config", 202),
)
with (
# mock the renderers to focus on permission testing and not the actual rendering logic
mock.patch.multiple(
"promgen.rest.prometheus",
render_config=mock.DEFAULT,
import_config=mock.DEFAULT,
render_rules=mock.DEFAULT,
render_urls=mock.DEFAULT,
) as renderers,
mock.patch("promgen.rest.tasks.write_urls"),
):
for name in ("render_config", "render_rules", "render_urls"):
renderers[name].return_value = ""
for method, path, permission, expected_status in cases:
with self.subTest(method=method, path=path, permission=permission):
user = User.objects.get(username="demo")
user.user_permissions.clear()
if permission:
user.user_permissions.add(
Permission.objects.get(
content_type__app_label="promgen", codename=permission
)
)
request = getattr(APIRequestFactory(), method)(path, data={}, format="json")
force_authenticate(request, user=user)
response = resolve(path).func(request)
self.assertEqual(response.status_code, expected_status)

def _run_rest_test(self, case):
def _parse_case_json(value, default):
if not value:
Expand Down
8 changes: 4 additions & 4 deletions promgen/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@
path("", include("django.contrib.auth.urls")),
path("", include("social_django.urls", namespace="social")),
# Legacy API
path("api/v1/config", csrf_exempt(views.ApiConfig.as_view())),
path("api/v1/config", csrf_exempt(rest.ApiConfig.as_view())),
path("api/", include((router.urls, "api"), namespace="old-api")),
path("api/v1/rules", csrf_exempt(views.RulesConfig.as_view())),
path("api/v1/targets", csrf_exempt(views.ApiConfig.as_view()), name="config-targets"),
path("api/v1/urls", csrf_exempt(views.URLConfig.as_view()), name="config-urls"),
path("api/v1/rules", csrf_exempt(rest.RulesConfig.as_view())),
path("api/v1/targets", csrf_exempt(rest.ApiConfig.as_view()), name="config-targets"),
path("api/v1/urls", csrf_exempt(rest.URLConfig.as_view()), name="config-urls"),
Comment thread
vincent-olivert-riera marked this conversation as resolved.
Resolved
path("api/v1/alerts", csrf_exempt(rest.AlertReceiver.as_view()), name="alert"),
path("api/v1/host/<slug>", views.HostDetail.as_view()),
# Prometheus Proxy
Expand Down
56 changes: 0 additions & 56 deletions promgen/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
plugins,
prometheus,
signals,
tasks,
util,
)
from promgen.forms import GroupMemberForm, UserPermissionForm
Expand Down Expand Up @@ -1367,61 +1366,6 @@ def get_check_permission_object(self):
return get_object_or_404(models.Farm, id=self.kwargs["pk"])


class ApiConfig(View):
def get(self, request):
return HttpResponse(prometheus.render_config(), content_type="application/json")

def post(self, request, *args, **kwargs):
try:
body = json.loads(request.body.decode("utf-8"))

prometheus.import_config(body, self.request.user, **kwargs)
except Exception as e:
return HttpResponse(e, status=400)

return HttpResponse("Success", status=202)


class ApiQueue(View):
def post(self, request):
signals.trigger_write_config.send(request)
signals.trigger_write_rules.send(request)
signals.trigger_write_urls.send(request)
return HttpResponse("OK", status=202)


class _ExportRules(View):
def format(self, rules=None, name="promgen"):
content = prometheus.render_rules(rules)
response = HttpResponse(content)
response["Content-Type"] = "application/x-yaml"
response["Content-Disposition"] = "attachment; filename=%s.rule.yml" % name
return response


class RulesConfig(_ExportRules):
def get(self, request):
return self.format()


class RuleExport(_ExportRules):
def get(self, request, content_type, object_id):
ct = ContentType.objects.get(
app_label="promgen", model=content_type
).get_object_for_this_type(pk=object_id)
rules = models.Rule.objects.filter(obj=ct)
return self.format(rules)


class URLConfig(View):
def get(self, request):
return HttpResponse(prometheus.render_urls(), content_type="application/json")

def post(self, request):
tasks.write_urls()
return HttpResponse("OK", status=202)


class AlertList(LoginRequiredMixin, ListView):
paginate_by = 20
queryset = models.Alert.objects.order_by("-created")
Expand Down