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