-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] v2/blobs: verify integrity on download
- Loading branch information
Showing
7 changed files
with
111 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/// Implements types and methods for content verification | ||
use sha2::{self, Digest}; | ||
use v2::*; | ||
|
||
/// ContentDigest stores a digest and its DigestAlgorithm | ||
#[derive(Clone, Debug, PartialEq)] | ||
pub struct ContentDigest { | ||
digest: String, | ||
algorithm: DigestAlgorithm, | ||
} | ||
|
||
/// DigestAlgorithm declares the supported algorithms | ||
#[derive(Display, Clone, Debug, PartialEq, EnumString)] | ||
pub enum DigestAlgorithm { | ||
sha256, | ||
} | ||
|
||
impl ContentDigest { | ||
pub fn try_new(digest: String) -> Result<Self> { | ||
let digest_split = digest.split(':').collect::<Vec<&str>>(); | ||
|
||
if digest_split.len() < 2 { | ||
return Err(format!("digest '{}' does not have an algorithm prefix", digest).into()); | ||
} | ||
|
||
let algorithm = | ||
std::str::FromStr::from_str(digest_split[0]).map_err(|e| format!("{}", e))?; | ||
Ok(ContentDigest { | ||
digest: digest_split[1..].join(""), | ||
algorithm, | ||
}) | ||
} | ||
|
||
pub fn try_verify(&self, input: &[u8]) -> Result<()> { | ||
let hash = self.algorithm.hash(input); | ||
let layer_digest = Self::try_new(hash)?; | ||
|
||
if self != &layer_digest { | ||
return Err(format!( | ||
"content verification failed. expected '{}', got '{}'", | ||
self.to_owned(), | ||
layer_digest.to_owned() | ||
) | ||
.into()); | ||
} | ||
|
||
trace!("content verification succeeded for '{}'", &layer_digest); | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl std::fmt::Display for ContentDigest { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
write!(f, "{}:{}", self.algorithm, self.digest) | ||
} | ||
} | ||
|
||
impl DigestAlgorithm { | ||
fn hash(&self, input: &[u8]) -> String { | ||
match self { | ||
DigestAlgorithm::sha256 => { | ||
let hash = sha2::Sha256::digest(input); | ||
format!("{}:{:x}", self, hash) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn try_verify_succeeds_with_same_content() -> Result<()> { | ||
let blob: &[u8] = b"somecontent"; | ||
let digest = DigestAlgorithm::sha256.hash(&blob); | ||
|
||
ContentDigest::try_new(digest)?.try_verify(&blob) | ||
} | ||
|
||
#[test] | ||
fn try_verify_fails_with_different_content() -> Result<()> { | ||
let blob: &[u8] = b"somecontent"; | ||
let different_blob: &[u8] = b"someothercontent"; | ||
let digest = DigestAlgorithm::sha256.hash(&blob); | ||
|
||
match ContentDigest::try_new(digest)?.try_verify(&different_blob) { | ||
Ok(()) => Err("expected try_verify to fail for a different blob".into()), | ||
Err(_) => Ok(()), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters