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