From 778a91851c2ef910abf196c61b906c30b5f5502d Mon Sep 17 00:00:00 2001 From: choupeanut Date: Tue, 10 Nov 2026 02:16:59 +0800 Subject: [PATCH 1/3] fix(earthdata): read the remembered token at launch (#1626) A command's first use of a remembered token changed the account identity the command had captured, so it cancelled its own requests. The app now reads the token on its first frame, and a run that still reads it mid-way says so instead of reporting a generic account change. --- crates/engine/src/cmd/plugins.rs | 2 + crates/engine/src/policy/plan.rs | 3 + crates/engine/src/policy/scope.rs | 4 + crates/engine/src/tests/permission_policy.rs | 78 ++++++++++++++++++++ crates/tiles/src/earthdata.rs | 50 +++++++++++++ crates/ui-egui/src/earthaccessui.rs | 28 ++++++- crates/ui-egui/src/lib.rs | 3 + 7 files changed, 167 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/plugins.rs b/crates/engine/src/cmd/plugins.rs index 9dbc9938b5..6439df20d6 100644 --- a/crates/engine/src/cmd/plugins.rs +++ b/crates/engine/src/cmd/plugins.rs @@ -493,6 +493,8 @@ 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 still = Arc::new(move || c.check().is_ok()); crate::render::rustgis_tiles::cache::with_still(still, || self.fetches.fetch(params)) } diff --git a/crates/engine/src/policy/plan.rs b/crates/engine/src/policy/plan.rs index 2b91e62ab4..2603e3f72a 100644 --- a/crates/engine/src/policy/plan.rs +++ b/crates/engine/src/policy/plan.rs @@ -347,6 +347,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()); } 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/tests/permission_policy.rs b/crates/engine/src/tests/permission_policy.rs index 8a0b1e3c24..674edfd5e4 100644 --- a/crates/engine/src/tests/permission_policy.rs +++ b/crates/engine/src/tests/permission_policy.rs @@ -920,6 +920,84 @@ 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, logout or replacement keeps the ordinary message. +#[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") + .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}"); + 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..4f6f620af5 100644 --- a/crates/tiles/src/earthdata.rs +++ b/crates/tiles/src/earthdata.rs @@ -57,6 +57,10 @@ 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. +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 +79,13 @@ 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 { + self.relevant && self.token.is_none() && RESTORED.load(Ordering::SeqCst) && !self.is_current() + } } /// Snapshots the active token only; an unread remembered token remains unread. @@ -116,6 +127,7 @@ pub fn set_token(token: Option<&str>) -> Result<(), String> { Some(t) => checked(t)?, None => None, }; + RESTORED.store(false, Ordering::SeqCst); *lock(token_store()) = token; Ok(()) } @@ -207,6 +219,7 @@ pub fn restore() -> Restored { } match checked(secret.expose()) { Ok(Some(token)) if !expired(&token, now_secs()) => { + RESTORED.store(true, Ordering::SeqCst); *lock(token_store()) = Some(token); REMEMBERED.kept.store(true, Ordering::SeqCst); Restored::Token @@ -232,6 +245,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,6 +271,7 @@ pub fn login(token: &str, remember: bool) -> Result, String let mut read = lock(&REMEMBERED.read); *read = true; REMEMBERED.expired.store(false, Ordering::SeqCst); + RESTORED.store(false, Ordering::SeqCst); *lock(token_store()) = Some(token.clone()); let Some(store) = secret_store() else { return Ok(remember.then_some(NO_STORE)) }; let kept = REMEMBERED.kept.load(Ordering::SeqCst); @@ -283,6 +298,7 @@ pub fn logout() -> Result<(), &'static str> { let mut read = lock(&REMEMBERED.read); *read = true; REMEMBERED.expired.store(false, Ordering::SeqCst); + RESTORED.store(false, Ordering::SeqCst); *lock(token_store()) = 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. @@ -891,6 +907,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 59102e35e4..946bd80db1 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -4999,6 +4999,9 @@ 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). + earthaccessui::restore(self, ctx); if let Some(relay) = &self.relay { // A relay started before the first frame has no wake yet; now there is a context // to ask for repaints with. From 185d5a6efbe5a5bd93a8ab3ebda4c42b5068a0dd Mon Sep 17 00:00:00 2001 From: choupeanut Date: Tue, 10 Nov 2026 15:35:24 +0800 Subject: [PATCH 2/3] fix(earthdata): the command line and Python read the remembered token up front (#1626) Their first download no longer cancels itself: opt_in reads the token before a command binds its accounts. A read during host.fetch says why it cancelled, and the restored flag now changes under the token's lock. --- crates/engine/src/cmd/plugins.rs | 3 ++ crates/engine/src/prefs.rs | 3 ++ crates/engine/src/tests/permission_policy.rs | 15 ++++++++- crates/tiles/src/earthdata.rs | 34 ++++++++++++++------ crates/ui-egui/src/lib.rs | 3 +- 5 files changed, 47 insertions(+), 11 deletions(-) diff --git a/crates/engine/src/cmd/plugins.rs b/crates/engine/src/cmd/plugins.rs index 6439df20d6..b0db4264c6 100644 --- a/crates/engine/src/cmd/plugins.rs +++ b/crates/engine/src/cmd/plugins.rs @@ -495,8 +495,11 @@ impl Host for Bridge<'_> { 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/prefs.rs b/crates/engine/src/prefs.rs index 4474db802c..585ff5492f 100644 --- a/crates/engine/src/prefs.rs +++ b/crates/engine/src/prefs.rs @@ -82,6 +82,9 @@ pub fn opt_in( }); let store: Arc = Arc::new(store); crate::render::rustgis_tiles::earthdata::set_secret_store(Some(store.clone())); + // Read the remembered Earthdata token now, so a command started later binds it instead of + // meeting it mid-run and cancelling (#1626). + crate::render::rustgis_tiles::earthdata::restore(); 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 674edfd5e4..a8708c68cf 100644 --- a/crates/engine/src/tests/permission_policy.rs +++ b/crates/engine/src/tests/permission_policy.rs @@ -921,7 +921,8 @@ fn permission_policy_exact_runtime_account_rotation() { } /// A remembered Earthdata token read before a node binds keeps it current; read after, it cancels -/// with a message that says why; a login, logout or replacement keeps the ordinary message. +/// with a message that says why; a login or replacement keeps the ordinary message. The command +/// line and Python read it in `prefs::opt_in`, so their first 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. @@ -930,6 +931,8 @@ fn permission_policy_remembered_earthdata_token_read_mid_run() { 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)); @@ -994,6 +997,16 @@ fn permission_policy_remembered_earthdata_token_read_mid_run() { earthdata::set_token(Some("synthetic-typed")).unwrap(); let said = s.download_continues(id).unwrap_err(); assert!(said.contains("account changed") && !said.contains("remembered"), "{said}"); + + // The command line and Python opt in to saved secrets: that reads the token, so the first + // command 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, |_| {}); + let (id, still) = start(&mut s, 1630); + 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(); } diff --git a/crates/tiles/src/earthdata.rs b/crates/tiles/src/earthdata.rs index 4f6f620af5..ee54edbec0 100644 --- a/crates/tiles/src/earthdata.rs +++ b/crates/tiles/src/earthdata.rs @@ -58,7 +58,8 @@ fn token_store() -> &'static Mutex> { } /// Whether the token kept is the one [`restore`] read and nothing has set, replaced or forgotten -/// it since. Never holds the token itself. +/// 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> { @@ -84,7 +85,12 @@ impl AccountIdentity { /// 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 { - self.relevant && self.token.is_none() && RESTORED.load(Ordering::SeqCst) && !self.is_current() + 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() } } @@ -127,8 +133,9 @@ pub fn set_token(token: Option<&str>) -> Result<(), String> { Some(t) => checked(t)?, None => None, }; + let mut kept = lock(token_store()); RESTORED.store(false, Ordering::SeqCst); - *lock(token_store()) = token; + *kept = token; Ok(()) } @@ -213,24 +220,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()) => { RESTORED.store(true, Ordering::SeqCst); - *lock(token_store()) = Some(token); + *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 } @@ -271,8 +281,11 @@ pub fn login(token: &str, remember: bool) -> Result, String let mut read = lock(&REMEMBERED.read); *read = true; REMEMBERED.expired.store(false, Ordering::SeqCst); - RESTORED.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() { @@ -298,8 +311,11 @@ pub fn logout() -> Result<(), &'static str> { let mut read = lock(&REMEMBERED.read); *read = true; REMEMBERED.expired.store(false, Ordering::SeqCst); - RESTORED.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)) { diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 946bd80db1..0ab1a91eca 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -5000,7 +5000,8 @@ impl RustGisApp { // 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). + // 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 { // A relay started before the first frame has no wake yet; now there is a context From d1db061eb0bcce19d84277736a60487b07b0547b Mon Sep 17 00:00:00 2001 From: choupeanut Date: Tue, 10 Nov 2026 21:15:12 +0800 Subject: [PATCH 3/3] fix(earthdata): read the remembered token only for a command that names a data host (#1626) Opting in to saved secrets read the keychain at once, for every command line run and Python session, and a keychain that made the read wait held `Session(use_saved_secrets=True)`. That broke what opting in promises and failed the Python binding test. Opting in now only marks the session. A command whose parameters name an Earthdata data host reads the token just before it binds its accounts, so it isn't cancelled by its own first request. Commands that find their addresses as they run (a script, a model, a plugin) still get the "run it again" message when the read lands. --- crates/engine/src/lib.rs | 4 ++++ crates/engine/src/policy/plan.rs | 21 ++++++++++++++++ crates/engine/src/prefs.rs | 4 +--- crates/engine/src/tests/permission_policy.rs | 25 ++++++++++++++++---- crates/tiles/src/earthdata.rs | 6 +++-- 5 files changed, 51 insertions(+), 9 deletions(-) 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 2603e3f72a..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, @@ -752,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/prefs.rs b/crates/engine/src/prefs.rs index 585ff5492f..cdde0b3f46 100644 --- a/crates/engine/src/prefs.rs +++ b/crates/engine/src/prefs.rs @@ -82,9 +82,7 @@ pub fn opt_in( }); let store: Arc = Arc::new(store); crate::render::rustgis_tiles::earthdata::set_secret_store(Some(store.clone())); - // Read the remembered Earthdata token now, so a command started later binds it instead of - // meeting it mid-run and cancelling (#1626). - crate::render::rustgis_tiles::earthdata::restore(); + 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 a8708c68cf..fd7e35bee7 100644 --- a/crates/engine/src/tests/permission_policy.rs +++ b/crates/engine/src/tests/permission_policy.rs @@ -922,7 +922,7 @@ fn permission_policy_exact_runtime_account_rotation() { /// 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 in `prefs::opt_in`, so their first command is not cancelled. +/// 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. @@ -998,13 +998,30 @@ fn permission_policy_remembered_earthdata_token_read_mid_run() { let said = s.download_continues(id).unwrap_err(); assert!(said.contains("account changed") && !said.contains("remembered"), "{said}"); - // The command line and Python opt in to saved secrets: that reads the token, so the first - // command binds it and is not cancelled. + // 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, |_| {}); - let (id, still) = start(&mut s, 1630); + 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); diff --git a/crates/tiles/src/earthdata.rs b/crates/tiles/src/earthdata.rs index ee54edbec0..1c94a8a3ae 100644 --- a/crates/tiles/src/earthdata.rs +++ b/crates/tiles/src/earthdata.rs @@ -188,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; @@ -211,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 {