1use std::collections::BTreeMap;
8use std::fmt;
9use std::path::Path;
10use std::time::Duration;
11
12use datumesh_core::json::{self, Value};
13use datumesh_core::lexical;
14
15use crate::apikeys::ApiKeys;
16
17pub const DEFAULT_BUDGET: Duration = Duration::from_secs(1);
19pub const DEFAULT_POOL_SIZE: usize = 32;
22pub const DEFAULT_BATCH_MAX: usize = 1000;
24pub const DEFAULT_SLICE_PAGE: usize = 1000;
26pub const MAX_SLICE_PAGE: usize = 10_000;
28pub const MAX_SLICE_MEMBERS: usize = 1_048_576;
30
31#[non_exhaustive]
34#[derive(Clone, Debug, PartialEq, Eq)]
35pub struct RateLimits {
36 pub enabled: bool,
38 pub per_ip: u32,
40 pub ip_burst: u32,
42 pub per_key: u32,
45 pub verify_per_ip: u32,
47 pub batch_cells_per_token: u32,
50}
51
52impl Default for RateLimits {
53 fn default() -> Self {
54 RateLimits {
55 enabled: true,
56 per_ip: 60,
57 ip_burst: 120,
58 per_key: 600,
59 verify_per_ip: 60,
60 batch_cells_per_token: 50,
61 }
62 }
63}
64
65#[non_exhaustive]
68#[derive(Clone, Debug, PartialEq, Eq)]
69pub struct Ttls {
70 pub key_doc: Duration,
73 pub key_refetch: Duration,
75 pub mesh_doc: Duration,
77 pub binding: Duration,
80 pub current: Duration,
83 pub latest_max_age: Duration,
85 pub job: Duration,
88}
89
90impl Default for Ttls {
91 fn default() -> Self {
92 Ttls {
93 key_doc: Duration::from_secs(300),
94 key_refetch: Duration::from_secs(10),
95 mesh_doc: Duration::from_secs(300),
96 binding: Duration::from_secs(60),
97 current: Duration::from_secs(60),
98 latest_max_age: Duration::from_secs(10),
99 job: Duration::from_secs(300),
100 }
101 }
102}
103
104#[non_exhaustive]
106#[derive(Clone, Debug, PartialEq, Eq)]
107pub enum Rule {
108 Principals(Vec<String>),
111 Price {
114 amount: String,
116 currency: String,
118 },
119}
120
121#[non_exhaustive]
123#[derive(Clone, Debug, Default, PartialEq, Eq)]
124pub struct Policy {
125 pub rules: BTreeMap<String, Rule>,
127}
128
129impl Policy {
130 pub fn rule(&self, fn_name: &str) -> Option<&Rule> {
132 self.rules.get(fn_name)
133 }
134
135 pub fn from_json(bytes: &[u8]) -> Result<Policy, ConfigError> {
137 let v = json::parse(bytes).map_err(|e| ConfigError::new("policy", e.to_string()))?;
138 let mut rules = BTreeMap::new();
139 for (name, r) in v.as_object().ok_or_else(|| ConfigError::new("policy", "not an object"))? {
140 lexical::check_function_name(name, "$").map_err(|e| ConfigError::new("policy", e.to_string()))?;
141 let rule = if let Some(Value::Array(ps)) = r.get("principals") {
142 Rule::Principals(
143 ps.iter()
144 .map(|p| p.as_str().map(str::to_string).ok_or_else(|| ConfigError::new("policy", "principal")))
145 .collect::<Result<_, _>>()?,
146 )
147 } else if let Some(p) = r.get("price") {
148 let amount =
149 p.get("amount").and_then(Value::as_str).ok_or_else(|| ConfigError::new("policy", "amount"))?;
150 let currency =
151 p.get("currency").and_then(Value::as_str).ok_or_else(|| ConfigError::new("policy", "currency"))?;
152 lexical::check_number(amount, "$").map_err(|e| ConfigError::new("policy", e.to_string()))?;
153 if amount.starts_with('-') || amount == "0" || !lexical::is_currency(currency) {
154 return Err(ConfigError::new("policy", format!("price of {name}")));
155 }
156 Rule::Price { amount: amount.to_string(), currency: currency.to_string() }
157 } else {
158 return Err(ConfigError::new("policy", format!("rule of {name}")));
159 };
160 rules.insert(name.clone(), rule);
161 }
162 Ok(Policy { rules })
163 }
164}
165
166#[non_exhaustive]
168#[derive(Clone, Debug)]
169pub struct Config {
170 pub host: String,
173 pub base_url: Option<String>,
192 pub namespaces: Vec<String>,
195 pub budget: Duration,
197 pub pool_size: usize,
199 pub batch_max: usize,
201 pub test_cells: bool,
204 pub slice_page: usize,
206 pub slice_max: usize,
208 pub explain_depth: usize,
210 pub max_jobs: usize,
212 pub rate: RateLimits,
214 pub ttl: Ttls,
216 pub trust_proxy: bool,
219 pub cross_mesh: bool,
221 pub cross_mesh_hosts: Option<Vec<String>>,
224 pub peers: BTreeMap<String, String>,
227 pub peer_timeout: Duration,
229 pub aliases: BTreeMap<String, String>,
231 pub policy: Policy,
233 pub catalog: BTreeMap<String, String>,
235 pub embed_url: Option<String>,
238 pub api_keys: ApiKeys,
240}
241
242impl Config {
243 pub fn new(host: impl Into<String>) -> Config {
245 let host = host.into();
246 Config {
247 namespaces: vec![host.clone()],
248 host,
249 base_url: None,
250 budget: DEFAULT_BUDGET,
251 pool_size: DEFAULT_POOL_SIZE,
252 batch_max: DEFAULT_BATCH_MAX,
253 test_cells: false,
254 slice_page: DEFAULT_SLICE_PAGE,
255 slice_max: MAX_SLICE_MEMBERS,
256 explain_depth: 4,
257 max_jobs: 10_000,
258 rate: RateLimits::default(),
259 ttl: Ttls::default(),
260 trust_proxy: false,
261 cross_mesh: true,
262 cross_mesh_hosts: None,
263 peers: BTreeMap::new(),
264 peer_timeout: Duration::from_secs(10),
265 aliases: BTreeMap::new(),
266 policy: Policy::default(),
267 catalog: BTreeMap::new(),
268 embed_url: None,
269 api_keys: ApiKeys::default(),
270 }
271 }
272
273 pub fn with_base_url(mut self, url: impl Into<String>) -> Config {
276 self.base_url = Some(url.into());
277 self
278 }
279
280 pub fn from_env() -> Result<Config, ConfigError> {
284 Config::from_vars(|k| std::env::var(k).ok())
285 }
286
287 pub fn from_vars(get: impl Fn(&str) -> Option<String>) -> Result<Config, ConfigError> {
289 let host = get("DATUMESH_HOST").ok_or_else(|| ConfigError::new("DATUMESH_HOST", "not set"))?;
290 let mut c = Config::new(host);
291 if let Some(v) = get("DATUMESH_BASE_URL").filter(|v| !v.is_empty()) {
292 c.base_url = Some(v);
293 }
294 if let Some(v) = get("DATUMESH_NAMESPACES") {
295 c.namespaces = v.split(',').map(|s| s.trim().to_string()).filter(|s| !s.is_empty()).collect();
296 }
297 let ms = |k: &str, d: Duration| -> Result<Duration, ConfigError> {
298 match get(k) {
299 Some(v) => v.parse::<u64>().map(Duration::from_millis).map_err(|e| ConfigError::new(k, e.to_string())),
300 None => Ok(d),
301 }
302 };
303 let secs = |k: &str, d: Duration| -> Result<Duration, ConfigError> {
304 match get(k) {
305 Some(v) => v.parse::<u64>().map(Duration::from_secs).map_err(|e| ConfigError::new(k, e.to_string())),
306 None => Ok(d),
307 }
308 };
309 let num = |k: &str, d: usize| -> Result<usize, ConfigError> {
310 match get(k) {
311 Some(v) => v.parse::<usize>().map_err(|e| ConfigError::new(k, e.to_string())),
312 None => Ok(d),
313 }
314 };
315 let flag = |k: &str, d: bool| -> Result<bool, ConfigError> {
316 match get(k).as_deref() {
317 None => Ok(d),
318 Some("1" | "true" | "yes") => Ok(true),
319 Some("0" | "false" | "no") => Ok(false),
320 Some(v) => Err(ConfigError::new(k, format!("not a boolean: {v}"))),
321 }
322 };
323 c.budget = ms("DATUMESH_BUDGET_MS", c.budget)?;
324 c.pool_size = num("DATUMESH_POOL_SIZE", c.pool_size)?;
325 c.batch_max = num("DATUMESH_BATCH_MAX", c.batch_max)?;
326 c.test_cells = flag("DATUMESH_TEST_CELLS", c.test_cells)?;
327 c.slice_page = num("DATUMESH_SLICE_PAGE", c.slice_page)?;
328 c.slice_max = num("DATUMESH_SLICE_MAX", c.slice_max)?;
329 c.explain_depth = num("DATUMESH_EXPLAIN_DEPTH", c.explain_depth)?;
330 c.max_jobs = num("DATUMESH_MAX_JOBS", c.max_jobs)?;
331 c.trust_proxy = flag("DATUMESH_TRUST_PROXY", c.trust_proxy)?;
332 c.cross_mesh = flag("DATUMESH_CROSS_MESH", c.cross_mesh)?;
333 c.rate.enabled = flag("DATUMESH_RATE_LIMITS", c.rate.enabled)?;
334 c.rate.per_ip = num("DATUMESH_RATE_PER_IP", c.rate.per_ip as usize)? as u32;
335 c.rate.ip_burst = num("DATUMESH_RATE_IP_BURST", c.rate.ip_burst as usize)? as u32;
336 c.rate.per_key = num("DATUMESH_RATE_PER_KEY", c.rate.per_key as usize)? as u32;
337 c.rate.verify_per_ip = num("DATUMESH_RATE_VERIFY_PER_IP", c.rate.verify_per_ip as usize)? as u32;
338 c.ttl.key_doc = secs("DATUMESH_TTL_KEY_DOC", c.ttl.key_doc)?;
339 c.ttl.mesh_doc = secs("DATUMESH_TTL_MESH_DOC", c.ttl.mesh_doc)?;
340 c.ttl.binding = secs("DATUMESH_TTL_BINDING", c.ttl.binding)?;
341 c.ttl.current = secs("DATUMESH_TTL_CURRENT", c.ttl.current)?;
342 c.ttl.latest_max_age = secs("DATUMESH_LATEST_MAX_AGE", c.ttl.latest_max_age)?;
343 c.ttl.job = secs("DATUMESH_JOB_TTL", c.ttl.job)?;
344 c.peer_timeout = ms("DATUMESH_PEER_TIMEOUT_MS", c.peer_timeout)?;
345 if let Some(v) = get("DATUMESH_CROSS_MESH_HOSTS") {
346 c.cross_mesh_hosts = Some(v.split(',').map(|s| s.trim().to_string()).filter(|s| !s.is_empty()).collect());
347 }
348 if let Some(v) = get("DATUMESH_PEERS") {
349 for pair in v.split(',').map(str::trim).filter(|s| !s.is_empty()) {
350 let (h, o) =
351 pair.split_once('=').ok_or_else(|| ConfigError::new("DATUMESH_PEERS", "host=origin pairs"))?;
352 c.peers.insert(h.trim().to_string(), o.trim().trim_end_matches('/').to_string());
353 }
354 }
355 if let Some(p) = get("DATUMESH_ALIASES_FILE") {
356 c.aliases = string_map(&read_file(&p)?, "DATUMESH_ALIASES_FILE")?;
357 }
358 if let Some(p) = get("DATUMESH_CATALOG_FILE") {
359 c.catalog = string_map(&read_file(&p)?, "DATUMESH_CATALOG_FILE")?;
360 }
361 if let Some(v) = get("DATUMESH_EMBED_URL").filter(|v| !v.trim().is_empty()) {
362 c.embed_url = Some(v.trim().trim_end_matches('/').to_string());
363 }
364 if let Some(p) = get("DATUMESH_POLICY_FILE") {
365 c.policy = Policy::from_json(&read_file(&p)?)?;
366 }
367 if let Some(p) = get("DATUMESH_API_KEYS_FILE") {
368 c.api_keys = ApiKeys::parse(&read_secret_file(Path::new(&p))?)?;
369 }
370 c.check()?;
371 Ok(c)
372 }
373
374 pub fn check(&self) -> Result<(), ConfigError> {
377 lexical::check_dns_name(&self.host, "$").map_err(|e| ConfigError::new("host", e.to_string()))?;
378 if let Some(u) = &self.base_url {
379 check_base_url(&self.host, u).map_err(|e| ConfigError::new("base URL", format!("{u}: {e}")))?;
380 }
381 for h in &self.namespaces {
382 lexical::check_dns_name(h, "$").map_err(|e| ConfigError::new("namespaces", e.to_string()))?;
383 }
384 for (h, o) in &self.peers {
385 lexical::check_dns_name(h, "$").map_err(|e| ConfigError::new("peers", e.to_string()))?;
386 if !(o.starts_with("http://") || o.starts_with("https://")) {
387 return Err(ConfigError::new("peers", format!("{o}: not an http(s) origin")));
388 }
389 }
390 for (a, n) in &self.aliases {
391 lexical::check_function_name(n, "$").map_err(|e| ConfigError::new("aliases", format!("{a}: {e}")))?;
392 }
393 if self.pool_size == 0 || self.batch_max == 0 || self.slice_page == 0 || self.slice_max == 0 {
394 return Err(ConfigError::new("sizes", "the pool, batch and slice bounds must be positive"));
395 }
396 if self.ttl.job < Duration::from_secs(300) {
397 return Err(ConfigError::new("job TTL", "at least 300 s (SPEC 5.5)"));
398 }
399 if self.budget.is_zero() {
400 return Err(ConfigError::new("budget", "must be positive"));
401 }
402 Ok(())
403 }
404
405 pub fn owns(&self, host: &str) -> bool {
407 self.namespaces.iter().any(|h| h == host)
408 }
409
410 pub fn origin(&self) -> String {
412 format!("https://{}", self.host)
413 }
414
415 pub fn mesh_document_base(&self) -> String {
418 self.base_url.clone().unwrap_or_else(|| self.origin())
419 }
420}
421
422#[non_exhaustive]
424#[derive(Clone, Copy, Debug, PartialEq, Eq)]
425pub enum BaseUrlError {
426 Scheme,
428 HttpRootedHost,
431 HttpNotLoopback,
434 Host,
437 Port,
439 Path,
441 TrailingSlash,
443 Query,
445 Fragment,
447}
448
449impl fmt::Display for BaseUrlError {
450 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
451 f.write_str(match self {
452 BaseUrlError::Scheme => "not an https:// or http:// URL",
453 BaseUrlError::HttpRootedHost => "http:// is for an unrooted host (under .invalid) only",
454 BaseUrlError::HttpNotLoopback => "http:// is for 127.0.0.1, [::1] and localhost only",
455 BaseUrlError::Host => "not a host name, IPv4 address or bracketed IPv6 address",
456 BaseUrlError::Port => "not a port from 1 to 65535",
457 BaseUrlError::Path => "a path (the base URL is a scheme, a host and a port)",
458 BaseUrlError::TrailingSlash => "a trailing slash",
459 BaseUrlError::Query => "a query",
460 BaseUrlError::Fragment => "a fragment",
461 })
462 }
463}
464
465impl std::error::Error for BaseUrlError {}
466
467pub fn check_base_url(host: &str, url: &str) -> Result<(), BaseUrlError> {
474 let mesh_host = host;
475 let (http, rest) = if let Some(r) = url.strip_prefix("https://") {
476 (false, r)
477 } else if let Some(r) = url.strip_prefix("http://") {
478 (true, r)
479 } else {
480 return Err(BaseUrlError::Scheme);
481 };
482 if rest.contains('#') {
483 return Err(BaseUrlError::Fragment);
484 }
485 if rest.contains('?') {
486 return Err(BaseUrlError::Query);
487 }
488 let (authority, path) = rest.split_at(rest.find('/').unwrap_or(rest.len()));
489 match path {
490 "" => {}
491 "/" => return Err(BaseUrlError::TrailingSlash),
492 _ => return Err(BaseUrlError::Path),
493 }
494 if authority.contains('@') {
495 return Err(BaseUrlError::Host);
496 }
497 let (host, port) = if let Some(r) = authority.strip_prefix('[') {
498 let (inside, after) = r.split_once(']').ok_or(BaseUrlError::Host)?;
499 inside.parse::<std::net::Ipv6Addr>().map_err(|_| BaseUrlError::Host)?;
500 let port = match after {
501 "" => None,
502 p => Some(p.strip_prefix(':').ok_or(BaseUrlError::Host)?),
503 };
504 (&authority[..inside.len() + 2], port)
505 } else {
506 let (host, port) = match authority.split_once(':') {
507 Some((h, p)) => (h, Some(p)),
508 None => (authority, None),
509 };
510 if host.parse::<std::net::Ipv4Addr>().is_err() && !is_dns_name(host) {
511 return Err(BaseUrlError::Host);
512 }
513 (host, port)
514 };
515 if let Some(p) = port {
516 let ok = !p.is_empty() && p.len() <= 5 && p.bytes().all(|b| b.is_ascii_digit());
517 if !ok || !(1..=65535).contains(&p.parse::<u32>().unwrap_or(0)) {
518 return Err(BaseUrlError::Port);
519 }
520 }
521 if http && !is_unrooted(mesh_host) {
522 return Err(BaseUrlError::HttpRootedHost);
523 }
524 if http && !matches!(host, "127.0.0.1" | "[::1]" | "localhost") {
525 return Err(BaseUrlError::HttpNotLoopback);
526 }
527 Ok(())
528}
529
530pub fn is_unrooted(host: &str) -> bool {
533 host.rsplit('.').next() == Some("invalid")
534}
535
536fn is_dns_name(h: &str) -> bool {
539 !h.is_empty()
540 && h.len() <= 253
541 && h.split('.').all(|l| {
542 !l.is_empty()
543 && l.len() <= 63
544 && !l.starts_with('-')
545 && !l.ends_with('-')
546 && l.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-')
547 })
548}
549
550fn string_map(bytes: &[u8], what: &str) -> Result<BTreeMap<String, String>, ConfigError> {
551 let v = json::parse(bytes).map_err(|e| ConfigError::new(what, e.to_string()))?;
552 let mut out = BTreeMap::new();
553 for (k, x) in v.as_object().ok_or_else(|| ConfigError::new(what, "not an object"))? {
554 let s = x.as_str().ok_or_else(|| ConfigError::new(what, format!("{k}: not a string")))?;
555 out.insert(k.clone(), s.to_string());
556 }
557 Ok(out)
558}
559
560fn read_file(path: &str) -> Result<Vec<u8>, ConfigError> {
561 std::fs::read(path).map_err(|e| ConfigError::new(path, e.to_string()))
562}
563
564pub fn read_secret_file(path: &Path) -> Result<Vec<u8>, ConfigError> {
567 let what = path.display().to_string();
568 let meta = std::fs::metadata(path).map_err(|e| ConfigError::new(&what, e.to_string()))?;
569 #[cfg(unix)]
570 {
571 use std::os::unix::fs::PermissionsExt;
572 let mode = meta.permissions().mode() & 0o777;
573 if mode & 0o077 != 0 {
574 return Err(ConfigError::new(&what, format!("mode {mode:o}: a secret file must be 0600")));
575 }
576 }
577 #[cfg(not(unix))]
578 let _ = meta;
579 std::fs::read(path).map_err(|e| ConfigError::new(&what, e.to_string()))
580}
581
582#[derive(Clone, Debug, PartialEq, Eq)]
584pub struct ConfigError {
585 what: String,
586 detail: String,
587}
588
589impl ConfigError {
590 pub(crate) fn new(what: &str, detail: impl Into<String>) -> ConfigError {
591 ConfigError { what: what.to_string(), detail: detail.into() }
592 }
593}
594
595impl fmt::Display for ConfigError {
596 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
597 write!(f, "configuration: {}: {}", self.what, self.detail)
598 }
599}
600
601impl std::error::Error for ConfigError {}