Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/engine/src/cmd/plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 4 additions & 0 deletions crates/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<cmd::plugins::fetch::Retry>,
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions crates/engine/src/policy/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()) {
Comment thread
deniial00 marked this conversation as resolved.
Resolved
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());
}
Expand Down Expand Up @@ -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) {
Comment thread
deniial00 marked this conversation as resolved.
Resolved
crate::render::rustgis_tiles::earthdata::restore();
}
self.auth = Some(Arc::new(super::scope::RuntimeAccounts::capture(session)?));
}
n.auth = self.auth.clone();
Expand Down
4 changes: 4 additions & 0 deletions crates/engine/src/policy/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
1 change: 1 addition & 0 deletions crates/engine/src/prefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub fn opt_in(
});
let store: Arc<dyn SecretStore> = 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
}
Expand Down
108 changes: 108 additions & 0 deletions crates/engine/src/tests/permission_policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
82 changes: 75 additions & 7 deletions crates/tiles/src/earthdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ fn token_store() -> &'static Mutex<Option<String>> {
&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<T>(m: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
m.lock().unwrap_or_else(|e| e.into_inner())
}
Expand All @@ -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.
Expand Down Expand Up @@ -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(())
}

Expand Down Expand Up @@ -169,7 +188,8 @@ fn secret_store() -> Option<Arc<dyn SecretStore>> {
}

/// 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<Arc<dyn SecretStore>>) {
let mut read = lock(&REMEMBERED.read);
*lock(&REMEMBERED.store) = store;
Expand All @@ -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 {
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -257,7 +283,11 @@ pub fn login(token: &str, remember: bool) -> Result<Option<&'static str>, 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() {
Expand All @@ -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)) {
Expand Down Expand Up @@ -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));
Expand Down
Loading
Loading