Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ChecksumAlgorithm field of client's ObjectInfo struct to be Vec over single element #1093

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mountpoint-s3-client/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
If specified, the result should contain the checksum for the object if available in the S3 response.
([#1083](https://github.com/awslabs/mountpoint-s3/pull/1083))
* Expose checksum algorithm in `ListObjectsResult`'s `ObjectInfo` struct.
([#1086](https://github.com/awslabs/mountpoint-s3/pull/1086))
([#1086](https://github.com/awslabs/mountpoint-s3/pull/1086),
[#1093](https://github.com/awslabs/mountpoint-s3/pull/1093))
* `ChecksumAlgorithm` has a new variant `Unknown(String)`,
to accomodate algorithms not recognized by the client should they be added in future.
([#1086](https://github.com/awslabs/mountpoint-s3/pull/1086))
Expand Down
8 changes: 4 additions & 4 deletions mountpoint-s3-client/src/mock_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ impl MockClient {
etag: object.etag.as_str().to_string(),
storage_class: object.storage_class.clone(),
restore_status: object.restore_status,
checksum_algorithm: object.checksum.algorithm(),
checksum_algorithms: object.checksum.algorithms(),
});
}
}
Expand Down Expand Up @@ -318,7 +318,7 @@ impl MockClient {
etag: object.etag.as_str().to_string(),
storage_class: object.storage_class.clone(),
restore_status: object.restore_status,
checksum_algorithm: object.checksum.algorithm(),
checksum_algorithms: object.checksum.algorithms(),
});
}
next_continuation_token += 1;
Expand Down Expand Up @@ -1608,8 +1608,8 @@ mod tests {
.list_objects("test_bucket", None, "/", 1000, "")
.await
.expect("should not fail");
assert_eq!(result.objects[0].checksum_algorithm, None);
assert_eq!(result.objects[1].checksum_algorithm, Some(ChecksumAlgorithm::Sha1));
assert_eq!(result.objects[0].checksum_algorithms, vec![]);
assert_eq!(result.objects[1].checksum_algorithms, vec![ChecksumAlgorithm::Sha1]);
}

#[tokio::test]
Expand Down
50 changes: 32 additions & 18 deletions mountpoint-s3-client/src/object_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,13 @@ pub struct ObjectInfo {
pub etag: String,

/// The algorithm that was used to create a checksum of the object.
pub checksum_algorithm: Option<ChecksumAlgorithm>,
///
/// The [Amazon S3 API Reference] specifies this field as a list of strings,
/// so we return here a [Vec] of [ChecksumAlgorithm].
///
/// [Amazon S3 API Reference]:
/// https://docs.aws.amazon.com/AmazonS3/latest/API/API_Object.html#AmazonS3-Type-Object-ChecksumAlgorithm
pub checksum_algorithms: Vec<ChecksumAlgorithm>,
}

/// All possible object attributes that can be retrived from [ObjectClient::get_object_attributes].
Expand Down Expand Up @@ -712,24 +718,33 @@ impl Checksum {
}
}

/// Provide [ChecksumAlgorithm] for the [Checksum], if set and recognized.
///
/// This method assumes that at most one checksum will be set and will return the first matched.
pub fn algorithm(&self) -> Option<ChecksumAlgorithm> {
/// Provide [ChecksumAlgorithm]s for the [Checksum], if set and recognized.
pub fn algorithms(&self) -> Vec<ChecksumAlgorithm> {
// We assume that at most one checksum will be set.
let mut algorithms = Vec::with_capacity(1);

// Pattern match forces us to accomodate any new fields when added.
let Self {
checksum_crc32,
checksum_crc32c,
checksum_sha1,
checksum_sha256,
} = &self;

match (checksum_crc32, checksum_crc32c, checksum_sha1, checksum_sha256) {
(Some(_), _, _, _) => Some(ChecksumAlgorithm::Crc32),
(_, Some(_), _, _) => Some(ChecksumAlgorithm::Crc32c),
(_, _, Some(_), _) => Some(ChecksumAlgorithm::Sha1),
(_, _, _, Some(_)) => Some(ChecksumAlgorithm::Sha256),
(None, None, None, None) => None,
if checksum_crc32.is_some() {
algorithms.push(ChecksumAlgorithm::Crc32);
}
if checksum_crc32c.is_some() {
algorithms.push(ChecksumAlgorithm::Crc32c);
}
if checksum_sha1.is_some() {
algorithms.push(ChecksumAlgorithm::Sha1);
}
if checksum_sha256.is_some() {
algorithms.push(ChecksumAlgorithm::Sha256);
}

algorithms
}
}

Expand Down Expand Up @@ -786,7 +801,7 @@ mod tests {
checksum_sha1: Some("checksum_sha1".to_string()),
checksum_sha256: None,
};
assert_eq!(checksum.algorithm(), Some(ChecksumAlgorithm::Sha1));
assert_eq!(checksum.algorithms(), vec![ChecksumAlgorithm::Sha1]);
}

#[test]
Expand All @@ -797,22 +812,21 @@ mod tests {
checksum_sha1: None,
checksum_sha256: None,
};
assert_eq!(checksum.algorithm(), None);
assert_eq!(checksum.algorithms(), vec![]);
}

#[test]
fn test_checksum_algorithm_many_set() {
// Amazon S3 doesn't support more than one algorithm, but just in case... let's show we don't panic.
// Amazon S3 doesn't support more than one algorithm today, but just in case... let's show we don't panic.
let checksum = Checksum {
checksum_crc32: None,
checksum_crc32c: Some("checksum_crc32c".to_string()),
checksum_sha1: Some("checksum_sha1".to_string()),
checksum_sha256: None,
};
let algorithm = checksum.algorithm().expect("checksum algorithm must be present");
assert!(
[ChecksumAlgorithm::Crc32c, ChecksumAlgorithm::Sha1].contains(&algorithm),
"algorithm should match one of the algorithms present in the struct",
assert_eq!(
checksum.algorithms(),
vec![ChecksumAlgorithm::Crc32c, ChecksumAlgorithm::Sha1],
);
}
}
39 changes: 21 additions & 18 deletions mountpoint-s3-client/src/s3_crt_client/list_objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ fn parse_result_from_bytes(bytes: &[u8]) -> Result<ListObjectsResult, ParseError
fn parse_result_from_xml(element: &mut xmltree::Element) -> Result<ListObjectsResult, ParseError> {
let mut objects = Vec::new();

while let Some(content) = element.take_child("Contents") {
objects.push(parse_object_info_from_xml(&content)?);
while let Some(mut content) = element.take_child("Contents") {
objects.push(parse_object_info_from_xml(&mut content)?);
}

let mut common_prefixes = Vec::new();
Expand Down Expand Up @@ -115,23 +115,26 @@ fn parse_restore_status(element: &xmltree::Element) -> Result<Option<RestoreStat
}))
}

