OXYGENHUB
# 16ee5d19d220 oxygenhub / crates / hub / src / config.rs
15.8 KiB · Rust · last changed Hubs witness each other's transparency logs (step A5) 18 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
}
232
233
#[derive(Clone, Debug, Deserialize)]
234
#[serde(default, deny_unknown_fields)]
235
pub struct Tls {
236
    /// Generate a certificate at `init` (developer edition).
237
    pub self_signed: bool,
238
    /// PEM files; relative paths are under the data directory.
239
    pub cert: String,
240
    pub key: String,
241
}
242
243
#[derive(Clone, Debug, Default, Deserialize)]
244
#[serde(default, deny_unknown_fields)]
245
pub struct Auth {
246
    /// Bearer tokens allowed to push until accounts exist (hub 0.2). Empty
247
    /// means every push is accepted, as `oxy serve` does.
248
    pub push_tokens: Vec<String>,
249
    /// For tests only: a base URL that stands in for every sign-in
250
    /// provider (`{base}/{provider}/authorize|token|userinfo`).
251
    pub signin_mock: Option<String>,
252
}
253
254
#[derive(Clone, Debug, Deserialize)]
255
#[serde(default, deny_unknown_fields)]
256
pub struct Limits {
257
    /// Largest file rendered in the browser, e.g. `2MiB`.
258
    pub render_limit: ByteSize,
259
    pub asset_limit: ByteSize,
260
    pub push_body_limit: ByteSize,
261
}
262
263
#[derive(Clone, Debug, Deserialize)]
264
#[serde(default, deny_unknown_fields)]
265
pub struct Retention {
266
    pub trash_days: u32,
267
    pub webhook_log_days: u32,
268
    /// The UTC hour at which every repository is garbage collected
269
    /// (spec §17.2); 24 or more turns the nightly gc off.
270
    pub gc_hour: u32,
271
}
272
273
/// A byte count written as `512`, `2MiB`, `4GiB`, `100kB`.
274
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
275
pub struct ByteSize(pub u64);
276
277
impl ByteSize {
278
    pub fn parse(s: &str) -> Option<ByteSize> {
279
        let s = s.trim();
280
        let digits: String = s.chars().take_while(|c| c.is_ascii_digit()).collect();
281
        let unit = s[digits.len()..].trim();
282
        let n: u64 = digits.parse().ok()?;
283
        let mult = match unit {
284
            "" | "B" => 1,
285
            "k" | "kB" | "KB" => 1000,
286
            "KiB" | "K" => 1 << 10,
287
            "M" | "MB" => 1_000_000,
288
            "MiB" => 1 << 20,
289
            "G" | "GB" => 1_000_000_000,
290
            "GiB" => 1 << 30,
291
            _ => return None,
292
        };
293
        Some(ByteSize(n.checked_mul(mult)?))
294
    }
295
}
296
297
impl<'de> Deserialize<'de> for ByteSize {
298
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<ByteSize, D::Error> {
299
        #[derive(Deserialize)]
300
        #[serde(untagged)]
301
        enum Raw {
302
            Number(u64),
303
            Text(String),
304
        }
305
        match Raw::deserialize(d)? {
306
            Raw::Number(n) => Ok(ByteSize(n)),
307
            Raw::Text(t) => ByteSize::parse(&t).ok_or_else(|| serde::de::Error::custom(format!("not a byte size: {t:?}"))),
308
        }
309
    }
310
}
311
312
impl Default for Site {
313
    fn default() -> Self {
314
        Site { name: "OxygenHub".into(), description: "Repositories, proposals and issues for Oxygen".into() }
315
    }
316
}
317
318
impl Default for Server {
319
    fn default() -> Self {
320
        Server { bind: "127.0.0.1:7300".into(), tls_bind: String::new(), base_url: "http://127.0.0.1:7300".into(), behind_proxy: false }
321
    }
322
}
323
324
impl Default for Tls {
325
    fn default() -> Self {
326
        Tls { self_signed: false, cert: "keys/tls.crt".into(), key: "keys/tls.key".into() }
327
    }
328
}
329
330
impl Default for Limits {
331
    fn default() -> Self {
332
        Limits { render_limit: ByteSize(2 << 20), asset_limit: ByteSize(512 << 20), push_body_limit: ByteSize(4 << 30) }
333
    }
334
}
335
336
impl Default for Retention {
337
    fn default() -> Self {
338
        Retention { trash_days: 30, webhook_log_days: 30, gc_hour: 3 }
339
    }
340
}
341
342
impl Default for Config {
343
    fn default() -> Self {
344
        Config {
345
            data: PathBuf::from("data"),
346
            site: Site::default(),
347
            server: Server::default(),
348
            tls: Tls::default(),
349
            auth: Auth::default(),
350
            registration: Registration::default(),
351
            email: Email::default(),
352
            limits: Limits::default(),
353
            retention: Retention::default(),
354
            flow: Flow::default(),
355
            apt: Apt::default(),
356
            diffusion: Diffusion::default(),
357
            backups: Backups::default(),
358
            outbound: Outbound::default(),
359
            database: Database::default(),
360
            tlog: Tlog::default(),
361
        }
362
    }
363
}
364
365
impl Config {
366
    /// Read `<data>/hub.toml` if present, then apply environment overrides.
367
    pub fn load(data: &Path) -> anyhow::Result<Config> {
368
        let path = data.join("hub.toml");
369
        let mut cfg = match std::fs::read_to_string(&path) {
370
            Ok(text) => Config::parse(&text)?,
371
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Config::default(),
372
            Err(e) => return Err(anyhow::anyhow!("reading {}: {e}", path.display())),
373
        };
374
        cfg.data = data.to_path_buf();
375
        cfg.apply_env(|k| std::env::var(k).ok());
376
        Ok(cfg)
377
    }
378
379
    pub fn parse(text: &str) -> anyhow::Result<Config> {
380
        Ok(toml::from_str(text)?)
381
    }
382
383
    /// `OXYHUB_SERVER_BIND`, `OXYHUB_SITE_NAME`, … override the file.
384
    pub fn apply_env(&mut self, get: impl Fn(&str) -> Option<String>) {
385
        if let Some(v) = get("OXYHUB_DATABASE_URL") {
386
            self.database.url = v;
387
        }
388
        if let Some(v) = get("OXYHUB_DATABASE_SCHEMA") {
389
            self.database.schema = v;
390
        }
391
        if let Some(v) = get("OXYHUB_SITE_NAME") {
392
            self.site.name = v;
393
        }
394
        if let Some(v) = get("OXYHUB_SITE_DESCRIPTION") {
395
            self.site.description = v;
396
        }
397
        if let Some(v) = get("OXYHUB_SERVER_BIND") {
398
            self.server.bind = v;
399
        }
400
        if let Some(v) = get("OXYHUB_SERVER_TLS_BIND") {
401
            self.server.tls_bind = v;
402
        }
403
        if let Some(v) = get("OXYHUB_SERVER_BASE_URL") {
404
            self.server.base_url = v;
405
        }
406
        if let Some(v) = get("OXYHUB_SERVER_BEHIND_PROXY") {
407
            self.server.behind_proxy = matches!(v.as_str(), "1" | "true" | "yes");
408
        }
409
        if let Some(v) = get("OXYHUB_TLS_SELF_SIGNED") {
410
            self.tls.self_signed = matches!(v.as_str(), "1" | "true" | "yes");
411
        }
412
        if let Some(v) = get("OXYHUB_REGISTRATION_MODE") {
413
            self.registration.mode = v;
414
        }
415
        if let Some(v) = get("OXYHUB_REGISTRATION_SETUP_CODE") {
416
            self.registration.setup_code = v;
417
        }
418
        if let Some(v) = get("OXYHUB_AUTH_PUSH_TOKENS") {
419
            self.auth.push_tokens = v.split(',').map(str::trim).filter(|s| !s.is_empty()).map(String::from).collect();
420
        }
421
        if let Some(v) = get("OXYHUB_LIMITS_RENDER_LIMIT").and_then(|v| ByteSize::parse(&v)) {
422
            self.limits.render_limit = v;
423
        }
424
    }
425
426
    pub fn bind_addr(&self) -> anyhow::Result<Option<SocketAddr>> {
427
        parse_addr(&self.server.bind)
428
    }
429
430
    pub fn tls_bind_addr(&self) -> anyhow::Result<Option<SocketAddr>> {
431
        parse_addr(&self.server.tls_bind)
432
    }
433
434
    pub fn tls_cert(&self) -> PathBuf {
435
        self.data.join(&self.tls.cert)
436
    }
437
438
    pub fn tls_key(&self) -> PathBuf {
439
        self.data.join(&self.tls.key)
440
    }
441
442
    pub fn repos_dir(&self) -> PathBuf {
443
        self.data.join("repos")
444
    }
445
446
    pub fn keys_dir(&self) -> PathBuf {
447
        self.data.join("keys")
448
    }
449
450
    pub fn db_path(&self) -> PathBuf {
451
        self.data.join("hub.db")
452
    }
453
454
    /// The base URL without a trailing slash.
455
    pub fn base_url(&self) -> &str {
456
        self.server.base_url.trim_end_matches('/')
457
    }
458
}
459
460
fn parse_addr(s: &str) -> anyhow::Result<Option<SocketAddr>> {
461
    if s.trim().is_empty() {
462
        return Ok(None);
463
    }
464
    Ok(Some(s.parse().map_err(|e| anyhow::anyhow!("bind address {s:?}: {e}"))?))
465
}
466
467
#[cfg(test)]
468
mod tests {
469
    use super::*;
470
471
    #[test]
472
    fn byte_sizes() {
473
        assert_eq!(ByteSize::parse("2MiB"), Some(ByteSize(2 << 20)));
474
        assert_eq!(ByteSize::parse("512"), Some(ByteSize(512)));
475
        assert_eq!(ByteSize::parse("4 GiB"), Some(ByteSize(4 << 30)));
476
        assert_eq!(ByteSize::parse("100kB"), Some(ByteSize(100_000)));
477
        assert_eq!(ByteSize::parse("lots"), None);
478
    }
479
480
    #[test]
481
    fn file_then_environment() {
482
        let mut cfg = Config::parse("[server]\nbind = \"0.0.0.0:7300\"\n[limits]\nrender_limit = \"1MiB\"\n").unwrap();
483
        assert_eq!(cfg.server.bind, "0.0.0.0:7300");
484
        assert_eq!(cfg.limits.render_limit, ByteSize(1 << 20));
485
        assert_eq!(cfg.site.name, "OxygenHub");
486
        cfg.apply_env(|k| match k {
487
            "OXYHUB_SITE_NAME" => Some("Earth".into()),
488
            "OXYHUB_AUTH_PUSH_TOKENS" => Some("a, b".into()),
489
            _ => None,
490
        });
491
        assert_eq!(cfg.site.name, "Earth");
492
        assert_eq!(cfg.auth.push_tokens, ["a", "b"]);
493
        assert!(Config::parse("[nope]\nx = 1\n").is_err(), "unknown sections are errors, not silence");
494
    }
495
}