(charmed-osm) Add auth to prometheus and update kafka/zk
[osm/devops.git] / installers / charm / grafana / src / charm.py
index 78ec0e3..778b5eb 100755 (executable)
@@ -60,6 +60,7 @@ class ConfigModel(ModelValidator):
     ingress_whitelist_source_range: Optional[str]
     tls_secret_name: Optional[str]
     image_pull_policy: str
+    security_context: bool
 
     @validator("log_level")
     def validate_log_level(cls, v):
@@ -144,11 +145,19 @@ class GrafanaCharm(CharmedOsmBase):
 
     def _build_datasources_files(self):
         files_builder = FilesV3Builder()
+        prometheus_user = self.prometheus_client.user
+        prometheus_password = self.prometheus_client.password
+        enable_basic_auth = all([prometheus_user, 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 "",
             ),
         )
         return files_builder.build()
@@ -183,7 +192,9 @@ class GrafanaCharm(CharmedOsmBase):
             self.grafana_cluster.set_initial_password(admin_initial_password)
 
         # Create Builder for the PodSpec
-        pod_spec_builder = PodSpecV3Builder()
+        pod_spec_builder = PodSpecV3Builder(
+            enable_security_context=config.security_context
+        )
 
         # Add secrets to the pod
         grafana_secret_name = f"{self.app.name}-admin-secret"
@@ -197,7 +208,10 @@ class GrafanaCharm(CharmedOsmBase):
 
         # Build Container
         container_builder = ContainerV3Builder(
-            self.app.name, image_info, config.image_pull_policy
+            self.app.name,
+            image_info,
+            config.image_pull_policy,
+            run_as_non_root=config.security_context,
         )
         container_builder.add_port(name=self.app.name, port=config.port)
         container_builder.add_http_readiness_probe(