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