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 #[derive(StructOpt,Debug)]
11 /// Top-level config file or directory
13 /// Look for `main.cfg`, `config.d` and `secrets.d` here.
15 /// Or if this is a file, just read that file.
16 #[structopt(long, default_value="/etc/hippotat")]
19 /// Additional config files or dirs, which can override the others
20 #[structopt(long, multiple=true, number_of_values=1)]
21 pub extra_config: Vec<PathBuf>,
24 pub struct CidrString(pub String);
26 #[derive(hippotat_macros::ResolveConfig)]
27 pub struct InstanceConfig {
29 // Exceptional settings
30 #[special(special_name, SKL::ServerName)] pub server: String,
31 pub secret: String, // xxx newytpe
32 #[special(special_ipif, SKL::Ordinary)] pub ipif: String,
36 #[limited] pub max_batch_down: u32,
38 #[limited] pub max_queue_time: Duration,
39 #[limited] pub http_timeout: Duration,
40 #[limited] pub target_requests_outstanding: u32,
43 pub addrs: Vec<IpAddr>,
44 pub vnetwork: Vec<CidrString>,
45 pub vaddr: Vec<IpAddr>,
49 pub ifname_server: String,
50 pub ifname_client: String,
52 // Ordinary settings, used by server only:
53 #[server] pub max_clock_skew: Duration,
55 // Ordinary settings, used by client only:
56 #[client] pub http_timeout_grace: Duration,
57 #[client] pub max_requests_outstanding: u32,
58 #[client] pub max_batch_up: u32,
59 #[client] pub http_retry: Duration,
60 #[client] pub url: Uri,
61 #[client] pub vroutes: Vec<CidrString>,
65 #[derive(Debug,Clone,Hash,Eq,PartialEq)]
66 pub enum SectionName {
69 Server(ServerName), // includes SERVER, which is slightly special
70 ServerLimit(ServerName),
75 pub use SectionName as SN;
77 #[derive(Debug,Clone)]
78 struct RawVal { val: Option<String>, loc: Arc<PathBuf> }
79 type SectionMap = HashMap<String, RawVal>;
85 static OUTSIDE_SECTION: &str = "[";
87 #[derive(Default,Debug)]
89 sections: HashMap<SectionName, SectionMap>,
92 type OkAnyway<'f,A> = &'f dyn Fn(ErrorKind) -> Option<A>;
94 impl<'f,A> OkAnyway<'f,A> {
95 fn ok<T>(self, r: &Result<T, io::Error>) -> Option<A> {
96 let e = r.as_ref().err()?;
103 impl FromStr for SectionName {
106 fn from_str(s: &str) -> Self {
108 "COMMON" => return SN::Common,
109 "DEFAULT" => return SN::Default,
110 "LIMIT" => return SN::GlobalLimit,
113 if let Ok(n@ ServerName(_)) = s.parse() { return SN::Server(n) }
114 if let Ok(n@ ClientName(_)) = s.parse() { return SN::Client(n) }
115 let (server, client) = s.split_ascii_whitespace().collect_tuple()
116 .ok_or_else(|| anyhow!(
117 "bad section name {:?} \
118 (must be COMMON, DEFAULT, <server>, <client>, or <server> <client>",
121 let server = server.parse().context("server name in link section name")?;
122 if client == "LIMIT" { return SN::ServerLimit(server) }
123 let client = client.parse().context("client name in link section name")?;
124 SN::Link(LinkName { server, client })
129 #[throws(AE)] // AE does not include path
130 fn read_file<A>(&mut self, path: &Path, anyway: OkAnyway<A>) -> Option<A>
132 let f = fs::File::open(path);
133 if let Some(anyway) = anyway.ok(&f) { return Some(anyway) }
134 let mut f = f.context("open")?;
136 let mut s = String::new();
137 let y = f.read_to_string(&mut s);
138 if let Some(anyway) = anyway.ok(&y) { return Some(anyway) }
141 let mut ini = Ini::new_cs();
142 ini.set_default_section(OUTSIDE_SECTION);
143 ini.read(s).map_err(|e| anyhow!("{}", e)).context("parse as INI")?;
144 let map = mem::take(ini.get_mut_map());
145 if map.get(OUTSIDE_SECTION).is_some() {
146 throw!(anyhow!("INI file contains settings outside a section"));
149 let loc = Arc::new(path.to_owned());
151 for (sn, vars) in map {
152 dbg!( InstanceConfig::FIELDS );// check xxx vars are in fields
154 let sn = sn.parse().dcontext(&sn)?;
155 self.sections.entry(sn)
161 RawVal { val, loc: loc.clone() })
168 #[throws(AE)] // AE includes path
169 fn read_dir_d<A>(&mut self, path: &Path, anyway: OkAnyway<A>) -> Option<A>
171 let dir = fs::read_dir(path);
172 if let Some(anyway) = anyway.ok(&dir) { return Some(anyway) }
173 let dir = dir.context("open directory").dcontext(path)?;
175 let ent = ent.context("read directory").dcontext(path)?;
176 let leaf = ent.file_name();
177 let leaf = leaf.to_str();
178 let leaf = if let Some(leaf) = leaf { leaf } else { continue }; //utf8?
179 if leaf.len() == 0 { continue }
180 if ! leaf.chars().all(
181 |c| c=='-' || c=='_' || c.is_ascii_alphanumeric()
184 // OK we want this one
185 let ent = ent.path();
186 self.read_file(&ent, &|_| None::<Void>).dcontext(&ent)?;
191 #[throws(AE)] // AE includes everything
192 fn read_toplevel(&mut self, toplevel: &Path) {
193 enum Anyway { None, Dir }
194 match self.read_file(toplevel, &|k| match k {
195 EK::NotFound => Some(Anyway::None),
196 EK::IsADirectory => Some(Anyway::Dir),
199 .dcontext(toplevel).context("top-level config directory (or file)")?
201 None | Some(Anyway::None) => { },
203 Some(Anyway::Dir) => {
205 let anyway_none = |k| match k {
206 EK::NotFound => Some(AnywayNone),
210 let mk = |leaf: &str| {
211 [ toplevel, &PathBuf::from(leaf) ]
212 .iter().collect::<PathBuf>()
215 for &(try_main, desc) in &[
216 ("main.cfg", "main config file"),
217 ("master.cfg", "obsolete-named main config file"),
219 let main = mk(try_main);
221 match self.read_file(&main, &anyway_none)
222 .dcontext(main).context(desc)?
225 Some(AnywayNone) => { },
229 for &(try_dir, desc) in &[
230 ("config.d", "per-link config directory"),
231 ("secrets.d", "per-link secrets directory"),
233 let dir = mk(try_dir);
234 match self.read_dir_d(&dir, &anyway_none).context(desc)? {
236 Some(AnywayNone) => { },
243 #[throws(AE)] // AE includes extra, but does that this is extra
244 fn read_extra(&mut self, extra: &Path) {
247 match self.read_file(extra, &|k| match k {
248 EK::IsADirectory => Some(AnywayDir),
255 self.read_dir_d(extra, &|_| None::<Void>)?;
262 enum LinkEnd { Server, Client }
264 struct ResolveContext<'c> {
268 all_sections: Vec<SectionName>,
271 trait Parseable: Sized {
272 fn parse(s: &Option<String>) -> Result<Self, AE>;
275 impl Parseable for Duration {
277 fn parse(s: &Option<String>) -> Duration {
278 let s = s.as_ref().ok_or_else(|| anyhow!("value needed"))?;
279 if let Ok(u64) = s.parse() { return Duration::from_secs(u64) }
280 throw!(anyhow!("xxx parse with humantime"))
283 macro_rules! parseable_from_str { ($t:ty) => {
284 impl Parseable for $t {
286 fn parse(s: &Option<String>) -> $t {
287 let s = s.as_ref().ok_or_else(|| anyhow!("value needed"))?;
292 parseable_from_str!{u32}
294 #[derive(Debug,Copy,Clone)]
295 enum SectionKindList {
302 use SectionKindList as SKL;
304 impl SectionKindList {
305 fn contains(self, s: &SectionName) -> bool {
307 SKL::Ordinary => matches!(s, SN::Link(_)
312 SKL::Limits => matches!(s, SN::ServerLimit(_)
315 SKL::ClientAgnostic => matches!(s, SN::Common
318 SKL::Limited => SKL::Ordinary.contains(s)
319 | SKL::Limits .contains(s),
321 SKL::ServerName => matches!(s, SN::Common)
322 | matches!(s, SN::Server(ServerName(name))
323 if name == "SERVER"),
328 impl<'c> ResolveContext<'c> {
329 fn first_of_raw(&self, key: &'static str, sections: SectionKindList)
330 -> Option<&'c RawVal> {
331 for section in self.all_sections.iter()
332 .filter(|s| sections.contains(s))
334 if let Some(raw) = self.agg.sections
336 .and_then(|vars: &SectionMap| vars.get(key))
345 fn first_of<T>(&self, key: &'static str, sections: SectionKindList)
349 match self.first_of_raw(key, sections) {
352 Parseable::parse(&raw.val)
354 // .with_context(|| format!(r#"in section "{}""#, §ion))
361 pub fn ordinary<T>(&self, key: &'static str) -> T
362 where T: Parseable + Default
364 self.first_of(key, SKL::Ordinary)?
369 pub fn limited<T>(&self, key: &'static str) -> T
370 where T: Parseable + Default + Ord
372 let val = self.ordinary(key)?;
373 if let Some(limit) = self.first_of(key, SKL::Limits)? {
381 pub fn client<T>(&self, key: &'static str) -> T
382 where T: Parseable + Default {
384 LinkEnd::Client => self.ordinary(key)?,
385 LinkEnd::Server => default(),
389 pub fn server<T>(&self, key: &'static str) -> T
390 where T: Parseable + Default {
392 LinkEnd::Server => self.ordinary(key)?,
393 LinkEnd::Client => default(),
398 pub fn special_ipif<T>(&self, key: &'static str) -> T
399 where T: Parseable + Default
402 LinkEnd::Client => self.ordinary(key)?,
404 self.first_of(key, SKL::ClientAgnostic)?
412 impl<'c> ResolveContext<'c> {
414 fn resolve_instance(&self) -> InstanceConfig {
416 max_batch_down: self.limited::<u32>("max_batch_down")?,
424 let opts = config::Opts::from_args();
427 let mut agg = Aggregate::default();
429 agg.read_toplevel(&opts.config)?;
430 for extra in &opts.extra_config {
431 agg.read_extra(extra).context("extra config")?;
434 eprintln!("GOT {:#?}", agg);
437 })().context("read configuration")?;
439 let link = LinkName {
440 server: "fooxxx".parse().unwrap(),
441 client: "127.0.0.1".parse().unwrap(),
444 let rctx = ResolveContext {
447 end: LinkEnd::Server,
449 SN::Link(link.clone()),
450 SN::Client(link.client.clone()),
451 SN::Server(link.server.clone()),
453 SN::ServerLimit(link.server.clone()),