fn parse_checksum_algorithm(element: &xmltree::Element) -> Result<Option<ChecksumAlgorithm>, ParseError> {
let Some(checksum_algorithm) = get_field(element, "ChecksumAlgorithm").ok() else {
return Ok(None);
};

let checksum_algorithm = match checksum_algorithm.as_str() {
"CRC32" => ChecksumAlgorithm::Crc32,
"CRC32C" => ChecksumAlgorithm::Crc32c,
"SHA1" => ChecksumAlgorithm::Sha1,
"SHA256" => ChecksumAlgorithm::Sha256,
_ => ChecksumAlgorithm::Unknown(checksum_algorithm.clone()),
};
fn parse_checksum_algorithm(element: &mut xmltree::Element) -> Result<Vec<ChecksumAlgorithm>, ParseError> {
// We expect there only to be at most one algorithm.
let mut algorithms = Vec::with_capacity(1);

while let Some(content) = element.take_child("ChecksumAlgorithm") {
let algo_string = get_text(&content)?;
let checksum_algorithm = match algo_string.as_str() {
"CRC32" => ChecksumAlgorithm::Crc32,
"CRC32C" => ChecksumAlgorithm::Crc32c,
"SHA1" => ChecksumAlgorithm::Sha1,
"SHA256" => ChecksumAlgorithm::Sha256,
_ => ChecksumAlgorithm::Unknown(algo_string),
};
algorithms.push(checksum_algorithm);
}

Ok(Some(checksum_algorithm))
Ok(algorithms)
}

fn parse_object_info_from_xml(element: &xmltree::Element) -> Result<ObjectInfo, ParseError> {
fn parse_object_info_from_xml(element: &mut xmltree::Element) -> Result<ObjectInfo, ParseError> {
let key = get_field(element, "Key")?;

let size = get_field(element, "Size")?;
Expand All @@ -151,7 +154,7 @@ fn parse_object_info_from_xml(element: &xmltree::Element) -> Result<ObjectInfo,

let etag = get_field(element, "ETag")?;

let checksum_algorithm = parse_checksum_algorithm(element)?;
let checksum_algorithms = parse_checksum_algorithm(element)?;

Ok(ObjectInfo {
key,
Expand All @@ -160,7 +163,7 @@ fn parse_object_info_from_xml(element: &xmltree::Element) -> Result<ObjectInfo,
storage_class,
restore_status,
etag,
checksum_algorithm,
checksum_algorithms,
})
}

Expand Down
2 changes: 1 addition & 1 deletion mountpoint-s3-client/tests/list_objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,5 +218,5 @@ async fn test_checksum_attribute(upload_checksum_algorithm: ChecksumAlgorithm) {
_ => todo!("update with new checksum algorithm should one come available"),
};

assert_eq!(Some(expected_checksum_algorithm), object.checksum_algorithm);
assert_eq!(vec![expected_checksum_algorithm], object.checksum_algorithms);
}
Loading