| 4 | use tauri::Manager; |
| 5 | use tauri_plugin_opener::OpenerExt; |
| 6 | |
| 7 | pub fn trust_certificate(app: &tauri::AppHandle) -> Result<(), String> { |
| 8 | // Two things get trusted: the loopback API's self-anchored leaf, and the |
| 9 | // anchor of the local CA that signs `*.stack.localhost`. Both are minted |
| 10 | // per install, so make sure they exist before handing the user a Terminal |
| 11 | // window that can only fail. |
| 12 | let cert_path = tls::cert_dir(app)?.join("cert.pem"); |
| 13 | if !cert_path.exists() { |
| 14 | log::error!("Local certificate not found: {:?}", cert_path); |
| 15 | return Err(format!( |
| 16 | "Local certificate not generated yet: {:?}", |
| 17 | cert_path |
| 18 | )); |
| 19 | } |
| 20 | |
| 21 | // Generates the local CA on the spot if the bootstrap stack never ran, so |
| 22 | // "Trust certificate" is never a no-op. |
| 23 | let proxy = tls::ensure_proxy(app)?; |
| 24 | if !proxy.root_path.exists() { |
| 25 | log::error!("Local trust anchor not found: {:?}", proxy.root_path); |
| 26 | return Err(format!( |
| 27 | "Local trust anchor not generated yet: {:?}", |
| 28 | proxy.root_path |
| 29 | )); |
| 30 | } |
| 31 | |
| 32 | let script_path = app |
| 33 | .path() |
| 34 | .resolve("scripts/trust-cert.sh", BaseDirectory::Resource) |
| 35 | .unwrap(); |
| 36 | |
| 37 | // Check if the script exists |
| 38 | if !Path::new(&script_path).exists() { |
| 39 | log::error!("Script not found: {:?}", script_path); |
| 40 | return Err(format!("Script not found: {:?}", script_path)); |
| 41 | } |
| 42 | |
| 43 | // Trust the certificate |
| 44 | log::info!("Adding certificate to keychain"); |
| 45 | log::debug!("Executing script: {:?}", script_path); |
| 46 | |
| 47 | app.opener() |
| 48 | .open_path( |
| 49 | script_path.to_string_lossy().to_string(), |
| 50 | Some("Terminal.app"), |
| 51 | ) |
| 52 | .map_err(|e| format!("Failed to execute command: {}", e))?; |
| 53 | |
| 54 | // if !output.status.success() { |
| 55 | // log::error!( |
| 56 | // "Failed to add certificate to keychain: {}", |
| 57 | // String::from_utf8_lossy(&output.stderr) |
| 58 | // ); |
| 59 | // return Err(format!( |
| 60 | // "Failed to add certificate to keychain: {}", |
| 61 | // String::from_utf8_lossy(&output.stderr) |
| 62 | // )); |
| 63 | // } |