diff --git a/Cargo.lock b/Cargo.lock index 7ff6296..4d061bf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -277,6 +277,7 @@ version = "0.1.0" dependencies = [ "beyond-handoff", "beyond-pg-core", + "beyond-pg-kernels", "clap", "libc", "nix 0.31.2", @@ -337,6 +338,13 @@ dependencies = [ "tokio-vsock", ] +[[package]] +name = "beyond-pg-kernels" +version = "0.1.0" +dependencies = [ + "vstd", +] + [[package]] name = "beyond-pg-sink" version = "0.1.0" @@ -620,6 +628,12 @@ version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a6ef517f0926dd24a1582492c791b6a4818a4d94e789a334894aa15b0d12f55c" +[[package]] +name = "convert_case" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" + [[package]] name = "core-foundation" version = "0.10.1" @@ -3451,6 +3465,60 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "verus_builtin" +version = "0.0.0-2026-11-01-0251" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5cecde3e49b95f670dee0000707b0fe24d3894ffec56d9e6c49f404878b30e9" + +[[package]] +name = "verus_builtin_macros" +version = "0.0.0-2026-11-01-0251" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94dbd5043818524d73436d4dd20a8f67f8f8c13dc00d2662d643ce994ff2dbb5" +dependencies = [ + "convert_case", + "proc-macro2", + "quote", + "syn", + "synstructure", + "verus_prettyplease", + "verus_syn", +] + +[[package]] +name = "verus_prettyplease" +version = "0.0.0-2026-11-01-0251" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b2cf0618d8d5ed0cde78122effaf56f5be0a0d0e0f9066735fa9f17d6ba6a4a" +dependencies = [ + "proc-macro2", + "verus_syn", +] + +[[package]] +name = "verus_state_machines_macros" +version = "0.0.0-2026-11-01-0251" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36b7a87f62a49719385cad07ad58ad9d17af29642dd331a5cdc25eba2423b716" +dependencies = [ + "indexmap 2.14.0", + "proc-macro2", + "quote", + "verus_syn", +] + +[[package]] +name = "verus_syn" +version = "0.0.0-2026-11-01-0251" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "461620a5ec0fb547ba398e42fb25a017bd8764798a32099eed73c77dd979ac39" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "vsock" version = "0.5.4" @@ -3461,6 +3529,17 @@ dependencies = [ "nix 0.31.2", ] +[[package]] +name = "vstd" +version = "0.0.0-2026-11-01-0251" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cfcc2fc379ffbcda31111f1a7da2a34d00bd984407cfa85d1edcf64da38dbd2" +dependencies = [ + "verus_builtin", + "verus_builtin_macros", + "verus_state_machines_macros", +] + [[package]] name = "wal-proto" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index ea5015f..a979506 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ members = [ "beyond-pg-cdc", "beyond-pg-core", "beyond-pg-init", + "beyond-pg-kernels", "beyond-pg-sink", "wal-proto", ] @@ -21,6 +22,7 @@ path = "src/main.rs" [dependencies] beyond-pg-core = { path = "./beyond-pg-core" } +beyond-pg-kernels = { path = "./beyond-pg-kernels" } clap = { version = "4", features = ["derive"] } quinn = "0.11" rcgen = "0.14" diff --git a/beyond-pg-kernels/Cargo.toml b/beyond-pg-kernels/Cargo.toml new file mode 100644 index 0000000..0f153e1 --- /dev/null +++ b/beyond-pg-kernels/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "beyond-pg-kernels" +version = "0.1.0" +edition = "2024" +rust-version = "1.92" +license = "UNLICENSED" +description = "beyond-pg's pure decision kernels: no I/O, no clock, proven with Verus" + +[lib] +name = "beyond_pg_kernels" + +[features] +default = [] +# `start::plan`: a start that runs post_start leaves the role's password as it was (a fork or restore keeps the source's hash). `cargo verus verify --features mut_start_keeps_stored_role` must go red. +mut_start_keeps_stored_role = [] + +[dependencies] +# The `verus!` macro and the spec library. A normal build erases every proof; only a `cargo verus` build checks them. +vstd = "=0.0.0-2026-11-01-0251" + +[package.metadata.verus] +verify = true + +[lints.rust] +unexpected_cfgs = { level = "warn", check-cfg = ['cfg(verus_only)'] } diff --git a/beyond-pg-kernels/src/lib.rs b/beyond-pg-kernels/src/lib.rs new file mode 100644 index 0000000..5f75c34 --- /dev/null +++ b/beyond-pg-kernels/src/lib.rs @@ -0,0 +1,3 @@ +//! beyond-pg's pure decision kernels (no I/O, no clock), proven with Verus. The supervisor calls them; nothing here is restated elsewhere. + +pub mod start; diff --git a/beyond-pg-kernels/src/start.rs b/beyond-pg-kernels/src/start.rs new file mode 100644 index 0000000..e9b34f2 --- /dev/null +++ b/beyond-pg-kernels/src/start.rs @@ -0,0 +1,72 @@ +//! What a start of the database does about its role's password. Row: `P.secrets.a-postgres-role-password-is-the-served-value-on-every-start` +//! (Beyond repo): "A Postgres guest that holds a platform-generated password has its database role's password set to that value on every start, so a +//! fork or restore carrying the source's role hash authenticates with its own value and never the source's." +//! +//! A start is a cold start of the supervisor (a boot, a restart, a fork-branch or restore on a cloned volume: the data directory may carry any role +//! hash) or a successor taking over a running postgres (the same process, so the role is whatever this instance last set). A cold primary start runs +//! `post_start`, whose FIRST statement is `ALTER ROLE postgres WITH PASSWORD `; a replica is a read-only standby and runs none. +//! `plan` is called by `supervisor::run` (whether post_start runs) and by `supervisor::build_post_start_script` (whether it opens with the reset). +//! The served verifier is the one the driver hands the guest over the env relay (`beyond-pg-init/src/env_relay.rs`, a verifier or nothing). +//! +//! Liveness partner: `plan`'s ensures is an iff, so a primary cold start is always planned the reset (completeness), and the reset is idempotent +//! (`role_after` of `served` is `served`), so a retry after a failed post_start converges. + +use vstd::prelude::*; + +verus! { + +/// What a start does. +#[derive(Clone, Copy)] +pub struct Plan { + /// `post_start` runs. + pub post_start: bool, + /// Its first statement sets the role's password to the served value. + pub reset_role: bool, +} + +/// The role's password hash after a start that planned `p`, given the one the data directory held and the served one. +pub open spec fn role_after(stored: Option>, served: Seq, p: Plan) -> Option> { + if p.reset_role { Some(served) } else { stored } +} + +/// The plan for a start: `replica` is a hot standby, `successor` a handover of a running postgres. +pub fn plan(replica: bool, successor: bool) -> (p: Plan) + ensures + p.post_start <==> (!replica && !successor), + // Every start that runs post_start resets the role first; none that skips it does. + p.reset_role <==> p.post_start, +{ + let runs = !replica && !successor; + if cfg!(feature = "mut_start_keeps_stored_role") { + return Plan { post_start: runs, reset_role: false }; + } + Plan { post_start: runs, reset_role: runs } +} + +/// A primary's cold start, whatever the data directory carried (a fork's or restore's source hash among it), leaves the role with exactly the served +/// value: the source's hash, when it differs, is gone. +pub proof fn a_start_holds_the_served_value(stored: Option>, served: Seq) + ensures + ({ + let p = plan_spec(false, false); + role_after(stored, served, p) == Some(served) + && (stored is Some && stored->Some_0 != served ==> role_after(stored, served, p) != stored) + }), +{ +} + +/// Retrying after a failed start converges: the reset applied twice is the reset applied once. +pub proof fn the_reset_is_idempotent(stored: Option>, served: Seq) + ensures + role_after(role_after(stored, served, plan_spec(false, false)), served, plan_spec(false, false)) + == role_after(stored, served, plan_spec(false, false)), +{ +} + +/// The spec of `plan`'s answer. +pub open spec fn plan_spec(replica: bool, successor: bool) -> Plan { + let runs = !replica && !successor; + Plan { post_start: runs, reset_role: runs } +} + +} // verus! diff --git a/src/supervisor.rs b/src/supervisor.rs index 44159a8..359b0fc 100644 --- a/src/supervisor.rs +++ b/src/supervisor.rs @@ -588,8 +588,11 @@ async fn run_inner_inner(role: MaybeRole) -> Result<(), Box Result<(), Box> { +async fn post_start(cfg: &MmdsConfig, start: beyond_pg_kernels::start::Plan) -> Result<(), Box> { // Resolve the extensions to (idempotently) create: required + optional, // filtered to those whose control file is present. A REQUIRED extension // missing its control file is downgraded to a warning (the standalone image @@ -1852,7 +1855,7 @@ async fn post_start(cfg: &MmdsConfig) -> Result<(), Box> // process spawns + reconnects. Every statement is idempotent and, on a // well-formed image, a no-op or fast role op — so a failure is a real problem // and rightly aborts post_start (→ restart). - pg::psql_script(&build_post_start_script(cfg, &extensions)) + pg::psql_script(&build_post_start_script(cfg, start, &extensions)) .await .map_err(|e| format!("post-start setup failed: {e}"))?; @@ -1869,15 +1872,17 @@ async fn post_start(cfg: &MmdsConfig) -> Result<(), Box> /// per `extensions` → (when WAL consumers exist) replicator role + password → /// (when CDC enabled) slot + publication. Every statement is `;`-terminated so /// the whole thing runs as one `psql -f -` with `ON_ERROR_STOP=1`. -fn build_post_start_script(cfg: &MmdsConfig, extensions: &[&str]) -> String { +fn build_post_start_script(cfg: &MmdsConfig, start: beyond_pg_kernels::start::Plan, extensions: &[&str]) -> String { let mut s = String::new(); // 1. Reset the superuser password from the per-instance MMDS secret. - s.push_str(&pg::alter_role_password_sql( - "postgres", - &cfg.postgres_password, - )); - s.push_str(";\n"); + if start.reset_role { + s.push_str(&pg::alter_role_password_sql( + "postgres", + &cfg.postgres_password, + )); + s.push_str(";\n"); + } // 2. pgbouncer auth (role + SECURITY DEFINER lookup function + grants). s.push_str(crate::sql::PGBOUNCER_AUTH_SQL); @@ -2286,7 +2291,7 @@ mod tests { #[test] fn post_start_script_is_one_terminated_batch() { - let s = build_post_start_script(&pg_cfg(None, None, false), &["vector", "postgis"]); + let s = build_post_start_script(&pg_cfg(None, None, false), beyond_pg_kernels::start::plan(false, false), &["vector", "postgis"]); // superuser password (dollar-quoted) + pgbouncer auth, each terminated. assert!(s.contains("ALTER ROLE postgres WITH PASSWORD $_beyond_$s3cr3t$_beyond_$;")); assert!(s.contains("CREATE SCHEMA IF NOT EXISTS pgbouncer;")); @@ -2304,7 +2309,7 @@ mod tests { #[test] fn post_start_script_adds_replicator_and_cdc_when_enabled() { // WAL sink ⇒ replicator role (no password set); CDC ⇒ slot + publication. - let s = build_post_start_script(&pg_cfg(Some("rpw"), Some("http://sink:9000"), true), &[]); + let s = build_post_start_script(&pg_cfg(Some("rpw"), Some("http://sink:9000"), true), beyond_pg_kernels::start::plan(false, false), &[]); assert!(s.contains("CREATE ROLE replicator LOGIN REPLICATION PASSWORD NULL")); assert!(s.contains("ALTER ROLE replicator WITH PASSWORD $_beyond_$rpw$_beyond_$;")); assert!(s.contains("pg_create_logical_replication_slot('cdc'")); @@ -2315,7 +2320,7 @@ mod tests { #[test] fn post_start_script_replicator_without_password_when_only_sink() { // wal_sink alone creates the role but sets NO password (PASSWORD NULL). - let s = build_post_start_script(&pg_cfg(None, Some("http://sink:9000"), false), &[]); + let s = build_post_start_script(&pg_cfg(None, Some("http://sink:9000"), false), beyond_pg_kernels::start::plan(false, false), &[]); assert!(s.contains("CREATE ROLE replicator")); assert!(!s.contains("ALTER ROLE replicator WITH PASSWORD")); }