// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. //! The host body of the API. It mirrors the host TOML file field for field (docs/ui-api.md //! §1.2), so control writes exactly what Mark would write by hand. Core validates the TOML //! control writes; this type only carries it. use serde::{Deserialize, Serialize}; use utoipa::ToSchema; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)] #[serde(deny_unknown_fields)] pub struct HostSpec { /// Lowercase DNS name, e.g. `wiki.home.example`. pub hostname: String, /// Where traffic goes, e.g. `http://10.0.0.23:3000`. Same scheme for all. pub upstreams: Vec, /// `letsencrypt` or `domi`. #[serde(default = "letsencrypt")] pub tls: String, /// `none` or `forward-auth`. #[serde(default = "auth_none")] pub auth: String, #[serde(default = "yes")] pub websockets: bool, #[serde(default)] pub share_links: bool, /// Visitors need a Domi device certificate (mTLS). #[serde(default, skip_serializing_if = "std::ops::Not::not")] pub device_certs: bool, /// `off` or `truncated`. #[serde(default = "off")] pub access_log: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub health_check: Option, #[serde(default, skip_serializing_if = "Vec::is_empty")] pub request_headers: Vec, #[serde(default, skip_serializing_if = "Vec::is_empty")] pub response_headers: Vec, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)] #[serde(deny_unknown_fields)] pub struct HealthCheckSpec { pub path: String, #[serde(default = "ten")] pub interval_secs: i64, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)] #[serde(deny_unknown_fields)] pub struct HeaderRuleSpec { /// `set` or `remove`. pub action: String, pub name: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub value: Option, /// Name of a secret declared in `ianitor.toml`. #[serde(default, skip_serializing_if = "Option::is_none")] pub secret: Option, } fn letsencrypt() -> String { "letsencrypt".into() } fn auth_none() -> String { "none".into() } fn off() -> String { "off".into() } fn yes() -> bool { true } fn ten() -> i64 { 10 } impl HostSpec { /// The TOML file control commits for this host. Every field is written out, so the file /// says exactly what the form said. pub fn to_toml(&self) -> Result { let body = toml::to_string(self).map_err(|e| e.to_string())?; Ok(format!( "# Written by Ianitor control. Edit through the web UI or the API.\n{body}" )) } pub fn from_toml(text: &str) -> Result { toml::from_str(text).map_err(|e| e.message().to_owned()) } /// Field-level differences, labelled for the restore preview (docs/ui-api.md §1.4). pub fn diff(&self, to: &HostSpec) -> Vec<(String, String, String)> { let mut out = Vec::new(); let mut push = |f: &str, a: String, b: String| { if a != b { out.push((f.to_owned(), a, b)); } }; push("hostname", self.hostname.clone(), to.hostname.clone()); push( "upstreams", self.upstreams.join(", "), to.upstreams.join(", "), ); push("tls", self.tls.clone(), to.tls.clone()); push("auth", self.auth.clone(), to.auth.clone()); push( "websockets", self.websockets.to_string(), to.websockets.to_string(), ); push( "share_links", self.share_links.to_string(), to.share_links.to_string(), ); push("access_log", self.access_log.clone(), to.access_log.clone()); push( "device_certs", self.device_certs.to_string(), to.device_certs.to_string(), ); let hc = |h: &Option| h.as_ref().map(|h| (h.path.clone(), h.interval_secs)); let (a, b) = (hc(&self.health_check), hc(&to.health_check)); push( "health_check.path", a.as_ref().map(|x| x.0.clone()).unwrap_or_default(), b.as_ref().map(|x| x.0.clone()).unwrap_or_default(), ); push( "health_check.interval_secs", a.map(|x| x.1.to_string()).unwrap_or_default(), b.map(|x| x.1.to_string()).unwrap_or_default(), ); let rules = |r: &[HeaderRuleSpec], q: &[HeaderRuleSpec]| format!("{} rules", r.len() + q.len()); if self.request_headers != to.request_headers || self.response_headers != to.response_headers { out.push(( "headers".into(), rules(&self.request_headers, &self.response_headers), rules(&to.request_headers, &to.response_headers), )); } out } } #[cfg(test)] mod tests { #![allow(clippy::unwrap_used)] use super::*; #[test] fn round_trips_and_core_accepts_it() { let h = HostSpec { hostname: "wiki.home.test".into(), upstreams: vec!["http://10.0.0.2:3000".into()], tls: "domi".into(), auth: "none".into(), websockets: false, share_links: true, device_certs: true, access_log: "truncated".into(), health_check: Some(HealthCheckSpec { path: "/health".into(), interval_secs: 15, }), request_headers: vec![HeaderRuleSpec { action: "set".into(), name: "X-A".into(), value: Some("1".into()), secret: None, }], response_headers: vec![], }; let text = h.to_toml().unwrap(); assert_eq!(HostSpec::from_toml(&text).unwrap(), h); let m = ianitor_core::load( &ianitor_core::SourceFile { name: "ianitor.toml", contents: "", }, &[ianitor_core::SourceFile { name: "hosts/wiki.home.test.toml", contents: &text, }], ); assert!(m.is_ok(), "{m:?}"); } }