Fix bug 1693: add prometheus authentication support 48/11248/4
authorDavid Garcia <david.garcia@canonical.com>
Mon, 11 Oct 2021 13:07:33 +0000 (15:07 +0200)
committerpalsus <subhankar.pal@aricent.com>
Tue, 12 Oct 2021 15:19:21 +0000 (17:19 +0200)
Change-Id: I82ef4c35deaf2d9a90a9a7e036da28c97026613a
Signed-off-by: David Garcia <david.garcia@canonical.com>
docker/Dockerfile
osm_mon/core/mon.yaml
osm_mon/evaluator/backends/prometheus.py
osm_mon/tests/unit/evaluator/test_prometheus.py

index df0bb8d..bc6f94a 100644 (file)
@@ -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
index d396934..85d21e3 100644 (file)
@@ -59,6 +59,8 @@ grafana:
 
 prometheus:
   url: http://prometheus:9090
+  user: admin
+  password: admin
 
 vca:
   host: localhost
index 94024d7..ad4919f 100644 (file)
@@ -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):
index 6919517..0b201e8 100644 (file)
@@ -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='})