feat(s3): serve UploadPart through the gateway

Share the streaming multipart write path and staged authorization across both edges. Resolve the native upload ID against the active storage record at the existing lookup. Carry checksums, trailers, SSE values and the stored ETag through each response projection.

Add seven tests for application lookup, route authorization, framing and durable part state. Exercise 62 refusals and 10 successful requests across those fixtures. The existing SSE-C completion test also covers the migrated UploadPart core.

Verification on 1467d32695: fixed legacy contract, 147 gateway-feature tests and 119 default tests, both all-target Clippy legs, script tests, test wiring and prepush checks. The test scopes include the entire access module. The gateway scope includes PutObject and both refusal matrices.

Twenty-two local fault controls exercised edge authorization, policy and generation, resource and upload ID projection, owner binding, lookup count and error ordering, logical length, trailer publication, checksum validation and typed producer errors. Each fault failed its intended test after the same artifact passed with the selector unset.

Linux RDMA and a native KMS denial through UploadPart remain unverified.
This commit is contained in:
overtrue
2026-11-15 13:41:07 +08:00
parent 77da63397c
commit 97a7639f59
17 changed files with 1149 additions and 227 deletions
+5 -2
View File
@@ -49,7 +49,9 @@ use super::storage_api::multipart_usecase::io::{DecryptReader, EncryptReader, Ha
use super::storage_api::multipart_usecase::io::{
EtagAlgorithm, HashReader, WriteEncryption, WritePlan, compression_metadata_value,
};
use super::storage_api::multipart_usecase::object_utils::{to_s3s_etag, to_s3s_xml_etag};
#[cfg(test)]
use super::storage_api::multipart_usecase::object_utils::to_s3s_etag;
use super::storage_api::multipart_usecase::object_utils::to_s3s_xml_etag;
use super::storage_api::multipart_usecase::options::{
copy_src_opts, extract_metadata_from_mime, get_complete_multipart_upload_opts_with_replication_authorization,
get_content_sha256_with_query, get_opts, has_replication_retention_update, namespace_reserved_user_metadata,
@@ -73,7 +75,6 @@ use super::storage_api::multipart_usecase::{
StorageObjectInfo as ObjectInfo, StorageObjectOptions as ObjectOptions, StoragePutObjReader as PutObjReader,
validate_new_object_key,
};
use super::trailer_adapter::trailer_source;
use crate::app::object::{
ConcurrencyManager, ForegroundWriteAdmission, get_concurrency_manager, put_object_body_read_timeout,
reject_non_wildcard_write_if_none_match, reject_oversize_single_upload, reject_veeam_sosapi_mutation,
@@ -134,6 +135,8 @@ mod list;
mod test_support;
mod upload_part;
mod upload_part_copy;
mod upload_values;
pub(crate) use upload_values::{UploadPartTarget, UploadPartWriteInput, UploadPartWriteOutput};
#[cfg(test)]
fn merge_part_encryption_metadata(
+187 -154
View File
@@ -15,17 +15,43 @@
//! UploadPart.
use super::*;
use crate::app::storage_api::s3::dto::{
ChecksumAlgorithm, ServerSideEncryption, StreamingBlob, UploadPartInput, UploadPartOutput,
};
use crate::app::storage_api::s3::stream::ByteStream;
use crate::app::storage_api::s3::{S3Error, S3ErrorCode, S3Request, S3Response, S3Result, s3_error};
use crate::app::RequestEnvelope;
use crate::app::object::BodyChunks;
use crate::app::object::request_body::{DemandReader, UploadChunks, guard_native_upload_progress};
use crate::app::storage_api::multipart_usecase::access::AuthorizationRequest;
use crate::app::storage_api::multipart_usecase::contract::multipart::MultipartInfo;
use http_body::Body as _;
use rustfs_gateway_stream::Body;
#[cfg(any(feature = "gateway-stack", test))]
use rustfs_gateway_types::{RecordedUpload, resolve_upload_async};
use rustfs_s3_types::{S3Error, S3ErrorCode, S3Result, s3_error};
fn resolve_upload_part_size(content_length: Option<i64>, body: Option<&StreamingBlob>) -> rustfs_s3_types::S3Result<Option<i64>> {
#[cfg(any(feature = "gateway-stack", test))]
struct PartUpload(MultipartInfo);
#[cfg(any(feature = "gateway-stack", test))]
impl RecordedUpload for PartUpload {
fn bucket(&self) -> &str {
&self.0.bucket
}
fn key(&self) -> &str {
&self.0.object
}
}
async fn lookup_part_upload(store: &ECStore, bucket: &str, key: &str, id: &str, opts: &ObjectOptions) -> S3Result<MultipartInfo> {
let result = store.get_multipart_info(bucket, key, id, opts).await;
#[cfg(test)]
let result = tests::observe_lookup(result);
result.map_err(|error| multipart_upload_api_error(error).into())
}
fn resolve_upload_part_size(content_length: Option<i64>, body: Option<&Body>) -> rustfs_s3_types::S3Result<Option<i64>> {
if let Some(length) = content_length {
return Ok(Some(length));
}
body.and_then(|body| body.remaining_length().exact())
body.and_then(|body| body.size_hint().exact())
.map(i64::try_from)
.transpose()
.map_err(|_| rustfs_s3_types::s3_error!(UnexpectedContent))
@@ -59,28 +85,29 @@ fn passthrough_part_actual_size(headers: &HeaderMap) -> Option<i64> {
// Hoisted values are signed query parameters, not HTTP headers. Normalize only
// after access control has verified the presigned request.
fn normalize_presigned_part_checksums(req: &mut S3Request<UploadPartInput>) -> S3Result<()> {
if req.extensions.get::<VerifiedPresignedRequest>().is_none() {
fn normalize_presigned_part_checksums(req: &mut RequestEnvelope, input: &mut UploadPartWriteInput) -> S3Result<()> {
if req.verified_presigned().is_none() {
return Ok(());
}
let Some(query) = req.uri.query() else {
let uri = req.uri().clone();
let Some(query) = uri.query() else {
return Ok(());
};
let mut seen = std::collections::HashSet::new();
for (name, value) in url::form_urlencoded::parse(query.as_bytes()) {
let field = match name.as_ref() {
"x-amz-checksum-crc32" => Some(&mut req.input.checksum_crc32),
"x-amz-checksum-crc32c" => Some(&mut req.input.checksum_crc32c),
"x-amz-checksum-crc64nvme" => Some(&mut req.input.checksum_crc64nvme),
"x-amz-checksum-sha1" => Some(&mut req.input.checksum_sha1),
"x-amz-checksum-sha256" => Some(&mut req.input.checksum_sha256),
"x-amz-checksum-crc32" => Some(&mut input.checksum_crc32),
"x-amz-checksum-crc32c" => Some(&mut input.checksum_crc32c),
"x-amz-checksum-crc64nvme" => Some(&mut input.checksum_crc64nvme),
"x-amz-checksum-sha1" => Some(&mut input.checksum_sha1),
"x-amz-checksum-sha256" => Some(&mut input.checksum_sha256),
"x-amz-sdk-checksum-algorithm" => None,
_ => continue,
};
if !seen.insert(name.clone()) {
return Err(S3Error::with_message(S3ErrorCode::InvalidRequest, "Duplicate checksum query parameter"));
}
if let Some(header) = req.headers.get(name.as_ref())
if let Some(header) = req.headers().get(name.as_ref())
&& header.as_bytes() != value.as_bytes()
{
return Err(S3Error::with_message(
@@ -90,7 +117,7 @@ fn normalize_presigned_part_checksums(req: &mut S3Request<UploadPartInput>) -> S
}
let header = http::HeaderValue::from_str(&value)
.map_err(|_| S3Error::with_message(S3ErrorCode::InvalidArgument, "Invalid checksum query parameter"))?;
req.headers.insert(
req.headers_mut().insert(
http::header::HeaderName::from_bytes(name.as_bytes())
.map_err(|_| S3Error::with_message(S3ErrorCode::InvalidArgument, "Invalid checksum query parameter"))?,
header,
@@ -98,59 +125,62 @@ fn normalize_presigned_part_checksums(req: &mut S3Request<UploadPartInput>) -> S
if let Some(field) = field {
*field = Some(value.into_owned());
} else {
req.input.checksum_algorithm = Some(ChecksumAlgorithm::from(value.into_owned()));
input.checksum_algorithm = Some(value.into_owned());
}
}
Ok(())
}
impl DefaultMultipartUsecase {
#[instrument(level = "debug", skip(self, req))]
#[instrument(name = "execute_upload_part", level = "debug", skip(self, req, input))]
#[hotpath::measure(impl_type = "MultipartUsecase")]
pub async fn execute_upload_part(&self, mut req: S3Request<UploadPartInput>) -> S3Result<S3Response<UploadPartOutput>> {
pub(crate) async fn execute_upload_part_owned(
&self,
mut req: RequestEnvelope,
mut input: UploadPartWriteInput,
) -> S3Result<UploadPartWriteOutput> {
reject_presigned_multipart_max_total_object_size_for_other_operation(
&req.headers,
req.uri.query(),
req.extensions.get::<VerifiedSigV4Request>().is_some(),
req.headers(),
req.uri().query(),
req.verified_sigv4().is_some(),
)?;
reject_presigned_put_max_content_length_for_other_operation(
&req.headers,
req.uri.query(),
req.extensions.get::<VerifiedPresignedRequest>().is_some(),
req.headers(),
req.uri().query(),
req.verified_presigned().is_some(),
)?;
normalize_presigned_part_checksums(&mut req)?;
normalize_presigned_part_checksums(&mut req, &mut input)?;
let (bucket, key) = input.target.resource();
let mut opts = ObjectOptions::default();
apply_bucket_generation_guard(&req, &req.input.bucket, &mut opts)?;
apply_bucket_generation_guard(&req, bucket, &mut opts)?;
let part_principal = SseKmsPrincipal::from_request(&req);
let input = req.input;
let UploadPartInput {
let UploadPartWriteInput {
body,
bucket,
key,
upload_id,
frame_trailers,
trailing_headers,
part_number,
content_length,
sse_customer_algorithm,
sse_customer_key,
sse_customer_key_md5,
// content_md5,
..
} = input;
let part_id = parse_upload_part_number(part_number)?;
reject_veeam_sosapi_mutation(|| self.object_store(), &bucket, &key).await?;
validate_table_catalog_object_mutation(&bucket, &key).await?;
reject_veeam_sosapi_mutation(|| self.object_store(), bucket, key).await?;
validate_table_catalog_object_mutation(bucket, key).await?;
// cuObject S3-over-RDMA UploadPart: an empty HTTP body plus
// `x-amz-rdma-token` means the part bytes are read from the client's
// registered buffer (see `object::cuobject::rdma_put_body`).
#[cfg(all(any(feature = "cuobject", feature = "nixl"), target_os = "linux"))]
let (body, content_length, rdma_put) = match crate::app::object::cuobject::rdma_token_for_request(&req.headers) {
let (body, content_length, rdma_put) = match crate::app::object::cuobject::rdma_token_for_request(req.headers()) {
Some(token) if content_length.unwrap_or(0) == 0 => {
let (body, total) = crate::app::object::cuobject::rdma_put_body(&bucket, &key, &token, true)?;
let (body, total) =
crate::app::object::cuobject::rdma_put_body(bucket, key, &token, true).map_err(S3Error::from)?;
let total = i64::try_from(total).map_err(|_| S3Error::new(S3ErrorCode::EntityTooLarge))?;
(Some(body), Some(total), Some(total))
(Some(crate::app::object::legacy_put_body(body)), Some(total), Some(total))
}
_ => (body, content_length, None),
};
@@ -165,10 +195,29 @@ impl DefaultMultipartUsecase {
return Err(S3Error::with_message(S3ErrorCode::InternalError, "Not init".to_string()));
};
let multipart_info_started = rustfs_io_metrics::put_stage_timer();
let fi = store
.get_multipart_info(&bucket, &key, &upload_id, &opts)
.await
.map_err(multipart_upload_api_error)?;
let (upload_id, fi) = match &input.target {
UploadPartTarget::Legacy { upload_id, .. } => {
let info = lookup_part_upload(&store, bucket, key, upload_id, &opts).await?;
(upload_id.clone(), info)
}
#[cfg(any(feature = "gateway-stack", test))]
UploadPartTarget::Gateway {
bucket: bucket_name,
key: object_key,
upload_id,
} => {
let lookup_store = &store;
let lookup_opts = &opts;
let (id, upload) = resolve_upload_async(upload_id, bucket_name, object_key, |id| async move {
lookup_part_upload(lookup_store, bucket, key, id, lookup_opts)
.await
.map(|info| Some(PartUpload(info)))
})
.await?
.map_err(|rejection| S3Error::with_message(S3ErrorCode::NoSuchUpload, rejection.reason()))?;
(id.id().to_owned(), upload.0)
}
};
rustfs_io_metrics::record_put_object_stage_duration_from("multipart_get_info", multipart_info_started);
authorize_sse_kms_multipart_write(part_principal.as_ref(), &fi.user_defined).await?;
let max_total_object_size = multipart_max_total_object_size(&fi.user_defined)?;
@@ -197,26 +246,16 @@ impl DefaultMultipartUsecase {
));
}
};
let request_id = req
.extensions
.get::<super::super::storage_api::multipart_usecase::request_context::RequestContext>()
.map(|ctx| ctx.request_id.as_str())
.unwrap_or_default();
let (chunks, _) = BodyChunks::with_trailers(body_stream, frame_trailers);
let body_stream: UploadChunks = Box::new(chunks);
let request_id = req.request_context().map(|ctx| ctx.request_id.as_str()).unwrap_or_default();
let timeout = upload_part_body_read_timeout(put_object_body_read_timeout(), max_total_object_size.is_some());
let (body_stream, progress_control) =
if let Some(control) = req.extensions.get::<super::super::object::request_body::BodyReadControl>() {
control.activate_upload(timeout, &bucket, &key, request_id, size.max(0) as u64);
(body_stream, control.clone())
} else {
super::super::object::request_body::guard_upload_progress(
body_stream,
timeout,
&bucket,
&key,
request_id,
size.max(0) as u64,
)
};
let (body_stream, progress_control) = if let Some(control) = req.body_read_control() {
control.activate_upload(timeout, bucket, key, request_id, size.max(0) as u64);
(body_stream, control.clone())
} else {
guard_native_upload_progress(body_stream, timeout, bucket, key, request_id, size.max(0) as u64)
};
let ingress_stage_start = rustfs_io_metrics::put_stage_metrics_enabled().then(std::time::Instant::now);
@@ -238,7 +277,7 @@ impl DefaultMultipartUsecase {
&& rustfs_utils::http::contains_key_str(&fi.user_defined, rustfs_utils::http::SUFFIX_COMPRESSION);
let actual_size = if preserve_ciphertext {
passthrough_part_actual_size(&req.headers).unwrap_or(size)
passthrough_part_actual_size(req.headers()).unwrap_or(size)
} else {
size
};
@@ -252,7 +291,7 @@ impl DefaultMultipartUsecase {
None
};
let mut sha256hex = get_content_sha256_with_query(&req.headers, req.uri.query());
let mut sha256hex = get_content_sha256_with_query(req.headers(), req.uri().query());
let mut write_plan = WritePlan::new();
let etag_algorithm = multipart_etag_algorithm(&fi.user_defined);
@@ -269,7 +308,7 @@ impl DefaultMultipartUsecase {
)
.map_err(ApiError::from)?;
if let Err(err) = hrd.add_checksum(&req.headers, trailer_source(req.trailing_headers.clone()), false) {
if let Err(err) = hrd.add_checksum(req.headers(), trailing_headers.clone(), false) {
return Err(ApiError::from(err).into());
}
@@ -281,7 +320,7 @@ impl DefaultMultipartUsecase {
.map_err(ApiError::from)?
};
if let Err(err) = reader.add_checksum(&req.headers, trailer_source(req.trailing_headers.clone()), size < 0) {
if let Err(err) = reader.add_checksum(req.headers(), trailing_headers.clone(), size < 0) {
return Err(ApiError::from(err).into());
}
opts.want_checksum = reader.checksum();
@@ -297,7 +336,7 @@ impl DefaultMultipartUsecase {
} else {
let sse = fi.user_defined.get("x-amz-server-side-encryption").cloned();
let key_id = match sse.as_ref() {
Some(sse) if sse.as_str() == ServerSideEncryption::AWS_KMS => fi
Some(sse) if sse.as_str() == "aws:kms" => fi
.user_defined
.get("x-amz-server-side-encryption-aws-kms-key-id")
.map(|s| s.to_string()),
@@ -307,8 +346,8 @@ impl DefaultMultipartUsecase {
};
if !preserve_ciphertext {
EncryptionRequest {
bucket: &bucket,
key: &key,
bucket,
key,
server_side_encryption: server_side_encryption.clone(),
ssekms_key_id: ssekms_key_id.clone(),
ssekms_context: None,
@@ -322,8 +361,8 @@ impl DefaultMultipartUsecase {
}
let (requested_sse, requested_kms_key_id) = if has_ssec {
let ssec_material = sse_decryption(DecryptionRequest {
bucket: &bucket,
key: &key,
bucket,
key,
metadata: &fi.user_defined,
sse_customer_key: sse_customer_key.as_ref(),
sse_customer_key_md5: sse_customer_key_md5.as_ref(),
@@ -343,8 +382,8 @@ impl DefaultMultipartUsecase {
// Reuse the stored envelope after authorizing this caller against its
// pinned destination key above.
let managed_material = sse_decryption(DecryptionRequest {
bucket: &bucket,
key: &key,
bucket,
key,
metadata: &fi.user_defined,
sse_customer_key: None,
sse_customer_key_md5: None,
@@ -369,8 +408,7 @@ impl DefaultMultipartUsecase {
{
use rustfs_rio::HashReaderMut;
let inner = reader.take_inner();
reader.inner =
rustfs_rio::boxed_reader(super::super::object::request_body::DemandReader::new(inner, progress_control));
reader.inner = rustfs_rio::boxed_reader(DemandReader::new(inner, progress_control));
}
let mut reader = PutObjReader::new(reader);
@@ -385,7 +423,7 @@ impl DefaultMultipartUsecase {
let _upload_part_admission = upload_part_admission;
let store_write_started = rustfs_io_metrics::put_stage_timer();
let result = store
.put_object_part(&bucket, &key, &upload_id, part_id, &mut reader, &opts)
.put_object_part(bucket, key, &upload_id, part_id, &mut reader, &opts)
.await;
rustfs_io_metrics::record_put_object_stage_duration_from("multipart_store_write", store_write_started);
drop(_upload_part_admission);
@@ -397,30 +435,21 @@ impl DefaultMultipartUsecase {
let mut checksum_sha256 = input.checksum_sha256;
let mut checksum_crc64nvme = input.checksum_crc64nvme;
if let Some(alg) = &input.checksum_algorithm
&& let Some(Some(checksum_str)) = req.trailing_headers.as_ref().map(|trailer| {
let key = match alg.as_str() {
ChecksumAlgorithm::CRC32 => rustfs_rio::ChecksumType::CRC32.key(),
ChecksumAlgorithm::CRC32C => rustfs_rio::ChecksumType::CRC32C.key(),
ChecksumAlgorithm::SHA1 => rustfs_rio::ChecksumType::SHA1.key(),
ChecksumAlgorithm::SHA256 => rustfs_rio::ChecksumType::SHA256.key(),
ChecksumAlgorithm::CRC64NVME => rustfs_rio::ChecksumType::CRC64_NVME.key(),
_ => return None,
};
trailer.read(|headers| {
headers
.get(key.unwrap_or_default())
.and_then(|value| value.to_str().ok().map(|s| s.to_string()))
})
})
{
match alg.as_str() {
ChecksumAlgorithm::CRC32 => checksum_crc32 = checksum_str,
ChecksumAlgorithm::CRC32C => checksum_crc32c = checksum_str,
ChecksumAlgorithm::SHA1 => checksum_sha1 = checksum_str,
ChecksumAlgorithm::SHA256 => checksum_sha256 = checksum_str,
ChecksumAlgorithm::CRC64NVME => checksum_crc64nvme = checksum_str,
_ => (),
if let (Some(algorithm), Some(trailers)) = (input.checksum_algorithm.as_deref(), trailing_headers) {
let echo = match algorithm {
"CRC32" => Some(("x-amz-checksum-crc32", &mut checksum_crc32)),
"CRC32C" => Some(("x-amz-checksum-crc32c", &mut checksum_crc32c)),
"SHA1" => Some(("x-amz-checksum-sha1", &mut checksum_sha1)),
"SHA256" => Some(("x-amz-checksum-sha256", &mut checksum_sha256)),
"CRC64NVME" => Some(("x-amz-checksum-crc64nvme", &mut checksum_crc64nvme)),
_ => None,
};
if let Some((name, field)) = echo {
match trailers.lookup(name) {
rustfs_rio::TrailerValue::Present(value) => *field = Some(value),
rustfs_rio::TrailerValue::Missing => *field = None,
rustfs_rio::TrailerValue::Pending => {}
}
}
}
@@ -441,8 +470,10 @@ impl DefaultMultipartUsecase {
.push((crate::app::object::cuobject::RDMA_BYTES_TRANSFERRED_HEADER, moved.to_string()));
}
let output = UploadPartOutput {
server_side_encryption: requested_sse.map(ServerSideEncryption::from),
let mut headers = HeaderMap::new();
crate::app::object_usecase::inject_additional_checksum_headers(&mut headers, &upload_part_extra_checksum_headers);
Ok(UploadPartWriteOutput {
server_side_encryption: requested_sse,
ssekms_key_id: requested_kms_key_id,
sse_customer_algorithm,
sse_customer_key_md5,
@@ -451,16 +482,9 @@ impl DefaultMultipartUsecase {
checksum_sha1,
checksum_sha256,
checksum_crc64nvme,
e_tag: info.etag.map(|etag| to_s3s_etag(&etag)),
..Default::default()
};
let mut response = S3Response::new(output);
crate::app::object_usecase::inject_additional_checksum_headers(
&mut response.headers,
&upload_part_extra_checksum_headers,
);
Ok(response)
e_tag: info.etag,
headers,
})
}
}
@@ -468,12 +492,15 @@ impl DefaultMultipartUsecase {
mod tests {
use super::super::test_support::*;
use super::*;
use crate::app::object::legacy_put_body;
use crate::app::storage_api::s3::S3ErrorCode as LegacyErrorCode;
use crate::app::storage_api::s3::dto::{StreamingBlob, UploadPartInput};
fn presigned_checksum_request(query: &str) -> S3Request<UploadPartInput> {
fn presigned_checksum_request(query: &str) -> (RequestEnvelope, UploadPartWriteInput) {
let mut req = build_request(UploadPartInput::default(), Method::PUT);
req.uri = format!("/bucket/object?{query}").parse().unwrap();
req.extensions.insert(VerifiedPresignedRequest);
req
(RequestEnvelope::from_s3s(&req), UploadPartWriteInput::from_legacy(req.input, None))
}
#[tokio::test]
@@ -483,15 +510,12 @@ mod tests {
let query =
"x-amz-checksum-sha256=ungWv48Bz%2BpBQUDeXa4iI7ADYaOWF3qctBD%2FYfIAFa0%3D&x-amz-sdk-checksum-algorithm=SHA256";
for payload in [b"abc", b"abd"] {
let mut req = presigned_checksum_request(query);
normalize_presigned_part_checksums(&mut req).unwrap();
assert_eq!(req.input.checksum_sha256.as_deref(), Some(checksum));
assert_eq!(
req.input.checksum_algorithm.as_ref().map(|algorithm| algorithm.as_str()),
Some(ChecksumAlgorithm::SHA256)
);
let (mut req, mut input) = presigned_checksum_request(query);
normalize_presigned_part_checksums(&mut req, &mut input).unwrap();
assert_eq!(input.checksum_sha256.as_deref(), Some(checksum));
assert_eq!(input.checksum_algorithm.as_deref(), Some("SHA256"));
let mut reader = HashReader::from_stream(Cursor::new(payload), 3, 3, None, None, false).unwrap();
reader.add_checksum(&req.headers, None, false).unwrap();
reader.add_checksum(req.headers(), None, false).unwrap();
assert_eq!(reader.content_crc_type(), Some(rustfs_rio::ChecksumType::SHA256));
let mut bytes = Vec::new();
let result = reader.read_to_end(&mut bytes).await;
@@ -508,11 +532,12 @@ mod tests {
#[tokio::test]
async fn normalize_presigned_part_checksums_accepts_sdk_crc32_default() {
let mut req = presigned_checksum_request("x-amz-checksum-crc32=y%2FQ5Jg%3D%3D&x-amz-sdk-checksum-algorithm=CRC32");
normalize_presigned_part_checksums(&mut req).unwrap();
assert_eq!(req.input.checksum_crc32.as_deref(), Some("y/Q5Jg=="));
let (mut req, mut input) =
presigned_checksum_request("x-amz-checksum-crc32=y%2FQ5Jg%3D%3D&x-amz-sdk-checksum-algorithm=CRC32");
normalize_presigned_part_checksums(&mut req, &mut input).unwrap();
assert_eq!(input.checksum_crc32.as_deref(), Some("y/Q5Jg=="));
let mut reader = HashReader::from_stream(Cursor::new(b"123456789"), 9, 9, None, None, false).unwrap();
reader.add_checksum(&req.headers, None, false).unwrap();
reader.add_checksum(req.headers(), None, false).unwrap();
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await.unwrap();
assert_eq!(bytes, b"123456789");
@@ -525,44 +550,47 @@ mod tests {
"x-amz-checksum-sha256=one&x-amz-checksum-sha256=two",
"x-amz-sdk-checksum-algorithm=SHA256&x-amz-sdk-checksum-algorithm=CRC32",
] {
let mut req = presigned_checksum_request(query);
let (mut req, mut input) = presigned_checksum_request(query);
assert_eq!(
*normalize_presigned_part_checksums(&mut req).unwrap_err().code(),
*normalize_presigned_part_checksums(&mut req, &mut input).unwrap_err().code(),
S3ErrorCode::InvalidRequest
);
}
let mut req = presigned_checksum_request("x-amz-checksum-sha256=one");
req.headers.insert("x-amz-checksum-sha256", HeaderValue::from_static("two"));
let (mut req, mut input) = presigned_checksum_request("x-amz-checksum-sha256=one");
req.headers_mut()
.insert("x-amz-checksum-sha256", HeaderValue::from_static("two"));
assert_eq!(
*normalize_presigned_part_checksums(&mut req).unwrap_err().code(),
*normalize_presigned_part_checksums(&mut req, &mut input).unwrap_err().code(),
S3ErrorCode::InvalidRequest
);
}
#[test]
fn normalize_presigned_part_checksums_preserves_headers_and_rejects_invalid_values() {
let mut req = presigned_checksum_request("partNumber=1&uploadId=test");
req.input.checksum_sha256 = Some("existing".to_owned());
req.headers
let (mut req, mut input) = presigned_checksum_request("partNumber=1&uploadId=test");
input.checksum_sha256 = Some("existing".to_owned());
req.headers_mut()
.insert("x-amz-checksum-sha256", HeaderValue::from_static("existing"));
normalize_presigned_part_checksums(&mut req).unwrap();
assert_eq!(req.input.checksum_sha256.as_deref(), Some("existing"));
assert_eq!(req.headers["x-amz-checksum-sha256"], "existing");
normalize_presigned_part_checksums(&mut req, &mut input).unwrap();
assert_eq!(input.checksum_sha256.as_deref(), Some("existing"));
assert_eq!(req.headers()["x-amz-checksum-sha256"], "existing");
let mut req = presigned_checksum_request("x-amz-checksum-sha256=%0D%0A");
let (mut req, mut input) = presigned_checksum_request("x-amz-checksum-sha256=%0D%0A");
assert_eq!(
*normalize_presigned_part_checksums(&mut req).unwrap_err().code(),
*normalize_presigned_part_checksums(&mut req, &mut input).unwrap_err().code(),
S3ErrorCode::InvalidArgument
);
}
#[test]
fn normalize_presigned_part_checksums_requires_verified_presign() {
let mut req = presigned_checksum_request("x-amz-checksum-sha256=one");
req.extensions.remove::<VerifiedPresignedRequest>();
normalize_presigned_part_checksums(&mut req).unwrap();
assert!(req.input.checksum_sha256.is_none());
assert!(req.headers.is_empty());
let mut legacy = build_request(UploadPartInput::default(), Method::PUT);
legacy.uri = "/bucket/object?x-amz-checksum-sha256=one".parse().unwrap();
let mut req = RequestEnvelope::from_s3s(&legacy);
let mut input = UploadPartWriteInput::from_legacy(legacy.input, None);
normalize_presigned_part_checksums(&mut req, &mut input).unwrap();
assert!(input.checksum_sha256.is_none());
assert!(req.headers().is_empty());
}
#[test]
@@ -570,12 +598,15 @@ mod tests {
let result: rustfs_s3_types::S3Result<Option<i64>> = resolve_upload_part_size(Some(5242880), None);
assert_eq!(result.expect("DTO length"), Some(5242880));
assert_eq!(resolve_upload_part_size(None, None).expect("unknown length"), None);
let body = StreamingBlob::from(Bytes::from_static(b"abc"));
let body = Body::from(Bytes::from_static(b"abc"));
assert_eq!(resolve_upload_part_size(None, Some(&body)).expect("exact bytes"), Some(3));
assert_eq!(resolve_upload_part_size(Some(0), Some(&body)).expect("explicit length wins"), Some(0));
let body = StreamingBlob::wrap(futures::stream::iter([Ok::<_, std::io::Error>(Bytes::from_static(b"abc"))]));
assert_eq!(futures::Stream::size_hint(&body), (1, Some(1)), "the stream knows its item count");
assert_eq!(resolve_upload_part_size(None, Some(&body)).expect("unknown byte length"), None);
assert_eq!(
resolve_upload_part_size(None, Some(&legacy_put_body(body))).expect("unknown byte length"),
None
);
}
#[test]
@@ -621,7 +652,7 @@ mod tests {
let req = build_request(input, Method::PUT);
let err = make_usecase().execute_upload_part(req).await.unwrap_err();
assert_eq!(err.code(), &S3ErrorCode::IncompleteBody);
assert_eq!(err.code(), &LegacyErrorCode::IncompleteBody);
}
/// issue #7596: a part whose declared length exceeds the 5 GiB
@@ -646,10 +677,10 @@ mod tests {
let err = make_usecase().execute_upload_part(req).await.unwrap_err();
if expect_too_large {
assert_eq!(err.code(), &S3ErrorCode::EntityTooLarge, "declared {declared}");
assert_eq!(err.code(), &LegacyErrorCode::EntityTooLarge, "declared {declared}");
assert_eq!(polls.load(std::sync::atomic::Ordering::SeqCst), 0, "body must not be polled");
} else {
assert_ne!(err.code(), &S3ErrorCode::EntityTooLarge, "declared {declared} must pass admission");
assert_ne!(err.code(), &LegacyErrorCode::EntityTooLarge, "declared {declared} must pass admission");
}
}
}
@@ -667,7 +698,7 @@ mod tests {
let req = build_request(input, Method::PUT);
let err = make_usecase().execute_upload_part(req).await.unwrap_err();
assert_eq!(err.code(), &S3ErrorCode::InvalidArgument);
assert_eq!(err.code(), &LegacyErrorCode::InvalidArgument);
assert_eq!(err.message(), Some("partNumber must be between 1 and 10000"));
}
}
@@ -734,9 +765,9 @@ mod tests {
assert_eq!(
error.code(),
&if capped || content_length.is_some() {
S3ErrorCode::UnexpectedContent
LegacyErrorCode::UnexpectedContent
} else {
S3ErrorCode::MissingContentLength
LegacyErrorCode::MissingContentLength
}
);
assert_eq!(polls.load(std::sync::atomic::Ordering::Relaxed), 0);
@@ -796,7 +827,7 @@ mod tests {
));
control.advance_wait_for_test(Duration::from_secs(60));
sender.send(Ok(Frame::data(Bytes::from_static(b"x")))).unwrap();
assert_eq!(owner.await.unwrap().unwrap_err().code(), &S3ErrorCode::RequestTimeout);
assert_eq!(owner.await.unwrap().unwrap_err().code(), &LegacyErrorCode::RequestTimeout);
assert!(matches!(
manager.admit_put_object(65536).await.unwrap(),
ForegroundWriteAdmission::Admitted(_)
@@ -852,9 +883,11 @@ mod tests {
.execute_upload_part(build_request(input, Method::PUT))
.await
.expect_err("full foreground write admission should reject before body ingest");
assert_eq!(err.code(), &S3ErrorCode::SlowDown);
assert_eq!(err.code(), &LegacyErrorCode::SlowDown);
drop(held);
}
mod body_read_tests;
mod claim_tests;
pub(super) use claim_tests::observe_lookup;
}
@@ -0,0 +1,127 @@
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Upload target ownership and refusal order against real multipart state.
use super::*;
use crate::app::storage_api::test::contract::bucket::{BucketOperations as _, MakeBucketOptions};
use rustfs_gateway_types::{BucketName, ObjectKey, UploadIdClaim};
use std::cell::Cell;
#[derive(Clone, Copy, Debug)]
enum LookupResult {
Real,
ForeignBucket,
ForeignKey,
Outage,
}
tokio::task_local! {
static LOOKUP: (Cell<usize>, LookupResult);
}
pub(in crate::app::multipart_usecase::upload_part) fn observe_lookup(
result: Result<MultipartInfo, StorageError>,
) -> Result<MultipartInfo, StorageError> {
let Ok(mode) = LOOKUP.try_with(|(calls, mode)| {
calls.set(calls.get() + 1);
*mode
}) else {
return result;
};
let mut info = result?;
match mode {
LookupResult::Real => {}
LookupResult::ForeignBucket => info.bucket = "foreign-bucket".to_owned(),
LookupResult::ForeignKey => info.object = "foreign-key".to_owned(),
LookupResult::Outage => return Err(StorageError::ErasureReadQuorum),
}
Ok(info)
}
#[tokio::test]
#[serial_test::serial]
async fn native_claim_checks_order_ownership_and_one_active_lookup() {
let store = crate::app::gating_test_env::shared_gating_ecstore().await;
let context = crate::app::gating_test_env::shared_gating_ambient().await;
let usecase = DefaultMultipartUsecase::with_context(Some(context));
let bucket = format!("part-claim-{}", Uuid::new_v4().simple());
let key = "encoded + key/";
let opts = ObjectOptions::default();
store.make_bucket(&bucket, &MakeBucketOptions::default()).await.unwrap();
let active = store.new_multipart_upload(&bucket, key, &opts).await.unwrap().upload_id;
for (number, id, length, body, mode, calls, expected) in [
(1, active.as_str(), Some(3), true, LookupResult::Real, 1, None),
(0, "/impossible", Some(3), true, LookupResult::Real, 0, Some("InvalidArgument")),
(-1, "/impossible", Some(3), true, LookupResult::Real, 0, Some("InvalidArgument")),
(10001, "/impossible", Some(3), true, LookupResult::Real, 0, Some("InvalidArgument")),
(2, "/impossible", Some(3), false, LookupResult::Real, 0, Some("IncompleteBody")),
(2, active.as_str(), Some(3), false, LookupResult::Real, 0, Some("IncompleteBody")),
(2, "/impossible", Some(i64::MAX), true, LookupResult::Real, 0, Some("EntityTooLarge")),
(2, "", Some(3), true, LookupResult::Real, 0, Some("NoSuchUpload")),
(2, "/impossible", Some(-1), true, LookupResult::Real, 0, Some("NoSuchUpload")),
(2, "malformed", None, true, LookupResult::Real, 1, Some("NoSuchUpload")),
(2, "malformed", Some(-1), true, LookupResult::Real, 1, Some("NoSuchUpload")),
(2, active.as_str(), Some(-1), true, LookupResult::Real, 1, Some("UnexpectedContent")),
(2, active.as_str(), None, true, LookupResult::Real, 1, Some("MissingContentLength")),
(2, active.as_str(), Some(3), true, LookupResult::ForeignBucket, 1, Some("NoSuchUpload")),
(2, active.as_str(), Some(3), true, LookupResult::ForeignKey, 1, Some("NoSuchUpload")),
(2, active.as_str(), Some(3), true, LookupResult::Outage, 1, Some("ServiceUnavailable")),
(10000, active.as_str(), Some(3), true, LookupResult::Real, 1, None),
] {
let mut input = UploadPartWriteInput::from_legacy(UploadPartInput::default(), None);
input.target = UploadPartTarget::Gateway {
bucket: BucketName::new(bucket.clone()).unwrap(),
key: ObjectKey::new(key).unwrap(),
upload_id: UploadIdClaim::from_wire(id),
};
input.part_number = number;
input.content_length = length;
input.body = body.then(|| {
if length.is_none() {
legacy_put_body(StreamingBlob::wrap(futures::stream::pending::<Result<Bytes, std::io::Error>>()))
} else {
Body::from(Bytes::from_static(b"abc"))
}
});
let envelope = RequestEnvelope::from_s3s(&build_request((), Method::PUT));
LOOKUP
.scope((Cell::new(0), mode), async {
let result = usecase.execute_upload_part_owned(envelope, input).await;
assert_eq!(
result.as_ref().err().map(|err| err.code().as_str()),
expected,
"part {number}, lookup {mode:?}"
);
assert_eq!(LOOKUP.with(|(observed, _)| observed.get()), calls, "part {number}, lookup {mode:?}");
if let Ok(output) = result {
assert_eq!(output.e_tag.as_deref(), Some("900150983cd24fb0d6963f7d28e17f72"));
}
})
.await;
}
let parts = store
.list_object_parts(&bucket, key, &active, None, 20, &opts)
.await
.unwrap()
.parts;
let parts: Vec<_> = parts
.iter()
.map(|part| (part.part_num, part.size, part.etag.as_deref()))
.collect();
let etag = Some("900150983cd24fb0d6963f7d28e17f72");
assert_eq!(parts, [(1, 3, etag), (10000, 3, etag)]);
}
@@ -0,0 +1,79 @@
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Owned UploadPart values projected by each S3 edge.
use crate::app::object::FrameTrailers;
use rustfs_gateway_stream::Body;
#[cfg(any(feature = "gateway-stack", test))]
use rustfs_gateway_types::{BucketName, ObjectKey, UploadIdClaim};
use rustfs_rio::SharedTrailerSource;
use std::sync::Arc;
pub(crate) enum UploadPartTarget {
Legacy {
bucket: String,
key: String,
upload_id: String,
},
#[cfg(any(feature = "gateway-stack", test))]
Gateway {
bucket: BucketName,
key: ObjectKey,
upload_id: UploadIdClaim,
},
}
impl UploadPartTarget {
pub(super) fn resource(&self) -> (&str, &str) {
match self {
Self::Legacy { bucket, key, .. } => (bucket, key),
#[cfg(any(feature = "gateway-stack", test))]
Self::Gateway { bucket, key, .. } => (bucket.as_str(), key.as_str()),
}
}
}
pub(crate) struct UploadPartWriteInput {
pub(crate) target: UploadPartTarget,
pub(crate) part_number: i32,
pub(crate) content_length: Option<i64>,
pub(crate) body: Option<Body>,
pub(crate) trailing_headers: Option<SharedTrailerSource>,
pub(crate) frame_trailers: Option<Arc<FrameTrailers>>,
pub(crate) content_md5: Option<String>,
pub(crate) checksum_algorithm: Option<String>,
pub(crate) checksum_crc32: Option<String>,
pub(crate) checksum_crc32c: Option<String>,
pub(crate) checksum_crc64nvme: Option<String>,
pub(crate) checksum_sha1: Option<String>,
pub(crate) checksum_sha256: Option<String>,
pub(crate) sse_customer_algorithm: Option<String>,
pub(crate) sse_customer_key: Option<String>,
pub(crate) sse_customer_key_md5: Option<String>,
}
pub(crate) struct UploadPartWriteOutput {
pub(crate) e_tag: Option<String>,
pub(crate) checksum_crc32: Option<String>,
pub(crate) checksum_crc32c: Option<String>,
pub(crate) checksum_crc64nvme: Option<String>,
pub(crate) checksum_sha1: Option<String>,
pub(crate) checksum_sha256: Option<String>,
pub(crate) server_side_encryption: Option<String>,
pub(crate) ssekms_key_id: Option<String>,
pub(crate) sse_customer_algorithm: Option<String>,
pub(crate) sse_customer_key_md5: Option<String>,
pub(crate) headers: http::HeaderMap,
}
+1
View File
@@ -224,6 +224,7 @@ mod on_demand_migration_put;
mod put;
mod put_input;
pub(crate) use native_body::from_legacy as legacy_put_body;
pub(crate) use native_body::{BodyChunks, FrameTrailers};
pub(crate) use put_input::{PutObjectWriteInput, PutObjectWriteOutput};
pub(crate) mod request_body;
mod restore;
+2 -2
View File
@@ -108,7 +108,7 @@ impl TrailerSource for FrameTrailers {
}
/// Sync is needed only by RIO's reader trait; polling owns this mutex exclusively.
pub(super) struct BodyChunks {
pub(crate) struct BodyChunks {
body: Mutex<Body>,
trailers: Arc<FrameTrailers>,
ended: bool,
@@ -120,7 +120,7 @@ impl BodyChunks {
Self::with_trailers(body, None)
}
pub(super) fn with_trailers(body: Body, trailers: Option<Arc<FrameTrailers>>) -> (Self, SharedTrailerSource) {
pub(crate) fn with_trailers(body: Body, trailers: Option<Arc<FrameTrailers>>) -> (Self, SharedTrailerSource) {
let trailers = trailers.unwrap_or_default();
(
Self {
+8 -7
View File
@@ -17,7 +17,9 @@
//! after its input returned Pending, so the transport cannot infer read demand.
use super::{LOG_COMPONENT_APP, LOG_SUBSYSTEM_OBJECT};
use crate::app::storage_api::request_body::{ByteStream, DynByteStream, RemainingLength, StdError, StreamingBlob};
use crate::app::storage_api::request_body::StdError;
#[cfg(test)]
use crate::app::storage_api::request_body::{ByteStream, DynByteStream, RemainingLength, StreamingBlob};
use crate::error::ClientBodyReadTimeout;
use bytes::Bytes;
use futures::{Stream, StreamExt};
@@ -421,13 +423,13 @@ impl<R: TryGetIndex> TryGetIndex for DemandReader<R> {
/// Establish demand for eager reads and archive sniffing before the final
/// transformed reader exists. Pending demand is subsequently paused by the
/// outer DemandReader when a transform returns buffered output.
pub(super) type UploadChunks = Box<dyn Stream<Item = Result<Bytes, StdError>> + Send + Sync + Unpin>;
pub(crate) type UploadChunks = Box<dyn Stream<Item = Result<Bytes, StdError>> + Send + Sync + Unpin>;
pub(super) fn demand_native_upload(body: UploadChunks, control: BodyReadControl) -> UploadChunks {
Box::new(DemandStream { inner: body, control })
}
pub(super) fn guard_native_upload_progress(
pub(crate) fn guard_native_upload_progress(
body: UploadChunks,
timeout: Duration,
bucket: &str,
@@ -464,10 +466,8 @@ impl<S: Stream<Item = Result<Bytes, StdError>> + Unpin> Stream for DemandStream<
self.inner.size_hint()
}
}
/// Fallback for direct protocol callers without the raw HTTP observation.
/// HTTP uses ObservedBody because a signed chunk may not yield decoded output
/// until its full payload has arrived and its signature has been verified.
/// The final DemandReader pauses pending reads when transforms return output.
/// Supplies the read-progress guard to tests using a legacy body.
#[cfg(test)]
pub(crate) fn guard_upload_progress(
body: StreamingBlob,
timeout: Duration,
@@ -554,6 +554,7 @@ impl<S: Stream<Item = Result<Bytes, StdError>> + Unpin> Stream for UploadProgres
self.inner.size_hint()
}
}
#[cfg(test)]
impl ByteStream for UploadProgressStream<DynByteStream> {
fn remaining_length(&self) -> RemainingLength {
self.inner.remaining_length()
+3 -1
View File
@@ -28,7 +28,9 @@ pub(crate) fn EndpointServerPools(
pub(crate) mod request_body {
pub(crate) use s3s::StdError;
pub(crate) use s3s::dto::StreamingBlob;
pub(crate) use s3s::stream::{ByteStream, DynByteStream, RemainingLength};
pub(crate) use s3s::stream::ByteStream;
#[cfg(test)]
pub(crate) use s3s::stream::{DynByteStream, RemainingLength};
}
#[cfg(feature = "diagnostic-read-version-caller")]
@@ -16,16 +16,20 @@
use super::{OpRow, Served, row};
use crate::app::RequestEnvelope;
use crate::app::multipart_usecase::{DefaultMultipartUsecase, UploadPartTarget, UploadPartWriteInput, UploadPartWriteOutput};
use crate::app::object::FrameTrailers;
use crate::app::object_usecase::{DefaultObjectUsecase, PutObjectWriteInput, PutObjectWriteOutput};
use crate::runtime_sources::AppContext;
use crate::storage_api::server::http::access::access_owned::object_write::{PutObjectAuthorization, authorize_put_object};
use crate::storage_api::server::http::access::access_owned::object_write::{
PutObjectAuthorization, authorize_put_object, authorize_upload_part,
};
use rustfs_gateway::{Req, Resp, dto};
use rustfs_gateway_types::{ChecksumAlgorithm, Timestamp};
use rustfs_s3_ops::S3Operation;
use rustfs_s3_types::{S3Result, s3_error};
use std::sync::Arc;
pub(super) const ROWS: &[OpRow] = &[row::<dto::PutObject>()];
pub(super) const ROWS: &[OpRow] = &[row::<dto::PutObject>(), row::<dto::UploadPart>()];
impl Served for dto::PutObject {
const METRIC: S3Operation = S3Operation::PutObject;
@@ -61,13 +65,7 @@ impl Served for dto::PutObject {
async fn execute(context: Option<Arc<AppContext>>, envelope: RequestEnvelope, request: Req<Self>) -> S3Result<Resp<Self>> {
let mut input = native_input(request.into_input())?;
input.version_id = request_version_id(&envelope);
if let Some(declaration) = envelope.headers().get("x-amz-trailer").filter(|value| !value.is_empty()) {
// The gateway verifies the declared trailer independently of the SDK algorithm.
let algorithm = declaration
.to_str()
.ok()
.and_then(|name| ChecksumAlgorithm::from_header_name(name.trim_matches([' ', '\t'])))
.ok_or_else(|| s3_error!(InvalidRequest, "Invalid checksum trailer declaration"))?;
if let Some(algorithm) = trailer_checksum_algorithm(&envelope)? {
input.checksum_algorithm = Some(algorithm.wire_name().to_owned());
input.capture_frame_trailers();
}
@@ -78,6 +76,110 @@ impl Served for dto::PutObject {
}
}
impl Served for dto::UploadPart {
const METRIC: S3Operation = S3Operation::UploadPart;
fn authorize(envelope: &mut RequestEnvelope, request: &Req<Self>) -> impl std::future::Future<Output = S3Result<()>> + Send {
let input = request.input();
let bucket = input.bucket.as_str().to_owned();
let key = input.key.as_str().to_owned();
async move {
let server = envelope
.server_context()
.cloned()
.ok_or_else(|| s3_error!(InternalError, "server context is not initialized"))?;
Ok(authorize_upload_part(envelope, &server, &bucket, &key).await?)
}
}
async fn execute(context: Option<Arc<AppContext>>, envelope: RequestEnvelope, request: Req<Self>) -> S3Result<Resp<Self>> {
let input = native_part_input(request.into_input(), trailer_checksum_algorithm(&envelope)?);
let output = DefaultMultipartUsecase::with_context(context)
.execute_upload_part_owned(envelope, input)
.await?;
native_part_output(output)
}
}
fn trailer_checksum_algorithm(envelope: &RequestEnvelope) -> S3Result<Option<ChecksumAlgorithm>> {
envelope
.headers()
.get("x-amz-trailer")
.filter(|value| !value.is_empty())
.map(|declaration| {
// The gateway verifies the declared trailer independently of the SDK algorithm.
declaration
.to_str()
.ok()
.and_then(|name| ChecksumAlgorithm::from_header_name(name.trim_matches([' ', '\t'])))
.ok_or_else(|| s3_error!(InvalidRequest, "Invalid checksum trailer declaration"))
})
.transpose()
}
fn native_part_input(input: dto::UploadPartInput, trailer_algorithm: Option<ChecksumAlgorithm>) -> UploadPartWriteInput {
let checksum = |algorithm| {
input
.checksum_spec
.as_ref()
.filter(|value| value.algorithm() == algorithm)
.map(|value| value.render_base64().to_owned())
};
let frame_trailers = trailer_algorithm.map(|_| Arc::new(FrameTrailers::default()));
UploadPartWriteInput {
checksum_crc32: checksum(ChecksumAlgorithm::Crc32),
checksum_crc32c: checksum(ChecksumAlgorithm::Crc32c),
checksum_crc64nvme: checksum(ChecksumAlgorithm::Crc64Nvme),
checksum_sha1: checksum(ChecksumAlgorithm::Sha1),
checksum_sha256: checksum(ChecksumAlgorithm::Sha256),
target: UploadPartTarget::Gateway {
bucket: input.bucket,
key: input.key,
upload_id: input.upload_id,
},
part_number: input.part_number,
content_length: Some(input.content_length),
body: input.body.map(|body| body.into_body()),
trailing_headers: frame_trailers.clone().map(|source| source as rustfs_rio::SharedTrailerSource),
frame_trailers,
content_md5: input.content_md5,
checksum_algorithm: trailer_algorithm
.map(|value| value.wire_name().to_owned())
.or_else(|| input.checksum_algorithm.map(|value| value.as_str().to_owned())),
sse_customer_algorithm: input.sse_customer_algorithm,
sse_customer_key: input.sse_customer_key.map(|value| value.expose_secret().to_owned()),
sse_customer_key_md5: input.sse_customer_key_md5,
}
}
fn native_part_output(output: UploadPartWriteOutput) -> S3Result<Resp<dto::UploadPart>> {
let mut headers = output.headers;
insert_checksum_headers(
&mut headers,
[
output.checksum_crc32,
output.checksum_crc32c,
output.checksum_sha1,
output.checksum_sha256,
output.checksum_crc64nvme,
],
)?;
let dto = dto::UploadPartOutput {
e_tag: crate::app::dto_values::stored_etag(
output
.e_tag
.as_deref()
.ok_or_else(|| s3_error!(InternalError, "UploadPart completed without an ETag"))?,
),
server_side_encryption: output.server_side_encryption.map(dto::ServerSideEncryption::custom),
sse_customer_algorithm: output.sse_customer_algorithm,
sse_customer_key_md5: output.sse_customer_key_md5,
ssekms_key_id: output.ssekms_key_id,
..Default::default()
};
Ok(Resp::new(dto).with_extra_headers(headers))
}
// PutObject's destination version is a RustFS replication extension, absent from the AWS DTO.
fn request_version_id(envelope: &RequestEnvelope) -> Option<String> {
url::form_urlencoded::parse(envelope.uri().query()?.as_bytes())
@@ -143,22 +245,16 @@ fn native_input(input: dto::PutObjectInput) -> S3Result<PutObjectWriteInput> {
fn native_output(output: PutObjectWriteOutput) -> S3Result<Resp<dto::PutObject>> {
let mut headers = output.headers;
for (name, value) in [
("x-amz-checksum-crc32", output.checksum_crc32),
("x-amz-checksum-crc32c", output.checksum_crc32c),
("x-amz-checksum-sha1", output.checksum_sha1),
("x-amz-checksum-sha256", output.checksum_sha256),
("x-amz-checksum-crc64nvme", output.checksum_crc64nvme),
] {
if let Some(value) = value {
headers.insert(
name,
value
.parse()
.map_err(|_| s3_error!(InternalError, "Invalid stored checksum"))?,
);
}
}
insert_checksum_headers(
&mut headers,
[
output.checksum_crc32,
output.checksum_crc32c,
output.checksum_sha1,
output.checksum_sha256,
output.checksum_crc64nvme,
],
)?;
let dto = dto::PutObjectOutput {
e_tag: crate::app::dto_values::stored_etag(
output
@@ -181,6 +277,29 @@ fn native_output(output: PutObjectWriteOutput) -> S3Result<Resp<dto::PutObject>>
.with_extra_headers(headers))
}
fn insert_checksum_headers(
headers: &mut http::HeaderMap,
[crc32, crc32c, sha1, sha256, crc64nvme]: [Option<String>; 5],
) -> S3Result<()> {
for (name, value) in [
("x-amz-checksum-crc32", crc32),
("x-amz-checksum-crc32c", crc32c),
("x-amz-checksum-sha1", sha1),
("x-amz-checksum-sha256", sha256),
("x-amz-checksum-crc64nvme", crc64nvme),
] {
if let Some(value) = value {
headers.insert(
name,
value
.parse()
.map_err(|_| s3_error!(InternalError, "Invalid stored checksum"))?,
);
}
}
Ok(())
}
#[cfg(test)]
#[path = "object_write_test.rs"]
mod tests;
@@ -17,9 +17,9 @@
use super::*;
use crate::runtime_sources::ServerContextSlot;
use crate::storage_api::server::http::access::test_support::{
AuthorizationRequest, BucketOperations as _, ETag, ETagCondition, FS, MakeBucketOptions, ObjectOperations as _,
ObjectOptions, PutObjectInput as LegacyPutObjectInput, ReqInfo, S3Access as _, S3Request, get_bucket_metadata,
set_bucket_metadata,
AuthorizationRequest, BucketOperations as _, CompletePart, DeleteBucketOptions, ETag, ETagCondition, FS, MakeBucketOptions,
MultipartOperations as _, ObjectIO as _, ObjectOperations as _, ObjectOptions, PutObjectInput as LegacyPutObjectInput,
ReqInfo, S3Access as _, S3Request, get_bucket_metadata, set_bucket_metadata,
};
use bytes::Bytes;
use http::{HeaderMap, HeaderValue, Method};
@@ -30,7 +30,7 @@ use std::collections::VecDeque;
use std::pin::Pin;
use std::task::{Context, Poll};
fn request(input: dto::PutObjectInput) -> Req<dto::PutObject> {
fn request<O: rustfs_gateway::Operation<DerivedResources = rustfs_gateway::NoDerived>>(input: O::Input) -> Req<O> {
let wire = WireRequest::accept(
http::Request::builder()
.uri("/")
@@ -466,3 +466,181 @@ fn native_put_response_refuses_a_missing_etag() {
};
assert_eq!(error.code().as_str(), "InternalError");
}
fn part_input(bucket: &str, upload_id: &str, part_number: i32) -> dto::UploadPartInput {
dto::UploadPartInput {
bucket: rustfs_gateway_types::BucketName::new(bucket).unwrap(),
key: rustfs_gateway_types::ObjectKey::new("allowed").unwrap(),
upload_id: rustfs_gateway_types::UploadIdClaim::from_wire(upload_id),
part_number,
content_length: 3,
body: Some(ByteStream::from_bytes(Bytes::from_static(b"abc"))),
..Default::default()
}
}
#[tokio::test]
#[serial]
async fn native_upload_part_rejects_missing_and_stale_generation() {
let (server, bucket) = fixture(&["s3:PutObject"]).await;
let store = server.object_store().unwrap();
let options = ObjectOptions::default();
let key = "allowed";
// The legitimate authorization captures an incarnation while the request is still being admitted.
let mut authorized = envelope(&server, HeaderMap::new());
let request_without_session = request::<dto::UploadPart>(part_input(&bucket, "not-yet-active", 1));
<dto::UploadPart as Served>::authorize(&mut authorized, &request_without_session)
.await
.unwrap();
let failed =
<dto::UploadPart as Served>::execute(server.app_context(), authorized.fork_for_authorization(), request_without_session)
.await
.err()
.unwrap();
assert_eq!(failed.code().as_str(), "NoSuchUpload");
store.delete_bucket(&bucket, &DeleteBucketOptions::default()).await.unwrap();
store.make_bucket(&bucket, &MakeBucketOptions::default()).await.unwrap();
let session = store.new_multipart_upload(&bucket, key, &options).await.unwrap().upload_id;
let missing = envelope(&server, HeaderMap::new());
for (envelope, code) in [(missing, "InternalError"), (authorized, "NoSuchBucket")] {
let request = request::<dto::UploadPart>(part_input(&bucket, &session, 1));
let error = <dto::UploadPart as Served>::execute(server.app_context(), envelope, request)
.await
.err()
.unwrap();
assert_eq!(error.code().as_str(), code);
assert!(
store
.list_object_parts(&bucket, key, &session, None, 20, &options)
.await
.unwrap()
.parts
.is_empty()
);
}
// The identical part is writable under a freshly captured generation.
set_policy(
&bucket,
serde_json::json!({
"Effect": "Allow",
"Principal": {"AWS": "*"},
"Action": "s3:PutObject",
"Resource": format!("arn:aws:s3:::{bucket}/{key}"),
}),
)
.await;
let req = request::<dto::UploadPart>(part_input(&bucket, &session, 1));
let mut fresh = envelope(&server, HeaderMap::new());
<dto::UploadPart as Served>::authorize(&mut fresh, &req).await.unwrap();
<dto::UploadPart as Served>::execute(server.app_context(), fresh, req)
.await
.unwrap();
let parts = store
.list_object_parts(&bucket, key, &session, None, 20, &options)
.await
.unwrap()
.parts;
assert_eq!(
parts.first().and_then(|part| part.etag.as_deref()),
Some("900150983cd24fb0d6963f7d28e17f72")
);
}
#[tokio::test]
#[serial]
async fn native_upload_part_uses_frames_and_leaves_the_prior_part_on_failure() {
use tokio::io::AsyncReadExt;
let (server, bucket) = fixture(&["s3:PutObject"]).await;
let store = server.object_store().unwrap();
let options = ObjectOptions::default();
let key = "allowed";
let upload_id = store.new_multipart_upload(&bucket, key, &options).await.unwrap().upload_id;
let etag = "900150983cd24fb0d6963f7d28e17f72";
for (label, payload, size, checksum, expected) in [
("valid", b"abc", 3, Some("NSRBwg=="), None),
("missing trailer", b"xyz", 3, None, Some("BadDigest")),
("wrong checksum", b"xyz", 3, Some("AAAAAA=="), Some("BadDigest")),
("short", b"xyz", 4, Some("6466Zw=="), Some("IncompleteBody")),
("digest error", b"xyz", 3, None, Some("BadDigest")),
("signature error", b"xyz", 3, None, Some("SignatureDoesNotMatch")),
] {
let mut headers = HeaderMap::new();
headers.insert("x-amz-trailer", HeaderValue::from_static("x-amz-checksum-crc32"));
let mut trailers = HeaderMap::new();
if let Some(value) = checksum {
trailers.insert("x-amz-checksum-crc32", HeaderValue::from_static(value));
}
let end = match label {
"digest error" => Err(StreamError::upstream(Box::new(rustfs_rio::BadDigest {
expected_md5: String::new(),
calculated_md5: String::new(),
}))),
"signature error" => Err(StreamError::upstream(Box::new(
crate::app::storage_api::s3::stream::aws_chunked_stream::AwsChunkedStreamError::SignatureMismatch,
))),
_ => Ok(PayloadRead::Eof {
trailers: TrailingHeaders::from_header_map(trailers),
}),
};
let mut input = part_input(&bucket, &upload_id, 1);
input.content_length = size;
input.body = Some(
ByteStream::new(Box::pin(Frames(VecDeque::from([
Ok(PayloadRead::Chunk(Bytes::from_static(payload))),
end,
]))))
.unwrap(),
);
// The decoded trailer declaration wins even with another SDK algorithm value.
input.checksum_algorithm = Some(dto::ChecksumAlgorithm::custom("SHA256"));
let request = request::<dto::UploadPart>(input);
let mut context = envelope(&server, headers);
<dto::UploadPart as Served>::authorize(&mut context, &request).await.unwrap();
let result = <dto::UploadPart as Served>::execute(server.app_context(), context, request).await;
assert_eq!(result.as_ref().err().map(|error| error.code().as_str()), expected, "{label}");
if let Ok(response) = result {
assert_eq!(response.output().unwrap().e_tag, crate::app::dto_values::stored_etag(etag));
let (_, status, headers) = response.into_parts();
assert_eq!(status, 200);
assert_eq!(headers["x-amz-checksum-crc32"], "NSRBwg==");
}
let parts = store
.list_object_parts(&bucket, key, &upload_id, None, 20, &options)
.await
.unwrap()
.parts;
assert_eq!(parts.len(), 1, "{label}");
assert_eq!(
(parts[0].part_num, parts[0].size, parts[0].etag.as_deref()),
(1, 3, Some(etag)),
"{label}"
);
}
store
.clone()
.complete_multipart_upload(
&bucket,
key,
&upload_id,
vec![CompletePart {
part_num: 1,
etag: Some(etag.to_owned()),
..Default::default()
}],
&options,
)
.await
.unwrap();
let mut old = store
.get_object_reader(&bucket, key, None, HeaderMap::new(), &options)
.await
.unwrap();
let mut bytes = Vec::new();
old.stream.read_to_end(&mut bytes).await.unwrap();
assert_eq!(bytes, b"abc");
}
+2 -8
View File
@@ -4393,14 +4393,8 @@ impl S3Access for FS {
/// This method returns `Ok(())` by default.
async fn upload_part(&self, req: &mut S3Request<UploadPartInput>) -> S3Result<()> {
let bucket = req.input.bucket.clone();
let bucket_generation = load_bucket_generation(self.server_ctx(), req, &bucket).await;
let req_info = ext_req_info_mut(&mut req.extensions)?;
req_info.bucket = Some(req.input.bucket.clone());
req_info.object = Some(req.input.key.clone());
authorize_staged_multipart_part(req).await?;
req.install_bucket_generation(bucket_generation?);
Ok(())
let key = req.input.key.clone();
access_owned::object_write::authorize_upload_part(req, self.server_ctx(), &bucket, &key).await
}
/// Checks whether the UploadPartCopy request has accesses to the resources.
@@ -23,10 +23,25 @@
use super::super::{
Action, ApiError, AuthorizationRequest, S3Action, S3Error, S3ErrorCode, S3Result, ServerContextSlot,
authorize_replication_only_put_headers, authorize_request, authorize_write_etag_condition, has_write_offset_bytes_header,
load_bucket_generation, missing_req_info_error, set_requested_object_lock_retain_until,
authorize_replication_only_put_headers, authorize_request, authorize_staged_multipart_part, authorize_write_etag_condition,
has_write_offset_bytes_header, load_bucket_generation, missing_req_info_error, set_requested_object_lock_retain_until,
};
pub(crate) async fn authorize_upload_part<R: AuthorizationRequest + ?Sized>(
req: &mut R,
server: &ServerContextSlot,
bucket: &str,
key: &str,
) -> S3Result<()> {
let generation = load_bucket_generation(server, req, bucket).await;
let info = req.req_info_mut().ok_or_else(missing_req_info_error)?;
info.bucket = Some(bucket.to_owned());
info.object = Some(key.to_owned());
authorize_staged_multipart_part(req).await?;
req.install_bucket_generation(generation?);
Ok(())
}
/// Request facts needed by both PutObject authorization edges.
pub(crate) struct PutObjectAuthorization<'a> {
pub(crate) bucket: &'a str,
+60 -1
View File
@@ -50,7 +50,10 @@ impl FS {
}
use crate::app::RequestEnvelope;
use crate::storage::s3_api::{DefaultObjectUsecase, PutObjectWriteInput, PutObjectWriteOutput};
use crate::storage::s3_api::{
DefaultMultipartUsecase, DefaultObjectUsecase, PutObjectWriteInput, PutObjectWriteOutput, UploadPartTarget,
UploadPartWriteInput, UploadPartWriteOutput,
};
impl PutObjectWriteInput {
pub(crate) fn from_legacy(input: PutObjectInput, trailing_headers: Option<TrailingHeaders>) -> Self {
@@ -137,6 +140,62 @@ impl DefaultObjectUsecase {
}
}
impl UploadPartWriteInput {
pub(crate) fn from_legacy(input: UploadPartInput, trailers: Option<TrailingHeaders>) -> Self {
Self {
target: UploadPartTarget::Legacy {
bucket: input.bucket,
key: input.key,
upload_id: input.upload_id,
},
part_number: input.part_number,
content_length: input.content_length,
body: input.body.map(s3_api::legacy_put_body),
trailing_headers: crate::app::trailer_adapter::trailer_source(trailers),
frame_trailers: None,
content_md5: input.content_md5,
checksum_algorithm: input.checksum_algorithm.map(|value| value.as_str().to_owned()),
checksum_crc32: input.checksum_crc32,
checksum_crc32c: input.checksum_crc32c,
checksum_crc64nvme: input.checksum_crc64nvme,
checksum_sha1: input.checksum_sha1,
checksum_sha256: input.checksum_sha256,
sse_customer_algorithm: input.sse_customer_algorithm,
sse_customer_key: input.sse_customer_key,
sse_customer_key_md5: input.sse_customer_key_md5,
}
}
}
fn upload_part_output_to_legacy(output: UploadPartWriteOutput) -> S3Response<UploadPartOutput> {
let mut response = S3Response::new(UploadPartOutput {
e_tag: output.e_tag.map(|tag| crate::storage::storage_api::to_s3s_etag(&tag)),
checksum_crc32: output.checksum_crc32,
checksum_crc32c: output.checksum_crc32c,
checksum_crc64nvme: output.checksum_crc64nvme,
checksum_sha1: output.checksum_sha1,
checksum_sha256: output.checksum_sha256,
server_side_encryption: output.server_side_encryption.map(ServerSideEncryption::from),
ssekms_key_id: output.ssekms_key_id,
sse_customer_algorithm: output.sse_customer_algorithm,
sse_customer_key_md5: output.sse_customer_key_md5,
..Default::default()
});
response.headers = output.headers;
response
}
impl DefaultMultipartUsecase {
pub async fn execute_upload_part(&self, req: S3Request<UploadPartInput>) -> S3Result<S3Response<UploadPartOutput>> {
let envelope = RequestEnvelope::from_s3s(&req);
let input = UploadPartWriteInput::from_legacy(req.input, req.trailing_headers);
self.execute_upload_part_owned(envelope, input)
.await
.map(upload_part_output_to_legacy)
.map_err(Into::into)
}
}
#[cfg(test)]
mod native_put_tests {
use super::{PutObjectInput, PutObjectWriteInput, PutObjectWriteOutput, put_output_to_legacy};
+3 -1
View File
@@ -19,7 +19,9 @@
use crate::app::bucket_usecase::DefaultBucketUsecase;
pub(crate) use crate::app::bucket_usecase::{PreparedLifecycleConfiguration, validate_lifecycle_rule_status};
use crate::app::multipart_usecase::DefaultMultipartUsecase;
pub(crate) use crate::app::multipart_usecase::{
DefaultMultipartUsecase, UploadPartTarget, UploadPartWriteInput, UploadPartWriteOutput,
};
pub(crate) use crate::app::object::reject_veeam_sosapi_mutation;
pub(crate) use crate::app::object::shared::mark_on_demand_migration_list_local_only;
pub(crate) use crate::app::object::{PutObjectWriteInput, PutObjectWriteOutput, legacy_put_body};
+5 -2
View File
@@ -245,8 +245,11 @@ pub(crate) mod server {
#[cfg(test)]
pub(crate) mod test_support {
pub(crate) use crate::storage::storage_api::access_consumer::{AuthorizationRequest, ReqInfo};
pub(crate) use crate::storage::storage_api::contract::bucket::{BucketOperations, MakeBucketOptions};
pub(crate) use crate::storage::storage_api::contract::object::ObjectOperations;
pub(crate) use crate::storage::storage_api::contract::bucket::{
BucketOperations, DeleteBucketOptions, MakeBucketOptions,
};
pub(crate) use crate::storage::storage_api::contract::multipart::{CompletePart, MultipartOperations};
pub(crate) use crate::storage::storage_api::contract::object::{ObjectIO, ObjectOperations};
pub(crate) use crate::storage::storage_api::{
FS, StorageObjectOptions as ObjectOptions, get_bucket_metadata, set_bucket_metadata,
};
@@ -12,26 +12,54 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//! PutObject refusal rows and legacy/gateway wire behavior. UploadPart remains unserved.
//! PutObject and UploadPart refusal rows and legacy/gateway wire behavior.
pub(crate) const MATRIX: &[super::MatrixRow] = &[super::MatrixRow {
operation: "PutObject",
metric: rustfs_s3_ops::S3Operation::PutObject,
has_error_body: true,
call: |client, bucket| {
Box::pin(async move {
super::outcome(
client
.put_object()
.bucket(bucket)
.key("put-matrix")
.body(aws_sdk_s3::primitives::ByteStream::from_static(b"matrix"))
.send()
.await,
)
})
pub(crate) const MATRIX: &[super::MatrixRow] = &[
super::MatrixRow {
operation: "PutObject",
metric: rustfs_s3_ops::S3Operation::PutObject,
has_error_body: true,
call: |client, bucket| {
Box::pin(async move {
super::outcome(
client
.put_object()
.bucket(bucket)
.key("put-matrix")
.body(aws_sdk_s3::primitives::ByteStream::from_static(b"matrix"))
.send()
.await,
)
})
},
},
}];
super::MatrixRow {
operation: "UploadPart",
metric: rustfs_s3_ops::S3Operation::UploadPart,
has_error_body: true,
call: |client, bucket| {
Box::pin(async move {
use rustfs_storage_api::MultipartOperations as _;
let store = super::get_global_app_context().expect("embedded context").object_store();
let upload = store
.new_multipart_upload(bucket, "part-matrix", &Default::default())
.await
.expect("matrix upload");
super::outcome(
client
.upload_part()
.bucket(bucket)
.key("part-matrix")
.upload_id(upload.upload_id)
.part_number(1)
.body(aws_sdk_s3::primitives::ByteStream::from_static(b"matrix"))
.send()
.await,
)
})
},
},
];
use hmac::{Hmac, KeyInit, Mac};
use sha2::{Digest, Sha256};
@@ -632,3 +660,6 @@ impl WireClient<'_> {
.await
}
}
#[path = "upload_part.rs"]
mod upload_part;
@@ -0,0 +1,275 @@
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Storage-backed UploadPart wire tests.
use super::{CRC32_TRAILER_HEADERS, VALID_CRC32_TRAILER, WireAnswer, WireClient};
use rustfs_storage_api::MultipartOperations;
const BUCKET: &str = "upload-part-contract";
const OTHER: &str = "upload-part-other";
const KEY: &str = "part-object";
const ETAG: &str = "5d41402abc4b2a76b9719d911017c592";
const BOTH_CHECKSUMS: &[(&str, &str)] = &[
("x-amz-checksum-crc32", "NhCmhg=="),
("x-amz-checksum-sha256", "LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ="),
];
async fn upload(
wire: &WireClient<'_>,
path: &str,
id: &str,
number: i32,
body: &[u8],
headers: &[(&str, &str)],
length: Option<usize>,
) -> WireAnswer {
wire.request("PUT", &format!("/{path}?partNumber={number}&uploadId={id}"), body, headers, length)
.await
}
fn xml_field<'a>(answer: &'a WireAnswer, name: &str) -> Option<&'a str> {
let (_, tail) = answer.body.split_once(&format!("<{name}>"))?;
let (value, _) = tail.split_once(&format!("</{name}>"))?;
Some(value)
}
const MISSING_SESSION: &str = "The specified multipart upload does not exist. The upload ID may be invalid, or the upload may have been aborted or completed.";
fn assert_reply(reply: &WireAnswer, status: u16, code: Option<&str>, message: Option<&str>) {
assert_eq!((reply.status, xml_field(reply, "Code")), (status, code), "{}", reply.body);
if let Some(message) = message {
assert_eq!(xml_field(reply, "Message"), Some(message), "{}", reply.body);
}
}
fn assert_upload_success(reply: &WireAnswer, sha256: Option<&str>) {
assert_reply(reply, 200, None, None);
assert_eq!(
reply.headers.get("etag").map(String::as_str),
Some("\"5d41402abc4b2a76b9719d911017c592\"")
);
assert_eq!(reply.headers.get("x-amz-checksum-crc32").map(String::as_str), Some("NhCmhg=="));
assert_eq!(reply.headers.get("x-amz-checksum-sha256").map(String::as_str), sha256);
}
async fn upload_part_contract(server: super::super::RustFSServer, gateway: bool) {
for bucket in [BUCKET, OTHER] {
super::super::create_bucket_in_the_store(bucket).await;
}
let endpoint = server.endpoint();
let wire = WireClient {
endpoint: &endpoint,
access_key: server.access_key(),
secret_key: server.secret_key(),
};
let store = super::super::get_global_app_context().unwrap().object_store();
let options = Default::default();
let path = format!("{BUCKET}/{KEY}");
let id = store.new_multipart_upload(BUCKET, KEY, &options).await.unwrap().upload_id;
let other_id = store.new_multipart_upload(OTHER, KEY, &options).await.unwrap().upload_id;
let denied_key_id = store
.new_multipart_upload(BUCKET, "other-key", &options)
.await
.unwrap()
.upload_id;
let aborted = store.new_multipart_upload(BUCKET, KEY, &options).await.unwrap().upload_id;
store.abort_multipart_upload(BUCKET, KEY, &aborted, &options).await.unwrap();
let both = upload(&wire, &path, &id, 1, b"hello", BOTH_CHECKSUMS, Some(5)).await;
let shipped = if gateway {
assert_reply(&both, 400, Some("InvalidRequest"), None);
assert!(
store
.list_object_parts(BUCKET, KEY, &id, None, 20, &options)
.await
.unwrap()
.parts
.is_empty()
);
upload(&wire, &path, &id, 1, b"hello", &BOTH_CHECKSUMS[..1], Some(5)).await
} else {
both
};
assert_upload_success(&shipped, (!gateway).then_some(BOTH_CHECKSUMS[1].1));
let max = upload(&wire, &path, &id, 10000, VALID_CRC32_TRAILER, &CRC32_TRAILER_HEADERS, None).await;
assert_upload_success(&max, None);
let unknown_id = store
.new_multipart_upload(BUCKET, "another-key", &options)
.await
.unwrap()
.upload_id;
let wrong_key = format!("{BUCKET}/other-key");
let wrong_bucket = format!("{OTHER}/{KEY}");
let nul_key = format!("{BUCKET}/bad%00key");
let bad_checksum = &[("x-amz-checksum-crc32", "AAAAAA==")];
let wrong_width = &[("x-amz-checksum-crc32", "AAAA")];
let bad_trailer = b"5\r\nhello\r\n0\r\nx-amz-checksum-crc32:AAAAAA==\r\n\r\n";
let missing_trailer = b"5\r\nhello\r\n0\r\n\r\n";
let mut replies = Vec::new();
for (label, path, id, number, body, headers, length) in [
("zero", path.as_str(), id.as_str(), 0, b"hello".as_slice(), &[][..], Some(5)),
("negative", &path, &id, -1, b"hello", &[], Some(5)),
("above-max", &path, &id, 10001, b"hello", &[], Some(5)),
("malformed", &path, "malformed", 2, b"hello", &[], Some(5)),
("not-here", &path, &unknown_id, 2, b"hello", &[], Some(5)),
("aborted", &path, &aborted, 2, b"hello", &[], Some(5)),
("wrong-key", &wrong_key, &id, 2, b"hello", &[], Some(5)),
("wrong-bucket", &wrong_bucket, &id, 2, b"hello", &[], Some(5)),
("nul-key", &nul_key, &id, 2, b"hello", &[], Some(5)),
("unknown-length", &path, &id, 2, b"hello", &[], None),
("short", &path, &id, 2, b"abc", &[], Some(5)),
("bad-checksum", &path, &id, 2, b"hello", bad_checksum, Some(5)),
("wrong-width", &path, &id, 2, b"hello", wrong_width, Some(5)),
("bad-trailer", &path, &id, 2, bad_trailer, &CRC32_TRAILER_HEADERS, None),
("missing-trailer", &path, &id, 2, missing_trailer, &CRC32_TRAILER_HEADERS, None),
("bad-replacement", &path, &id, 1, b"world", bad_checksum, Some(5)),
] {
replies.push((label, upload(&wire, path, id, number, body, headers, length).await));
}
let responses: Vec<_> = replies
.iter()
.map(|(label, reply)| (*label, reply.status, xml_field(reply, "Code")))
.collect();
for (label, reply) in &replies {
let (status, code, message) = match *label {
"zero" | "negative" | "above-max" => (400, "InvalidArgument", "partNumber must be between 1 and 10000"),
"malformed" => (
404,
"NoSuchUpload",
"The specified multipart upload does not exist. The upload ID might be invalid, or the multipart upload might have been aborted or completed.",
),
"not-here" | "aborted" | "wrong-key" | "wrong-bucket" => (404, "NoSuchUpload", MISSING_SESSION),
"nul-key" => (400, "InvalidArgument", "Invalid argument"),
"unknown-length" => (411, "MissingContentLength", "missing header: content-length"),
"short" => (
400,
"IncompleteBody",
"You did not provide the number of bytes specified by the Content-Length HTTP header.",
),
"bad-checksum" | "wrong-width" | "bad-trailer" | "missing-trailer" | "bad-replacement" => {
(400, "BadDigest", "The Content-Md5 you specified did not match what we received.")
}
_ => panic!("unexpected case {label}"),
};
assert_eq!((reply.status, xml_field(reply, "Code")), (status, Some(code)), "{label}: {responses:?}");
if !gateway {
assert_eq!(xml_field(reply, "Message"), Some(message), "{label}");
}
for header in ["etag", "x-amz-checksum-crc32", "x-amz-checksum-sha256"] {
assert!(!reply.headers.contains_key(header), "{label}: {header}");
}
}
let parts = store
.list_object_parts(BUCKET, KEY, &id, None, 20, &options)
.await
.unwrap()
.parts;
let summary: Vec<_> = parts
.iter()
.map(|part| (part.part_num, part.size, part.etag.as_deref()))
.collect();
for (bucket, key, session) in [(OTHER, KEY, &other_id), (BUCKET, "other-key", &denied_key_id)] {
assert!(
store
.list_object_parts(bucket, key, session, None, 20, &options)
.await
.unwrap()
.parts
.is_empty()
);
}
assert_eq!(summary, [(1, 5, Some(ETAG)), (10000, 5, Some(ETAG))]);
let sdk = super::super::s3_client(&endpoint, server.access_key(), server.secret_key());
let part = aws_sdk_s3::types::CompletedPart::builder().part_number(1).e_tag(ETAG).build();
sdk.complete_multipart_upload()
.bucket(BUCKET)
.key(KEY)
.upload_id(&id)
.multipart_upload(aws_sdk_s3::types::CompletedMultipartUpload::builder().parts(part).build())
.send()
.await
.unwrap();
let completed = upload(&wire, &path, &id, 2, b"hello", &[], Some(5)).await;
assert_reply(&completed, 404, Some("NoSuchUpload"), (!gateway).then_some(MISSING_SESSION));
let readback = sdk.get_object().bucket(BUCKET).key(KEY).send().await.unwrap();
assert_eq!(readback.body.collect().await.unwrap().into_bytes().as_ref(), b"hello");
server.shutdown().await;
}
#[test]
#[serial_test::serial]
fn legacy_upload_part_fixed_main_contract() {
super::super::with_stack(super::super::LEGACY, |server| upload_part_contract(server, false));
}
#[test]
#[serial_test::serial]
fn gateway_upload_part_wire_contract() {
super::super::with_stack(super::super::GATEWAY, |server| upload_part_contract(server, true));
}
async fn scoped_user(server: super::super::RustFSServer) {
const USER: &str = "uploadpartscoped";
let endpoint = server.endpoint();
for bucket in [BUCKET, OTHER] {
super::super::create_bucket_in_the_store(bucket).await;
}
super::super::create_user_allowed_on_one_bucket(USER, "s3:PutObject", &format!("{BUCKET}/{KEY}")).await;
let wire = WireClient {
endpoint: &endpoint,
access_key: USER,
secret_key: super::super::USER_SECRET_KEY,
};
let store = super::super::get_global_app_context().unwrap().object_store();
let opts = Default::default();
for (bucket, key, allowed) in [(BUCKET, KEY, true), (BUCKET, "denied", false), (OTHER, KEY, false)] {
let session = store.new_multipart_upload(bucket, key, &opts).await.unwrap().upload_id;
let answer = upload(&wire, &format!("{bucket}/{key}"), &session, 1, b"hello", &[], Some(5)).await;
let parts = store
.list_object_parts(bucket, key, &session, None, 20, &opts)
.await
.unwrap()
.parts;
let facts: Vec<_> = parts
.iter()
.map(|part| (part.part_num, part.size, part.etag.as_deref()))
.collect();
let expected = [(1, 5, Some(ETAG))];
assert_eq!(
facts.as_slice(),
if allowed { expected.as_slice() } else { &[] },
"{bucket}/{key} after HTTP {}",
answer.status,
);
if allowed {
assert_reply(&answer, 200, None, None);
} else {
assert_reply(&answer, 403, Some("AccessDenied"), None);
}
}
server.shutdown().await;
}
#[test]
#[serial_test::serial]
fn legacy_upload_part_scoped_policy() {
super::super::with_stack(super::super::LEGACY, scoped_user);
}
#[test]
#[serial_test::serial]
fn gateway_upload_part_scoped_policy() {
super::super::with_stack(super::super::GATEWAY, scoped_user);
}