Fix bug 1694: add prometheus authentication support 49/11249/4
authorDavid Garcia <david.garcia@canonical.com>
Mon, 11 Oct 2021 13:07:33 +0000 (15:07 +0200)
committerDavid Garcia <david.garcia@canonical.com>
Tue, 12 Oct 2021 08:42:01 +0000 (10:42 +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 c22058b..1d4db78 100644 (file)
@@ -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
index d396934..85d21e3 100644 (file)
@@ -59,6 +59,8 @@ grafana:
 
 prometheus:
   url: http://prometheus:9090
+  user: admin
+  password: admin
 
 vca:
   host: localhost
index 070cf69..416b38f 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
 
@@ -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):
index 1d20c1e..8ae2a4a 100644 (file)
@@ -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='})