chiark / gitweb /
config: check key sections
[hippotat.git] / src / config.rs
1 // Copyright 2021 Ian Jackson and contributors to Hippotat
2 // SPDX-License-Identifier: AGPL-3.0-or-later
3 // There is NO WARRANTY.
4
5 use crate::prelude::*;
6
7 use configparser::ini::Ini;
8
9 static DEFAULT_CONFIG: &str = r#"
10 [COMMON]
11 max_batch_down = 65536
12 max_queue_time = 10
13 target_requests_outstanding = 3
14 http_timeout = 30
15 http_timeout_grace = 5
16 max_requests_outstanding = 6
17 max_batch_up = 4000
18 http_retry = 5
19 port = 80
20 vroutes = ''
21 ifname_client = hippo%%d
22 ifname_server = shippo%%d
23 max_clock_skew = 300
24
25 ipif = userv root ipif %(local)s,%(peer)s,%(mtu)s,slip,%(ifname)s %(rnets)s
26
27 mtu = 1500
28
29 vnetwork = 172.24.230.192
30
31 [LIMIT]
32 max_batch_down = 262144
33 max_queue_time = 121
34 http_timeout = 121
35 target_requests_outstanding = 10
36 "#;
37
38 #[derive(StructOpt,Debug)]
39 pub struct Opts {
40   /// Top-level config file or directory
41   ///
42   /// Look for `main.cfg`, `config.d` and `secrets.d` here.
43   ///
44   /// Or if this is a file, just read that file.
45   #[structopt(long, default_value="/etc/hippotat")]
46   pub config: PathBuf,
47   
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>,
51 }
52
53 #[ext]
54 impl<'s> Option<&'s str> {
55   #[throws(AE)]
56   fn value(self) -> &'s str {
57     self.ok_or_else(|| anyhow!("value needed"))?
58   }
59 }
60
61 #[derive(Clone)]
62 pub struct Secret(pub String);
63 impl Parseable for Secret {
64   #[throws(AE)]
65   fn parse(s: Option<&str>) -> Self {
66     let s = s.value()?;
67     if s.is_empty() { throw!(anyhow!("secret value cannot be empty")) }
68     Secret(s.into())
69   }
70   #[throws(AE)]
71   fn default() -> Self { Secret(default()) }
72 }
73 impl Debug for Secret {
74   #[throws(fmt::Error)]
75   fn fmt(&self, f: &mut fmt::Formatter) { write!(f, "Secret(***)")? }
76 }
77
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,
83   pub                                             secret: Secret,
84   #[special(special_ipif, SKL::Ordinary)]     pub ipif:   String,
85
86   // Capped settings:
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,
91
92   // Ordinary settings:
93   pub addrs:                        Vec<IpAddr>,
94   pub vnetwork:                     Vec<IpNet>,
95   pub vaddr:                        IpAddr,
96   pub vrelay:                       IpAddr,
97   pub port:                         u16,
98   pub mtu:                          u32,
99   pub ifname_server:                String,
100   pub ifname_client:                String,
101
102   // Ordinary settings, used by server only:
103   #[server]  pub max_clock_skew:               Duration,
104
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>,
112 }
113
114 #[derive(Debug,Clone,Hash,Eq,PartialEq)]
115 pub enum SectionName {
116   Link(LinkName),
117   Client(ClientName),
118   Server(ServerName), // includes SERVER, which is slightly special
119   ServerLimit(ServerName),
120   GlobalLimit,
121   Common,
122 }
123 pub use SectionName as SN;
124
125 #[derive(Debug,Clone)]
126 struct RawVal { raw: Option<String>, loc: Arc<PathBuf> }
127 type SectionMap = HashMap<String, RawVal>;
128
129 #[derive(Debug)]
130 struct RawValRef<'v,'l,'s> {
131   raw: Option<&'v str>,
132   key: &'static str,
133   loc: &'l Path,
134   section: &'s SectionName,
135 }
136
137 impl<'v> RawValRef<'v,'_,'_> {
138   #[throws(AE)]
139   fn try_map<F,T>(&self, f: F) -> T
140   where F: FnOnce(Option<&'v str>) -> Result<T, AE> {
141     f(self.raw)
142       .with_context(|| format!(r#"file {:?}, section {}, key "{}""#,
143                                self.loc, self.section, self.key))?
144   }
145 }
146
147 pub struct Config {
148   opts: Opts,
149 }
150
151 static OUTSIDE_SECTION: &str = "[";
152 static SPECIAL_SERVER_SECTION: &str = "SERVER";
153
154 #[derive(Default,Debug)]
155 struct Aggregate {
156   keys_allowed: HashMap<&'static str, SectionKindList>,
157   sections: HashMap<SectionName, SectionMap>,
158 }
159
160 type OkAnyway<'f,A> = &'f dyn Fn(ErrorKind) -> Option<A>;
161 #[ext]
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()?;
165     let k = e.kind();
166     let a = self(k)?;
167     Some(a)
168   }
169 }
170
171 impl FromStr for SectionName {
172   type Err = AE;
173   #[throws(AE)]
174   fn from_str(s: &str) -> Self {
175     match s {
176       "COMMON" => return SN::Common,
177       "LIMIT" => return SN::GlobalLimit,
178       _ => { }
179     };
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>",
186         s
187       ))?;
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 })
192   }
193 }
194 impl Display for SectionName {
195   #[throws(fmt::Error)]
196   fn fmt(&self, f: &mut fmt::Formatter) {
197     match self {
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]"      )?,
204     }
205   }
206 }
207
208 impl Aggregate {
209   #[throws(AE)] // AE does not include path
210   fn read_file<A>(&mut self, path: &Path, anyway: OkAnyway<A>) -> Option<A>
211   {
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")?;
215
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) }
219     y.context("read")?;
220
221     self.read_string(s, path)?;
222     None
223   }
224
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"));
233     }
234
235     let loc = Arc::new(path_for_loc.to_owned());
236
237     for (sn, vars) in map {
238       let sn = sn.parse().dcontext(&sn)?;
239
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)
243         )?;
244         if ! skl.contains(&sn) {
245           throw!(anyhow!("configuration key {:?} not applicable \
246                           in this kind of section {:?}", key, &sn))
247         }
248       }
249
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(
254             (||{
255               if raw.contains('\\') {
256                 throw!(
257                   anyhow!("quoted value contains backslash, not supported")
258                 );
259               }
260               let unq = raw[1..].strip_suffix(&raw[0..1])
261                 .ok_or_else(
262                   || anyhow!("mismatched quotes around quoted value")
263                 )?
264                 .to_owned();
265               Ok::<_,AE>(unq)
266             })()
267               .with_context(|| format!("key {:?}", key))
268               .dcontext(path_for_loc)?
269           ),
270           x => x,
271         };
272         let key = key.replace('-',"_");
273         ent.insert(key, RawVal { raw, loc: loc.clone() });
274       }
275     }
276   }
277
278   #[throws(AE)] // AE includes path
279   fn read_dir_d<A>(&mut self, path: &Path, anyway: OkAnyway<A>) -> Option<A>
280   {
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)?;
284     for ent in dir {
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()
292       ) { continue }
293
294       // OK we want this one
295       let ent = ent.path();
296       self.read_file(&ent, &|_| None::<Void>).dcontext(&ent)?;
297     }
298     None
299   }
300
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),
307       _ => None,
308     })
309       .dcontext(toplevel).context("top-level config directory (or file)")?
310     {
311       None | Some(Anyway::None) => { },
312
313       Some(Anyway::Dir) => {
314         struct AnywayNone;
315         let anyway_none = |k| match k {
316           EK::NotFound => Some(AnywayNone),
317           _ => None,
318         };
319
320         let mk = |leaf: &str| {
321           [ toplevel, &PathBuf::from(leaf) ]
322             .iter().collect::<PathBuf>()
323         };
324
325         for &(try_main, desc) in &[
326           ("main.cfg", "main config file"),
327           ("master.cfg", "obsolete-named main config file"),
328         ] {
329           let main = mk(try_main);
330
331           match self.read_file(&main, &anyway_none)
332             .dcontext(main).context(desc)?
333           {
334             None => break,
335             Some(AnywayNone) => { },
336           }
337         }
338
339         for &(try_dir, desc) in &[
340           ("config.d", "per-link config directory"),
341           ("secrets.d", "per-link secrets directory"),
342         ] {
343           let dir = mk(try_dir);
344           match self.read_dir_d(&dir, &anyway_none).context(desc)? {
345             None => { },
346             Some(AnywayNone) => { },
347           }
348         }
349       }
350     }
351   }
352
353   #[throws(AE)] // AE includes extra, but does that this is extra
354   fn read_extra(&mut self, extra: &Path) {
355     struct AnywayDir;
356
357     match self.read_file(extra, &|k| match k {
358       EK::IsADirectory => Some(AnywayDir),
359       _ => None,
360     })
361       .dcontext(extra)?
362     {
363       None => return,
364       Some(AnywayDir) => {
365         self.read_dir_d(extra, &|_| None::<Void>)?;
366       }
367     }
368
369   }
370 }
371
372 impl Aggregate {
373   fn instances(&self, only_server: Option<&ServerName>) -> BTreeSet<LinkName> {
374     let mut links:              BTreeSet<LinkName> = default();
375
376     let mut secrets_anyserver:  BTreeSet<&ClientName> = default();
377     let mut secrets_anyclient:  BTreeSet<&ServerName> = default();
378     let mut secret_global       = false;
379
380     let mut putative_servers   = BTreeSet::new();
381     let mut putative_clients   = BTreeSet::new();
382
383     let mut note_server = |s| {
384       if let Some(only) = only_server { if s != only { return false } }
385       putative_servers.insert(s);
386       true
387     };
388     let mut note_client = |c| {
389       putative_clients.insert(c);
390     };
391
392     for (section, vars) in &self.sections {
393       let has_secret = || vars.contains_key("secret");
394
395       match section {
396         SN::Link(l) => {
397           if ! note_server(&l.server) { continue }
398           note_client(&l.client);
399           if has_secret() { links.insert(l.clone()); }
400         },
401         SN::Server(ref s) => {
402           if ! note_server(s) { continue }
403           if has_secret() { secrets_anyclient.insert(s); }
404         },
405         SN::Client(ref c) => {
406           note_client(c);
407           if has_secret() { secrets_anyserver.insert(c); }
408         },
409         SN::Common => {
410           if has_secret() { secret_global = true; }
411         },
412         _ => { },
413       }
414     }
415
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)
420       ),
421       putative_servers.iter().cloned().filter(
422         |s| secret_global || secrets_anyclient.contains(s)
423       )
424     ) {
425       links.insert(LinkName {
426         client: client.clone(),
427         server: server.clone(),
428       });
429     }
430
431     links
432   }
433 }
434
435 struct ResolveContext<'c> {
436   agg: &'c Aggregate,
437   link: &'c LinkName,
438   end: LinkEnd,
439   all_sections: Vec<SectionName>,
440 }
441
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"))
446   }
447   #[throws(AE)]
448   fn default_for_key(key: &str) -> Self {
449     Self::default().with_context(|| key.to_string())?
450   }
451 }
452
453 impl Parseable for Duration {
454   #[throws(AE)]
455   fn parse(s: Option<&str>) -> Duration {
456     // todo: would be nice to parse with humantime maybe
457     Duration::from_secs( s.value()?.parse()? )
458   }
459 }
460 macro_rules! parseable_from_str { ($t:ty $(, $def:expr)? ) => {
461   impl Parseable for $t {
462     #[throws(AE)]
463     fn parse(s: Option<&str>) -> $t { s.value()?.parse()? }
464     $( #[throws(AE)] fn default() -> Self { $def } )?
465   }
466 } }
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() }
473
474 impl<T:Parseable> Parseable for Vec<T> {
475   #[throws(AE)]
476   fn parse(s: Option<&str>) -> Vec<T> {
477     s.value()?
478       .split_ascii_whitespace()
479       .map(|s| Parseable::parse(Some(s)))
480       .collect::<Result<Vec<_>,_>>()?
481   }
482   #[throws(AE)]
483   fn default() -> Self { default() }
484 }
485
486
487 #[derive(Debug,Copy,Clone)]
488 enum SectionKindList {
489   Ordinary,
490   Limited,
491   Limits,
492   ClientAgnostic,
493   ServerName,
494 }
495 use SectionKindList as SKL;
496
497 impl SectionName {
498   fn special_server_section() -> Self { SN::Server(ServerName(
499     SPECIAL_SERVER_SECTION.into()
500   )) }
501 }
502
503 impl SectionKindList {
504   fn contains(self, s: &SectionName) -> bool {
505     match self {
506       SKL::Ordinary       => matches!(s, SN::Link(_)
507                                        | SN::Client(_)
508                                        | SN::Server(_)
509                                        | SN::Common),
510
511       SKL::Limits         => matches!(s, SN::ServerLimit(_)
512                                        | SN::GlobalLimit),
513
514       SKL::ClientAgnostic => matches!(s, SN::Common
515                                        | SN::Server(_)),
516
517       SKL::Limited        => SKL::Ordinary.contains(s)
518                            | SKL::Limits  .contains(s),
519
520       SKL::ServerName     => matches!(s, SN::Common)
521                            | matches!(s, SN::Server(ServerName(name))
522                                          if name == SPECIAL_SERVER_SECTION),
523     }
524   }
525 }
526
527 impl Aggregate {
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>
531   {
532     for section in sections {
533       if let Some(raw) = self.sections
534         .get(section)
535         .and_then(|vars: &SectionMap| vars.get(key))
536       {
537         return Some(RawValRef {
538           raw: raw.raw.as_deref(),
539           loc: &raw.loc,
540           section, key,
541         })
542       }
543     }
544     None
545   }
546
547   #[throws(AE)]
548   pub fn establish_server_name(&self) -> ServerName {
549     let key = "server";
550     let raw = match self.lookup_raw(
551       key,
552       [ &SectionName::Common, &SN::special_server_section() ].iter().cloned()
553     ) {
554       Some(raw) => raw.try_map(|os| os.value())?,
555       None => SPECIAL_SERVER_SECTION,
556     };
557     ServerName(raw.into())
558   }
559 }
560
561 impl<'c> ResolveContext<'c> {
562   fn first_of_raw(&'c self, key: &'static str, sections: SectionKindList)
563                   -> Option<RawValRef<'c,'c,'c>> {
564     self.agg.lookup_raw(
565       key,
566       self.all_sections.iter()
567         .filter(|s| sections.contains(s))
568     )
569   }
570
571   #[throws(AE)]
572   fn first_of<T>(&self, key: &'static str, sections: SectionKindList)
573                  -> Option<T>
574   where T: Parseable
575   {
576     match self.first_of_raw(key, sections) {
577       None => None,
578       Some(raw) => Some(raw.try_map(Parseable::parse)?),
579     }
580   }
581
582   #[throws(AE)]
583   pub fn ordinary<T>(&self, key: &'static str) -> T
584   where T: Parseable
585   {
586     match self.first_of(key, SKL::Ordinary)? {
587       Some(y) => y,
588       None => Parseable::default_for_key(key)?,
589     }
590   }
591
592   #[throws(AE)]
593   pub fn limited<T>(&self, key: &'static str) -> T
594   where T: Parseable + Ord
595   {
596     let val = self.ordinary(key)?;
597     if let Some(limit) = self.first_of(key, SKL::Limits)? {
598       min(val, limit)
599     } else {
600       val
601     }
602   }
603
604   #[throws(AE)]
605   pub fn client<T>(&self, key: &'static str) -> T
606   where T: Parseable + Default {
607     match self.end {
608       LinkEnd::Client => self.ordinary(key)?,
609       LinkEnd::Server => default(),
610     }
611   }
612   #[throws(AE)]
613   pub fn server<T>(&self, key: &'static str) -> T
614   where T: Parseable + Default {
615     match self.end {
616       LinkEnd::Server => self.ordinary(key)?,
617       LinkEnd::Client => default(),
618     }
619   }
620
621   #[throws(AE)]
622   pub fn special_ipif(&self, key: &'static str) -> String {
623     match self.end {
624       LinkEnd::Client => self.ordinary(key)?,
625       LinkEnd::Server => {
626         self.first_of(key, SKL::ClientAgnostic)?
627           .unwrap_or_default()
628       },
629     }
630   }
631
632   #[throws(AE)]
633   pub fn special_server(&self, key: &'static str) -> ServerName {
634     self.link.server.clone()
635   }
636 }
637
638 impl InstanceConfig {
639   #[throws(AE)]
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 });
644
645     if self.vaddr.is_unspecified() {
646       self.vaddr = vhosts.next().ok_or_else(
647         || anyhow!("vnetwork too small to generate vaddrr")
648       )?;
649     }
650     if self.vrelay.is_unspecified() {
651       self.vrelay = vhosts.next().ok_or_else(
652         || anyhow!("vnetwork too small to generate vrelay")
653       )?;
654     }
655
656     match end {
657       LinkEnd::Client => {
658         if &self.url == &default::<Uri>() {
659           let addr = self.addrs.get(0).ok_or_else(
660             || anyhow!("client needs addrs or url set")
661           )?;
662           self.url = format!(
663             "http://{}{}/",
664             match addr {
665               IpAddr::V4(a) => format!("{}", a),
666               IpAddr::V6(a) => format!("[{}]", a),
667             },
668             match self.port {
669               80 => format!(""),
670               p => format!(":{}", p),
671             })
672             .parse().unwrap()
673         }
674       },
675
676       LinkEnd::Server => {
677         if self.addrs.is_empty() {
678           throw!(anyhow!("missing 'addrs' setting"))
679         }
680       },
681     }
682   }
683 }
684
685 #[throws(AE)]
686 pub fn read(end: LinkEnd) -> Vec<InstanceConfig> {
687   let opts = config::Opts::from_args();
688
689   let agg = (||{
690     let mut agg = Aggregate::default();
691     agg.keys_allowed.extend(
692       InstanceConfig::FIELDS.iter().cloned()
693     );
694
695     agg.read_string(DEFAULT_CONFIG.into(),
696                     "<build-in defaults>".as_ref()).unwrap();
697
698     agg.read_toplevel(&opts.config)?;
699     for extra in &opts.extra_config {
700       agg.read_extra(extra).context("extra config")?;
701     }
702
703     eprintln!("GOT {:#?}", agg);
704
705     Ok::<_,AE>(agg)
706   })().context("read configuration")?;
707
708   let server_name = match end {
709     LinkEnd::Server => Some(agg.establish_server_name()?),
710     LinkEnd::Client => None,
711   };
712
713   let instances = agg.instances(server_name.as_ref());
714   let mut ics = vec![];
715
716   for link in instances {
717     let rctx = ResolveContext {
718       agg: &agg,
719       link: &link,
720       end,
721       all_sections: vec![
722         SN::Link(link.clone()),
723         SN::Client(link.client.clone()),
724         SN::Server(link.server.clone()),
725         SN::Common,
726         SN::ServerLimit(link.server.clone()),
727         SN::GlobalLimit,
728       ],
729     };
730
731     let mut ic = InstanceConfig::resolve_instance(&rctx)
732       .with_context(|| format!("resolve config for {}", &link))?;
733
734     ic.complete(end)
735       .with_context(|| format!("complete config for {}", &link))?;
736
737     ics.push(ic);
738   }
739
740   ics
741 }