From: David Garcia Date: Mon, 11 Oct 2021 13:07:33 +0000 (+0200) Subject: Fix bug 1694: add prometheus authentication support X-Git-Tag: v9.1.5~1 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F49%2F11249%2F4;p=osm%2FMON.git Fix bug 1694: add prometheus authentication support Change-Id: I82ef4c35deaf2d9a90a9a7e036da28c97026613a Signed-off-by: David Garcia --- diff --git a/docker/Dockerfile b/docker/Dockerfile index c22058b..1d4db78 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -54,6 +54,8 @@ ENV OSMMON_DATABASE_COMMONKEY changeme ENV OSMMON_COLLECTOR_INTERVAL 30 ENV OSMMON_EVALUATOR_INTERVAL 30 ENV OSMMON_PROMETHEUS_URL http://prometheus:9090 +ENV OSMMON_PROMETHEUS_USER admin +ENV OSMMON_PROMETHEUS_PASSWORD admin ENV OSMMON_GRAFANA_URL http://grafana:3000 ENV OSMMON_GRAFANA_USER admin ENV OSMMON_GRAFANA_PASSWORD admin diff --git a/osm_mon/core/mon.yaml b/osm_mon/core/mon.yaml index d396934..85d21e3 100644 --- a/osm_mon/core/mon.yaml +++ b/osm_mon/core/mon.yaml @@ -59,6 +59,8 @@ grafana: prometheus: url: http://prometheus:9090 + user: admin + password: admin vca: host: localhost diff --git a/osm_mon/evaluator/backends/prometheus.py b/osm_mon/evaluator/backends/prometheus.py index 070cf69..416b38f 100644 --- a/osm_mon/evaluator/backends/prometheus.py +++ b/osm_mon/evaluator/backends/prometheus.py @@ -20,7 +20,9 @@ # For those usages not covered by the Apache License, Version 2.0 please # contact: bdiaz@whitestack.com or glavado@whitestack.com ## +import base64 import logging +from typing import Dict import requests @@ -41,8 +43,16 @@ class PrometheusBackend(BaseBackend): def get_metric_value(self, metric_name: str, tags: dict): query = self._build_query(metric_name, tags) request_url = self._build_url(query) + request_headers = self._build_headers() + return self._query_metric(request_url, request_headers) + + def _query_metric(self, request_url: str, headers: Dict[str, str]): log.info("Querying Prometheus: %s", request_url) - r = requests.get(request_url, timeout=int(self.conf.get('global', 'request_timeout'))) + r = requests.get( + request_url, + timeout=int(self.conf.get("global", "request_timeout")), + headers=headers, + ) if r.status_code == 200: json_response = r.json() if json_response['status'] == 'success': @@ -63,6 +73,16 @@ class PrometheusBackend(BaseBackend): def _build_url(self, query: str): return self.conf.get('prometheus', 'url') + "/api/v1/query?" + query + def _build_headers(self) -> Dict[str, str]: + headers = {} + user = self.conf.get("prometheus", "user") + password = self.conf.get("prometheus", "password") + if user and password: + _phrase = f"{user}:{password}".encode("utf-8") + token = base64.b64encode(_phrase).decode("utf-8") + headers["Authorization"] = f"Basic {token}" + return headers + def _get_metric_value_from_response(self, json_response): result = json_response['data']['result'] if len(result): diff --git a/osm_mon/tests/unit/evaluator/test_prometheus.py b/osm_mon/tests/unit/evaluator/test_prometheus.py index 1d20c1e..8ae2a4a 100644 --- a/osm_mon/tests/unit/evaluator/test_prometheus.py +++ b/osm_mon/tests/unit/evaluator/test_prometheus.py @@ -39,3 +39,8 @@ class EvaluatorTest(TestCase): alarm_tags['tag_2'] = 'value_2' query = prometheus._build_query('metric_name', alarm_tags) self.assertEqual(query, 'query=osm_metric_name{tag_1="value_1",tag_2="value_2"}') + + def test_build_headers(self): + prometheus = PrometheusBackend(self.config) + headers = prometheus._build_headers() + self.assertEqual(headers, {'Authorization': 'Basic YWRtaW46YWRtaW4='})