From 71cd379f50b6e079b37639bc41d4cc4bda125554 Mon Sep 17 00:00:00 2001 From: brianheineman Date: Mon, 1 Jul 2024 10:02:07 -0600 Subject: [PATCH] feat: update hasher registry to work with Maven central and add MD5 hash --- Cargo.lock | 1 + Cargo.toml | 1 + README.md | 3 +- postgresql_archive/Cargo.toml | 1 + postgresql_archive/src/hasher/md5.rs | 26 +++++++++++++++++ postgresql_archive/src/hasher/mod.rs | 1 + postgresql_archive/src/hasher/registry.rs | 28 +++++++++---------- .../src/repository/maven/mod.rs | 2 ++ 8 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 postgresql_archive/src/hasher/md5.rs diff --git a/Cargo.lock b/Cargo.lock index 96ae1dd..5759ebd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1792,6 +1792,7 @@ dependencies = [ "http", "human_bytes", "lazy_static", + "md-5", "num-format", "quick-xml", "regex", diff --git a/Cargo.toml b/Cargo.toml index 5e08298..f661e5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ home = "0.5.9" http = "1.1.0" human_bytes = { version = "0.4.3", default-features = false } lazy_static = "1.5.0" +md-5 = "0.10.6" num-format = "0.4.4" quick-xml = "0.35.0" rand = "0.8.5" diff --git a/README.md b/README.md index bf17a11..5f07670 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,8 @@ In either case, PostgreSQL will run in a separate process space. - running PostgreSQL on ephemeral ports - async and blocking API - bundling the PostgreSQL archive in an executable -- dynamic version resolution +- semantic version resolution +- support for custom PostgreSQL archives / binaries - ability to configure PostgreSQL startup options - URL based configuration - choice of native-tls vs rustls diff --git a/postgresql_archive/Cargo.toml b/postgresql_archive/Cargo.toml index 52edf72..96f7a1f 100644 --- a/postgresql_archive/Cargo.toml +++ b/postgresql_archive/Cargo.toml @@ -18,6 +18,7 @@ hex = { workspace = true } http = { workspace = true } human_bytes = { workspace = true, default-features = false } lazy_static = { workspace = true } +md-5 = { workspace = true } num-format = { workspace = true } quick-xml = { workspace = true, features = ["serialize"] } regex = { workspace = true } diff --git a/postgresql_archive/src/hasher/md5.rs b/postgresql_archive/src/hasher/md5.rs new file mode 100644 index 0000000..0218879 --- /dev/null +++ b/postgresql_archive/src/hasher/md5.rs @@ -0,0 +1,26 @@ +use crate::Result; +use md5::{Digest, Md5}; + +/// Hashes the data using MD5. +/// +/// # Errors +/// * If the data cannot be hashed. +pub fn hash(data: &Vec) -> Result { + let mut hasher = Md5::new(); + hasher.update(data); + let hash = hex::encode(hasher.finalize()); + Ok(hash) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_hash() -> Result<()> { + let data = vec![4, 2]; + let hash = hash(&data)?; + assert_eq!("21fb3d1d1a91a7e80dff456205f3380b", hash); + Ok(()) + } +} diff --git a/postgresql_archive/src/hasher/mod.rs b/postgresql_archive/src/hasher/mod.rs index 84595c1..a214eaf 100644 --- a/postgresql_archive/src/hasher/mod.rs +++ b/postgresql_archive/src/hasher/mod.rs @@ -1,5 +1,6 @@ pub mod blake2b_512; pub mod blake2s_256; +pub mod md5; pub mod registry; pub mod sha1; pub mod sha2_256; diff --git a/postgresql_archive/src/hasher/registry.rs b/postgresql_archive/src/hasher/registry.rs index 5d053e6..7ac9a59 100644 --- a/postgresql_archive/src/hasher/registry.rs +++ b/postgresql_archive/src/hasher/registry.rs @@ -1,5 +1,6 @@ use crate::configuration::theseus; -use crate::hasher::{sha1, sha2_256, sha2_512}; +use crate::hasher::{md5, sha1, sha2_256, sha2_512}; +use crate::repository::maven; use crate::Error::{PoisonedLock, UnsupportedHasher}; use crate::Result; use lazy_static::lazy_static; @@ -70,21 +71,21 @@ impl Default for HasherRegistry { |url, extension| Ok(url.starts_with(theseus::URL) && extension == "sha256"), sha2_256::hash, ); - // The zonky maven central releases prior to version 13.2.0 only provide MD5/SHA-1 hashes. + // Register the Maven hashers: https://maven.apache.org/resolver/about-checksums.html#implemented-checksum-algorithms registry.register( - |url, extension| { - Ok(url.contains("zonky") - && url.contains("embedded-postgres-binaries") - && extension == "sha1") - }, + |url, extension| Ok(url.starts_with(maven::URL) && extension == "md5"), + md5::hash, + ); + registry.register( + |url, extension| Ok(url.starts_with(maven::URL) && extension == "sha1"), sha1::hash, ); registry.register( - |url, extension| { - Ok(url.contains("zonky") - && url.contains("embedded-postgres-binaries") - && extension == "sha512") - }, + |url, extension| Ok(url.starts_with(maven::URL) && extension == "sha256"), + sha2_256::hash, + ); + registry.register( + |url, extension| Ok(url.starts_with(maven::URL) && extension == "sha512"), sha2_512::hash, ); registry @@ -119,7 +120,6 @@ pub fn get>(url: S, extension: S) -> Result { #[cfg(test)] mod tests { use super::*; - use crate::configuration::zonky; fn test_hasher(extension: &str, expected: &str) -> Result<()> { let hasher = get("https://foo.com", extension)?; @@ -163,6 +163,6 @@ mod tests { #[test] fn test_get_zonkyio_postgresql_binaries() { - assert!(get(zonky::URL, "sha512").is_ok()); + assert!(get(maven::URL, "sha512").is_ok()); } } diff --git a/postgresql_archive/src/repository/maven/mod.rs b/postgresql_archive/src/repository/maven/mod.rs index fa55e95..f6b2cc9 100644 --- a/postgresql_archive/src/repository/maven/mod.rs +++ b/postgresql_archive/src/repository/maven/mod.rs @@ -1,2 +1,4 @@ pub(crate) mod models; pub mod repository; + +pub const URL: &str = "https://repo1.maven.org/maven2";