From 9172da1d161d1c4989504493c7f9804fcc718b6c Mon Sep 17 00:00:00 2001 From: tahahawa Date: Mon, 30 Jun 2025 15:48:52 -0400 Subject: [PATCH] add some helper functions --- Cargo.lock | 27 +++++- harmony/Cargo.toml | 1 + harmony/src/modules/monitoring/ntfy/ntfy.rs | 99 ++++++++++++++++++--- 3 files changed, 114 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a4671df..1e3f7f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1736,6 +1736,7 @@ dependencies = [ "serde_json", "serde_yaml", "similar", + "strum 0.27.1", "temp-dir", "temp-file", "tokio", @@ -3552,7 +3553,7 @@ dependencies = [ "itertools", "lru", "paste", - "strum", + "strum 0.26.3", "unicode-segmentation", "unicode-truncate", "unicode-width 0.2.0", @@ -4511,7 +4512,16 @@ version = "0.26.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06" dependencies = [ - "strum_macros", + "strum_macros 0.26.4", +] + +[[package]] +name = "strum" +version = "0.27.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f64def088c51c9510a8579e3c5d67c65349dcf755e5479ad3d010aa6454e2c32" +dependencies = [ + "strum_macros 0.27.1", ] [[package]] @@ -4527,6 +4537,19 @@ dependencies = [ "syn", ] +[[package]] +name = "strum_macros" +version = "0.27.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c77a8c5abcaf0f9ce05d62342b7d298c346515365c36b673df4ebe3ced01fde8" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "rustversion", + "syn", +] + [[package]] name = "subtle" version = "2.6.1" diff --git a/harmony/Cargo.toml b/harmony/Cargo.toml index 00582ef..97ed693 100644 --- a/harmony/Cargo.toml +++ b/harmony/Cargo.toml @@ -56,3 +56,4 @@ dyn-clone = "1.0.19" similar.workspace = true futures-util = "0.3.31" tokio-util = "0.7.15" +strum = { version = "0.27.1", features = ["derive"] } diff --git a/harmony/src/modules/monitoring/ntfy/ntfy.rs b/harmony/src/modules/monitoring/ntfy/ntfy.rs index 33e7b73..0a4d90e 100644 --- a/harmony/src/modules/monitoring/ntfy/ntfy.rs +++ b/harmony/src/modules/monitoring/ntfy/ntfy.rs @@ -1,6 +1,9 @@ +use std::sync::Arc; + use async_trait::async_trait; use log::debug; use serde::Serialize; +use strum::{Display, EnumString}; use crate::{ data::{Id, Version}, @@ -8,7 +11,7 @@ use crate::{ inventory::Inventory, modules::monitoring::ntfy::helm::ntfy_helm_chart::ntfy_helm_chart_score, score::Score, - topology::{HelmCommand, K8sclient, Topology}, + topology::{HelmCommand, K8sclient, Topology, k8s::K8sClient}, }; #[derive(Debug, Clone, Serialize)] @@ -33,6 +36,88 @@ pub struct NtfyInterpret { pub score: NtfyScore, } +#[derive(Debug, EnumString, Display)] +enum NtfyAccessMode { + #[strum(serialize = "read-write", serialize = "rw", to_string = "read-write")] + ReadWrite, + #[strum( + serialize = "read-only", + serialize = "ro", + serialize = "read", + to_string = "read-only" + )] + ReadOnly, + #[strum( + serialize = "write-only", + serialize = "wo", + serialize = "write", + to_string = "write-only" + )] + WriteOnly, + #[strum(serialize = "none", to_string = "deny")] + Deny, +} + +#[derive(Debug, EnumString, Display)] +enum NtfyRole { + #[strum(serialize = "user", to_string = "user")] + User, + #[strum(serialize = "admin", to_string = "admin")] + Admin, +} + +impl NtfyInterpret { + async fn add_user( + &self, + k8s_client: Arc, + username: &str, + password: &str, + role: Option, + ) -> Result<(), String> { + let role = match role { + Some(r) => r, + None => NtfyRole::User, + }; + + k8s_client + .exec_pod( + "ntfy".to_string(), + Some(&self.score.namespace), + vec![ + "sh", + "-c", + format!("NTFY_PASSWORD={password} ntfy user add --role={role} {username}") + .as_str(), + ], + ) + .await?; + + Ok(()) + } + + async fn set_access( + &self, + k8s_client: Arc, + username: &str, + topic: &str, + mode: NtfyAccessMode, + ) -> Result<(), String> { + k8s_client + .exec_pod( + "ntfy".to_string(), + Some(&self.score.namespace), + vec![ + "sh", + "-c", + format!("ntfy access {username} {topic} {mode}").as_str(), + ], + ) + .await?; + + Ok(()) + } +} + #[async_trait] impl Interpret for NtfyInterpret { async fn execute( @@ -60,17 +145,9 @@ impl Interpret for NtfyInterpret { .await?; debug!("created k8s client"); - client - .exec_pod( - "ntfy".to_string(), - Some(&self.score.namespace), - vec![ - "sh", - "-c", - "NTFY_PASSWORD=harmony ntfy user add --role=admin harmony", - ], - ) + self.add_user(client, "harmony", "harmony", Some(NtfyRole::Admin)) .await?; + debug!("exec into pod done"); Ok(Outcome::success("installed ntfy".to_string()))