trifid/tfclient/src/nebulaworker.rs

185 lines
6.4 KiB
Rust
Raw Normal View History

2023-03-22 18:34:06 +00:00
// Code to handle the nebula worker
2023-03-30 16:28:34 +00:00
use crate::config::{load_cdata, NebulaConfig, TFClientConfig};
2023-03-22 18:34:06 +00:00
use crate::daemon::ThreadMessageSender;
2023-03-30 14:43:09 +00:00
use crate::dirs::get_nebulaconfig_file;
use crate::embedded_nebula::run_embedded_nebula;
2023-05-14 17:47:49 +00:00
use log::{debug, error, info};
use std::error::Error;
use std::fs;
use std::sync::mpsc::Receiver;
use std::time::{Duration, SystemTime};
2023-03-22 18:34:06 +00:00
pub enum NebulaWorkerMessage {
Shutdown,
2023-03-31 12:33:02 +00:00
ConfigUpdated,
2023-05-14 17:47:49 +00:00
WakeUp,
2023-03-22 18:34:06 +00:00
}
2023-03-30 14:43:09 +00:00
fn insert_private_key(instance: &str) -> Result<(), Box<dyn Error>> {
2023-05-14 17:47:49 +00:00
if !get_nebulaconfig_file(instance)
.ok_or("Could not get config file location")?
.exists()
{
2023-03-30 16:13:29 +00:00
return Ok(()); // cant insert private key into a file that does not exist - BUT. we can gracefully handle nebula crashing - we cannot gracefully handle this fn failing
}
2023-03-30 14:43:09 +00:00
let cdata = load_cdata(instance)?;
let key = cdata.dh_privkey.ok_or("Missing private key")?;
2023-05-14 17:47:49 +00:00
let config_str = fs::read_to_string(
get_nebulaconfig_file(instance).ok_or("Could not get config file location")?,
)?;
2023-03-30 14:43:09 +00:00
let mut config: NebulaConfig = serde_yaml::from_str(&config_str)?;
2023-03-30 16:13:29 +00:00
config.pki.key = Some(String::from_utf8(key)?);
2023-03-30 14:43:09 +00:00
debug!("inserted private key into config: {:?}", config);
let config_str = serde_yaml::to_string(&config)?;
2023-05-14 17:47:49 +00:00
fs::write(
get_nebulaconfig_file(instance).ok_or("Could not get config file location")?,
config_str,
)?;
2023-03-30 14:43:09 +00:00
Ok(())
}
2023-05-14 17:47:49 +00:00
pub fn nebulaworker_main(
_config: TFClientConfig,
instance: String,
_transmitter: ThreadMessageSender,
rx: Receiver<NebulaWorkerMessage>,
) {
2023-03-30 16:28:34 +00:00
let _cdata = match load_cdata(&instance) {
2023-03-30 11:29:02 +00:00
Ok(data) => data,
Err(e) => {
error!("unable to load cdata: {}", e);
error!("nebula thread exiting with error");
return;
}
};
2023-03-30 14:43:09 +00:00
info!("fixing config...");
match insert_private_key(&instance) {
Ok(_) => {
info!("config fixed (private-key embedded)");
2023-05-14 17:47:49 +00:00
}
2023-03-30 14:43:09 +00:00
Err(e) => {
error!("unable to fix config: {}", e);
error!("nebula thread exiting with error");
return;
}
}
info!("starting nebula child...");
2023-05-14 17:47:49 +00:00
let mut child = match run_embedded_nebula(&[
"-config".to_string(),
get_nebulaconfig_file(&instance)
.unwrap()
.to_str()
.unwrap()
.to_string(),
]) {
2023-03-30 14:43:09 +00:00
Ok(c) => c,
Err(e) => {
error!("unable to start embedded nebula binary: {}", e);
error!("nebula thread exiting with error");
return;
}
};
info!("nebula process started");
2023-03-30 16:13:29 +00:00
let mut last_restart_time = SystemTime::now();
2023-03-30 11:29:02 +00:00
// dont need to save it, because we do not, in any circumstance, write to it
loop {
2023-03-30 16:13:29 +00:00
if let Ok(e) = child.try_wait() {
if e.is_some() && SystemTime::now() > last_restart_time + Duration::from_secs(5) {
info!("nebula process has exited, restarting");
2023-05-14 17:47:49 +00:00
child = match run_embedded_nebula(&[
"-config".to_string(),
get_nebulaconfig_file(&instance)
.unwrap()
.to_str()
.unwrap()
.to_string(),
]) {
2023-03-30 16:13:29 +00:00
Ok(c) => c,
Err(e) => {
error!("unable to start embedded nebula binary: {}", e);
error!("nebula thread exiting with error");
return;
}
};
info!("nebula process started");
last_restart_time = SystemTime::now();
}
}
2023-03-31 12:33:02 +00:00
match rx.recv() {
2023-05-14 17:47:49 +00:00
Ok(msg) => match msg {
NebulaWorkerMessage::WakeUp => {
continue;
}
NebulaWorkerMessage::Shutdown => {
info!("recv on command socket: shutdown, stopping");
info!("shutting down nebula binary");
match child.kill() {
Ok(_) => {
debug!("nebula process exited");
2023-03-30 14:43:09 +00:00
}
2023-05-14 17:47:49 +00:00
Err(e) => {
error!("nebula process already exited: {}", e);
2023-03-30 14:43:09 +00:00
}
2023-05-14 17:47:49 +00:00
}
info!("nebula shut down");
break;
}
NebulaWorkerMessage::ConfigUpdated => {
info!("our configuration has been updated - restarting");
debug!("killing existing process");
match child.kill() {
Ok(_) => {
debug!("nebula process exited");
}
Err(e) => {
error!("nebula process already exited: {}", e);
2023-03-30 14:43:09 +00:00
}
}
2023-05-14 17:47:49 +00:00
debug!("fixing config...");
match insert_private_key(&instance) {
Ok(_) => {
debug!("config fixed (private-key embedded)");
}
Err(e) => {
error!("unable to fix config: {}", e);
error!("nebula thread exiting with error");
return;
}
}
debug!("restarting nebula process");
child = match run_embedded_nebula(&[
"-config".to_string(),
get_nebulaconfig_file(&instance)
.unwrap()
.to_str()
.unwrap()
.to_string(),
]) {
Ok(c) => c,
Err(e) => {
error!("unable to start embedded nebula binary: {}", e);
error!("nebula thread exiting with error");
return;
}
};
last_restart_time = SystemTime::now();
debug!("nebula process restarted");
}
},
Err(e) => {
2023-03-31 12:33:02 +00:00
error!("nebulaworker command socket errored: {}", e);
return;
}
}
}
2023-05-14 17:47:49 +00:00
}