diff --git a/crates/jmap/src/registry/set.rs b/crates/jmap/src/registry/set.rs index 6cf2be9c..e2d53dd8 100644 --- a/crates/jmap/src/registry/set.rs +++ b/crates/jmap/src/registry/set.rs @@ -1424,6 +1424,12 @@ pub(crate) fn map_write_error(err: RegistryWriteResult) -> SetError { } RegistryWriteResult::NotSupported => SetError::forbidden() .with_description("The requested action is not supported by the registry store"), + // inbuxa: K-S5 + RegistryWriteResult::ManagedOutside => SetError::forbidden().with_description( + "The data store settings are in config.json, which this server can't write: \ + it's managed outside the server (on Kubernetes, the chart's server.dataStore \ + value). Change them there.", + ), RegistryWriteResult::NotFound { .. } => SetError::not_found(), RegistryWriteResult::Success(_) => unreachable!(), } diff --git a/crates/store/src/registry/local.rs b/crates/store/src/registry/local.rs index 6a0a1369..a6c5f043 100644 --- a/crates/store/src/registry/local.rs +++ b/crates/store/src/registry/local.rs @@ -109,7 +109,60 @@ impl RegistryStore { trc::EventType::Registry(trc::RegistryEvent::LocalWriteError) .into_err() .caused_by(trc::location!()) + .ctx(trc::Key::Path, self.0.local_path.display().to_string()) .reason(err) }) } + + /// inbuxa: K-S5: whether the data store settings file can be written. It + /// can't where something outside the server manages it, such as a + /// read-only mount on Kubernetes (the chart's server.dataStore). + pub async fn data_store_is_writable(&self) -> bool { + match tokio::fs::OpenOptions::new() + .write(true) + .open(&self.0.local_path) + .await + { + Ok(_) => true, + Err(err) => !is_read_only(&err), + } + } +} + +/// inbuxa: K-S5: an error that says the file is managed elsewhere, as +/// against one worth reporting as a failure +pub(crate) fn is_read_only(err: &std::io::Error) -> bool { + matches!( + err.kind(), + std::io::ErrorKind::ReadOnlyFilesystem | std::io::ErrorKind::PermissionDenied + ) +} + +#[cfg(test)] +mod tests { + use super::is_read_only; + + // inbuxa: K-S5: a read-only config.json is one managed elsewhere, so a + // save is refused as that rather than failed + #[test] + fn a_read_only_file_is_managed_elsewhere() { + use std::os::unix::fs::PermissionsExt; + let dir = std::env::temp_dir().join(format!("inbuxa-ro-config-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("config.json"); + std::fs::write(&path, "{}").unwrap(); + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o444)).unwrap(); + let err = std::fs::OpenOptions::new().write(true).open(&path); + // root can write anything; the check only means something otherwise + if let Err(err) = err { + assert!(is_read_only(&err)); + } + let missing = std::fs::OpenOptions::new() + .write(true) + .open(dir.join("absent.json")) + .unwrap_err(); + assert!(!is_read_only(&missing)); + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).unwrap(); + std::fs::remove_dir_all(&dir).unwrap(); + } } diff --git a/crates/store/src/registry/write.rs b/crates/store/src/registry/write.rs index 301699ec..bccf8583 100644 --- a/crates/store/src/registry/write.rs +++ b/crates/store/src/registry/write.rs @@ -55,6 +55,9 @@ pub enum RegistryWriteResult { errors: Vec, }, NotSupported, + /// inbuxa: K-S5: the object lives in a file the server can't write, + /// because something outside it manages that file + ManagedOutside, } pub enum RegistryWrite<'x> { @@ -212,6 +215,11 @@ impl RegistryStore { if generate_id { return Ok(RegistryWriteResult::NotSupported); } + // inbuxa: K-S5: a config.json managed outside the server (a + // read-only mount) is refused as that, not failed as an error + if !self.data_store_is_writable().await { + return Ok(RegistryWriteResult::ManagedOutside); + } return self .write_data_store(data_store) @@ -671,6 +679,9 @@ impl Display for RegistryWriteResult { Ok(()) } RegistryWriteResult::NotSupported => write!(f, "Operation not supported"), + RegistryWriteResult::ManagedOutside => { + write!(f, "Managed outside the server: the file is read-only") + } } } }