diff --git a/CHANGELOG.md b/CHANGELOG.md index c3c742439..42be271a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,7 +28,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). - **Database errors logged at debug level**: errors that PostgreSQL returns to a client are now logged concisely at `debug` level (`CS_LOG__PROTOCOL_LEVEL=debug` shows them), instead of in full at `error` level. Errors that Proxy itself generates are still logged. -- **Minimum supported Rust version**: building Proxy from source now requires Rust 1.89.0 or later, declared as `rust-version` in `Cargo.toml` so Cargo reports an older compiler clearly instead of failing deep in a dependency. CI builds the production binary on Rust 1.89.0 to keep that minimum from rising by accident. Development, tests, and release binaries use the Rust version pinned in `rust-toolchain.toml` (currently 1.100.0). See [Supported Rust versions](DEVELOPMENT.md#supported-rust-versions). +- **Minimum supported Rust version**: building Proxy from source now requires Rust 1.100.0 or later, declared as `rust-version` in `Cargo.toml` so Cargo reports an older compiler clearly instead of failing deep in a dependency. Proxy uses standard library APIs stabilized in Rust 1.100. CI builds the production binary on Rust 1.100.0 to keep that minimum from rising by accident. Development, tests, and release binaries use the Rust version pinned in `rust-toolchain.toml` (currently 1.100.0). See [Supported Rust versions](DEVELOPMENT.md#supported-rust-versions). - **PostgreSQL execution lifecycle ownership**: Proxy now tracks prepared statements, portals, Describe operations, execution outcomes, and execution metrics as one connection-local protocol lifecycle. Suspended executions retain their original metrics scope until completion, repeated portal executions receive distinct scopes, and terminal errors replace upstream responses atomically while schema outcomes are reported exactly once. @@ -47,6 +47,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ### Fixed +- **Active connections metric**: `cipherstash_proxy_clients_active_connections` now goes down when a client connection's handler panics. It previously went down only when the handler returned, so a panic left the gauge permanently higher. + - **`--db-user`**: the flag now sets the database username. It previously set an unused variable, so it had no effect. - **Unknown column error message**: the type-checking error for a column a table does not have now names the column and the table the right way round, for example `Column: email not found for table: users`. It previously swapped them. diff --git a/Cargo.toml b/Cargo.toml index 91d8d148c..5d1ab2d4c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ edition = "2021" # Minimum supported Rust version (MSRV). CI builds the production binary on # exactly this toolchain. See "Supported Rust versions" in DEVELOPMENT.md # before changing it. -rust-version = "1.89.0" +rust-version = "1.100.0" [profile.dev] incremental = true diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index eab6fc650..94f665d59 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -145,19 +145,20 @@ CI uses three Rust toolchains: |---|---|---| | **Pinned** (currently 1.100.0) | `rust-toolchain.toml` | Formatting, Clippy, unit and integration tests across the full PostgreSQL matrix, burn-in, benchmarks, and release binaries (`mise run build:binary`). | | **Latest stable** | floating `stable` channel | `Lint (Rust stable)`: formatting and Clippy only. | -| **Minimum supported (MSRV)** (currently 1.89.0) | `rust-version` under `[workspace.package]` in `Cargo.toml` | `Build (Rust MSRV)`: builds the production binary with `cargo build --locked --release --package cipherstash-proxy`. | +| **Minimum supported (MSRV)** (currently 1.100.0) | `rust-version` under `[workspace.package]` in `Cargo.toml` | `Build (Rust MSRV)`: builds the production binary with `cargo build --locked --release --package cipherstash-proxy`. | The MSRV build and stable lint jobs need no PostgreSQL or credentials. Locally, rustup selects the pinned toolchain automatically from `rust-toolchain.toml`. -Rust 1.88 cannot build Proxy: a transitive dependency (`stack-profile`, via `cipherstash-client`) uses `std::fs::File::lock`, which stabilized in 1.89, without declaring that requirement. +The MSRV is 1.100.0 because Proxy uses standard library APIs stabilized in that release, such as `str::trim_prefix`, `Vec::from_fn` and `core::mem::DropGuard`. +The MSRV and the pinned toolchain are currently the same version, so the two jobs differ only in what they run. To run those checks locally: ```shell cargo +stable clippy --all --no-deps --all-targets --all-features -- -D warnings -rustup toolchain install 1.89.0 --profile minimal -cargo +1.89.0 build --locked --release --package cipherstash-proxy +rustup toolchain install 1.100.0 --profile minimal +cargo +1.100.0 build --locked --release --package cipherstash-proxy ``` #### Bumping the pinned toolchain diff --git a/packages/cipherstash-proxy-integration/src/common.rs b/packages/cipherstash-proxy-integration/src/common.rs index 685ced4c3..34ab08033 100644 --- a/packages/cipherstash-proxy-integration/src/common.rs +++ b/packages/cipherstash-proxy-integration/src/common.rs @@ -588,21 +588,8 @@ where /// Returns indices in zigzag order so insertion is never accidentally sorted. /// For len=5: [4, 0, 3, 1, 2] pub fn interleaved_indices(len: usize) -> Vec { - let mut indices = Vec::with_capacity(len); - let mut lo = 0; - let mut hi = len; - let mut take_hi = true; - while lo < hi { - if take_hi { - hi -= 1; - indices.push(hi); - } else { - indices.push(lo); - lo += 1; - } - take_hi = !take_hi; - } - indices + // Even positions count down from the end, odd positions count up from 0. + Vec::from_fn(len, |i| if i % 2 == 0 { len - 1 - i / 2 } else { i / 2 }) } /// Asserts that `result` failed with a PostgreSQL `ErrorResponse` carrying @@ -709,3 +696,17 @@ impl ServerCertVerifier for DangerousTestCertVerifier { .supported_schemes() } } + +#[cfg(test)] +mod tests { + use super::interleaved_indices; + + #[test] + fn interleaved_indices_alternate_from_the_ends() { + assert_eq!(interleaved_indices(0), Vec::::new()); + assert_eq!(interleaved_indices(1), vec![0]); + assert_eq!(interleaved_indices(2), vec![1, 0]); + assert_eq!(interleaved_indices(5), vec![4, 0, 3, 1, 2]); + assert_eq!(interleaved_indices(6), vec![5, 0, 4, 1, 3, 2]); + } +} diff --git a/packages/cipherstash-proxy-integration/src/pipeline.rs b/packages/cipherstash-proxy-integration/src/pipeline.rs index 008777004..a0a8651ac 100644 --- a/packages/cipherstash-proxy-integration/src/pipeline.rs +++ b/packages/cipherstash-proxy-integration/src/pipeline.rs @@ -190,7 +190,7 @@ mod tests { .iter() .map(|sql| Box::pin(client.simple_query(sql)) as Pending<'_>) .collect(); - let mut results = (0..pending.len()).map(|_| None).collect::>(); + let mut results = Vec::from_fn(pending.len(), |_| None); // Poll every query in order on each wake, so all requests are written // in order before any response arrives. diff --git a/packages/cipherstash-proxy/src/main.rs b/packages/cipherstash-proxy/src/main.rs index efb25a4c9..9921f46f9 100644 --- a/packages/cipherstash-proxy/src/main.rs +++ b/packages/cipherstash-proxy/src/main.rs @@ -7,6 +7,7 @@ use cipherstash_proxy::{cli, log, postgresql as pg, prometheus, Args}; use clap::Parser; use metrics::gauge; use std::io::IsTerminal; +use std::mem::DropGuard; use tokio::signal::unix::{signal, SignalKind}; use tokio_util::task::TaskTracker; use tracing::{error, info, warn}; @@ -112,9 +113,14 @@ fn main() -> Result<(), Box> { tracker.spawn(async move { gauge!(CLIENTS_ACTIVE_CONNECTIONS).increment(1); + // Decrements even if the handler panics or the task + // is dropped, so the gauge cannot drift upwards. + let active_connection = DropGuard::new((), |()| { + gauge!(CLIENTS_ACTIVE_CONNECTIONS).decrement(1); + }); let result = pg::handler(client_stream,context).await; - gauge!(CLIENTS_ACTIVE_CONNECTIONS).decrement(1); + drop(active_connection); match result { Ok(_) => (), diff --git a/packages/cipherstash-proxy/src/postgresql/context/mod.rs b/packages/cipherstash-proxy/src/postgresql/context/mod.rs index 44077828b..3cda8a9a0 100644 --- a/packages/cipherstash-proxy/src/postgresql/context/mod.rs +++ b/packages/cipherstash-proxy/src/postgresql/context/mod.rs @@ -1650,9 +1650,7 @@ where columns: &[Option], ) -> Result>, Error> { if plaintexts.iter().all(Option::is_none) { - return Ok(std::iter::repeat_with(|| None) - .take(plaintexts.len()) - .collect()); + return Ok(Vec::from_fn(plaintexts.len(), |_| None)); } let keyset_id = self.keyset_identifier()?; diff --git a/packages/cipherstash-proxy/src/postgresql/driver.rs b/packages/cipherstash-proxy/src/postgresql/driver.rs index b5dfb440c..e9420d1f7 100644 --- a/packages/cipherstash-proxy/src/postgresql/driver.rs +++ b/packages/cipherstash-proxy/src/postgresql/driver.rs @@ -15,6 +15,7 @@ use pg_proto::{ }; use std::{ convert::Infallible, + mem::DropGuard, sync::{Arc, LazyLock}, }; use tokio::net::TcpStream; @@ -25,23 +26,20 @@ use tracing::info; static CANCELLATION_REGISTRY: LazyLock = LazyLock::new(InMemoryCancellationRegistry::default); -struct CancellationRegistrationGuard { +/// A session's cancellation key and the registry it is attached to. +type CancellationRegistration = (InMemoryCancellationRegistry, Option); + +/// Detaches a session's cancellation key from the registry when dropped, so a +/// closed session can no longer be cancelled through its old key. +fn cancellation_registration_guard( registry: InMemoryCancellationRegistry, key: Option, -} - -impl CancellationRegistrationGuard { - fn new(registry: InMemoryCancellationRegistry, key: Option) -> Self { - Self { registry, key } - } -} - -impl Drop for CancellationRegistrationGuard { - fn drop(&mut self) { - if let Some(key) = self.key.take() { - let _ = self.registry.detach(&key); +) -> DropGuard { + DropGuard::new((registry, key), |(registry, key)| { + if let Some(key) = key { + let _ = registry.detach(&key); } - } + }) } #[derive(Clone)] @@ -122,7 +120,7 @@ where pg_proto::IntermediaryAccept::Session(session) => session, pg_proto::IntermediaryAccept::CancellationForwarded => return Ok(()), }; - let _cancellation_guard = CancellationRegistrationGuard::new( + let _cancellation_guard = cancellation_registration_guard( CANCELLATION_REGISTRY.clone(), session.cancellation_key().cloned(), ); @@ -422,8 +420,7 @@ mod tests { ); { - let _guard = - CancellationRegistrationGuard::new(registry.clone(), Some(key.clone())); + let _guard = cancellation_registration_guard(registry.clone(), Some(key.clone())); let _ = result; } assert_eq!(registry.register(route), Ok(key)); diff --git a/packages/cipherstash-proxy/src/postgresql/middleware/backend.rs b/packages/cipherstash-proxy/src/postgresql/middleware/backend.rs index 4cf92d413..33d31230b 100644 --- a/packages/cipherstash-proxy/src/postgresql/middleware/backend.rs +++ b/packages/cipherstash-proxy/src/postgresql/middleware/backend.rs @@ -761,13 +761,13 @@ impl PostgreSqlErrorHandler for Backend { /// Whether a `CommandComplete` tag confirms a `DEALLOCATE` or `DISCARD ALL` pub(super) fn is_deallocation(tag: &[u8]) -> bool { - let tag = tag.strip_suffix(b"\0").unwrap_or(tag); + let tag = tag.trim_suffix(b"\0"); matches!(tag, b"DEALLOCATE" | b"DEALLOCATE ALL" | b"DISCARD ALL") } /// Whether a deallocation command tag removes every prepared statement fn removes_all_statements(tag: &[u8]) -> bool { - let tag = tag.strip_suffix(b"\0").unwrap_or(tag); + let tag = tag.trim_suffix(b"\0"); matches!(tag, b"DEALLOCATE ALL" | b"DISCARD ALL") } diff --git a/packages/cipherstash-proxy/src/proxy/zerokms/zerokms.rs b/packages/cipherstash-proxy/src/proxy/zerokms/zerokms.rs index ef9ea98f2..013b406ef 100644 --- a/packages/cipherstash-proxy/src/proxy/zerokms/zerokms.rs +++ b/packages/cipherstash-proxy/src/proxy/zerokms/zerokms.rs @@ -338,12 +338,12 @@ impl EncryptionService for ZeroKms { // If no plaintexts to encrypt, return all None. // - // Built by iteration rather than `vec![None; n]`: that needs `Clone`, - // and `EqlOutputV3` does not derive it (neither does the v2 + // Built with `Vec::from_fn` rather than `vec![None; n]`: that needs + // `Clone`, and `EqlOutputV3` does not derive it (neither does the v2 // `EqlOutput` — the ciphertext types are `Clone`, the output wrappers // are not). if prepared_plaintexts.is_empty() { - return Ok((0..plaintexts.len()).map(|_| None).collect()); + return Ok(Vec::from_fn(plaintexts.len(), |_| None)); } // Use default opts since cipher is already initialized with the correct keyset @@ -506,7 +506,7 @@ fn place_ciphertexts( } .into()); } - let mut result: Vec> = (0..len).map(|_| None).collect(); + let mut result: Vec> = Vec::from_fn(len, |_| None); for (idx, ciphertext) in indices.into_iter().zip(ciphertexts) { result[idx] = Some(ciphertext); } diff --git a/packages/eql-mapper/src/inference/unifier/types.rs b/packages/eql-mapper/src/inference/unifier/types.rs index a5ced6b49..9f435501e 100644 --- a/packages/eql-mapper/src/inference/unifier/types.rs +++ b/packages/eql-mapper/src/inference/unifier/types.rs @@ -434,7 +434,7 @@ impl DomainIdentity { self.domain .value .get(prefix_len..) - .map(|rest| rest.strip_prefix('_').unwrap_or(rest)) + .map(|rest| rest.trim_prefix('_')) .unwrap_or("") } @@ -515,11 +515,7 @@ impl DomainIdentity { if self.token == TokenType::Json { return ("eql_v3", "query_json".to_string()); } - let bare = self - .domain - .value - .strip_prefix("eql_v3_") - .unwrap_or(&self.domain.value); + let bare = self.domain.value.trim_prefix("eql_v3_"); ("eql_v3", format!("query_{bare}")) }