| 1 | //! Trusting the certificate a sandbox's HTTPS is re-signed with. |
| 2 | //! |
| 3 | //! When a workflow job may be given brokered secrets, its HTTP and HTTPS |
| 4 | //! pass the runner Worker, which fills them in and re-signs HTTPS with a |
| 5 | //! certificate Cloudflare puts in the sandbox. Other sandboxes reach the |
| 6 | //! internet directly. The credentials a sandbox holds and branch |
| 7 | //! protection on g1t's side are what bound a sandbox's work. |
| 8 | |
| 9 | use std::path::Path; |
| 10 | use std::process::{Command, Stdio}; |
| 11 | |
| 12 | /// The certificate the sandbox's HTTPS is re-signed with, when it passes |
| 13 | /// the Worker. |
| 14 | const EGRESS_CA: &str = "/etc/cloudflare/certs/cloudflare-containers-ca.crt"; |
| 15 | |
| 16 | /// Runs a command as root without asking. False where that is not allowed. |
| 17 | fn sudo(args: &[&str]) -> bool { |
| 18 | Command::new("sudo") |
| 19 | .arg("-n") |
| 20 | .args(args) |
| 21 | .stdin(Stdio::null()) |
| 22 | .stdout(Stdio::null()) |
| 23 | .stderr(Stdio::null()) |
| 24 | .status() |
| 25 | .is_ok_and(|status| status.success()) |
| 26 | } |
| 27 | |
| 28 | /// Trusts the certificate a sandbox's HTTPS is re-signed with, so that |
| 29 | /// git, package managers and this program can reach the hosts it |
| 30 | /// intercepts. Does nothing where Cloudflare put no certificate. Called |
| 31 | /// first thing, before any request is made. |
| 32 | pub fn trust_egress_ca() { |
| 33 | const TRUSTED: &str = "/usr/local/share/ca-certificates/cloudflare-containers-ca.crt"; |
| 34 | // Once per sandbox. |
| 35 | if !Path::new(EGRESS_CA).exists() || Path::new(TRUSTED).exists() { |
| 36 | return; |
| 37 | } |
| 38 | let trusted = sudo(&["cp", EGRESS_CA, TRUSTED]) && sudo(&["update-ca-certificates"]); |
| 39 | if !trusted { |
| 40 | eprintln!("g1t-runner: could not trust the sandbox's egress certificate; HTTPS will fail"); |
| 41 | } |
| 42 | } |