1 // Copyright 2021 Ian Jackson and contributors to Hippotat
2 // SPDX-License-Identifier: AGPL-3.0-or-later
3 // There is NO WARRANTY.
7 use configparser::ini::Ini;
9 static DEFAULT_CONFIG: &str = r#"
11 max_batch_down = 65536
13 target_requests_outstanding = 3
15 http_timeout_grace = 5
16 max_requests_outstanding = 6
21 ifname_client = hippo%%d
22 ifname_server = shippo%%d
25 ipif = userv root ipif %(local)s,%(peer)s,%(mtu)s,slip,%(ifname)s %(rnets)s
29 vnetwork = 172.24.230.192
32 max_batch_down = 262144
35 target_requests_outstanding = 10
38 #[derive(StructOpt,Debug)]
40 /// Top-level config file or directory
42 /// Look for `main.cfg`, `config.d` and `secrets.d` here.
44 /// Or if this is a file, just read that file.
45 #[structopt(long, default_value="/etc/hippotat")]
48 /// Additional config files or dirs, which can override the others
49 #[structopt(long, multiple=true, number_of_values=1)]
50 pub extra_config: Vec<PathBuf>,
54 impl<'s> Option<&'s str> {
56 fn value(self) -> &'s str {
57 self.ok_or_else(|| anyhow!("value needed"))?
62 pub struct Secret(pub String);
63 impl Parseable for Secret {
65 fn parse(s: Option<&str>) -> Self {
67 if s.is_empty() { throw!(anyhow!("secret value cannot be empty")) }
71 fn default() -> Self { Secret(default()) }
73 impl Debug for Secret {
75 fn fmt(&self, f: &mut fmt::Formatter) { write!(f, "Secret(***)")? }
78 #[derive(hippotat_macros::ResolveConfig)]
79 #[derive(Debug,Clone)]
80 pub struct InstanceConfig {
81 // Exceptional settings
82 #[special(special_server, SKL::ServerName)] pub server: ServerName,
84 #[special(special_ipif, SKL::Ordinary)] pub ipif: String,
87 #[limited] pub max_batch_down: u32,
88 #[limited] pub max_queue_time: Duration,
89 #[limited] pub http_timeout: Duration,
90 #[limited] pub target_requests_outstanding: u32,
93 pub addrs: Vec<IpAddr>,
94 pub vnetwork: Vec<IpNet>,
99 pub ifname_server: String,
100 pub ifname_client: String,
102 // Ordinary settings, used by server only:
103 #[server] pub max_clock_skew: Duration,
105 // Ordinary settings, used by client only:
106 #[client] pub http_timeout_grace: Duration,
107 #[client] pub max_requests_outstanding: u32,
108 #[client] pub max_batch_up: u32,
109 #[client] pub http_retry: Duration,
110 #[client] pub url: Uri,
111 #[client] pub vroutes: Vec<IpNet>,
114 #[derive(Debug,Clone,Hash,Eq,PartialEq)]
115 pub enum SectionName {
118 Server(ServerName), // includes SERVER, which is slightly special
119 ServerLimit(ServerName),
123 pub use SectionName as SN;
125 #[derive(Debug,Clone)]
126 struct RawVal { raw: Option<String>, loc: Arc<PathBuf> }
127 type SectionMap = HashMap<String, RawVal>;
130 struct RawValRef<'v,'l,'s> {
131 raw: Option<&'v str>,
134 section: &'s SectionName,
137 impl<'v> RawValRef<'v,'_,'_> {
139 fn try_map<F,T>(&self, f: F) -> T
140 where F: FnOnce(Option<&'v str>) -> Result<T, AE> {
142 .with_context(|| format!(r#"file {:?}, section {}, key "{}""#,
143 self.loc, self.section, self.key))?
151 static OUTSIDE_SECTION: &str = "[";
152 static SPECIAL_SERVER_SECTION: &str = "SERVER";
154 #[derive(Default,Debug)]
156 keys_allowed: HashMap<&'static str, SectionKindList>,
157 sections: HashMap<SectionName, SectionMap>,
160 type OkAnyway<'f,A> = &'f dyn Fn(ErrorKind) -> Option<A>;
162 impl<'f,A> OkAnyway<'f,A> {
163 fn ok<T>(self, r: &Result<T, io::Error>) -> Option<A> {
164 let e = r.as_ref().err()?;
171 impl FromStr for SectionName {
174 fn from_str(s: &str) -> Self {
176 "COMMON" => return SN::Common,
177 "LIMIT" => return SN::GlobalLimit,
180 if let Ok(n@ ServerName(_)) = s.parse() { return SN::Server(n) }
181 if let Ok(n@ ClientName(_)) = s.parse() { return SN::Client(n) }
182 let (server, client) = s.split_ascii_whitespace().collect_tuple()
183 .ok_or_else(|| anyhow!(
184 "bad section name {:?} \
185 (must be COMMON, DEFAULT, <server>, <client>, or <server> <client>",
188 let server = server.parse().context("server name in link section name")?;
189 if client == "LIMIT" { return SN::ServerLimit(server) }
190 let client = client.parse().context("client name in link section name")?;
191 SN::Link(LinkName { server, client })
194 impl Display for SectionName {
195 #[throws(fmt::Error)]
196 fn fmt(&self, f: &mut fmt::Formatter) {
198 SN::Link (ref l) => Display::fmt(l, f)?,
199 SN::Client(ref c) => write!(f, "[{}]" , c)?,
200 SN::Server(ref s) => write!(f, "[{}]" , s)?,
201 SN::ServerLimit(ref s) => write!(f, "[{} LIMIT] ", s)?,
202 SN::GlobalLimit => write!(f, "[LIMIT]" )?,
203 SN::Common => write!(f, "[COMMON]" )?,
209 #[throws(AE)] // AE does not include path
210 fn read_file<A>(&mut self, path: &Path, anyway: OkAnyway<A>) -> Option<A>
212 let f = fs::File::open(path);
213 if let Some(anyway) = anyway.ok(&f) { return Some(anyway) }
214 let mut f = f.context("open")?;
216 let mut s = String::new();
217 let y = f.read_to_string(&mut s);
218 if let Some(anyway) = anyway.ok(&y) { return Some(anyway) }
221 self.read_string(s, path)?;
225 #[throws(AE)] // AE does not include path
226 fn read_string(&mut self, s: String, path_for_loc: &Path) {
227 let mut ini = Ini::new_cs();
228 ini.set_default_section(OUTSIDE_SECTION);
229 ini.read(s).map_err(|e| anyhow!("{}", e)).context("parse as INI")?;
230 let map = mem::take(ini.get_mut_map());
231 if map.get(OUTSIDE_SECTION).is_some() {
232 throw!(anyhow!("INI file contains settings outside a section"));
235 let loc = Arc::new(path_for_loc.to_owned());
237 for (sn, vars) in map {
238 let sn = sn.parse().dcontext(&sn)?;
240 for key in vars.keys() {
241 let skl = self.keys_allowed.get(key.as_str()).ok_or_else(
242 || anyhow!("unknown configuration key {:?}", key)
244 if ! skl.contains(&sn) {
245 throw!(anyhow!("configuration key {:?} not applicable \
246 in this kind of section {:?}", key, &sn))
250 let ent = self.sections.entry(sn).or_default();
251 for (key, raw) in vars {
252 let raw = match raw {
253 Some(raw) if raw.starts_with('\'') || raw.starts_with('"') => Some(
255 if raw.contains('\\') {
257 anyhow!("quoted value contains backslash, not supported")
260 let unq = raw[1..].strip_suffix(&raw[0..1])
262 || anyhow!("mismatched quotes around quoted value")
267 .with_context(|| format!("key {:?}", key))
268 .dcontext(path_for_loc)?
272 let key = key.replace('-',"_");
273 ent.insert(key, RawVal { raw, loc: loc.clone() });
278 #[throws(AE)] // AE includes path
279 fn read_dir_d<A>(&mut self, path: &Path, anyway: OkAnyway<A>) -> Option<A>
281 let dir = fs::read_dir(path);
282 if let Some(anyway) = anyway.ok(&dir) { return Some(anyway) }
283 let dir = dir.context("open directory").dcontext(path)?;
285 let ent = ent.context("read directory").dcontext(path)?;
286 let leaf = ent.file_name();
287 let leaf = leaf.to_str();
288 let leaf = if let Some(leaf) = leaf { leaf } else { continue }; //utf8?
289 if leaf.len() == 0 { continue }
290 if ! leaf.chars().all(
291 |c| c=='-' || c=='_' || c.is_ascii_alphanumeric()
294 // OK we want this one
295 let ent = ent.path();
296 self.read_file(&ent, &|_| None::<Void>).dcontext(&ent)?;
301 #[throws(AE)] // AE includes everything
302 fn read_toplevel(&mut self, toplevel: &Path) {
303 enum Anyway { None, Dir }
304 match self.read_file(toplevel, &|k| match k {
305 EK::NotFound => Some(Anyway::None),
306 EK::IsADirectory => Some(Anyway::Dir),
309 .dcontext(toplevel).context("top-level config directory (or file)")?
311 None | Some(Anyway::None) => { },
313 Some(Anyway::Dir) => {
315 let anyway_none = |k| match k {
316 EK::NotFound => Some(AnywayNone),
320 let mk = |leaf: &str| {
321 [ toplevel, &PathBuf::from(leaf) ]
322 .iter().collect::<PathBuf>()
325 for &(try_main, desc) in &[
326 ("main.cfg", "main config file"),
327 ("master.cfg", "obsolete-named main config file"),
329 let main = mk(try_main);
331 match self.read_file(&main, &anyway_none)
332 .dcontext(main).context(desc)?
335 Some(AnywayNone) => { },
339 for &(try_dir, desc) in &[
340 ("config.d", "per-link config directory"),
341 ("secrets.d", "per-link secrets directory"),
343 let dir = mk(try_dir);
344 match self.read_dir_d(&dir, &anyway_none).context(desc)? {
346 Some(AnywayNone) => { },
353 #[throws(AE)] // AE includes extra, but does that this is extra
354 fn read_extra(&mut self, extra: &Path) {
357 match self.read_file(extra, &|k| match k {
358 EK::IsADirectory => Some(AnywayDir),
365 self.read_dir_d(extra, &|_| None::<Void>)?;
373 fn instances(&self, only_server: Option<&ServerName>) -> BTreeSet<LinkName> {
374 let mut links: BTreeSet<LinkName> = default();
376 let mut secrets_anyserver: BTreeSet<&ClientName> = default();
377 let mut secrets_anyclient: BTreeSet<&ServerName> = default();
378 let mut secret_global = false;
380 let mut putative_servers = BTreeSet::new();
381 let mut putative_clients = BTreeSet::new();
383 let mut note_server = |s| {
384 if let Some(only) = only_server { if s != only { return false } }
385 putative_servers.insert(s);
388 let mut note_client = |c| {
389 putative_clients.insert(c);
392 for (section, vars) in &self.sections {
393 let has_secret = || vars.contains_key("secret");
397 if ! note_server(&l.server) { continue }
398 note_client(&l.client);
399 if has_secret() { links.insert(l.clone()); }
401 SN::Server(ref s) => {
402 if ! note_server(s) { continue }
403 if has_secret() { secrets_anyclient.insert(s); }
405 SN::Client(ref c) => {
407 if has_secret() { secrets_anyserver.insert(c); }
410 if has_secret() { secret_global = true; }
416 // Add links which are justified by blanket secrets
417 for (client, server) in iproduct!(
418 putative_clients.into_iter().filter(
419 |c| secret_global || secrets_anyserver.contains(c)
421 putative_servers.iter().cloned().filter(
422 |s| secret_global || secrets_anyclient.contains(s)
425 links.insert(LinkName {
426 client: client.clone(),
427 server: server.clone(),
435 struct ResolveContext<'c> {
439 all_sections: Vec<SectionName>,
442 trait Parseable: Sized {
443 fn parse(s: Option<&str>) -> Result<Self, AE>;
444 fn default() -> Result<Self, AE> {
445 Err(anyhow!("setting must be specified"))
448 fn default_for_key(key: &str) -> Self {
449 Self::default().with_context(|| key.to_string())?
453 impl Parseable for Duration {
455 fn parse(s: Option<&str>) -> Duration {
456 // todo: would be nice to parse with humantime maybe
457 Duration::from_secs( s.value()?.parse()? )
460 macro_rules! parseable_from_str { ($t:ty $(, $def:expr)? ) => {
461 impl Parseable for $t {
463 fn parse(s: Option<&str>) -> $t { s.value()?.parse()? }
464 $( #[throws(AE)] fn default() -> Self { $def } )?
467 parseable_from_str!{u16, default() }
468 parseable_from_str!{u32, default() }
469 parseable_from_str!{String, default() }
470 parseable_from_str!{IpNet, default() }
471 parseable_from_str!{IpAddr, Ipv4Addr::UNSPECIFIED.into() }
472 parseable_from_str!{Uri, default() }
474 impl<T:Parseable> Parseable for Vec<T> {
476 fn parse(s: Option<&str>) -> Vec<T> {
478 .split_ascii_whitespace()
479 .map(|s| Parseable::parse(Some(s)))
480 .collect::<Result<Vec<_>,_>>()?
483 fn default() -> Self { default() }
487 #[derive(Debug,Copy,Clone)]
488 enum SectionKindList {
495 use SectionKindList as SKL;
498 fn special_server_section() -> Self { SN::Server(ServerName(
499 SPECIAL_SERVER_SECTION.into()
503 impl SectionKindList {
504 fn contains(self, s: &SectionName) -> bool {
506 SKL::Ordinary => matches!(s, SN::Link(_)
511 SKL::Limits => matches!(s, SN::ServerLimit(_)
514 SKL::ClientAgnostic => matches!(s, SN::Common
517 SKL::Limited => SKL::Ordinary.contains(s)
518 | SKL::Limits .contains(s),
520 SKL::ServerName => matches!(s, SN::Common)
521 | matches!(s, SN::Server(ServerName(name))
522 if name == SPECIAL_SERVER_SECTION),
528 fn lookup_raw<'a,'s,S>(&'a self, key: &'static str, sections: S)
529 -> Option<RawValRef<'a,'a,'s>>
530 where S: Iterator<Item=&'s SectionName>
532 for section in sections {
533 if let Some(raw) = self.sections
535 .and_then(|vars: &SectionMap| vars.get(key))
537 return Some(RawValRef {
538 raw: raw.raw.as_deref(),
548 pub fn establish_server_name(&self) -> ServerName {
550 let raw = match self.lookup_raw(
552 [ &SectionName::Common, &SN::special_server_section() ].iter().cloned()
554 Some(raw) => raw.try_map(|os| os.value())?,
555 None => SPECIAL_SERVER_SECTION,
557 ServerName(raw.into())
561 impl<'c> ResolveContext<'c> {
562 fn first_of_raw(&'c self, key: &'static str, sections: SectionKindList)
563 -> Option<RawValRef<'c,'c,'c>> {
566 self.all_sections.iter()
567 .filter(|s| sections.contains(s))
572 fn first_of<T>(&self, key: &'static str, sections: SectionKindList)
576 match self.first_of_raw(key, sections) {
578 Some(raw) => Some(raw.try_map(Parseable::parse)?),
583 pub fn ordinary<T>(&self, key: &'static str) -> T
586 match self.first_of(key, SKL::Ordinary)? {
588 None => Parseable::default_for_key(key)?,
593 pub fn limited<T>(&self, key: &'static str) -> T
594 where T: Parseable + Ord
596 let val = self.ordinary(key)?;
597 if let Some(limit) = self.first_of(key, SKL::Limits)? {
605 pub fn client<T>(&self, key: &'static str) -> T
606 where T: Parseable + Default {
608 LinkEnd::Client => self.ordinary(key)?,
609 LinkEnd::Server => default(),
613 pub fn server<T>(&self, key: &'static str) -> T
614 where T: Parseable + Default {
616 LinkEnd::Server => self.ordinary(key)?,
617 LinkEnd::Client => default(),
622 pub fn special_ipif(&self, key: &'static str) -> String {
624 LinkEnd::Client => self.ordinary(key)?,
626 self.first_of(key, SKL::ClientAgnostic)?
633 pub fn special_server(&self, key: &'static str) -> ServerName {
634 self.link.server.clone()
638 impl InstanceConfig {
640 fn complete(&mut self, end: LinkEnd) {
641 let mut vhosts = self.vnetwork.iter()
642 .map(|n| n.hosts()).flatten()
643 .filter({ let vaddr = self.vaddr; move |v| v != &vaddr });
645 if self.vaddr.is_unspecified() {
646 self.vaddr = vhosts.next().ok_or_else(
647 || anyhow!("vnetwork too small to generate vaddrr")
650 if self.vrelay.is_unspecified() {
651 self.vrelay = vhosts.next().ok_or_else(
652 || anyhow!("vnetwork too small to generate vrelay")
658 if &self.url == &default::<Uri>() {
659 let addr = self.addrs.get(0).ok_or_else(
660 || anyhow!("client needs addrs or url set")
665 IpAddr::V4(a) => format!("{}", a),
666 IpAddr::V6(a) => format!("[{}]", a),
670 p => format!(":{}", p),
677 if self.addrs.is_empty() {
678 throw!(anyhow!("missing 'addrs' setting"))
686 pub fn read(end: LinkEnd) -> Vec<InstanceConfig> {
687 let opts = config::Opts::from_args();
690 let mut agg = Aggregate::default();
691 agg.keys_allowed.extend(
692 InstanceConfig::FIELDS.iter().cloned()
695 agg.read_string(DEFAULT_CONFIG.into(),
696 "<build-in defaults>".as_ref()).unwrap();
698 agg.read_toplevel(&opts.config)?;
699 for extra in &opts.extra_config {
700 agg.read_extra(extra).context("extra config")?;
703 eprintln!("GOT {:#?}", agg);
706 })().context("read configuration")?;
708 let server_name = match end {
709 LinkEnd::Server => Some(agg.establish_server_name()?),
710 LinkEnd::Client => None,
713 let instances = agg.instances(server_name.as_ref());
714 let mut ics = vec![];
716 for link in instances {
717 let rctx = ResolveContext {
722 SN::Link(link.clone()),
723 SN::Client(link.client.clone()),
724 SN::Server(link.server.clone()),
726 SN::ServerLimit(link.server.clone()),
731 let mut ic = InstanceConfig::resolve_instance(&rctx)
732 .with_context(|| format!("resolve config for {}", &link))?;
735 .with_context(|| format!("complete config for {}", &link))?;