From df26a9c911c481f1c46cc2ac79f9659c435a35ef Mon Sep 17 00:00:00 2001 From: beck-8 <34204218+beck-8@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:10:05 +0800 Subject: [PATCH 1/2] fix: support Curio v1.29.3 Web RPC authentication --- Cargo.toml | 2 +- README.md | 10 + src/commands/start/curio/constants.rs | 1 + src/commands/start/curio/daemon.rs | 6 +- src/commands/start/curio/mod.rs | 1 + src/commands/start/curio/pdp.rs | 73 +---- src/commands/start/curio/web_rpc.rs | 369 ++++++++++++++++++++++++++ 7 files changed, 392 insertions(+), 70 deletions(-) create mode 100644 src/commands/start/curio/web_rpc.rs diff --git a/Cargo.toml b/Cargo.toml index f3b43bdd..18d0fc6e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,7 @@ bls-signatures = "0.15" shellexpand = "3" flate2 = "1.0" tar = "0.4" -reqwest = { version = "0.12", features = ["blocking"] } +reqwest = { version = "0.12", features = ["blocking", "json"] } tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] } num_cpus = "1.16" \ No newline at end of file diff --git a/README.md b/README.md index e05745a6..585c5c0b 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,16 @@ This will: - Start storage provider(s) - Launch [Portainer UI](https://docs.docksal.io/use-cases/portainer/) for container management +Curio v1.29.3 and newer require HTTPS and an administrator session for the +management GUI. Startup automatically creates an administrator independently for +each SP, or logs in when one already exists. Credentials are stored with owner-only +permissions in `docker/volumes/run-specific//curio//gui-admin.json` +under the devnet base directory. Keep this file when reusing its database. If an administrator already exists but this file is missing, +startup fails instead of resetting the administrator. Local GUI requests accept +Curio's self-signed certificate. The GUI port is published on host loopback only. +Older Curio releases retain their HTTP management +flow. PDP upload and download endpoints are unchanged. + Curio providers initialize their storage metadata and register container-visible paths before launching, so PDP hashspaces include long-term storage on first startup. Startup retries RPC readiness checks while the daemon initializes. diff --git a/src/commands/start/curio/constants.rs b/src/commands/start/curio/constants.rs index b735012c..1fb90df8 100644 --- a/src/commands/start/curio/constants.rs +++ b/src/commands/start/curio/constants.rs @@ -18,6 +18,7 @@ Enable = true ListenAddress = "0.0.0.0:4702" [Subsystems] +GuiAddress = "0.0.0.0:4701" EnableCommP = true EnableMoveStorage = true EnablePDP = true diff --git a/src/commands/start/curio/daemon.rs b/src/commands/start/curio/daemon.rs index 059d40ad..60a44655 100644 --- a/src/commands/start/curio/daemon.rs +++ b/src/commands/start/curio/daemon.rs @@ -189,7 +189,11 @@ fn build_docker_create_args( "-p".to_string(), format!("{}:12301", api_port_alt), "-p".to_string(), - format!("{}:4701", gui_port), + format!( + "127.0.0.1:{}:{}", + gui_port, + super::constants::CURIO_WEB_RPC_PORT + ), "-p".to_string(), format!("{}:4702", pdp_port), ]); diff --git a/src/commands/start/curio/mod.rs b/src/commands/start/curio/mod.rs index c28fafdd..b9366e29 100644 --- a/src/commands/start/curio/mod.rs +++ b/src/commands/start/curio/mod.rs @@ -13,6 +13,7 @@ pub mod pre_execute; mod rpc; pub mod storage; pub mod verification; +mod web_rpc; use super::step::{SetupContext, Step}; use std::error::Error; diff --git a/src/commands/start/curio/pdp.rs b/src/commands/start/curio/pdp.rs index a39f08cc..6e1402b6 100644 --- a/src/commands/start/curio/pdp.rs +++ b/src/commands/start/curio/pdp.rs @@ -3,9 +3,9 @@ //! Handles importing PDP private keys via Curio Web RPC API. use super::super::step::SetupContext; -use super::constants::{CURIO_WEB_RPC_PORT, PDP_KEY_IMPORT_WAIT_SECS}; +use super::constants::PDP_KEY_IMPORT_WAIT_SECS; +use super::web_rpc::WebRpc; use crate::commands::init::keys::KeyInfo; -use crate::docker::command_logger::run_and_log_command; use std::error::Error; use std::fs; use std::thread; @@ -19,15 +19,15 @@ use tracing::info; pub fn import_pdp_key(context: &SetupContext, sp_index: usize) -> Result<(), Box> { info!("Importing PDP private key for PDP SP {}...", sp_index); - let run_id = context.run_id(); - let container_name = format!("foc-{}-curio-{}", run_id, sp_index); + let rpc = WebRpc::connect(context, sp_index)?; // Get PDP_SP_{sp_index} private key and expected eth address let pdp_sp_name = format!("PDP_SP_{}", sp_index); let (private_key, expected_eth_address) = get_pdp_sp_credentials(&pdp_sp_name)?; // Import key via JSON-RPC - let returned_address = import_key_via_rpc(context, &container_name, &private_key)?; + let returned_address = rpc.import_pdp_key(&private_key)?; + thread::sleep(Duration::from_secs(PDP_KEY_IMPORT_WAIT_SECS)); // Verify the returned address matches expected if returned_address.to_lowercase() != expected_eth_address.to_lowercase() { @@ -69,66 +69,3 @@ fn get_pdp_sp_credentials(pdp_sp_name: &str) -> Result<(String, String), Box Result> { - // Construct JSON-RPC payload - let payload = format!( - r#"{{"jsonrpc":"2.0","method":"CurioWeb.ImportPDPKey","params":["{}"],"id":1}}"#, - private_key - ); - - // Call via curl inside container - let key = format!("curio_pdp_import_key_{}", container_name); - let output = run_and_log_command( - "docker", - &[ - "exec", - container_name, - "curl", - "-s", - "-X", - "POST", - "-H", - "Content-Type: application/json", - "-d", - &payload, - &format!("http://localhost:{}/api/webrpc/v0", CURIO_WEB_RPC_PORT), - ], - context, - &key, - )?; - - if !output.status.success() { - return Err(format!( - "Failed to call ImportPDPKey: {}", - String::from_utf8_lossy(&output.stderr) - ) - .into()); - } - - // Parse JSON response to extract address - let response = String::from_utf8_lossy(&output.stdout); - let json: serde_json::Value = serde_json::from_str(&response)?; - - // Check for JSON-RPC error - if let Some(error) = json.get("error") { - return Err(format!("ImportPDPKey RPC error: {}", error).into()); - } - - // Extract result (the address) - let address = json - .get("result") - .and_then(|r| r.as_str()) - .ok_or("Missing or invalid 'result' in response")?; - - thread::sleep(Duration::from_secs(PDP_KEY_IMPORT_WAIT_SECS)); - - Ok(address.to_string()) -} diff --git a/src/commands/start/curio/web_rpc.rs b/src/commands/start/curio/web_rpc.rs new file mode 100644 index 00000000..e1189ee5 --- /dev/null +++ b/src/commands/start/curio/web_rpc.rs @@ -0,0 +1,369 @@ +//! Local Curio GUI transport and per-SP administrator sessions. + +use crate::commands::start::step::SetupContext; +use crate::paths::foc_devnet_curio_sp_volume; +use crate::utils::retry::{retry_with_fixed_delay, DEFAULT_MAX_RETRIES, DEFAULT_RETRY_DELAY_SECS}; +use reqwest::blocking::{Client, Response}; +use reqwest::header::{COOKIE, SET_COOKIE}; +use serde::{Deserialize, Serialize}; +use serde_json::{json, Value}; +use std::error::Error; +use std::fs; +use std::io::Write; +use std::path::Path; +use std::time::Duration; + +type Result = std::result::Result>; + +pub(super) struct WebRpc { + client: Client, + base: String, + session: Option, +} + +#[derive(Deserialize)] +struct AuthStatus { + configured: bool, + #[serde(rename = "authenticated")] + _authenticated: bool, +} + +#[derive(Serialize, Deserialize)] +struct Credentials { + username: String, + password: String, +} + +impl WebRpc { + pub(super) fn connect(context: &SetupContext, sp_index: usize) -> Result { + let port: u16 = context + .get(&format!("pdp_sp_{}_gui_port", sp_index)) + .ok_or("Curio GUI port missing")? + .parse()?; + let path = foc_devnet_curio_sp_volume(context.run_id(), sp_index).join("gui-admin.json"); + Self::connect_local(port, &path, sp_index) + } + + fn connect_local(port: u16, path: &Path, sp_index: usize) -> Result { + // Only this loopback GUI client accepts Curio's self-signed certificate. + let client = Client::builder() + .danger_accept_invalid_certs(true) + .no_proxy() + .redirect(reqwest::redirect::Policy::none()) + .connect_timeout(Duration::from_secs(2)) + .timeout(Duration::from_secs(30)) + .build()?; + let mut rpc = Self { + client, + base: format!("https://127.0.0.1:{}", port), + session: None, + }; + let status = retry_with_fixed_delay( + || rpc.probe(port), + DEFAULT_MAX_RETRIES, + DEFAULT_RETRY_DELAY_SECS, + &format!("Curio SP {} GUI readiness", sp_index), + )?; + if let Some(status) = status { + let credentials = credentials(path, status.configured)?; + let action = if status.configured { "login" } else { "setup" }; + let response = rpc + .client + .post(format!("{}/api/auth/{}", rpc.base, action)) + .json(&json!({ + "username": credentials.username, + "password": credentials.password, + "confirm": credentials.password, + })) + .send()?; + if !response.status().is_success() { + return Err(format!( + "Curio SP {} {} failed: HTTP {}", + sp_index, + action, + response.status() + ) + .into()); + } + rpc.session = Some(session_cookie(&response)?); + } + Ok(rpc) + } + + fn probe(&mut self, port: u16) -> Result> { + let https = self + .client + .get(format!("https://127.0.0.1:{}/api/auth/status", port)) + .send(); + match https { + Ok(response) => { + let status = response.error_for_status()?.json::()?; + self.base = format!("https://127.0.0.1:{}", port); + Ok(Some(status)) + } + Err(https_error) if https_error.is_connect() => { + // Older Curio cannot establish TLS. Confirm it answers a read-only + // Web RPC before sending a key over HTTP. + let base = format!("http://127.0.0.1:{}", port); + self.check_http_version(&base).map_err(|http_error| { + format!("Curio GUI HTTPS unavailable ({https_error}); legacy HTTP probe failed ({http_error})") + })?; + self.base = base; + Ok(None) + } + Err(error) => Err(error.into()), + } + } + + fn check_http_version(&self, base: &str) -> Result<()> { + let value: Value = self + .client + .post(format!("{}/api/webrpc/v0", base)) + .json(&json!({"jsonrpc":"2.0", "method":"CurioWeb.Version", "params":[], "id":1})) + .send()? + .error_for_status()? + .json()?; + if value["jsonrpc"] == "2.0" + && value["id"] == 1 + && value["error"].is_null() + && value["result"].as_str().is_some_and(|v| !v.is_empty()) + { + Ok(()) + } else { + Err("Curio legacy GUI did not return a JSON-RPC version result".into()) + } + } + + pub(super) fn import_pdp_key(&self, private_key: &str) -> Result { + let mut request = self.client.post(format!("{}/api/webrpc/v0", self.base)) + .json(&json!({"jsonrpc":"2.0", "method":"CurioWeb.ImportPDPKey", "params":[private_key], "id":1})); + if let Some(session) = &self.session { + request = request.header(COOKIE, session); + } + let response = request.send()?; + if !response.status().is_success() { + return Err(format!("Curio ImportPDPKey failed: HTTP {}", response.status()).into()); + } + let value: Value = response + .json() + .map_err(|_| "Curio ImportPDPKey returned invalid JSON")?; + if value.get("error").is_some_and(|e| !e.is_null()) { + // Do not echo server messages which might contain the private key. + return Err(match value["error"]["code"].as_i64() { + Some(code) => format!("Curio ImportPDPKey RPC failed (code {code})"), + None => "Curio ImportPDPKey RPC failed".to_owned(), + } + .into()); + } + value["result"] + .as_str() + .filter(|address| address.parse::().is_ok()) + .map(str::to_owned) + .ok_or_else(|| "Curio ImportPDPKey response missing address".into()) + } +} + +fn session_cookie(response: &Response) -> Result { + response + .headers() + .get_all(SET_COOKIE) + .iter() + .filter_map(|h| h.to_str().ok()) + .filter_map(|h| h.split(';').next()) + .find(|h| h.starts_with("curio_session=") && h.len() > "curio_session=".len()) + .map(str::to_owned) + .ok_or_else(|| "Curio authentication response missing session cookie".into()) +} + +fn credentials(path: &Path, configured: bool) -> Result { + match fs::read(path) { + Ok(bytes) => return Ok(serde_json::from_slice(&bytes)?), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => return Err(e.into()), + } + if configured { + return Err(format!( + "Curio administrator already configured but credentials are missing: {}", + path.display() + ) + .into()); + } + let value = Credentials { + username: "devnet-admin".into(), + password: hex::encode(rand::random::<[u8; 32]>()), + }; + // Publish complete credentials atomically before setup. A failed write must + // not leave a truncated file that breaks the next startup. + let parent = path + .parent() + .ok_or("Curio credentials path has no parent")?; + let mut file = tempfile::NamedTempFile::new_in(parent)?; + file.write_all(&serde_json::to_vec(&value)?)?; + file.as_file().sync_all()?; + file.persist_noclobber(path)?; + Ok(value) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Read; + use std::net::TcpListener; + use std::os::unix::fs::PermissionsExt; + use std::thread; + + /// Run against a started devnet with FOC_DEVNET_BASEDIR and + /// FOC_CURIO_TEST_RUN_DIR pointing to that devnet's run directory. + #[test] + #[ignore = "requires a running Curio devnet"] + fn live_admin_login_for_both_providers() { + let run_dir = std::path::PathBuf::from(std::env::var("FOC_CURIO_TEST_RUN_DIR").unwrap()); + let state: std::collections::HashMap = + serde_json::from_slice(&fs::read(run_dir.join("step_context.json")).unwrap()).unwrap(); + let run_id = run_dir.file_name().unwrap().to_str().unwrap(); + for sp in 1..=2 { + let key = format!("pdp_sp_{}_gui_port", sp); + let port: u16 = state.get(&key).unwrap().parse().unwrap(); + let path = foc_devnet_curio_sp_volume(run_id, sp).join("gui-admin.json"); + let rpc = WebRpc::connect_local(port, &path, sp).unwrap(); + let session = rpc.session.as_ref().expect("new Curio requires a session"); + let value: Value = rpc + .client + .post(format!("{}/api/webrpc/v0", rpc.base)) + .header(COOKIE, session) + .json(&json!({"jsonrpc":"2.0","method":"CurioWeb.Version","params":[],"id":1})) + .send() + .unwrap() + .error_for_status() + .unwrap() + .json() + .unwrap(); + assert!(value["result"].as_str().is_some_and(|v| !v.is_empty())); + let unauthenticated = rpc + .client + .post(format!("{}/api/webrpc/v0", rpc.base)) + .json(&json!({"jsonrpc":"2.0","method":"CurioWeb.Version","params":[],"id":1})) + .send() + .unwrap(); + assert_eq!(unauthenticated.status(), reqwest::StatusCode::UNAUTHORIZED); + } + } + + #[test] + fn legacy_http_requires_a_valid_version_response() { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let port = listener.local_addr().unwrap().port(); + let server = thread::spawn(move || { + // An old HTTP server cannot complete the initial TLS handshake. + let (mut tls, _) = listener.accept().unwrap(); + let mut buffer = [0; 4096]; + assert!(tls.read(&mut buffer).unwrap() > 0); + drop(tls); + let (mut http, _) = listener.accept().unwrap(); + assert!(http.read(&mut buffer).unwrap() > 0); + let body = r#"{"jsonrpc":"2.0","id":1,"result":"1.29.2"}"#; + write!( + http, + "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}", + body.len(), + body + ) + .unwrap(); + }); + let mut rpc = WebRpc { + client: Client::builder() + .no_proxy() + .timeout(Duration::from_secs(5)) + .build() + .unwrap(), + base: String::new(), + session: None, + }; + assert!(rpc.probe(port).unwrap().is_none()); + assert_eq!(rpc.base, format!("http://127.0.0.1:{}", port)); + server.join().unwrap(); + } + + #[test] + fn credentials_survive_restart_and_are_private() { + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("gui-admin.json"); + let initial = credentials(&path, false).unwrap(); + let restored = credentials(&path, true).unwrap(); + assert_eq!(initial.password, restored.password); + assert!(initial.password.len() >= 12); + assert_eq!( + fs::metadata(path).unwrap().permissions().mode() & 0o777, + 0o600 + ); + } + + #[test] + fn existing_admin_without_credentials_is_not_overwritten() { + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("gui-admin.json"); + assert!(credentials(&path, true).is_err()); + assert!(!path.exists()); + } + + #[test] + fn import_sends_session_and_does_not_echo_rpc_error_secrets() { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let address = listener.local_addr().unwrap(); + let server = thread::spawn(move || { + let (mut stream, _) = listener.accept().unwrap(); + stream + .set_read_timeout(Some(Duration::from_secs(5))) + .unwrap(); + let mut request = Vec::new(); + let mut buffer = [0; 4096]; + loop { + let count = stream.read(&mut buffer).unwrap(); + assert!(count > 0); + request.extend_from_slice(&buffer[..count]); + if let Some(end) = request.windows(4).position(|w| w == b"\r\n\r\n") { + let headers = String::from_utf8_lossy(&request[..end]).to_lowercase(); + let length: usize = headers + .lines() + .find_map(|line| line.strip_prefix("content-length: ")) + .unwrap() + .parse() + .unwrap(); + if request.len() >= end + 4 + length { + break; + } + } + } + let request = String::from_utf8(request).unwrap(); + assert!(request + .to_lowercase() + .contains("cookie: curio_session=test-session")); + assert!(request.contains("CurioWeb.ImportPDPKey")); + let body = + r#"{"jsonrpc":"2.0","id":1,"error":{"code":-1,"message":"secret-private-key"}}"#; + write!( + stream, + "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}", + body.len(), + body + ) + .unwrap(); + }); + let rpc = WebRpc { + client: Client::builder() + .no_proxy() + .timeout(Duration::from_secs(5)) + .build() + .unwrap(), + base: format!("http://{}", address), + session: Some("curio_session=test-session".into()), + }; + let error = rpc + .import_pdp_key("secret-private-key") + .unwrap_err() + .to_string(); + assert!(error.contains("-1")); + assert!(!error.contains("secret-private-key")); + server.join().unwrap(); + } +} From 6659f0601bd90475fca34efba9a69811ebad4e50 Mon Sep 17 00:00:00 2001 From: beck-8 <34204218+beck-8@users.noreply.github.com> Date: Wed, 18 Nov 2026 18:54:11 +0800 Subject: [PATCH 2/2] docs: remove Curio Web RPC setup details from README --- README.md | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/README.md b/README.md index 585c5c0b..e05745a6 100644 --- a/README.md +++ b/README.md @@ -92,16 +92,6 @@ This will: - Start storage provider(s) - Launch [Portainer UI](https://docs.docksal.io/use-cases/portainer/) for container management -Curio v1.29.3 and newer require HTTPS and an administrator session for the -management GUI. Startup automatically creates an administrator independently for -each SP, or logs in when one already exists. Credentials are stored with owner-only -permissions in `docker/volumes/run-specific//curio//gui-admin.json` -under the devnet base directory. Keep this file when reusing its database. If an administrator already exists but this file is missing, -startup fails instead of resetting the administrator. Local GUI requests accept -Curio's self-signed certificate. The GUI port is published on host loopback only. -Older Curio releases retain their HTTP management -flow. PDP upload and download endpoints are unchanged. - Curio providers initialize their storage metadata and register container-visible paths before launching, so PDP hashspaces include long-term storage on first startup. Startup retries RPC readiness checks while the daemon initializes.