diff --git a/crates/engine/src/cmd/plugins.rs b/crates/engine/src/cmd/plugins.rs index 9dbc9938b5..b0db4264c6 100644 --- a/crates/engine/src/cmd/plugins.rs +++ b/crates/engine/src/cmd/plugins.rs @@ -493,8 +493,13 @@ impl Host for Bridge<'_> { // The address, and what the plugin answers, can carry what it read from the project. self.need(Permission::Network, "fetch from the web")?; let c = self.s.continuation()?; + // Says why, where the still below would only cancel. + c.check()?; + let after = c.clone(); let still = Arc::new(move || c.check().is_ok()); + // A restore during the fetch ends it as the still's generic cancellation: say why. crate::render::rustgis_tiles::cache::with_still(still, || self.fetches.fetch(params)) + .map_err(|e| after.check().err().unwrap_or(e)) } _ if op.starts_with("host.") => Err(format!("unknown request '{op}'")), _ => self.command(op, params), diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 50b213aa37..7a67c550cb 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -844,6 +844,9 @@ pub struct Session { /// Set by [`Session::run_leaving_downloads`]: `layer.addUrl` leaves what it would wait for /// to the front end ([`Session::request_url`]), as in the browser. leave_downloads: bool, + /// Set by [`prefs::opt_in`]: the command line and Python read the remembered Earthdata token + /// for a command that names a data host, before it binds its accounts. + saved_secrets_opted_in: bool, /// Plugin commands waiting for their `host.fetch` answers, or the downloads their /// `layer.addUrl` left to the app, to run again. pub(crate) plugin_retries: Vec, @@ -1181,6 +1184,7 @@ impl Session { queue_gp_files: false, request_only: false, leave_downloads: false, + saved_secrets_opted_in: false, plugin_retries: Vec::new(), url_waits: cmd::plugins::fetch::UrlWaits::default(), next_request: 0, diff --git a/crates/engine/src/policy/plan.rs b/crates/engine/src/policy/plan.rs index 2b91e62ab4..365196cc35 100644 --- a/crates/engine/src/policy/plan.rs +++ b/crates/engine/src/policy/plan.rs @@ -7,6 +7,21 @@ use crate::{CommandSpec, Session}; use serde_json::{Value, json}; use std::sync::Arc; +/// Whether a parameter is the address of a file on an Earthdata data host. Walks with a list, not +/// by recursion: parameters are hostile. +fn names_earthdata_host(params: &Value) -> bool { + let mut left = vec![params]; + while let Some(value) = left.pop() { + match value { + Value::String(text) if crate::render::rustgis_tiles::earthdata::is_data_host(text) => return true, + Value::Array(items) => left.extend(items), + Value::Object(fields) => left.extend(fields.values()), + _ => {} + } + } + false +} + pub struct Call { pub(super) kind: CallKind, pub(super) request: String, @@ -347,6 +362,9 @@ impl Node { } /// Rejects a withdrawn load or reloaded schema before a new stage begins. pub(crate) fn current(&self) -> Result<(), String> { + if self.auth.as_ref().is_some_and(|a| a.restored_since()) { + return Err("the Earthdata token remembered on this computer was read while this command ran; run it again".into()); + } if self.auth.as_ref().is_some_and(|a| !a.is_current()) { return Err("the active account changed since this operation was approved".into()); } @@ -749,6 +767,12 @@ impl<'a> Builder<'a> { let session = self.s; if reads { if self.auth.is_none() { + // The command line and Python read the remembered token only for a command that + // needs it, and it must be read before this snapshot or the command's first + // request changes the account and cancels it (#1626). The app reads it at launch. + if session.saved_secrets_opted_in && names_earthdata_host(params) { + crate::render::rustgis_tiles::earthdata::restore(); + } self.auth = Some(Arc::new(super::scope::RuntimeAccounts::capture(session)?)); } n.auth = self.auth.clone(); diff --git a/crates/engine/src/policy/scope.rs b/crates/engine/src/policy/scope.rs index 0c5aedcc1e..e66b076142 100644 --- a/crates/engine/src/policy/scope.rs +++ b/crates/engine/src/policy/scope.rs @@ -24,6 +24,10 @@ impl RuntimeAccounts { pub(super) fn is_current(&self) -> bool { self.arcgis.is_current() && self.earthdata.is_current() && self.web.is_current() } + /// Whether the only account that changed is Earthdata's, by its remembered token being read. + pub(super) fn restored_since(&self) -> bool { + self.arcgis.is_current() && self.web.is_current() && self.earthdata.restored_since() + } } #[derive(Clone, Debug, PartialEq, Eq)] diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs index 4474db802c..cdde0b3f46 100644 --- a/crates/engine/src/prefs.rs +++ b/crates/engine/src/prefs.rs @@ -82,6 +82,7 @@ pub fn opt_in( }); let store: Arc = Arc::new(store); crate::render::rustgis_tiles::earthdata::set_secret_store(Some(store.clone())); + s.saved_secrets_opted_in = true; use_saved_secrets(s, &prefs, store); prefs } diff --git a/crates/engine/src/tests/permission_policy.rs b/crates/engine/src/tests/permission_policy.rs index 8a0b1e3c24..fd7e35bee7 100644 --- a/crates/engine/src/tests/permission_policy.rs +++ b/crates/engine/src/tests/permission_policy.rs @@ -920,6 +920,114 @@ fn permission_policy_exact_runtime_account_rotation() { arcgis_auth::sign_out(Some(root.trim_end_matches('/'))); } +/// A remembered Earthdata token read before a node binds keeps it current; read after, it cancels +/// with a message that says why; a login or replacement keeps the ordinary message. The command +/// line and Python read it for a command that names a data host, so that command is not cancelled. +#[test] +fn permission_policy_remembered_earthdata_token_read_mid_run() { + // The token and its store are process-wide: isolate them from every parallel plan reader. + #[cfg(not(target_arch = "wasm32"))] + if std::env::var_os("RUSTGIS_POLICY_REMEMBERED_CHILD").is_none() { + let output = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "tests::permission_policy::permission_policy_remembered_earthdata_token_read_mid_run", "--nocapture"]) + .env("RUSTGIS_POLICY_REMEMBERED_CHILD", "1") + // `opt_in` reads saved preferences: none, from a folder that doesn't exist. + .env("RUSTGIS_CONFIG_DIR", std::env::temp_dir().join(format!("rustgis-policy-remembered-{}", std::process::id()))) + .output() + .unwrap(); + assert!(output.status.success(), "{}{}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr)); + assert!(String::from_utf8_lossy(&output.stdout).contains("1 passed")); + return; + } + + use crate::render::rustgis_tiles::earthdata; + use rustgis_data::secrets::{MemorySecrets, Secret, SecretStore}; + // A new run of the app with a token remembered, not yet read. + let relaunch = || { + let store = Arc::new(MemorySecrets::new()); + store.save(earthdata::ACCOUNT, &Secret::new("synthetic-remembered")).unwrap(); + earthdata::set_token(None).unwrap(); + earthdata::set_secret_store(Some(store)); + }; + // Starts a command that sends (a download), as the app does after the plan binds its accounts. + let start = |s: &mut Session, n: u64| { + let (scope, _) = assistant(n); + let call = Call::command("layer.addUrl", json!({"url":"https://example.invalid/remembered.geojson"})); + let p = pending(s, &scope, &call); + s.run_permitted(scope, p.allow(), &call).unwrap(); + let request = s.take_url_requests().pop().unwrap(); + let id = s.download_started_for(&request).unwrap(); + (id, s.download_still(id).unwrap()) + }; + let mut s = world(); + #[cfg(not(target_arch = "wasm32"))] + { + s.leave_downloads = true; + } + + // Read at startup, before the node binds: nothing changes mid-run. + relaunch(); + assert_eq!(earthdata::restore(), earthdata::Restored::Token); + let (id, still) = start(&mut s, 1601); + assert_eq!(earthdata::restore(), earthdata::Restored::Nothing); + assert!(still() && s.download_continues(id).is_ok()); + + // Read while the command runs: cancelled, and told why. + relaunch(); + let (id, still) = start(&mut s, 1602); + assert_eq!(earthdata::restore(), earthdata::Restored::Token); + assert!(!still()); + let said = s.download_continues(id).unwrap_err(); + assert!(said.contains("remembered on this computer was read while this command ran") && !said.contains("synthetic"), "{said}"); + + // A login or replacement instead of that read, or after it, is the ordinary change. + let changes: [&dyn Fn(); 2] = + [&|| drop(earthdata::login("synthetic-typed", false)), &|| earthdata::set_token(Some("synthetic-typed")).unwrap()]; + for (n, change) in changes.into_iter().enumerate() { + relaunch(); + let (id, still) = start(&mut s, 1610 + n as u64); + change(); + assert!(!still()); + let said = s.download_continues(id).unwrap_err(); + assert!(said.contains("account changed") && !said.contains("remembered"), "{said}"); + } + relaunch(); + let (id, _) = start(&mut s, 1620); + earthdata::restore(); + earthdata::set_token(Some("synthetic-typed")).unwrap(); + let said = s.download_continues(id).unwrap_err(); + assert!(said.contains("account changed") && !said.contains("remembered"), "{said}"); + + // Run as the command line and Python run a command. + let run = |s: &mut Session, url: &str| { + s.run("layer.addUrl", &json!({ "url": url })).unwrap(); + let request = s.take_url_requests().pop().unwrap(); + let id = s.download_started_for(&request).unwrap(); + (id, s.download_still(id).unwrap()) + }; + let data_host = "https://data.ornldaac.earthdata.nasa.gov/remembered.geojson"; + // Without the opt-in (the app, which reads it off the frame) a command doesn't read the token. + relaunch(); + run(&mut s, data_host); + assert!(!earthdata::has_token()); + + // Opting in to saved secrets reads nothing, and neither does a command naming no data host. + // One that names a data host reads the token first, binds it and is not cancelled. + let store = MemorySecrets::new(); + store.save(earthdata::ACCOUNT, &Secret::new("synthetic-remembered")).unwrap(); + earthdata::set_token(None).unwrap(); + crate::prefs::opt_in(&mut s, store, |_| {}); + assert!(!earthdata::has_token()); + run(&mut s, "https://example.invalid/remembered.geojson"); + assert!(!earthdata::has_token()); + let (id, still) = run(&mut s, data_host); + assert!(earthdata::has_token()); + assert_eq!(earthdata::restore(), earthdata::Restored::Nothing); + assert!(still() && s.download_continues(id).is_ok()); + earthdata::set_secret_store(None); + earthdata::set_token(None).unwrap(); +} + /// Foreign in-place completions cannot borrow/consume ownership; omitted options retain defaults. #[test] fn permission_policy_download_refinements_rollback_and_abandonment() { diff --git a/crates/tiles/src/earthdata.rs b/crates/tiles/src/earthdata.rs index a210ec808e..1c94a8a3ae 100644 --- a/crates/tiles/src/earthdata.rs +++ b/crates/tiles/src/earthdata.rs @@ -57,6 +57,11 @@ fn token_store() -> &'static Mutex> { &T } +/// Whether the token kept is the one [`restore`] read and nothing has set, replaced or forgotten +/// it since. Never holds the token itself. Read and written only while holding the `token_store` +/// lock, so it never disagrees with the token. +static RESTORED: AtomicBool = AtomicBool::new(false); + fn lock(m: &Mutex) -> std::sync::MutexGuard<'_, T> { m.lock().unwrap_or_else(|e| e.into_inner()) } @@ -75,6 +80,18 @@ impl AccountIdentity { pub fn is_current(&self) -> bool { identity_in(token_store(), self.relevant).is_ok_and(|current| *self == current) } + + /// Whether this identity, taken with no token, is stale only because the token remembered on + /// this computer was read since ([`restore`]) and nothing was given, forgotten or replaced + /// after. For telling a cancelled request why; it never makes a stale identity current. + pub fn restored_since(&self) -> bool { + if !self.relevant || self.token.is_some() { + return false; + } + // Not `is_current`, which locks too: with no token in the snapshot, stale means one is active. + let token = lock(token_store()); + RESTORED.load(Ordering::SeqCst) && token.is_some() + } } /// Snapshots the active token only; an unread remembered token remains unread. @@ -116,7 +133,9 @@ pub fn set_token(token: Option<&str>) -> Result<(), String> { Some(t) => checked(t)?, None => None, }; - *lock(token_store()) = token; + let mut kept = lock(token_store()); + RESTORED.store(false, Ordering::SeqCst); + *kept = token; Ok(()) } @@ -169,7 +188,8 @@ fn secret_store() -> Option> { } /// Gives the credential store a token is remembered in (the desktop's keychain; the command -/// line's and Python's when they opt in to saved secrets). It is read when first needed. +/// line's and Python's when they opt in to saved secrets). The app reads it at launch, the command +/// line and Python when a command first needs it. pub fn set_secret_store(store: Option>) { let mut read = lock(&REMEMBERED.read); *lock(&REMEMBERED.store) = store; @@ -192,7 +212,8 @@ pub enum Restored { /// this session first wins: the store then only tells whether it holds that token (another session /// of the process may have remembered it). An expired or damaged one is deleted. Reading may ask /// the system to unlock its store: the app calls this off the frame (requests to data hosts call -/// it themselves). +/// it themselves, and the command line and Python before a command that names one binds its +/// accounts). pub fn restore() -> Restored { let mut read = lock(&REMEMBERED.read); if *read { @@ -201,23 +222,27 @@ pub fn restore() -> Restored { *read = true; let Some(store) = secret_store() else { return Restored::Nothing }; let Ok(Some(secret)) = store.load(ACCOUNT) else { return Restored::Nothing }; - if let Some(session) = lock(token_store()).as_deref() { + let mut kept = lock(token_store()); + if let Some(session) = kept.as_deref() { REMEMBERED.kept.store(checked(secret.expose()).ok().flatten().as_deref() == Some(session), Ordering::SeqCst); return Restored::Nothing; } match checked(secret.expose()) { Ok(Some(token)) if !expired(&token, now_secs()) => { - *lock(token_store()) = Some(token); + RESTORED.store(true, Ordering::SeqCst); + *kept = Some(token); REMEMBERED.kept.store(true, Ordering::SeqCst); Restored::Token } Ok(Some(_)) => { + drop(kept); REMEMBERED.kept.store(store.delete(ACCOUNT).is_err(), Ordering::SeqCst); REMEMBERED.expired.store(true, Ordering::SeqCst); Restored::Expired } // Damaged: of no use to anyone. _ => { + drop(kept); REMEMBERED.kept.store(store.delete(ACCOUNT).is_err(), Ordering::SeqCst); Restored::Nothing } @@ -232,6 +257,7 @@ pub fn forget_if_expired() -> bool { let mut token = lock(token_store()); let gone = token.as_deref().is_some_and(|t| expired(t, now_secs())); if gone { + RESTORED.store(false, Ordering::SeqCst); *token = None; } gone @@ -257,7 +283,11 @@ pub fn login(token: &str, remember: bool) -> Result, String let mut read = lock(&REMEMBERED.read); *read = true; REMEMBERED.expired.store(false, Ordering::SeqCst); - *lock(token_store()) = Some(token.clone()); + { + let mut kept = lock(token_store()); + RESTORED.store(false, Ordering::SeqCst); + *kept = Some(token.clone()); + } let Some(store) = secret_store() else { return Ok(remember.then_some(NO_STORE)) }; let kept = REMEMBERED.kept.load(Ordering::SeqCst); if remember && store.save(ACCOUNT, &Secret::new(token)).is_ok() { @@ -283,7 +313,11 @@ pub fn logout() -> Result<(), &'static str> { let mut read = lock(&REMEMBERED.read); *read = true; REMEMBERED.expired.store(false, Ordering::SeqCst); - *lock(token_store()) = None; + { + let mut kept = lock(token_store()); + RESTORED.store(false, Ordering::SeqCst); + *kept = None; + } // Deleted even when this session didn't read it: forgetting is asked for. A system without // a credential store had nowhere to keep one, unless this session saw it there. match secret_store().map_or(Ok(()), |store| store.delete(ACCOUNT)) { @@ -891,6 +925,40 @@ mod tests { use rustgis_data::secrets::{MemorySecrets, SecretError}; let store = Arc::new(MemorySecrets::new()); + // Reading the remembered token is told apart from a login, a replacement or a logout. + relaunch(&store); + login(LASTING, true).unwrap(); + relaunch(&store); + let before = account_identity().unwrap(); + assert!(!before.restored_since()); + assert_eq!(restore(), Restored::Token); + assert!(!before.is_current() && before.restored_since()); + assert!(account_identity().unwrap().is_current(), "a snapshot taken after the read is current"); + for change in [|| drop(login("synthetic-typed", false)), || set_token(Some("synthetic-typed")).unwrap()] { + relaunch(&store); + login(LASTING, true).unwrap(); + relaunch(&store); + let before = account_identity().unwrap(); + assert_eq!(restore(), Restored::Token); + change(); + assert!(!before.is_current() && !before.restored_since()); + } + // A logout puts the identity back as it was: current, with nothing to say. + relaunch(&store); + login(LASTING, true).unwrap(); + relaunch(&store); + let before = account_identity().unwrap(); + assert_eq!(restore(), Restored::Token); + logout().unwrap(); + assert!(before.is_current() && !before.restored_since()); + // Login or logout first leaves nothing to restore. + relaunch(&store); + let before = account_identity().unwrap(); + login(LASTING, true).unwrap(); + assert_eq!(restore(), Restored::Nothing); + assert!(!before.is_current() && !before.restored_since()); + relaunch(&store); + // Remembered, then read back by the next run's first request to a data host only. relaunch(&store); assert_eq!(login(LASTING, true), Ok(None)); diff --git a/crates/ui-egui/src/earthaccessui.rs b/crates/ui-egui/src/earthaccessui.rs index 93c94a9bbf..c5241355a5 100644 --- a/crates/ui-egui/src/earthaccessui.rs +++ b/crates/ui-egui/src/earthaccessui.rs @@ -476,7 +476,7 @@ fn auth(app: &mut RustGisApp, ui: &mut egui::Ui, w: f32) { /// Reads the token remembered on this computer, once, off the frame: the system may ask for its /// store to be unlocked. -fn restore(app: &mut RustGisApp, ctx: &egui::Context) { +pub(crate) fn restore(app: &mut RustGisApp, ctx: &egui::Context) { if app.ui.earthaccess.restoring || app.services.secrets.is_none() { return; } @@ -658,6 +658,32 @@ fn granules(app: &mut RustGisApp, ui: &mut egui::Ui, t: &crate::theme::Tokens, w mod tests { use super::*; + /// The first frame reads the remembered token, only where there is a credential store. + #[test] + #[cfg(not(target_arch = "wasm32"))] + fn the_first_frame_reads_the_remembered_token() { + use rustgis_engine::data::secrets::{MemorySecrets, Secret, SecretStore}; + // The token and its store are process-wide: this scenario runs alone. + if crate::isolate_account_test("earthaccessui::tests::the_first_frame_reads_the_remembered_token") { + return; + } + let ctx = egui::Context::default(); + let mut none = RustGisApp::with_sample(); + none.logic(&ctx); + assert!(!none.ui.earthaccess.restoring && !earthdata::has_token(), "no store, nothing read"); + let store = std::sync::Arc::new(MemorySecrets::new()); + store.save(earthdata::ACCOUNT, &Secret::new("synthetic-remembered")).unwrap(); + let mut app = RustGisApp::with_sample().with_services(crate::Services { secrets: Some(store), ..Default::default() }); + app.logic(&ctx); + assert!(app.ui.earthaccess.restoring, "the first frame starts the read"); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5); + while !earthdata::has_token() { + assert!(std::time::Instant::now() < deadline, "the token was never read"); + std::thread::sleep(std::time::Duration::from_millis(20)); + } + earthdata::set_secret_store(None); + } + #[test] fn bounds_and_names() { let g = json!({"type": "MultiPolygon", "coordinates": [[[[1.0, 2.0], [3.0, -4.0], [0.5, 9.0]]]]}); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 031bfcd44b..ed5bf32578 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -5031,6 +5031,10 @@ impl RustGisApp { self.frame_ctx = Some(ctx.clone()); // Agents the user turned on last time: the relay starts with the app. connectui::resume(self); + // The Earthdata token remembered on this computer is read now, off the frame, so a + // command started later binds it instead of meeting it mid-run (#1626). This runs every + // frame; after the first call it returns at once. + earthaccessui::restore(self, ctx); if let Some(relay) = self.relay.clone() { // A relay started before the first frame has no wake yet; now there is a context // to ask for repaints with.