runnerctl 0.3.0

Manage independent ephemeral GitHub Actions runner pools on one Docker host
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
use anyhow::{Context, Result, bail, ensure};
use serde::{Deserialize, Serialize};
#[cfg(unix)]
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
use std::{
    collections::{BTreeMap, BTreeSet},
    fs,
    io::Write,
    path::{Path, PathBuf},
};

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct Config {
    pub schema_version: u32,
    #[serde(default)]
    pub auth: BTreeMap<String, Auth>,
    #[serde(default)]
    pub manager: Manager,
    #[serde(default)]
    pub pools: Vec<Pool>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct Auth {
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub credential: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub app: Option<AppAuth>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct AppAuth {
    pub app_id: String,
    pub installation_id: u64,
    pub private_key: String,
}
impl Auth {
    pub fn pat(credential: String) -> Self {
        Self {
            credential,
            app: None,
        }
    }
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(default, deny_unknown_fields)]
pub struct Manager {
    pub poll_interval_seconds: u64,
    pub max_runners: u32,
    pub max_parallel_creates: u32,
    pub log_retention_days: u64,
}
impl Default for Manager {
    fn default() -> Self {
        Self {
            poll_interval_seconds: 30,
            max_runners: 16,
            max_parallel_creates: 1,
            log_retention_days: 7,
        }
    }
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct Pool {
    pub name: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub organization: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub repository: Option<String>,
    pub auth: String,
    pub labels: Vec<String>,
    #[serde(default = "default_group")]
    pub runner_group: String,
    pub replicas: u32,
    #[serde(default = "default_image")]
    pub image: String,
    #[serde(default)]
    pub docker_socket: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cpus: Option<f64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub memory: Option<String>,
}
impl Pool {
    pub fn target(&self) -> &str {
        self.repository.as_deref().unwrap_or(&self.organization)
    }
    pub fn api_scope(&self) -> String {
        match &self.repository {
            Some(repo) => format!("/repos/{repo}"),
            None => format!("/orgs/{}", self.organization),
        }
    }
    pub fn url(&self) -> String {
        format!("https://github.com/{}", self.target())
    }
}
fn default_group() -> String {
    "Default".into()
}
pub fn default_image() -> String {
    format!(
        "ghcr.io/clroot/runnerctl-runner:v{}",
        env!("CARGO_PKG_VERSION")
    )
}
pub fn validate_image(image: &str) -> Result<()> {
    ensure!(
        !image.is_empty() && !image.starts_with('-') && !image.chars().any(char::is_whitespace),
        "invalid image"
    );
    ensure!(
        image.contains('@') || image.rsplit('/').next().unwrap_or_default().contains(':'),
        "pin image with a version tag or digest"
    );
    ensure!(
        !image.ends_with(":latest"),
        "latest image tag is not allowed; pin a version"
    );
    Ok(())
}
pub fn valid_org(s: &str) -> bool {
    !s.is_empty()
        && s.len() <= 39
        && s.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-')
        && s.as_bytes()[0].is_ascii_alphanumeric()
}
pub fn valid_repo(s: &str) -> bool {
    s.split_once('/').is_some_and(|(owner, repo)| {
        valid_org(owner)
            && !repo.is_empty()
            && repo.len() <= 100
            && !matches!(repo, "." | "..")
            && repo
                .bytes()
                .all(|b| b.is_ascii_alphanumeric() || b"-_.".contains(&b))
    })
}
fn validate_reference(reference: &str) -> Result<()> {
    if let Some(p) = reference.strip_prefix("file:") {
        ensure!(
            Path::new(p).is_absolute(),
            "credential file must be absolute"
        );
    } else if let Some(e) = reference.strip_prefix("env:") {
        ensure!(
            !e.is_empty() && e.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_'),
            "invalid credential environment reference"
        );
    } else {
        bail!("credential must use file: or env:");
    }
    Ok(())
}
pub fn memory_bytes(s: &str) -> Result<u64> {
    let split = s.find(|c: char| !c.is_ascii_digit()).unwrap_or(s.len());
    let value: u64 = s[..split]
        .parse()
        .context("memory must be an integer with optional b/k/m/g suffix")?;
    let multiplier = match s[split..].to_ascii_lowercase().as_str() {
        "" | "b" => 1,
        "k" => 1024,
        "m" => 1024 * 1024,
        "g" => 1024 * 1024 * 1024,
        _ => bail!("memory must use b/k/m/g suffix"),
    };
    let bytes = value
        .checked_mul(multiplier)
        .context("memory limit too large")?;
    ensure!(bytes >= 6 * 1024 * 1024, "memory must be at least 6 MiB");
    Ok(bytes)
}
pub fn valid_name(s: &str) -> bool {
    !s.is_empty()
        && s.len() <= 48
        && s.bytes()
            .all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_')
        && s.as_bytes()[0].is_ascii_alphanumeric()
}
impl Default for Config {
    fn default() -> Self {
        Self {
            schema_version: 1,
            auth: BTreeMap::new(),
            manager: Manager::default(),
            pools: vec![],
        }
    }
}
impl Config {
    pub fn parse(text: &str) -> Result<Self> {
        let c: Self = toml::from_str(text).context("invalid config TOML")?;
        c.validate()?;
        Ok(c)
    }
    pub fn validate(&self) -> Result<()> {
        ensure!(self.schema_version == 1, "unsupported schema_version");
        ensure!(
            (5..=3600).contains(&self.manager.poll_interval_seconds),
            "poll interval must be 5..3600 seconds"
        );
        ensure!(
            self.manager.max_runners > 0
                && self.manager.max_parallel_creates > 0
                && self.manager.max_parallel_creates <= self.manager.max_runners,
            "invalid manager capacity"
        );
        let mut names = BTreeSet::new();
        for (name, auth) in &self.auth {
            ensure!(valid_name(name), "invalid auth profile name");
            if let Some(app) = &auth.app {
                ensure!(
                    auth.credential.is_empty(),
                    "choose PAT or GitHub App authentication, not both"
                );
                ensure!(
                    app.installation_id > 0
                        && !app.app_id.is_empty()
                        && app.app_id.len() <= 64
                        && app
                            .app_id
                            .bytes()
                            .all(|b| b.is_ascii_alphanumeric() || b == b'_'),
                    "invalid GitHub App identity"
                );
                validate_reference(&app.private_key)?;
            } else {
                validate_reference(&auth.credential)?;
            }
        }
        for p in &self.pools {
            ensure!(
                valid_name(&p.name) && names.insert(&p.name),
                "invalid or duplicate pool name: {}",
                p.name
            );
            match &p.repository {
                Some(repo) => ensure!(
                    p.organization.is_empty() && valid_repo(repo),
                    "invalid repository or both organization/repository set"
                ),
                None => ensure!(valid_org(&p.organization), "invalid organization"),
            }
            ensure!(
                p.repository.is_none() || p.runner_group == "Default",
                "runner groups are only available for organization pools"
            );
            if let Some(cpus) = p.cpus {
                ensure!(
                    cpus.is_finite() && cpus >= 0.01,
                    "cpus must be finite and at least 0.01"
                );
            }
            if let Some(memory) = &p.memory {
                memory_bytes(memory)?;
            }
            ensure!(
                self.auth.contains_key(&p.auth),
                "unknown auth profile: {}",
                p.auth
            );
            ensure!(
                !p.labels.is_empty() && p.labels.len() < 100,
                "pool needs 1..99 custom labels"
            );
            ensure!(
                p.labels.iter().all(|l| !l.trim().is_empty()
                    && l.len() <= 255
                    && !l.contains([',', '\n', '\r'])
                    && !l.starts_with("pool:")),
                "invalid or reserved label"
            );
            ensure!(
                !p.runner_group.trim().is_empty() && !p.runner_group.contains(['\n', '\r']),
                "invalid runner group"
            );
            validate_image(&p.image)?;
        }
        ensure!(
            self.pools
                .iter()
                .map(|p| u64::from(p.replicas))
                .sum::<u64>()
                <= u64::from(self.manager.max_runners),
            "sum of replicas exceeds max_runners (including stopped pools)"
        );
        Ok(())
    }
    pub fn warnings(&self) -> Vec<String> {
        let mut out = vec![];
        if self.manager.max_parallel_creates > 1 {
            out.push("Provisioning is serialized; max_parallel_creates values above 1 do not enable concurrency".into());
        }
        for (i, a) in self.pools.iter().enumerate() {
            for b in self.pools.iter().skip(i + 1) {
                if a.api_scope() == b.api_scope() && a.labels.iter().any(|l| b.labels.contains(l)) {
                    out.push(format!(
                        "{} and {} share routing labels; use pool:<name> to target a pool",
                        a.name, b.name
                    ));
                }
            }
        }
        out
    }
}
#[derive(Clone)]
pub struct Paths {
    pub home: PathBuf,
}
impl Paths {
    pub fn new(home: PathBuf) -> Result<Self> {
        let home = if home.is_absolute() {
            home
        } else {
            std::env::current_dir()?.join(home)
        };
        Ok(Self { home })
    }
    pub fn prepare(&self) -> Result<()> {
        secure_dir(&self.home)?;
        secure_dir(&self.home.join("credentials"))?;
        secure_dir(&self.home.join("runs"))?;
        secure_dir(&self.home.join("logs"))
    }
    pub fn config(&self) -> PathBuf {
        self.home.join("config.toml")
    }
    /// The manager's Unix socket, or on Windows a file naming its named pipe.
    pub fn socket(&self) -> PathBuf {
        if cfg!(windows) {
            self.home.join("manager.pipe")
        } else {
            self.home.join("manager.sock")
        }
    }
}
/// The default --home: XDG_CONFIG_HOME or ~/.config, or %LOCALAPPDATA% on Windows.
pub fn default_home() -> PathBuf {
    let base = if cfg!(windows) {
        std::env::var_os("LOCALAPPDATA").map(PathBuf::from)
    } else {
        std::env::var_os("XDG_CONFIG_HOME").map(PathBuf::from)
    };
    base.unwrap_or_else(|| {
        PathBuf::from(std::env::var_os("HOME").unwrap_or_default()).join(".config")
    })
    .join("runnerctl")
}
pub fn secure_dir(path: &Path) -> Result<()> {
    fs::create_dir_all(path)?;
    ensure!(
        !fs::symlink_metadata(path)?.file_type().is_symlink(),
        "directory must not be a symlink"
    );
    #[cfg(unix)]
    fs::set_permissions(path, fs::Permissions::from_mode(0o700))?;
    // Resetting a DACL rewrites every inherited entry below it, so do it only once.
    #[cfg(windows)]
    if !crate::windows::private_path(path)? {
        crate::windows::restrict(path, true)?;
    }
    Ok(())
}
pub fn atomic_write(path: &Path, data: &[u8]) -> Result<()> {
    let parent = path.parent().context("missing parent")?;
    let mut f = tempfile::NamedTempFile::new_in(parent)?;
    #[cfg(unix)]
    f.as_file()
        .set_permissions(fs::Permissions::from_mode(0o600))?;
    #[cfg(windows)]
    crate::windows::restrict(f.path(), false)?;
    f.write_all(data)?;
    f.as_file().sync_all()?;
    f.persist(path).map_err(|e| e.error)?;
    // Windows cannot open a directory as a file; NTFS journals the rename itself.
    #[cfg(unix)]
    fs::File::open(parent)?.sync_all()?;
    Ok(())
}
#[cfg(unix)]
fn open_private(path: &str) -> Result<fs::File> {
    let f = fs::OpenOptions::new()
        .read(true)
        .custom_flags(libc::O_NOFOLLOW)
        .open(path)
        .context("cannot open credential file")?;
    let meta = f.metadata()?;
    ensure!(
        meta.is_file() && meta.permissions().mode() & 0o077 == 0,
        "credential must be a regular file with mode 0600"
    );
    Ok(f)
}
#[cfg(windows)]
fn open_private(path: &str) -> Result<fs::File> {
    use std::os::windows::fs::OpenOptionsExt;
    use windows_sys::Win32::Storage::FileSystem::FILE_FLAG_OPEN_REPARSE_POINT;
    // Opening the reparse point itself makes a symlink fail the regular-file check.
    let f = fs::OpenOptions::new()
        .read(true)
        .custom_flags(FILE_FLAG_OPEN_REPARSE_POINT)
        .open(path)
        .context("cannot open credential file")?;
    ensure!(
        f.metadata()?.is_file() && crate::windows::private_file(&f)?,
        "credential must be a regular file only you can access; run icacls \"{path}\" /inheritance:r /grant:r \"%USERNAME%:F\""
    );
    Ok(f)
}
pub fn credential(auth: &Auth) -> Result<String> {
    let value = secret(&auth.credential, 16384)?;
    ensure!(
        !value.chars().any(char::is_whitespace),
        "invalid token contents"
    );
    Ok(value)
}
pub fn secret(reference: &str, max_bytes: u64) -> Result<String> {
    let value = if let Some(path) = reference.strip_prefix("file:") {
        let mut f = open_private(path)?;
        use std::io::Read;
        let mut value = String::new();
        (&mut f).take(max_bytes + 1).read_to_string(&mut value)?;
        value
    } else if let Some(name) = reference.strip_prefix("env:") {
        std::env::var(name).context("credential environment variable is missing")?
    } else {
        bail!("invalid credential reference");
    };
    let value = value.trim().to_owned();
    ensure!(
        !value.is_empty() && value.len() as u64 <= max_bytes,
        "invalid credential contents"
    );
    Ok(value)
}

#[cfg(test)]
mod tests {
    use super::*;
    pub fn fixture() -> Config {
        let mut c = Config::default();
        c.auth.insert(
            "team".into(),
            Auth {
                credential: "env:RUNNERCTL_TEST_TOKEN".into(),
                app: None,
            },
        );
        c.pools.push(Pool {
            name: "validate".into(),
            organization: "test-org".into(),
            repository: None,
            auth: "team".into(),
            labels: vec!["validate".into()],
            runner_group: "Default".into(),
            replicas: 4,
            image: default_image(),
            docker_socket: false,
            cpus: None,
            memory: None,
        });
        let mut b = c.pools[0].clone();
        b.name = "build".into();
        b.labels = vec!["build".into()];
        b.replicas = 2;
        c.pools.push(b);
        c
    }
    #[test]
    fn config_roundtrip_and_capacity() {
        let mut c = fixture();
        assert_eq!(Config::parse(&toml::to_string(&c).unwrap()).unwrap(), c);
        c.manager.max_runners = 5;
        assert!(c.validate().is_err());
    }
    #[test]
    fn reject_ambiguous_and_unsafe_config() {
        let mut c = fixture();
        c.pools[1].name = "validate".into();
        assert!(c.validate().is_err());
        let mut c = fixture();
        c.pools[0].auth = "missing".into();
        assert!(c.validate().is_err());
        let mut c = fixture();
        c.pools[0].labels = vec!["pool:other".into()];
        assert!(c.validate().is_err());
        let mut c = fixture();
        c.pools[0].organization = "../bad".into();
        assert!(c.validate().is_err());
        let mut c = fixture();
        c.pools[0].image = "image:latest".into();
        assert!(c.validate().is_err());
        assert!(Config::parse("schema_version=1\nunknown=true").is_err());
    }
    #[test]
    fn token_permissions_and_symlinks() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("token");
        atomic_write(&file, b"example-secret").unwrap();
        let auth = Auth {
            credential: format!("file:{}", file.display()),
            app: None,
        };
        assert_eq!(credential(&auth).unwrap(), "example-secret");
        #[cfg(unix)]
        fs::set_permissions(&file, fs::Permissions::from_mode(0o644)).unwrap();
        #[cfg(windows)]
        crate::windows::share(&file);
        assert!(credential(&auth).is_err());
        // Creating symlinks on Windows requires Developer Mode.
        #[cfg(unix)]
        {
            let link = dir.path().join("link");
            std::os::unix::fs::symlink(&file, &link).unwrap();
            assert!(
                credential(&Auth {
                    credential: format!("file:{}", link.display()),
                    app: None,
                })
                .is_err()
            );
        }
    }
    #[cfg(windows)]
    #[test]
    fn secure_dir_replaces_inherited_access() {
        let dir = tempfile::tempdir().unwrap();
        let home = dir.path().join("home");
        fs::create_dir(&home).unwrap();
        crate::windows::share(&home);
        assert!(!crate::windows::private_path(&home).unwrap());
        secure_dir(&home).unwrap();
        assert!(crate::windows::private_path(&home).unwrap());
        fs::write(home.join("inherited"), b"x").unwrap();
        assert!(crate::windows::private_path(&home.join("inherited")).unwrap());
    }
    #[test]
    fn shared_labels_warn_but_remain_valid() {
        let mut c = fixture();
        c.pools[1].labels = c.pools[0].labels.clone();
        c.validate().unwrap();
        assert_eq!(c.warnings().len(), 1);
    }
    #[test]
    fn repository_scope_and_resource_limits_are_validated() {
        let mut c = fixture();
        let p = &mut c.pools[0];
        p.organization.clear();
        p.repository = Some("octocat/example.repo".into());
        p.cpus = Some(0.5);
        p.memory = Some("512m".into());
        assert_eq!(p.api_scope(), "/repos/octocat/example.repo");
        assert_eq!(p.url(), "https://github.com/octocat/example.repo");
        c.validate().unwrap();
        c.pools[0].organization = "conflicting-org".into();
        assert!(c.validate().is_err());
        c.pools[0].organization.clear();
        c.pools[0].runner_group = "custom".into();
        assert!(c.validate().is_err());
        c.pools[0].runner_group = "Default".into();
        for value in [0.0, -1.0, f64::NAN, f64::INFINITY] {
            c.pools[0].cpus = Some(value);
            assert!(c.validate().is_err());
        }
        for value in [
            "0",
            "5m",
            "--privileged",
            "2g extra",
            "18446744073709551615g",
        ] {
            assert!(memory_bytes(value).is_err());
        }
        for value in ["/repo", "owner/../repo", "owner/repo?query", "owner/.."] {
            assert!(!valid_repo(value));
        }
    }
    #[test]
    fn legacy_auth_and_pool_settings_deserialize_without_new_fields() {
        let c = Config::parse(
            r#"schema_version=1
[auth.team]
credential="env:TEST_TOKEN"
[[pools]]
name="test"
organization="example"
auth="team"
labels=["test"]
replicas=1
image="local/existing:1"
"#,
        )
        .unwrap();
        assert!(c.auth["team"].app.is_none());
        assert!(c.pools[0].repository.is_none());
        assert!(c.pools[0].cpus.is_none());
        assert!(c.pools[0].memory.is_none());
    }
}