From 862da336ea8328261293bbe17ed72d9256ad3a3b Mon Sep 17 00:00:00 2001 From: Zeeshan Abid Date: Mon, 30 Sep 2024 16:07:24 +0100 Subject: [PATCH 1/3] feat: added flag for notification --- client/src/main.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/client/src/main.rs b/client/src/main.rs index e3392af..a2c9f76 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -13,8 +13,37 @@ use tracing_subscriber::{EnvFilter, FmtSubscriber}; use uuid::Uuid; use zerotier_api::Identity; mod validation; +use std::fs::OpenOptions; +use std::io::Read; use validation::init_validation; +const FLAG_FILE: &str = "/var/lib/holo-auth/holo-auth-status"; +const SUCCESS_FLAG: &str = "SUCCESS"; +const FAIL_FLAG: &str = "FAIL"; + +fn read_flag_file() -> Option { + if Path::new(FLAG_FILE).exists() { + let mut file = File::open(FLAG_FILE).expect("Failed to open flag file"); + let mut contents = String::new(); + file.read_to_string(&mut contents) + .expect("Failed to read flag file"); + Some(contents) + } else { + None + } +} + +fn write_flag_file(flag: &str) { + let mut file = OpenOptions::new() + .write(true) + .create(true) + .truncate(true) // Overwrite the file + .open(FLAG_FILE) + .expect("Failed to open flag file"); + file.write_all(flag.as_bytes()) + .expect("Failed to write flag file"); +} + fn get_holoport_url(id: VerifyingKey) -> String { if let Ok(network) = env::var("HOLO_NETWORK") { if network == "devNet" { @@ -158,6 +187,10 @@ struct NotifyPayload { } async fn send_failure_email(email: String, data: String) -> Fallible<()> { info!("Sending Failure Email to: {:?}", email); + if read_flag_file() != Some(SUCCESS_FLAG.to_string()) { + return Ok(()); + } + write_flag_file(FAIL_FLAG); send_email(email, data, false).await } async fn send_email(email: String, data: String, success: bool) -> Fallible<()> { @@ -299,5 +332,6 @@ async fn main() -> Fallible<()> { thread::sleep(backoff); backoff += backoff; } + write_flag_file(SUCCESS_FLAG); Ok(()) } From 6192aa509bfa17054685677806e2f123bca86c2e Mon Sep 17 00:00:00 2001 From: Zeeshan Abid Date: Mon, 30 Sep 2024 16:14:57 +0100 Subject: [PATCH 2/3] feat: using env instead of a const --- client/src/main.rs | 56 ++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/client/src/main.rs b/client/src/main.rs index a2c9f76..83e5322 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -17,33 +17,9 @@ use std::fs::OpenOptions; use std::io::Read; use validation::init_validation; -const FLAG_FILE: &str = "/var/lib/holo-auth/holo-auth-status"; const SUCCESS_FLAG: &str = "SUCCESS"; const FAIL_FLAG: &str = "FAIL"; -fn read_flag_file() -> Option { - if Path::new(FLAG_FILE).exists() { - let mut file = File::open(FLAG_FILE).expect("Failed to open flag file"); - let mut contents = String::new(); - file.read_to_string(&mut contents) - .expect("Failed to read flag file"); - Some(contents) - } else { - None - } -} - -fn write_flag_file(flag: &str) { - let mut file = OpenOptions::new() - .write(true) - .create(true) - .truncate(true) // Overwrite the file - .open(FLAG_FILE) - .expect("Failed to open flag file"); - file.write_all(flag.as_bytes()) - .expect("Failed to write flag file"); -} - fn get_holoport_url(id: VerifyingKey) -> String { if let Ok(network) = env::var("HOLO_NETWORK") { if network == "devNet" { @@ -53,6 +29,13 @@ fn get_holoport_url(id: VerifyingKey) -> String { format!("https://{}.holohost.net", public_key::to_base36_id(&id)) } +fn holo_auth_flag_file() -> String { + match env::var("HOLO_AUTH_FLAG_FILE") { + Ok(file) => file, + _ => "/var/lib/holo-auth/holo-auth-status".to_string(), + } +} + fn mem_proof_server_url() -> String { match env::var("MEM_PROOF_SERVER_URL") { Ok(url) => url, @@ -81,6 +64,31 @@ fn device_bundle_password() -> Option { } } +fn read_flag_file() -> Option { + let file_path = holo_auth_flag_file(); + if Path::new(&file_path).exists() { + let mut file = File::open(file_path).expect("Failed to open flag file"); + let mut contents = String::new(); + file.read_to_string(&mut contents) + .expect("Failed to read flag file"); + Some(contents) + } else { + None + } +} + +fn write_flag_file(flag: &str) { + let file_path = holo_auth_flag_file(); + let mut file = OpenOptions::new() + .write(true) + .create(true) + .truncate(true) // Overwrite the file + .open(file_path) + .expect("Failed to open flag file"); + file.write_all(flag.as_bytes()) + .expect("Failed to write flag file"); +} + lazy_static! { static ref CLIENT: Client = Client::new(); } From 729f9fb9859d829bcd551ec25db8eb9c6ab1fc12 Mon Sep 17 00:00:00 2001 From: Zeeshan Abid Date: Tue, 1 Oct 2024 10:17:57 +0100 Subject: [PATCH 3/3] fix: set flag to fail after it is successful --- client/src/main.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/client/src/main.rs b/client/src/main.rs index 83e5322..725db4a 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -195,10 +195,11 @@ struct NotifyPayload { } async fn send_failure_email(email: String, data: String) -> Fallible<()> { info!("Sending Failure Email to: {:?}", email); - if read_flag_file() != Some(SUCCESS_FLAG.to_string()) { + let flag = read_flag_file(); + write_flag_file(FAIL_FLAG); + if flag != Some(SUCCESS_FLAG.to_string()) { return Ok(()); } - write_flag_file(FAIL_FLAG); send_email(email, data, false).await } async fn send_email(email: String, data: String, success: bool) -> Fallible<()> {