nomilo/src/config.rs

75 lines
1.9 KiB
Rust
Raw Normal View History

2021-03-20 02:31:41 +00:00
use std::net::SocketAddr;
2022-04-27 17:54:40 +00:00
use std::time::Duration;
2021-03-20 02:31:41 +00:00
2021-03-27 17:23:19 +00:00
use serde::{Deserialize, Deserializer};
2021-03-20 02:31:41 +00:00
2022-04-27 17:42:25 +00:00
use crate::models::name::SerdeName;
use crate::dns::TsigAlgorithm;
2021-03-27 17:23:19 +00:00
#[derive(Debug, Deserialize)]
2021-03-20 02:31:41 +00:00
pub struct Config {
2022-04-22 17:16:57 +00:00
pub dns: DnsClientConfig,
2021-03-27 17:23:19 +00:00
pub web_app: WebAppConfig,
2021-03-20 02:31:41 +00:00
}
2021-03-27 17:23:19 +00:00
#[derive(Debug, Deserialize)]
2022-04-22 17:16:57 +00:00
pub struct DnsClientConfig {
2022-04-27 17:42:25 +00:00
pub server: SocketAddr,
#[serde(deserialize_with = "from_std_duration")]
2022-04-27 17:54:40 +00:00
pub timeout: Duration,
2022-04-27 17:42:25 +00:00
pub tsig: Option<TsigConfig>,
}
#[derive(Debug, Deserialize)]
pub struct TsigConfig {
pub name: SerdeName,
#[serde(deserialize_with = "from_base64")]
pub key: Vec<u8>,
#[serde(deserialize_with = "from_tsigalg")]
pub algorithm: TsigAlgorithm,
2021-03-27 17:23:19 +00:00
}
#[derive(Debug, Deserialize)]
pub struct WebAppConfig {
2022-04-27 17:54:40 +00:00
#[serde(deserialize_with = "from_std_duration")]
pub token_duration: Duration,
2021-03-27 17:23:19 +00:00
}
2022-04-27 17:42:25 +00:00
2022-04-27 17:54:40 +00:00
fn from_std_duration<'de, D>(deserializer: D) -> Result<Duration, D::Error>
2021-03-27 17:23:19 +00:00
where D: Deserializer<'de>
{
use serde::de::Error;
String::deserialize(deserializer)
.and_then(|string| humantime::parse_duration(&string).map_err(|err| Error::custom(err.to_string())))
2021-03-20 02:31:41 +00:00
}
2022-04-27 17:42:25 +00:00
fn from_base64<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
where D: Deserializer<'de>
{
use serde::de::Error;
String::deserialize(deserializer)
.and_then(|string| base64::decode(&string).map_err(|err| Error::custom(err.to_string())))
}
2022-04-27 17:42:25 +00:00
fn from_tsigalg<'de, D>(deserializer: D) -> Result<TsigAlgorithm, D::Error>
where D: Deserializer<'de>
{
use serde::de::Error;
let algo = match String::deserialize(deserializer)?.as_str() {
"hmac-sha256" => TsigAlgorithm::HmacSha256,
"hmac-sha384" => TsigAlgorithm::HmacSha384,
"hmac-sha512" => TsigAlgorithm::HmacSha512,
_ => return Err(Error::custom("Unsupported mac algorithm"))
};
if !algo.supported() {
Err(Error::custom("Unsupported mac algorithm"))
} else {
Ok(algo)
}
2022-04-27 17:42:25 +00:00
}