OXYGENHUB
◆ oh-comment-body-file oxygenhub / crates / hub / src / config.rs
17.2 KiB · Rust · last changed Metrics token and Grafana URL from the environment 17 days ago source blame history raw
1
//! Configuration (hub spec §17.1): `hub.toml` in the data directory, every
2
//! key overridable by `OXYHUB_<SECTION>_<KEY>`.
3
4
use serde::Deserialize;
5
use std::net::SocketAddr;
6
use std::path::{Path, PathBuf};
7
8
#[derive(Clone, Debug, Deserialize)]
9
#[serde(default, deny_unknown_fields)]
10
pub struct Config {
11
    /// Set from the command line, never from the file.
12
    #[serde(skip)]
13
    pub data: PathBuf,
14
    pub site: Site,
15
    pub server: Server,
16
    pub tls: Tls,
17
    pub auth: Auth,
18
    pub registration: Registration,
19
    pub email: Email,
20
    pub limits: Limits,
21
    pub retention: Retention,
22
    pub flow: Flow,
23
    pub apt: Apt,
24
    pub diffusion: Diffusion,
25
    pub backups: Backups,
26
    pub outbound: Outbound,
27
    pub database: Database,
28
    pub tlog: Tlog,
29
}
30
31
/// Where a hub built with the `postgres` feature keeps its tables (#211); a
32
/// SQLite build keeps them in `<data>/hub.db` and ignores this.
33
#[derive(Clone, Debug, Default, Deserialize)]
34
#[serde(default, deny_unknown_fields)]
35
pub struct Database {
36
    /// `postgres://user@host/db`; the password from the URL or `PGPASSWORD`.
37
    pub url: String,
38
    /// The schema to use, made if missing; empty for `public`, `*` for one
39
    /// per data directory (named after it), as the tests use.
40
    pub schema: String,
41
}
42
43
/// Outbound requests to addresses people give (oxygenhub#219): private
44
/// and local addresses are refused unless listed here, as `scheme://host`
45
/// (any port) or `scheme://host:port`, e.g. a local Ollama:
46
/// `allow = ["http://127.0.0.1:11434"]`.
47
#[derive(Clone, Debug, Default, Deserialize)]
48
#[serde(default, deny_unknown_fields)]
49
pub struct Outbound {
50
    pub allow: Vec<String>,
51
}
52
53
/// The transparency log's witnesses (docs/transparency.md, #238): who
54
/// cosigns this hub's checkpoints, and whose logs this hub cosigns.
55
#[derive(Clone, Debug, Default, Deserialize)]
56
#[serde(default, deny_unknown_fields)]
57
pub struct Tlog {
58
    pub witnesses: Vec<TlogWitness>,
59
    pub witness_for: Vec<TlogWitnessed>,
60
}
61
62
/// A witness this hub's log asks: its name in cosignatures, its
63
/// add-checkpoint address (`https://HUB/log/witness/add-checkpoint` for
64
/// another hub), and its identity object in base64 (another hub's
65
/// `/.well-known/oxyhub-key`).
66
#[derive(Clone, Debug, Deserialize)]
67
#[serde(deny_unknown_fields)]
68
pub struct TlogWitness {
69
    pub name: String,
70
    pub url: String,
71
    pub key: String,
72
}
73
74
/// A log this hub witnesses: its origin (`HOST/log`) and its key, an
75
/// identity object in base64.
76
#[derive(Clone, Debug, Deserialize)]
77
#[serde(deny_unknown_fields)]
78
pub struct TlogWitnessed {
79
    pub origin: String,
80
    pub key: String,
81
}
82
83
/// Backups (docs/backups.md, oxygenhub#169): the directories on this host
84
/// an owner may back up to. Empty, only GCS destinations can be chosen.
85
#[derive(Clone, Debug, Deserialize)]
86
#[serde(default, deny_unknown_fields)]
87
pub struct Backups {
88
    pub directories: Vec<PathBuf>,
89
    /// Google's token exchange and Cloud Storage, for GCS destinations;
90
    /// set only to point them elsewhere (tests, a private endpoint).
91
    pub sts_url: String,
92
    pub storage_url: String,
93
    /// AWS STS and S3, for S3 destinations; `{bucket}` and `{region}` in
94
    /// `s3_url` are filled in. Set only to point them elsewhere.
95
    pub aws_sts_url: String,
96
    pub s3_url: String,
97
}
98
99
impl Default for Backups {
100
    fn default() -> Self {
101
        Backups {
102
            directories: Vec::new(),
103
            sts_url: "https://sts.googleapis.com".into(),
104
            storage_url: "https://storage.googleapis.com".into(),
105
            aws_sts_url: "https://sts.amazonaws.com".into(),
106
            s3_url: "https://{bucket}.s3.{region}.amazonaws.com".into(),
107
        }
108
    }
109
}
110
111
/// Diffusion (#148, docs/diffusion.md §4.2): the site's default for
112
/// repositories that say nothing.
113
#[derive(Clone, Debug, Deserialize)]
114
#[serde(default, deny_unknown_fields)]
115
pub struct Diffusion {
116
    /// `off`, `green` or `any`.
117
    pub follow: String,
118
}
119
120
impl Default for Diffusion {
121
    fn default() -> Self {
122
        Diffusion { follow: "off".into() }
123
    }
124
}
125
126
/// The apt repository at `/apt` (oxygenhub#85): the `.deb` assets of these
127
/// public repositories' releases, signed with the key in `gnupg_home`.
128
#[derive(Clone, Debug, Deserialize)]
129
#[serde(default, deny_unknown_fields)]
130
pub struct Apt {
131
    /// `owner/name` of each repository whose release `.deb`s are served;
132
    /// none turns `/apt` off.
133
    pub repos: Vec<String>,
134
    /// The suite apt's `sources` names.
135
    pub suite: String,
136
    pub component: String,
137
    /// GnuPG home holding the signing key (relative to the data directory).
138
    pub gnupg_home: String,
139
    /// Architectures the index lists, each with its Packages file.
140
    pub architectures: Vec<String>,
141
}
142
143
impl Default for Apt {
144
    fn default() -> Self {
145
        Apt {
146
            repos: vec![],
147
            suite: "stable".into(),
148
            component: "main".into(),
149
            gnupg_home: "apt-gnupg".into(),
150
            architectures: vec!["amd64".into(), "arm64".into(), "riscv64".into()],
151
        }
152
    }
153
}
154
155
/// OxygenFlow runs (hub spec §13.2).
156
#[derive(Clone, Debug, Deserialize)]
157
#[serde(default, deny_unknown_fields)]
158
pub struct Flow {
159
    /// Bearer tokens runners present to claim jobs.
160
    pub runner_tokens: Vec<String>,
161
    /// `owner/*` or `owner/name`: repositories whose pushes start runs.
162
    pub repos: Vec<String>,
163
    pub job_timeout_minutes: u64,
164
    /// A runner whose last this many jobs all ended in an infrastructure
165
    /// error (status `error`, not `failed`) is drained and the
166
    /// administrators told (oxygenflow#30); 0 never drains one.
167
    pub drain_after_errors: u32,
168
}
169
170
impl Default for Flow {
171
    fn default() -> Self {
172
        Flow { runner_tokens: vec![], repos: vec![], job_timeout_minutes: 60, drain_after_errors: 5 }
173
    }
174
}
175
176
impl Flow {
177
    /// Whether pushes to `owner/name` start runs.
178
    pub fn allows(&self, owner: &str, name: &str) -> bool {
179
        self.repos.iter().any(|p| match p.split_once('/') {
180
            Some((o, "*")) => o == owner,
181
            Some((o, n)) => o == owner && n == name,
182
            None => false,
183
        })
184
    }
185
}
186
187
#[derive(Clone, Debug, Default, Deserialize)]
188
#[serde(default, deny_unknown_fields)]
189
pub struct Email {
190
    /// `smtp://user:pass@host:port` or `smtps://…`; empty disables email.
191
    pub smtp: String,
192
    /// `OxygenHub <hub@example>`.
193
    pub from: String,
194
}
195
196
#[derive(Clone, Debug, Deserialize)]
197
#[serde(default, deny_unknown_fields)]
198
pub struct Registration {
199
    /// `open`, `invite` or `closed`; the admin page can override it.
200
    pub mode: String,
201
    /// The setup code the first account must bring (oxygenhub#245), for
202
    /// automation; empty, the hub makes one and keeps it in
203
    /// `<data>/setup-code`.
204
    pub setup_code: String,
205
}
206
207
impl Default for Registration {
208
    fn default() -> Self {
209
        Registration { mode: "invite".into(), setup_code: String::new() }
210
    }
211
}
212
213
#[derive(Clone, Debug, Deserialize)]
214
#[serde(default, deny_unknown_fields)]
215
pub struct Site {
216
    pub name: String,
217
    pub description: String,
218
}
219
220
#[derive(Clone, Debug, Deserialize)]
221
#[serde(default, deny_unknown_fields)]
222
pub struct Server {
223
    /// Plain HTTP; empty disables it.
224
    pub bind: String,
225
    /// HTTPS; empty disables it. Needs `tls`.
226
    pub tls_bind: String,
227
    /// What links and clone URLs start with.
228
    pub base_url: String,
229
    /// Trust `X-Forwarded-*` from a terminating proxy.
230
    pub behind_proxy: bool,
231
    /// Strict-Transport-Security's max-age in seconds, sent when
232
    /// `base_url` is https; 0 sends none (oxygenhub#231). Raise it in
233
    /// stages: browsers keep it, so it is hard to take back.
234
    pub hsts_max_age: u64,
235
    /// The bearer token `/metrics` asks for (oxygenhub#212); empty, the
236
    /// metrics are off.
237
    pub metrics_token: String,
238
    /// A Grafana for this hub's metrics, linked from Insights (oxygenhub#257)
239
    /// for charts of one's own; empty, no link.
240
    pub grafana_url: String,
241
}
242
243
#[derive(Clone, Debug, Deserialize)]
244
#[serde(default, deny_unknown_fields)]
245
pub struct Tls {
246
    /// Generate a certificate at `init` (developer edition).
247
    pub self_signed: bool,
248
    /// PEM files; relative paths are under the data directory.
249
    pub cert: String,
250
    pub key: String,
251
}
252
253
#[derive(Clone, Debug, Default, Deserialize)]
254
#[serde(default, deny_unknown_fields)]
255
pub struct Auth {
256
    /// Bearer tokens allowed to push until accounts exist (hub 0.2). Empty
257
    /// means every push is accepted, as `oxy serve` does.
258
    pub push_tokens: Vec<String>,
259
    /// For tests only: a base URL that stands in for every sign-in
260
    /// provider (`{base}/{provider}/authorize|token|userinfo`).
261
    pub signin_mock: Option<String>,
262
}
263
264
#[derive(Clone, Debug, Deserialize)]
265
#[serde(default, deny_unknown_fields)]
266
pub struct Limits {
267
    /// Largest file rendered in the browser, e.g. `2MiB`.
268
    pub render_limit: ByteSize,
269
    pub asset_limit: ByteSize,
270
    pub push_body_limit: ByteSize,
271
}
272
273
#[derive(Clone, Debug, Deserialize)]
274
#[serde(default, deny_unknown_fields)]
275
pub struct Retention {
276
    pub trash_days: u32,
277
    pub webhook_log_days: u32,
278
    /// The UTC hour at which every repository is garbage collected
279
    /// (spec §17.2); 24 or more turns the nightly gc off.
280
    pub gc_hour: u32,
281
}
282
283
/// A byte count written as `512`, `2MiB`, `4GiB`, `100kB`.
284
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
285
pub struct ByteSize(pub u64);
286
287
impl ByteSize {
288
    pub fn parse(s: &str) -> Option<ByteSize> {
289
        let s = s.trim();
290
        let digits: String = s.chars().take_while(|c| c.is_ascii_digit()).collect();
291
        let unit = s[digits.len()..].trim();
292
        let n: u64 = digits.parse().ok()?;
293
        let mult = match unit {
294
            "" | "B" => 1,
295
            "k" | "kB" | "KB" => 1000,
296
            "KiB" | "K" => 1 << 10,
297
            "M" | "MB" => 1_000_000,
298
            "MiB" => 1 << 20,
299
            "G" | "GB" => 1_000_000_000,
300
            "GiB" => 1 << 30,
301
            _ => return None,
302
        };
303
        Some(ByteSize(n.checked_mul(mult)?))
304
    }
305
}
306
307
impl<'de> Deserialize<'de> for ByteSize {
308
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<ByteSize, D::Error> {
309
        #[derive(Deserialize)]
310
        #[serde(untagged)]
311
        enum Raw {
312
            Number(u64),
313
            Text(String),
314
        }
315
        match Raw::deserialize(d)? {
316
            Raw::Number(n) => Ok(ByteSize(n)),
317
            Raw::Text(t) => ByteSize::parse(&t).ok_or_else(|| serde::de::Error::custom(format!("not a byte size: {t:?}"))),
318
        }
319
    }
320
}
321
322
impl Default for Site {
323
    fn default() -> Self {
324
        Site { name: "OxygenHub".into(), description: "Repositories, proposals and issues for Oxygen".into() }
325
    }
326
}
327
328
impl Default for Server {
329
    fn default() -> Self {
330
        Server {
331
            bind: "127.0.0.1:7300".into(),
332
            tls_bind: String::new(),
333
            base_url: "http://127.0.0.1:7300".into(),
334
            behind_proxy: false,
335
            hsts_max_age: 0,
336
            metrics_token: String::new(),
337
            grafana_url: String::new(),
338
        }
339
    }
340
}
341
342
impl Default for Tls {
343
    fn default() -> Self {
344
        Tls { self_signed: false, cert: "keys/tls.crt".into(), key: "keys/tls.key".into() }
345
    }
346
}
347
348
impl Default for Limits {
349
    fn default() -> Self {
350
        Limits { render_limit: ByteSize(2 << 20), asset_limit: ByteSize(512 << 20), push_body_limit: ByteSize(4 << 30) }
351
    }
352
}
353
354
impl Default for Retention {
355
    fn default() -> Self {
356
        Retention { trash_days: 30, webhook_log_days: 30, gc_hour: 3 }
357
    }
358
}
359
360
impl Default for Config {
361
    fn default() -> Self {
362
        Config {
363
            data: PathBuf::from("data"),
364
            site: Site::default(),
365
            server: Server::default(),
366
            tls: Tls::default(),
367
            auth: Auth::default(),
368
            registration: Registration::default(),
369
            email: Email::default(),
370
            limits: Limits::default(),
371
            retention: Retention::default(),
372
            flow: Flow::default(),
373
            apt: Apt::default(),
374
            diffusion: Diffusion::default(),
375
            backups: Backups::default(),
376
            outbound: Outbound::default(),
377
            database: Database::default(),
378
            tlog: Tlog::default(),
379
        }
380
    }
381
}
382
383
impl Config {
384
    /// Read `<data>/hub.toml` if present, then apply environment overrides.
385
    pub fn load(data: &Path) -> anyhow::Result<Config> {
386
        let path = data.join("hub.toml");
387
        let mut cfg = match std::fs::read_to_string(&path) {
388
            Ok(text) => Config::parse(&text)?,
389
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Config::default(),
390
            Err(e) => return Err(anyhow::anyhow!("reading {}: {e}", path.display())),
391
        };
392
        cfg.data = data.to_path_buf();
393
        cfg.apply_env(|k| std::env::var(k).ok());
394
        Ok(cfg)
395
    }
396
397
    pub fn parse(text: &str) -> anyhow::Result<Config> {
398
        Ok(toml::from_str(text)?)
399
    }
400
401
    /// `OXYHUB_SERVER_BIND`, `OXYHUB_SITE_NAME`, … override the file.
402
    pub fn apply_env(&mut self, get: impl Fn(&str) -> Option<String>) {
403
        if let Some(v) = get("OXYHUB_DATABASE_URL") {
404
            self.database.url = v;
405
        }
406
        if let Some(v) = get("OXYHUB_DATABASE_SCHEMA") {
407
            self.database.schema = v;
408
        }
409
        if let Some(v) = get("OXYHUB_SITE_NAME") {
410
            self.site.name = v;
411
        }
412
        if let Some(v) = get("OXYHUB_SITE_DESCRIPTION") {
413
            self.site.description = v;
414
        }
415
        if let Some(v) = get("OXYHUB_SERVER_BIND") {
416
            self.server.bind = v;
417
        }
418
        if let Some(v) = get("OXYHUB_SERVER_TLS_BIND") {
419
            self.server.tls_bind = v;
420
        }
421
        if let Some(v) = get("OXYHUB_SERVER_BASE_URL") {
422
            self.server.base_url = v;
423
        }
424
        if let Some(v) = get("OXYHUB_SERVER_BEHIND_PROXY") {
425
            self.server.behind_proxy = matches!(v.as_str(), "1" | "true" | "yes");
426
        }
427
        if let Some(v) = get("OXYHUB_SERVER_HSTS_MAX_AGE") {
428
            self.server.hsts_max_age = v.parse().unwrap_or(0);
429
        }
430
        // The VM's setup gives these (oxygenhub-gcp#12): the token its
431
        // collector scrapes /metrics with, and its Grafana for Insights' link.
432
        if let Some(v) = get("OXYHUB_SERVER_METRICS_TOKEN") {
433
            self.server.metrics_token = v;
434
        }
435
        if let Some(v) = get("OXYHUB_SERVER_GRAFANA_URL") {
436
            self.server.grafana_url = v;
437
        }
438
        if let Some(v) = get("OXYHUB_TLS_SELF_SIGNED") {
439
            self.tls.self_signed = matches!(v.as_str(), "1" | "true" | "yes");
440
        }
441
        if let Some(v) = get("OXYHUB_REGISTRATION_MODE") {
442
            self.registration.mode = v;
443
        }
444
        if let Some(v) = get("OXYHUB_REGISTRATION_SETUP_CODE") {
445
            self.registration.setup_code = v;
446
        }
447
        if let Some(v) = get("OXYHUB_AUTH_PUSH_TOKENS") {
448
            self.auth.push_tokens = v.split(',').map(str::trim).filter(|s| !s.is_empty()).map(String::from).collect();
449
        }
450
        if let Some(v) = get("OXYHUB_LIMITS_RENDER_LIMIT").and_then(|v| ByteSize::parse(&v)) {
451
            self.limits.render_limit = v;
452
        }
453
    }
454
455
    pub fn bind_addr(&self) -> anyhow::Result<Option<SocketAddr>> {
456
        parse_addr(&self.server.bind)
457
    }
458
459
    pub fn tls_bind_addr(&self) -> anyhow::Result<Option<SocketAddr>> {
460
        parse_addr(&self.server.tls_bind)
461
    }
462
463
    pub fn tls_cert(&self) -> PathBuf {
464
        self.data.join(&self.tls.cert)
465
    }
466
467
    pub fn tls_key(&self) -> PathBuf {
468
        self.data.join(&self.tls.key)
469
    }
470
471
    pub fn repos_dir(&self) -> PathBuf {
472
        self.data.join("repos")
473
    }
474
475
    pub fn keys_dir(&self) -> PathBuf {
476
        self.data.join("keys")
477
    }
478
479
    pub fn db_path(&self) -> PathBuf {
480
        self.data.join("hub.db")
481
    }
482
483
    /// The base URL without a trailing slash.
484
    pub fn base_url(&self) -> &str {
485
        self.server.base_url.trim_end_matches('/')
486
    }
487
}
488
489
fn parse_addr(s: &str) -> anyhow::Result<Option<SocketAddr>> {
490
    if s.trim().is_empty() {
491
        return Ok(None);
492
    }
493
    Ok(Some(s.parse().map_err(|e| anyhow::anyhow!("bind address {s:?}: {e}"))?))
494
}
495
496
#[cfg(test)]
497
mod tests {
498
    use super::*;
499
500
    #[test]
501
    fn byte_sizes() {
502
        assert_eq!(ByteSize::parse("2MiB"), Some(ByteSize(2 << 20)));
503
        assert_eq!(ByteSize::parse("512"), Some(ByteSize(512)));
504
        assert_eq!(ByteSize::parse("4 GiB"), Some(ByteSize(4 << 30)));
505
        assert_eq!(ByteSize::parse("100kB"), Some(ByteSize(100_000)));
506
        assert_eq!(ByteSize::parse("lots"), None);
507
    }
508
509
    #[test]
510
    fn file_then_environment() {
511
        let mut cfg = Config::parse("[server]\nbind = \"0.0.0.0:7300\"\n[limits]\nrender_limit = \"1MiB\"\n").unwrap();
512
        assert_eq!(cfg.server.bind, "0.0.0.0:7300");
513
        assert_eq!(cfg.limits.render_limit, ByteSize(1 << 20));
514
        assert_eq!(cfg.site.name, "OxygenHub");
515
        cfg.apply_env(|k| match k {
516
            "OXYHUB_SITE_NAME" => Some("Earth".into()),
517
            "OXYHUB_AUTH_PUSH_TOKENS" => Some("a, b".into()),
518
            "OXYHUB_SERVER_METRICS_TOKEN" => Some("m".into()),
519
            "OXYHUB_SERVER_GRAFANA_URL" => Some("https://grafana.example".into()),
520
            _ => None,
521
        });
522
        assert_eq!(cfg.site.name, "Earth");
523
        assert_eq!(cfg.auth.push_tokens, ["a", "b"]);
524
        assert_eq!((cfg.server.metrics_token.as_str(), cfg.server.grafana_url.as_str()), ("m", "https://grafana.example"));
525
        assert!(Config::parse("[nope]\nx = 1\n").is_err(), "unknown sections are errors, not silence");
526
    }
527
}