Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added flag for notification #92

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ 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 SUCCESS_FLAG: &str = "SUCCESS";
const FAIL_FLAG: &str = "FAIL";

fn get_holoport_url(id: VerifyingKey) -> String {
if let Ok(network) = env::var("HOLO_NETWORK") {
if network == "devNet" {
Expand All @@ -24,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,
Expand Down Expand Up @@ -52,6 +64,31 @@ fn device_bundle_password() -> Option<String> {
}
}

fn read_flag_file() -> Option<String> {
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();
}
Expand Down Expand Up @@ -158,6 +195,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(());
}
zeeshan595 marked this conversation as resolved.
Show resolved Hide resolved
write_flag_file(FAIL_FLAG);
send_email(email, data, false).await
}
async fn send_email(email: String, data: String, success: bool) -> Fallible<()> {
Expand Down Expand Up @@ -299,5 +340,6 @@ async fn main() -> Fallible<()> {
thread::sleep(backoff);
backoff += backoff;
}
write_flag_file(SUCCESS_FLAG);
Ok(())
}