add some helper functions
All checks were successful
Run Check Script / check (pull_request) Successful in -9s

This commit is contained in:
tahahawa 2025-06-30 15:48:52 -04:00
parent 1adc2db5d9
commit 9172da1d16
3 changed files with 114 additions and 13 deletions

27
Cargo.lock generated
View File

@ -1736,6 +1736,7 @@ dependencies = [
"serde_json", "serde_json",
"serde_yaml", "serde_yaml",
"similar", "similar",
"strum 0.27.1",
"temp-dir", "temp-dir",
"temp-file", "temp-file",
"tokio", "tokio",
@ -3552,7 +3553,7 @@ dependencies = [
"itertools", "itertools",
"lru", "lru",
"paste", "paste",
"strum", "strum 0.26.3",
"unicode-segmentation", "unicode-segmentation",
"unicode-truncate", "unicode-truncate",
"unicode-width 0.2.0", "unicode-width 0.2.0",
@ -4511,7 +4512,16 @@ version = "0.26.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06" checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06"
dependencies = [ 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]] [[package]]
@ -4527,6 +4537,19 @@ dependencies = [
"syn", "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]] [[package]]
name = "subtle" name = "subtle"
version = "2.6.1" version = "2.6.1"

View File

@ -56,3 +56,4 @@ dyn-clone = "1.0.19"
similar.workspace = true similar.workspace = true
futures-util = "0.3.31" futures-util = "0.3.31"
tokio-util = "0.7.15" tokio-util = "0.7.15"
strum = { version = "0.27.1", features = ["derive"] }

View File

@ -1,6 +1,9 @@
use std::sync::Arc;
use async_trait::async_trait; use async_trait::async_trait;
use log::debug; use log::debug;
use serde::Serialize; use serde::Serialize;
use strum::{Display, EnumString};
use crate::{ use crate::{
data::{Id, Version}, data::{Id, Version},
@ -8,7 +11,7 @@ use crate::{
inventory::Inventory, inventory::Inventory,
modules::monitoring::ntfy::helm::ntfy_helm_chart::ntfy_helm_chart_score, modules::monitoring::ntfy::helm::ntfy_helm_chart::ntfy_helm_chart_score,
score::Score, score::Score,
topology::{HelmCommand, K8sclient, Topology}, topology::{HelmCommand, K8sclient, Topology, k8s::K8sClient},
}; };
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
@ -33,6 +36,88 @@ pub struct NtfyInterpret {
pub score: NtfyScore, 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<K8sClient>,
username: &str,
password: &str,
role: Option<NtfyRole>,
) -> 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<K8sClient>,
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] #[async_trait]
impl<T: Topology + HelmCommand + K8sclient> Interpret<T> for NtfyInterpret { impl<T: Topology + HelmCommand + K8sclient> Interpret<T> for NtfyInterpret {
async fn execute( async fn execute(
@ -60,17 +145,9 @@ impl<T: Topology + HelmCommand + K8sclient> Interpret<T> for NtfyInterpret {
.await?; .await?;
debug!("created k8s client"); debug!("created k8s client");
client self.add_user(client, "harmony", "harmony", Some(NtfyRole::Admin))
.exec_pod(
"ntfy".to_string(),
Some(&self.score.namespace),
vec![
"sh",
"-c",
"NTFY_PASSWORD=harmony ntfy user add --role=admin harmony",
],
)
.await?; .await?;
debug!("exec into pod done"); debug!("exec into pod done");
Ok(Outcome::success("installed ntfy".to_string())) Ok(Outcome::success("installed ntfy".to_string()))