Environment parsing done with Serde Envy #5

Merged
quentin merged 3 commits from adrien/diplonat:feature/config-handling into main 2021-08-19 06:32:55 +00:00
7 changed files with 34 additions and 78 deletions
Showing only changes of commit 644e707956 - Show all commits

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
target/
*.swp
.env

View file

@ -9,7 +9,7 @@ use crate::config::RuntimeConfig;
// runtime.rs applies business logic and builds RuntimeConfig structs.
/// Base configuration options
#[derive(Clone, Debug, Default, Deserialize)]
#[derive(Clone, Default, Deserialize)]
pub struct ConfigOptsBase {
/// This node's private IP address [default: None]
pub private_ip: Option<String>,
@ -20,7 +20,7 @@ pub struct ConfigOptsBase {
}
/// ACME configuration options
#[derive(Clone, Debug, Default, Deserialize)]
#[derive(Clone, Default, Deserialize)]
pub struct ConfigOptsAcme {
/// Whether ACME is enabled [default: false]
#[serde(default)]
@ -31,7 +31,7 @@ pub struct ConfigOptsAcme {
}
/// Consul configuration options
#[derive(Clone, Debug, Default, Deserialize)]
#[derive(Clone, Default, Deserialize)]
pub struct ConfigOptsConsul {
/// Consul's node name [default: None]
pub node_name: Option<String>,
@ -40,7 +40,6 @@ pub struct ConfigOptsConsul {
}
/// Model of all potential configuration options
#[derive(Debug)]
pub struct ConfigOpts {
pub base: ConfigOptsBase,
pub acme: ConfigOptsAcme,

View file

@ -9,25 +9,30 @@ use crate::config::{ConfigOpts, ConfigOptsAcme, ConfigOptsBase, ConfigOptsConsul
// In this file, we take ConfigOpts and transform them into ready-to-use RuntimeConfig.
// We apply default values and business logic.
#[derive(Debug)]
pub struct RuntimeConfigAcme {
pub email: String,
}
#[derive(Debug)]
pub struct RuntimeConfigConsul {
pub node_name: String,
pub url: String,
}
#[derive(Debug)]
pub struct RuntimeConfigFirewall {
pub refresh_time: Duration,
}
#[derive(Debug)]
pub struct RuntimeConfigIgd {
pub private_ip: String,
pub expiration_time: Duration,
pub refresh_time: Duration,
}
#[derive(Debug)]
pub struct RuntimeConfig {
pub acme: Option<RuntimeConfigAcme>,
pub consul: RuntimeConfigConsul,
@ -57,7 +62,9 @@ impl RuntimeConfigAcme {
return Ok(None);
}
let email = opts.email.unwrap();
let email = opts.email.expect(
"'DIPLONAT_ACME_EMAIL' environment variable is required \
if 'DIPLONAT_ACME_ENABLE' == 'true'");
Ok(Some(Self {
email,
@ -67,7 +74,8 @@ impl RuntimeConfigAcme {
impl RuntimeConfigConsul {
pub(super) fn new(opts: ConfigOptsConsul) -> Result<Self> {
let node_name = opts.node_name.unwrap();
let node_name = opts.node_name.expect(
"'DIPLONAT_CONSUL_NODE_NAME' environment variable is required");
let url = opts.url.unwrap_or(super::CONSUL_URL.to_string());
Ok(Self {
@ -90,7 +98,8 @@ impl RuntimeConfigFirewall {
impl RuntimeConfigIgd {
pub(super) fn new(opts: ConfigOptsBase) -> Result<Self> {
let private_ip = opts.private_ip.unwrap();
let private_ip = opts.private_ip.expect(
"'DIPLONAT_PRIVATE_IP' environment variable is required");
let expiration_time = Duration::from_secs(
opts.expiration_time.unwrap_or(super::EXPIRATION_TIME).into());
let refresh_time = Duration::from_secs(

View file

@ -1,31 +1,35 @@
use anyhow::Result;
use tokio::try_join;
use crate::config::ConfigOpts;
use crate::consul_actor::ConsulActor;
use crate::igd_actor::IgdActor;
use crate::environment::Environment;
use crate::fw_actor::FirewallActor;
use crate::igd_actor::IgdActor;
pub struct Diplonat {
consul: ConsulActor,
firewall: FirewallActor,
igd: IgdActor,
firewall: FirewallActor
}
impl Diplonat {
pub async fn new() -> Result<Self> {
let env = Environment::new()?;
let ca = ConsulActor::new(&env.consul_url, &env.consul_node_name);
let ia = IgdActor::new(
&env.private_ip,
env.refresh_time,
env.expiration_time,
&ca.rx_open_ports
).await?;
let rt_cfg = ConfigOpts::from_env()?;
println!("{:#?}", rt_cfg);
let ca = ConsulActor::new(&rt_cfg.consul.url, &rt_cfg.consul.node_name);
let fw = FirewallActor::new(
env.refresh_time,
rt_cfg.firewall.refresh_time,
&ca.rx_open_ports
).await?;
let ia = IgdActor::new(
&rt_cfg.igd.private_ip,
rt_cfg.igd.refresh_time,
rt_cfg.igd.expiration_time,
&ca.rx_open_ports
).await?;
let ctx = Self {
consul: ca,

View file

@ -1,56 +0,0 @@
use std::env;
use anyhow::{Result, Context, anyhow};
use std::time::Duration;
use log::*;
const epi: &'static str = "DIPLONAT_PRIVATE_IP";
const ert: &'static str = "DIPLONAT_REFRESH_TIME";
const eet: &'static str = "DIPLONAT_EXPIRATION_TIME";
const ecnd: &'static str = "DIPLONAT_CONSUL_NODE_NAME";
const ecu: &'static str = "DIPLONAT_CONSUL_URL";
pub struct Environment {
pub consul_node_name: String,
pub consul_url: String,
pub refresh_time: Duration,
pub expiration_time: Duration,
pub private_ip: String,
}
/* @FIXME: Rewrite with Serde Envi */
impl Environment {
pub fn new() -> Result<Self> {
let ctx = Self {
consul_url: match env::var(ecu) { Ok(e) => e, Err(_) => "http://127.0.0.1:8500".to_string() },
consul_node_name: env::var(ecnd).with_context(|| format!("{} env var must be defined", ecnd))?,
private_ip: env::var(epi).with_context(|| format!("{} env var must be defined, eg: 192.168.0.18", epi))?,
refresh_time: Duration::from_secs(env::var(ert)
.with_context(|| format!("{} env var must be defined, eg: 60", ert))?
.parse()
.with_context(|| format!("{} env var must be an integer, eg: 60", ert))?),
expiration_time: Duration::from_secs(env::var(eet)
.with_context(|| format!("{} env var must be defined, eg: 300", eet))?
.parse()
.with_context(|| format!("{} env var must be an integer, eg: 300", eet))?),
};
if ctx.refresh_time.as_secs() * 2 > ctx.expiration_time.as_secs() {
return Err(anyhow!(
"Expiration time (currently: {}s) must be twice bigger than refresh time (currently: {}s)",
ctx.refresh_time.as_secs(),
ctx.expiration_time.as_secs()));
}
info!("Consul URL: {:#?}", ctx.consul_url);
info!("Consul node name: {:#?}", ctx.consul_node_name);
info!("Private IP address: {:#?}", ctx.private_ip);
info!("Refresh time: {:#?} seconds", ctx.refresh_time.as_secs());
info!("Expiration time: {:#?} seconds", ctx.expiration_time.as_secs());
return Ok(ctx);
}
}

View file

@ -25,8 +25,8 @@ impl IgdActor {
pub async fn new(priv_ip: &str, refresh: Duration, expire: Duration, rxp: &watch::Receiver<messages::PublicExposedPorts>) -> Result<Self> {
let gw = search_gateway(Default::default())
.await
.context("Failed to find gateway")?;
info!("Gateway: {}", gw);
.context("Failed to find IGD gateway")?;
info!("IGD gateway: {}", gw);
let ctx = Self {
gateway: gw,

View file

@ -2,7 +2,6 @@ mod config;
mod consul;
mod consul_actor;
mod diplonat;
mod environment;
mod fw;
mod fw_actor;
mod igd_actor;