From: David Garcia Date: Mon, 11 Oct 2021 13:07:33 +0000 (+0200) Subject: Fix bug 1692: add prometheus authentication support X-Git-Tag: release-v11.0-start~2 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F47%2F11247%2F3;p=osm%2FMON.git Fix bug 1692: add prometheus authentication support Change-Id: I82ef4c35deaf2d9a90a9a7e036da28c97026613a Signed-off-by: David Garcia --- diff --git a/docker/Dockerfile b/docker/Dockerfile index df0bb8d..bc6f94a 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -58,6 +58,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 94024d7..ad4919f 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 @@ -40,9 +42,15 @@ 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")) + request_url, + timeout=int(self.conf.get("global", "request_timeout")), + headers=headers, ) if r.status_code == 200: json_response = r.json() @@ -73,6 +81,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 6919517..0b201e8 100644 --- a/osm_mon/tests/unit/evaluator/test_prometheus.py +++ b/osm_mon/tests/unit/evaluator/test_prometheus.py @@ -41,3 +41,8 @@ class EvaluatorTest(TestCase): 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='})