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