Skip to content

Commit

Permalink
feat: update hasher registry to work with Maven central and add MD5 hash
Browse files Browse the repository at this point in the history
  • Loading branch information
brianheineman committed Jul 1, 2024
1 parent 97a7447 commit 71cd379
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions postgresql_archive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
26 changes: 26 additions & 0 deletions postgresql_archive/src/hasher/md5.rs
Original file line number Diff line number Diff line change
@@ -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<u8>) -> Result<String> {
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(())
}
}
1 change: 1 addition & 0 deletions postgresql_archive/src/hasher/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
28 changes: 14 additions & 14 deletions postgresql_archive/src/hasher/registry.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -119,7 +120,6 @@ pub fn get<S: AsRef<str>>(url: S, extension: S) -> Result<HasherFn> {
#[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)?;
Expand Down Expand Up @@ -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());
}
}
2 changes: 2 additions & 0 deletions postgresql_archive/src/repository/maven/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub(crate) mod models;
pub mod repository;

pub const URL: &str = "https://repo1.maven.org/maven2";

0 comments on commit 71cd379

Please sign in to comment.