|
1 | 1 | use std::time::Duration; |
2 | 2 |
|
3 | | -/// Shared HTTP client for server-side outbound requests (GitHub API, scheduler). |
4 | | -/// Configured with standard timeouts and user-agent. |
5 | | -pub static CLIENT: std::sync::LazyLock<reqwest::Client> = std::sync::LazyLock::new(|| { |
6 | | - reqwest::Client::builder() |
| 3 | +/// Root certificates loaded for server-side outbound HTTPS. |
| 4 | +/// |
| 5 | +/// Sources (in order): |
| 6 | +/// 1. `PRELOOP_GITHUB_CA_FILE` — a PEM bundle that should additionally be |
| 7 | +/// trusted when talking to a GHES / GitHub-emulator forge (gh-simulate |
| 8 | +/// serves a self-signed CA). Forge-scoped, additive to the system roots. |
| 9 | +/// 2. `SSL_CERT_FILE` — the OpenSSL/native convention the runner already |
| 10 | +/// honors in `client::http::with_control`; kept here so the engine's |
| 11 | +/// GitHub clients follow the same environment override on platforms |
| 12 | +/// whose reqwest backend does not read it. |
| 13 | +/// |
| 14 | +/// Both are optional and additive to the bundled webpki/native roots. A |
| 15 | +/// missing or unreadable file fails closed (the client simply has the default |
| 16 | +/// roots) — we log rather than abort startup, since TLS verification itself |
| 17 | +/// stays on. |
| 18 | +fn ca_certificates() -> Vec<reqwest::Certificate> { |
| 19 | + let mut out = Vec::new(); |
| 20 | + for var in ["PRELOOP_GITHUB_CA_FILE", "SSL_CERT_FILE"] { |
| 21 | + let Ok(path) = std::env::var(var) else { |
| 22 | + continue; |
| 23 | + }; |
| 24 | + if path.trim().is_empty() { |
| 25 | + continue; |
| 26 | + } |
| 27 | + out.extend(load_ca_bundle(std::path::Path::new(&path), var)); |
| 28 | + } |
| 29 | + out |
| 30 | +} |
| 31 | + |
| 32 | +/// Read one PEM bundle from `path` and parse its certificates. Missing or |
| 33 | +/// invalid files log and return empty — verification stays on, we just add no |
| 34 | +/// roots. Split out so tests exercise it without mutating process env. |
| 35 | +fn load_ca_bundle(path: &std::path::Path, env: &str) -> Vec<reqwest::Certificate> { |
| 36 | + match std::fs::read(path) { |
| 37 | + Ok(pem) => match reqwest::Certificate::from_pem_bundle(&pem) { |
| 38 | + Ok(certs) => { |
| 39 | + tracing::info!(path = %path.display(), env, count = certs.len(), |
| 40 | + "loaded additional TLS root certificates for outbound GitHub calls"); |
| 41 | + certs |
| 42 | + } |
| 43 | + Err(e) => { |
| 44 | + tracing::warn!(path = %path.display(), env, error = %e, |
| 45 | + "ignoring CA bundle with invalid PEM"); |
| 46 | + Vec::new() |
| 47 | + } |
| 48 | + }, |
| 49 | + Err(e) => { |
| 50 | + tracing::warn!(path = %path.display(), env, error = %e, |
| 51 | + "ignoring unreadable CA bundle"); |
| 52 | + Vec::new() |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/// Build a reqwest client with the shared timeouts and the extra CA roots |
| 58 | +/// from [`ca_certificates`]. Use this for every server-side outbound HTTPS |
| 59 | +/// call that can target a GHES/emulator forge instead of ad-hoc |
| 60 | +/// `reqwest::Client::builder()` so custom roots apply uniformly. |
| 61 | +pub fn github_client_builder() -> reqwest::ClientBuilder { |
| 62 | + let mut b = reqwest::Client::builder() |
7 | 63 | .timeout(Duration::from_secs(30)) |
8 | 64 | .connect_timeout(Duration::from_secs(10)) |
9 | | - .user_agent("preloop-runner-server") |
| 65 | + .user_agent("preloop-runner-server"); |
| 66 | + for cert in ca_certificates() { |
| 67 | + b = b.add_root_certificate(cert); |
| 68 | + } |
| 69 | + b |
| 70 | +} |
| 71 | + |
| 72 | +/// Shared HTTP client for server-side outbound requests (GitHub API, scheduler). |
| 73 | +/// Configured with standard timeouts, user-agent, and the CA roots from |
| 74 | +/// [`ca_certificates`] so `PRELOOP_GITHUB_CA_FILE`/`SSL_CERT_FILE` reach every |
| 75 | +/// caller. |
| 76 | +pub static CLIENT: std::sync::LazyLock<reqwest::Client> = std::sync::LazyLock::new(|| { |
| 77 | + github_client_builder() |
10 | 78 | .build() |
11 | 79 | .expect("HTTP client builder") |
12 | 80 | }); |
| 81 | + |
| 82 | +#[cfg(test)] |
| 83 | +mod tests { |
| 84 | + use super::*; |
| 85 | + |
| 86 | + fn write_self_signed_pem(path: &std::path::Path) { |
| 87 | + let key = std::env::temp_dir().join("ghsim_ca_test_key.pem"); |
| 88 | + let _ = std::process::Command::new("openssl") |
| 89 | + .args([ |
| 90 | + "req", |
| 91 | + "-x509", |
| 92 | + "-newkey", |
| 93 | + "rsa:2048", |
| 94 | + "-keyout", |
| 95 | + key.to_str().unwrap(), |
| 96 | + "-out", |
| 97 | + path.to_str().unwrap(), |
| 98 | + "-days", |
| 99 | + "1", |
| 100 | + "-nodes", |
| 101 | + "-subj", |
| 102 | + "/CN=gh-simulate-test", |
| 103 | + ]) |
| 104 | + .output(); |
| 105 | + } |
| 106 | + |
| 107 | + #[test] |
| 108 | + fn parses_pem_bundle() { |
| 109 | + let p = std::env::temp_dir().join("ghsim_ca_file.pem"); |
| 110 | + write_self_signed_pem(&p); |
| 111 | + let certs = load_ca_bundle(&p, "TEST"); |
| 112 | + assert!(!certs.is_empty()); |
| 113 | + } |
| 114 | + |
| 115 | + #[test] |
| 116 | + fn missing_ca_file_returns_empty_not_panic() { |
| 117 | + let certs = load_ca_bundle(std::path::Path::new("/nonexistent/ca.pem"), "TEST"); |
| 118 | + assert!(certs.is_empty()); |
| 119 | + } |
| 120 | + |
| 121 | + #[test] |
| 122 | + fn invalid_pem_returns_empty_not_panic() { |
| 123 | + let p = std::env::temp_dir().join("ghsim_bad.pem"); |
| 124 | + std::fs::write(&p, b"not a pem").unwrap(); |
| 125 | + assert!(load_ca_bundle(&p, "TEST").is_empty()); |
| 126 | + } |
| 127 | +} |
0 commit comments