diff --git a/.changeset/cli-migrate-cfg-gate.md b/.changeset/cli-migrate-cfg-gate.md new file mode 100644 index 00000000..a4002a93 --- /dev/null +++ b/.changeset/cli-migrate-cfg-gate.md @@ -0,0 +1,4 @@ +--- +--- + +Gate Unix-only migration APIs and directory permissions behind cfg(unix) (#1121) diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index fff0fe61..d59da805 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -284,6 +284,23 @@ jobs: - name: Release build run: cargo build --workspace --all-features --release + windows-cli-check: + name: Windows CLI Check + runs-on: windows-latest + needs: changes + if: needs.changes.outputs.rust == 'true' + steps: + - name: Checkout + uses: actions/checkout@v4.4.0 + with: + persist-credentials: false + - name: Install Rust toolchain + uses: actions-rust-lang/setup-rust-toolchain@v1.17.0 + - name: CLI builds on Windows + run: cargo build -p nostream-cli --no-default-features + - name: CLI refuses the migration on Windows + run: cargo test -p nostream-cli --no-default-features --test migrate_cli_unsupported + changeset-check: name: Changeset Required runs-on: ubuntu-latest diff --git a/crates/nostream-cli/src/commands/migrate.rs b/crates/nostream-cli/src/commands/migrate.rs index 8d5b07bc..2fd5d685 100644 --- a/crates/nostream-cli/src/commands/migrate.rs +++ b/crates/nostream-cli/src/commands/migrate.rs @@ -13,6 +13,7 @@ use std::collections::{BTreeMap, BTreeSet}; use std::fs; use std::io; use std::io::Write; +#[cfg(unix)] use std::os::unix::fs::{DirBuilderExt, MetadataExt, OpenOptionsExt, PermissionsExt}; use std::path::{Path, PathBuf}; use std::time::{Duration, Instant}; @@ -42,6 +43,7 @@ pub struct MigrateOptions { } /// `migrate pg-to-lmdb`: the copy, or the verification pass when `verify` is set. +#[cfg(unix)] pub fn run(options: MigrateOptions, out: &mut dyn Write) -> Result { let runtime = tokio::runtime::Builder::new_current_thread() .enable_all() @@ -110,6 +112,16 @@ pub fn run(options: MigrateOptions, out: &mut dyn Write) -> Result Result { + writeln!(out, "migrate pg-to-lmdb is unsupported on this platform") + .map_err(|e| CliError::Migrate(e.to_string()))?; + Ok(CommandOutput { + lines: Vec::new(), + exit_code: 1, + }) +} + /// The copy over an injected source, target and writer, which is how the whole run is tested /// without a process and without a database. /// @@ -575,10 +587,11 @@ fn write_manifest(target: &Path, manifest: &Manifest) -> Result<(), CliError> { // Without the file sync, a power loss can leave the new name pointing at a truncated // manifest; `read_manifest` then refuses it, and the nonempty-target rule refuses to // migrate over the store it describes, so the operator's only way forward is a discard. - let mut file = fs::OpenOptions::new() - .write(true) - .create_new(true) - .mode(0o600) + let mut open_options = fs::OpenOptions::new(); + open_options.write(true).create_new(true); + #[cfg(unix)] + open_options.mode(0o600); + let mut file = open_options .open(&temporary) .map_err(|error| CliError::Migrate(format!("{}: {error}", temporary.display())))?; file.write_all(&bytes) @@ -739,6 +752,7 @@ fn probe_budget() -> Duration { /// sends the copy somewhere the operator did not name. Both are refused before anything is /// opened. A target that does not exist yet cannot be judged here, so `create_target` creates it /// before the store opens and runs this check again over what is then on disk. +#[cfg(unix)] fn refuse_unsafe_target(options: &MigrateOptions) -> Result<(), CliError> { refuse_unsafe_target_for(options, effective_uid()) } @@ -747,12 +761,14 @@ fn refuse_unsafe_target(options: &MigrateOptions) -> Result<(), CliError> { /// /// `std::fs` exposes a file's owner but not the caller's, and reading it here costs no /// dependency: this crate runs on Linux, where the kernel publishes it in one line. +#[cfg(unix)] fn effective_uid() -> Option { let status = fs::read_to_string("/proc/self/status").ok()?; let line = status.lines().find(|line| line.starts_with("Uid:"))?; line.split_whitespace().nth(2)?.parse().ok() } +#[cfg(unix)] fn refuse_unsafe_target_for(options: &MigrateOptions, caller: Option) -> Result<(), CliError> { for path in [&options.target, &store_path(options)] { let metadata = match fs::symlink_metadata(path) { @@ -849,6 +865,7 @@ fn refuse_unsafe_target_for(options: &MigrateOptions, caller: Option) -> Re /// the entry itself, so a group- or world-writable ancestor lets another user move this target /// aside and put their own there before the store opens. The sticky bit is the exception that /// makes `/tmp` usable: it refuses the rename of an entry its writer does not own. +#[cfg(unix)] fn refuse_swappable_ancestry(target: &Path) -> Result<(), CliError> { let mut path = match target.parent() { Some(parent) if parent.as_os_str().is_empty() => PathBuf::from("."), @@ -893,6 +910,7 @@ fn refuse_swappable_ancestry(target: &Path) -> Result<(), CliError> { /// by `create_dir_all` without a word. Creating it here, under the mode the store itself uses, /// and checking again closes the window: after this call the target is a directory this process /// made, or the run has ended. +#[cfg(unix)] fn create_target(options: &MigrateOptions) -> Result<(), CliError> { fs::DirBuilder::new() .recursive(true) @@ -1038,6 +1056,7 @@ mod tests { /// A link where the data file belongs is followed by `is_file()` and by LMDB both, so the /// copy would read or write through it and heed would put `lock.mdb` beside the link's /// target. The predicate over the file's own metadata is what AC18 asks for. + #[cfg(unix)] #[test] #[allow(clippy::expect_used)] fn a_symlinked_data_file_is_refused() { @@ -1063,6 +1082,7 @@ mod tests { /// checked against the caller rather than the mode. Measured as if a different uid were /// running, because a directory this process owns cannot be made to belong to another user /// without root. + #[cfg(unix)] #[test] #[allow(clippy::expect_used)] fn a_target_owned_by_another_user_is_refused() { @@ -1091,6 +1111,7 @@ mod tests { /// needs no ownership of the target to do it: a rename takes write permission on the holder. /// Without the sticky bit that permission is refused; `/tmp` is exactly this mode plus the /// sticky bit, which is why the bit, and not the mode alone, is the question. + #[cfg(unix)] #[test] #[allow(clippy::expect_used)] fn a_target_under_a_swappable_directory_is_refused() { @@ -1121,6 +1142,7 @@ mod tests { /// target is created and then judged. Both halves are needed: the creation gives the second /// check something to see, and the second check is what refuses a directory another user /// planted while the source was being probed. + #[cfg(unix)] #[test] #[allow(clippy::expect_used)] fn a_missing_target_is_created_and_then_judged() { diff --git a/crates/nostream-cli/tests/migrate_cli.rs b/crates/nostream-cli/tests/migrate_cli.rs index 6d92b898..834a6797 100644 --- a/crates/nostream-cli/tests/migrate_cli.rs +++ b/crates/nostream-cli/tests/migrate_cli.rs @@ -8,6 +8,9 @@ //! These targets may unwrap and assert by panicking: a failed assertion is a failed test. #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] +// The copy drives the Unix-only half of the command; the refusal on other platforms is pinned in +// `migrate_cli_unsupported.rs`, which is the only file that compiles there. +#![cfg(unix)] use std::collections::BTreeMap; use std::process::Command; diff --git a/crates/nostream-cli/tests/migrate_cli_unsupported.rs b/crates/nostream-cli/tests/migrate_cli_unsupported.rs new file mode 100644 index 00000000..2af51c54 --- /dev/null +++ b/crates/nostream-cli/tests/migrate_cli_unsupported.rs @@ -0,0 +1,29 @@ +//! The `migrate pg-to-lmdb` command where the copy cannot run: every part of it needs Unix file +//! modes, so the command still exists but refuses, and this file pins what it answers. +//! +//! The copy, the manifest, resume and verify live in `migrate_cli.rs`, which compiles only on +//! Unix. CI runs this one from the `windows-latest` job. + +#![cfg(not(unix))] +#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] + +use std::process::Command; + +/// The refusal names the command, says the platform is the reason, and exits non-zero, so an +/// operator reading a pipeline log learns why rather than seeing a compile error or a hang. +#[test] +fn the_command_is_refused_with_a_named_reason_and_exit_1() { + let directory = tempfile::tempdir().expect("a temporary directory"); + let output = Command::new(env!("CARGO_BIN_EXE_nostream-cli")) + .args(["migrate", "pg-to-lmdb", "--target"]) + .arg(directory.path()) + .output() + .expect("the binary runs"); + + assert_eq!(output.status.code(), Some(1), "the refusal exits 1"); + let stdout = String::from_utf8_lossy(&output.stdout); + assert!( + stdout.contains("migrate pg-to-lmdb is unsupported on this platform"), + "the refusal names the command and the reason: {stdout}" + ); +} diff --git a/crates/nostream-config/src/load.rs b/crates/nostream-config/src/load.rs index 985cdb55..28f8c87f 100644 --- a/crates/nostream-config/src/load.rs +++ b/crates/nostream-config/src/load.rs @@ -5,7 +5,6 @@ //! validate never aborts the process. It is reported as [`Issue`] values and the caller gets //! the shipped defaults, so a broken `settings.yaml` cannot stop a relay from starting. -use std::ffi::OsStr; use std::fs; use std::path::{Path, PathBuf}; @@ -70,6 +69,7 @@ pub fn load_document(path: &Path) -> Result { /// and non-UTF-8 paths are handled because the work is on `OsStr` bytes, not a `to_str`. #[cfg(unix)] fn trim_trailing_separators(path: &Path) -> &Path { + use std::ffi::OsStr; use std::os::unix::ffi::OsStrExt; let bytes = path.as_os_str().as_bytes(); diff --git a/crates/nostream-storage/src/backend.rs b/crates/nostream-storage/src/backend.rs index 07a89460..8c8ed39a 100644 --- a/crates/nostream-storage/src/backend.rs +++ b/crates/nostream-storage/src/backend.rs @@ -1,7 +1,7 @@ //! Backend selection and the LMDB environment. Mirrors `src/utils/event-store.ts` (the //! selection half) and the storage boundary in `src/@types/adapters.ts`. -#[cfg(feature = "lmdb")] +#[cfg(all(feature = "lmdb", unix))] use std::os::unix::fs::DirBuilderExt; use std::path::{Path, PathBuf}; @@ -99,6 +99,7 @@ impl LmdbEnv { // host whose umask is 002 `create_dir_all` leaves it group-writable, which is both a // wider audience than the store's own 0600 files and something the migration's target // check refuses on the next run. + #[cfg(unix)] std::fs::DirBuilder::new() .recursive(true) .mode(0o700) @@ -107,6 +108,12 @@ impl LmdbEnv { path: path.clone(), source, })?; + #[cfg(not(unix))] + std::fs::create_dir_all(&path).map_err(|source| StorageError::CreateDirectory { + path: path.clone(), + source, + })?; + let env = Self::open_with(&path, map_size, EnvFlags::empty())?; Ok(Self { env, path }) }