Fix grafana datasource authentication in Charmed OSM 07/11307/3
authorDavid Garcia <david.garcia@canonical.com>
Fri, 5 Nov 2021 11:33:03 +0000 (12:33 +0100)
committerbeierlm <mark.beierl@canonical.com>
Tue, 9 Nov 2021 13:48:21 +0000 (14:48 +0100)
Change-Id: I7620f0a3b52f378f97fea92e8542418a5da36e5f
Signed-off-by: David Garcia <david.garcia@canonical.com>
installers/charm/grafana/src/charm.py

index 778b5eb..28d28ae 100755 (executable)
@@ -148,16 +148,22 @@ class GrafanaCharm(CharmedOsmBase):
         prometheus_user = self.prometheus_client.user
         prometheus_password = self.prometheus_client.password
         enable_basic_auth = all([prometheus_user, prometheus_password])
+        kwargs = {
+            "prometheus_host": self.prometheus_client.hostname,
+            "prometheus_port": self.prometheus_client.port,
+            "enable_basic_auth": enable_basic_auth,
+            "user": "",
+            "password": "",
+        }
+        if enable_basic_auth:
+            kwargs["user"] = f"basic_auth_user: {prometheus_user}"
+            kwargs[
+                "password"
+            ] = f"secure_json_data:\n      basicAuthPassword: {prometheus_password}"
         files_builder.add_file(
             "datasource_prometheus.yaml",
             Template(Path("templates/default_datasources.yaml").read_text()).substitute(
-                prometheus_host=self.prometheus_client.hostname,
-                prometheus_port=self.prometheus_client.port,
-                enable_basic_auth=enable_basic_auth,
-                user=f"user: {prometheus_user}" if enable_basic_auth else "",
-                password=f"password: {prometheus_password}"
-                if enable_basic_auth
-                else "",
+                **kwargs
             ),
         )
         return files_builder.build()