mirror of
https://github.com/rustfs/rustfs.git
synced 2026-11-16 04:52:36 +00:00
feat(gateway): serve PutBucketVersioning
Route bucket versioning writes through the shared authorization and audited use case. Preserve legacy configuration preparation, the incarnation fence, refusal order, persisted XML, and the reload, replication, and scanner callbacks. Count each admitted write once at its edge.
Verified against RustFS 0567078f5 and gateway d75b6ceb. The signed wire cases cover six accepted writes and nine refusals per stack. The affected suite passed 29 tests, the typed-error suite passed 26, and both Clippy configurations, script tests, wiring, and prepush checks passed. The wire cases and both refusal matrices passed again after boxing the test fixture's SDK error.
Eight fault controls went red for both authorization hooks, the legacy counter, an omitted write, a dropped empty excluded prefix, Object Lock precedence, replication refusal, and the custom-status error. CI, the Linux reference comparison, and live multi-node callback delivery remain unverified.
This commit is contained in:
@@ -16,8 +16,7 @@
|
||||
|
||||
use super::*;
|
||||
use crate::app::storage_api::bucket_usecase::bucket::{ObjectLockConfigExt as _, VersioningConfigExt as _};
|
||||
use crate::app::storage_api::s3::dto::{PutBucketVersioningInput, PutBucketVersioningOutput};
|
||||
use crate::app::storage_api::s3::{S3Error, S3ErrorCode, S3Request, S3Response, S3Result};
|
||||
use rustfs_s3_types::{S3Error, S3ErrorCode, S3Result};
|
||||
|
||||
fn versioning_configuration_has_object_lock_incompatible_settings(config: &PersistedVersioningConfiguration) -> bool {
|
||||
config.suspended()
|
||||
@@ -96,40 +95,44 @@ impl DefaultBucketUsecase {
|
||||
})
|
||||
}
|
||||
|
||||
#[instrument(level = "debug", skip(self, req))]
|
||||
pub async fn execute_put_bucket_versioning(
|
||||
/// Stores the versioning configuration prepared by the request edge.
|
||||
#[instrument(level = "debug", skip(self, envelope, prepared))]
|
||||
pub(crate) async fn execute_put_bucket_versioning(
|
||||
&self,
|
||||
req: S3Request<PutBucketVersioningInput>,
|
||||
envelope: RequestEnvelope,
|
||||
bucket: &str,
|
||||
prepared: S3Result<PersistedVersioningConfiguration>,
|
||||
) -> S3Result<S3Response<PutBucketVersioningOutput>> {
|
||||
let expected_incarnation_id = bucket_config_mutation_incarnation(&req, &req.input.bucket)?;
|
||||
let request_context = req.extensions.get::<request_context::RequestContext>().cloned();
|
||||
let PutBucketVersioningInput { bucket, .. } = req.input;
|
||||
) -> S3Result<gateway_dto::PutBucketVersioningOutput> {
|
||||
RequestEnvelope::audit_only_operation(envelope, S3Operation::PutBucketVersioning, StatusCode::OK, |envelope| async move {
|
||||
let expected_incarnation_id = bucket_config_mutation_incarnation(&envelope, bucket)?;
|
||||
let request_context = envelope.request_context().cloned();
|
||||
|
||||
let versioning_configuration = prepared?;
|
||||
validate_bucket_versioning_update(&bucket, &versioning_configuration).await?;
|
||||
let versioning_configuration = prepared?;
|
||||
validate_bucket_versioning_update(bucket, &versioning_configuration).await?;
|
||||
|
||||
let data = serialize_config(&versioning_configuration)?;
|
||||
let data = serialize_config(&versioning_configuration)?;
|
||||
update_bucket_config_for_incarnation(bucket, BUCKET_VERSIONING_CONFIG, data, expected_incarnation_id)
|
||||
.await
|
||||
.map_err(ApiError::from)?;
|
||||
|
||||
update_bucket_config_for_incarnation(&bucket, BUCKET_VERSIONING_CONFIG, data, expected_incarnation_id)
|
||||
.await
|
||||
.map_err(ApiError::from)?;
|
||||
notify_bucket_metadata_reload(bucket.to_owned(), "put bucket versioning", request_context, false).await;
|
||||
|
||||
notify_bucket_metadata_reload(bucket.clone(), "put bucket versioning", request_context, false).await;
|
||||
let mut item = sr_bucket_meta_item(bucket.to_owned(), "version-config");
|
||||
item.versioning = Some(
|
||||
serialize_config(&versioning_configuration)
|
||||
.and_then(|bytes| String::from_utf8(bytes).map_err(to_internal_error))?,
|
||||
);
|
||||
if let Err(err) = site_replication_bucket_meta_hook(item).await {
|
||||
warn!(bucket = %bucket, error = ?err, "site replication bucket versioning hook failed");
|
||||
}
|
||||
|
||||
let mut item = sr_bucket_meta_item(bucket.clone(), "version-config");
|
||||
item.versioning = Some(
|
||||
serialize_config(&versioning_configuration).and_then(|bytes| String::from_utf8(bytes).map_err(to_internal_error))?,
|
||||
);
|
||||
if let Err(err) = site_replication_bucket_meta_hook(item).await {
|
||||
warn!(bucket = %bucket, error = ?err, "site replication bucket versioning hook failed");
|
||||
}
|
||||
|
||||
rustfs_scanner::record_dirty_usage_bucket_from_producer(
|
||||
&bucket,
|
||||
rustfs_scanner::SegmentInvalidationProducerIdentity::BucketMetadata,
|
||||
);
|
||||
Ok(S3Response::new(PutBucketVersioningOutput {}))
|
||||
rustfs_scanner::record_dirty_usage_bucket_from_producer(
|
||||
bucket,
|
||||
rustfs_scanner::SegmentInvalidationProducerIdentity::BucketMetadata,
|
||||
);
|
||||
Ok(gateway_dto::PutBucketVersioningOutput::default())
|
||||
})
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,17 +22,8 @@ const RESTORE_SUSPENDED_WAIT_TIMEOUT: Duration = Duration::from_secs(15);
|
||||
async fn suspend_test_bucket(bucket: &str) {
|
||||
DefaultBucketUsecase::from_global()
|
||||
.execute_put_bucket_versioning(
|
||||
build_request(
|
||||
PutBucketVersioningInput::builder()
|
||||
.bucket(bucket.to_string())
|
||||
.versioning_configuration(VersioningConfiguration {
|
||||
status: Some(BucketVersioningStatus::from_static(BucketVersioningStatus::SUSPENDED)),
|
||||
..Default::default()
|
||||
})
|
||||
.build()
|
||||
.expect("suspended versioning request should build"),
|
||||
Method::PUT,
|
||||
),
|
||||
crate::app::RequestEnvelope::from_s3s(&build_request((), Method::PUT)),
|
||||
bucket,
|
||||
Ok(rustfs_gateway_types::persistence::PersistedVersioningConfiguration {
|
||||
status: Some("Suspended".to_owned()),
|
||||
..Default::default()
|
||||
|
||||
@@ -13,14 +13,15 @@
|
||||
// limitations under the License.
|
||||
|
||||
//! The gateway operations of migration batch A2 (rustfs/backlog#2765): bucket policy, policy status, versioning, notification and ACL.
|
||||
//! None is served yet; the batch appends its rows here.
|
||||
|
||||
use super::access::authorize_bucket_config_mutation;
|
||||
use super::{OpRow, Served, row};
|
||||
use crate::app::RequestEnvelope;
|
||||
use crate::app::bucket_usecase::DefaultBucketUsecase;
|
||||
use crate::runtime_sources::AppContext;
|
||||
use crate::storage_api::server::http::access::authorize_bucket_request;
|
||||
use rustfs_gateway::{Req, Resp, dto};
|
||||
use rustfs_gateway_types::persistence::{parse_versioning, serialize_versioning_dto};
|
||||
use rustfs_policy::policy::action::{Action, S3Action};
|
||||
use rustfs_s3_ops::S3Operation;
|
||||
use rustfs_s3_types::S3Result;
|
||||
@@ -28,6 +29,7 @@ use std::sync::Arc;
|
||||
|
||||
pub(super) const ROWS: &[OpRow] = &[
|
||||
row::<dto::GetBucketVersioning>(),
|
||||
row::<dto::PutBucketVersioning>(),
|
||||
row::<dto::GetBucketPolicy>(),
|
||||
row::<dto::GetBucketPolicyStatus>(),
|
||||
row::<dto::GetBucketAcl>(),
|
||||
@@ -50,6 +52,25 @@ impl Served for dto::GetBucketVersioning {
|
||||
}
|
||||
}
|
||||
|
||||
impl Served for dto::PutBucketVersioning {
|
||||
const METRIC: S3Operation = S3Operation::PutBucketVersioning;
|
||||
|
||||
async fn authorize(envelope: &mut RequestEnvelope, request: &Req<Self>) -> S3Result<()> {
|
||||
let action = Action::S3Action(S3Action::PutBucketVersioningAction);
|
||||
authorize_bucket_config_mutation(envelope, request.input().bucket.as_str(), action).await
|
||||
}
|
||||
|
||||
async fn execute(context: Option<Arc<AppContext>>, envelope: RequestEnvelope, request: Req<Self>) -> S3Result<Resp<Self>> {
|
||||
let input = request.into_input();
|
||||
let prepared = parse_versioning(&serialize_versioning_dto(&input.versioning_configuration))
|
||||
.map_err(|err| rustfs_s3_types::s3_error!(MalformedXML, "{err}"));
|
||||
let output = DefaultBucketUsecase::with_context(context)
|
||||
.execute_put_bucket_versioning(envelope, input.bucket.as_str(), prepared)
|
||||
.await?;
|
||||
Ok(Resp::new(output))
|
||||
}
|
||||
}
|
||||
|
||||
impl Served for dto::GetBucketPolicy {
|
||||
const METRIC: S3Operation = S3Operation::GetBucketPolicy;
|
||||
|
||||
|
||||
@@ -173,11 +173,12 @@ impl FS {
|
||||
&self,
|
||||
req: S3Request<PutBucketVersioningInput>,
|
||||
) -> S3Result<S3Response<PutBucketVersioningOutput>> {
|
||||
let prepared = persisted_from_s3s(&req.input.versioning_configuration);
|
||||
let usecase = s3_api::bucket_usecase_for(self);
|
||||
audit_only_operation(req, S3Operation::PutBucketVersioning, |req| {
|
||||
usecase.execute_put_bucket_versioning(req, prepared)
|
||||
})
|
||||
.await
|
||||
record_s3_op(S3Operation::PutBucketVersioning);
|
||||
let prepared = persisted_from_s3s(&req.input.versioning_configuration).map_err(rustfs_s3_types::S3Error::from);
|
||||
let envelope = RequestEnvelope::from_s3s(&req);
|
||||
s3_api::bucket_usecase_for(self)
|
||||
.execute_put_bucket_versioning(envelope, &req.input.bucket, prepared)
|
||||
.await?;
|
||||
Ok(S3Response::new(PutBucketVersioningOutput::default()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,11 @@
|
||||
|
||||
use super::{GATEWAY, LEGACY, MatrixRow, Outcome, create_bucket_in_the_store, outcome, refusal, s3_client, with_stack};
|
||||
use aws_sdk_s3::Client;
|
||||
use aws_sdk_s3::types::BucketVersioningStatus;
|
||||
use aws_sdk_s3::error::SdkError;
|
||||
use aws_sdk_s3::operation::put_bucket_versioning::{
|
||||
PutBucketVersioningError, PutBucketVersioningOutput, builders::PutBucketVersioningFluentBuilder,
|
||||
};
|
||||
use aws_sdk_s3::types::{BucketVersioningStatus, VersioningConfiguration};
|
||||
use futures::future::BoxFuture;
|
||||
use rustfs::app::context::get_global_app_context;
|
||||
use rustfs::embedded::RustFSServer;
|
||||
@@ -29,6 +33,12 @@ pub(crate) const MATRIX: &[MatrixRow] = &[
|
||||
has_error_body: true,
|
||||
call: get_bucket_versioning,
|
||||
},
|
||||
MatrixRow {
|
||||
operation: "PutBucketVersioning",
|
||||
metric: S3Operation::PutBucketVersioning,
|
||||
has_error_body: true,
|
||||
call: put_enabled_bucket_versioning,
|
||||
},
|
||||
MatrixRow {
|
||||
operation: "GetBucketPolicy",
|
||||
metric: S3Operation::GetBucketPolicy,
|
||||
@@ -59,6 +69,10 @@ fn get_bucket_versioning<'a>(client: &'a Client, bucket: &'a str) -> BoxFuture<'
|
||||
Box::pin(async move { outcome(client.get_bucket_versioning().bucket(bucket).send().await) })
|
||||
}
|
||||
|
||||
fn put_enabled_bucket_versioning<'a>(client: &'a Client, bucket: &'a str) -> BoxFuture<'a, Outcome> {
|
||||
Box::pin(async move { outcome(put_versioning(client, bucket, BucketVersioningStatus::Enabled).send().await) })
|
||||
}
|
||||
|
||||
/// A policy that grants nobody anything the matrix exercises, so GetBucketPolicy has a document to
|
||||
/// answer without changing what any other row is admitted for.
|
||||
const INERT_POLICY: &str = r#"{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":["arn:aws:iam::rustfs:user/nobody"]},"Action":["s3:GetObject"],"Resource":["arn:aws:s3:::gateway-stack-matrix/*"]}]}"#;
|
||||
@@ -379,3 +393,175 @@ fn notification_authorization_binds_the_bucket_on_the_gateway_stack() {
|
||||
fn notification_authorization_binds_the_bucket_on_the_legacy_stack() {
|
||||
with_stack(LEGACY, notification_authorization_is_bound_to_the_requested_bucket);
|
||||
}
|
||||
|
||||
const VERSIONING_ENABLED: &str = "<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>";
|
||||
const VERSIONING_SUSPENDED_BYTES: &[u8] = b"<VersioningConfiguration><Status>Suspended</Status></VersioningConfiguration>";
|
||||
|
||||
fn put_versioning(client: &Client, bucket: &str, status: BucketVersioningStatus) -> PutBucketVersioningFluentBuilder {
|
||||
client
|
||||
.put_bucket_versioning()
|
||||
.bucket(bucket)
|
||||
.versioning_configuration(VersioningConfiguration::builder().status(status).build())
|
||||
}
|
||||
|
||||
async fn put_versioning_document(
|
||||
client: &Client,
|
||||
bucket: &str,
|
||||
document: &'static str,
|
||||
) -> Result<PutBucketVersioningOutput, Box<SdkError<PutBucketVersioningError>>> {
|
||||
use md5::{Digest as _, Md5};
|
||||
|
||||
let content_md5 = base64_simd::STANDARD.encode_to_string(Md5::digest(document.as_bytes()));
|
||||
put_versioning(client, bucket, BucketVersioningStatus::Enabled)
|
||||
.content_md5(content_md5)
|
||||
.customize()
|
||||
.mutate_request(move |request| {
|
||||
*request.body_mut() = aws_smithy_types::body::SdkBody::from(document);
|
||||
let headers = request.headers_mut();
|
||||
headers.remove("content-length");
|
||||
headers.remove("x-amz-sdk-checksum-algorithm");
|
||||
headers.remove("x-amz-checksum-crc32");
|
||||
})
|
||||
.send()
|
||||
.await
|
||||
.map_err(Box::new)
|
||||
}
|
||||
|
||||
async fn stored_versioning(bucket: &str) -> Vec<u8> {
|
||||
rustfs_ecstore::api::bucket::metadata_sys::get_config_from_disk(bucket)
|
||||
.await
|
||||
.expect("bucket metadata on disk")
|
||||
.versioning_config_xml
|
||||
}
|
||||
|
||||
async fn versioning_writes_preserve_refusal_order_and_stored_bytes(server: RustFSServer) {
|
||||
use aws_sdk_s3::error::ProvideErrorMetadata as _;
|
||||
|
||||
const WRITER: &str = "versioningwriter";
|
||||
const BUCKET: &str = "versioning-allowed";
|
||||
const OTHER: &str = "versioning-other";
|
||||
const LOCKED: &str = "versioning-locked";
|
||||
const REPLICATED: &str = "versioning-replicated";
|
||||
const MISSING: &str = "versioning-missing";
|
||||
for bucket in [BUCKET, OTHER, LOCKED, REPLICATED, "xn--abc"]
|
||||
.into_iter()
|
||||
.chain(SDK_REACHABLE_NAMES)
|
||||
{
|
||||
create_bucket_in_the_store(bucket).await;
|
||||
store_bucket_config(bucket, "versioning.xml", VERSIONING_ENABLED).await;
|
||||
}
|
||||
const LOCK: &str = "<ObjectLockConfiguration><ObjectLockEnabled>Enabled</ObjectLockEnabled></ObjectLockConfiguration>";
|
||||
const REPLICATION: &str = "<ReplicationConfiguration><Role>arn:aws:s3:::target-bucket</Role><Rule><ID>rule1</ID><Status>Enabled</Status><Prefix></Prefix><Destination><Bucket>arn:aws:s3:::target-bucket</Bucket></Destination></Rule></ReplicationConfiguration>";
|
||||
store_bucket_config(LOCKED, "replication.xml", REPLICATION).await;
|
||||
store_bucket_config(LOCKED, "object-lock.xml", LOCK).await;
|
||||
store_bucket_config(REPLICATED, "replication.xml", REPLICATION).await;
|
||||
|
||||
super::create_user_allowed_on_one_bucket(WRITER, "s3:PutBucketVersioning", BUCKET).await;
|
||||
let root = s3_client(&server.endpoint(), server.access_key(), server.secret_key());
|
||||
let writer = s3_client(&server.endpoint(), WRITER, super::USER_SECRET_KEY);
|
||||
let handled = || {
|
||||
rustfs_io_metrics::s3_op_metrics_snapshot()
|
||||
.into_iter()
|
||||
.find(|metric| metric.op == S3Operation::PutBucketVersioning.as_str())
|
||||
.map_or(0, |metric| metric.total)
|
||||
};
|
||||
let before = handled();
|
||||
let status = super::bucket_configs::StatusCapture::default();
|
||||
put_versioning(&writer, BUCKET, BucketVersioningStatus::Suspended)
|
||||
.customize()
|
||||
.interceptor(status.clone())
|
||||
.send()
|
||||
.await
|
||||
.expect("authorized versioning update");
|
||||
assert_eq!(status.take(), Some(200));
|
||||
assert_eq!(stored_versioning(BUCKET).await, VERSIONING_SUSPENDED_BYTES);
|
||||
assert_eq!(handled() - before, 1, "the admitted edge counts once");
|
||||
|
||||
let denied = put_versioning(&writer, OTHER, BucketVersioningStatus::Suspended)
|
||||
.send()
|
||||
.await
|
||||
.expect_err("bucket outside the writer's policy");
|
||||
assert_eq!(refusal(&denied), (Some("AccessDenied".into()), Some(403)));
|
||||
assert_eq!(stored_versioning(OTHER).await, VERSIONING_ENABLED.as_bytes());
|
||||
assert_eq!(handled() - before, 1, "authorization refusals do not reach the edge counter");
|
||||
|
||||
for (bucket, code, http, message) in [
|
||||
(MISSING, "NoSuchBucket", 404, None),
|
||||
("xn--abc", "InvalidBucketName", 400, None),
|
||||
(
|
||||
LOCKED,
|
||||
"InvalidBucketState",
|
||||
409,
|
||||
Some("An Object Lock configuration is present on this bucket, versioning cannot be suspended."),
|
||||
),
|
||||
(
|
||||
REPLICATED,
|
||||
"InvalidBucketState",
|
||||
409,
|
||||
Some("A replication configuration is present on this bucket, bucket wide versioning cannot be suspended."),
|
||||
),
|
||||
] {
|
||||
let error = put_versioning(&root, bucket, BucketVersioningStatus::Suspended)
|
||||
.send()
|
||||
.await
|
||||
.expect_err(bucket);
|
||||
assert_eq!(refusal(&error), (Some(code.into()), Some(http)), "{bucket}");
|
||||
if let Some(message) = message {
|
||||
assert_eq!(error.message(), Some(message), "{bucket}");
|
||||
}
|
||||
if bucket != MISSING {
|
||||
assert_eq!(stored_versioning(bucket).await, VERSIONING_ENABLED.as_bytes(), "{bucket}");
|
||||
}
|
||||
}
|
||||
|
||||
for bucket in SDK_REACHABLE_NAMES {
|
||||
put_versioning(&root, bucket, BucketVersioningStatus::Suspended)
|
||||
.send()
|
||||
.await
|
||||
.expect(bucket);
|
||||
assert_eq!(stored_versioning(bucket).await, VERSIONING_SUSPENDED_BYTES, "{bucket}");
|
||||
}
|
||||
|
||||
for (client, bucket, code, http) in [
|
||||
(&root, OTHER, "InvalidArgument", 400),
|
||||
(&root, MISSING, "NoSuchBucket", 404),
|
||||
(&writer, OTHER, "AccessDenied", 403),
|
||||
] {
|
||||
let invalid = put_versioning(client, bucket, BucketVersioningStatus::from("FutureStatus"))
|
||||
.send()
|
||||
.await
|
||||
.expect_err("invalid versioning status");
|
||||
assert_eq!(refusal(&invalid), (Some(code.into()), Some(http)), "{bucket}");
|
||||
if code == "InvalidArgument" {
|
||||
assert_eq!(invalid.message(), Some("bucket versioning configuration has an invalid status"));
|
||||
}
|
||||
}
|
||||
|
||||
let malformed = put_versioning_document(&root, OTHER, "<VersioningConfiguration>")
|
||||
.await
|
||||
.expect_err("incomplete versioning document");
|
||||
assert_eq!(refusal(malformed.as_ref()), (Some("MalformedXML".into()), Some(400)));
|
||||
assert_eq!(stored_versioning(OTHER).await, VERSIONING_ENABLED.as_bytes());
|
||||
|
||||
const EXTENSIONS: &str = "<VersioningConfiguration><Status>Enabled</Status><ExcludeFolders>true</ExcludeFolders><ExcludedPrefixes><Prefix>archive/</Prefix></ExcludedPrefixes><ExcludedPrefixes/></VersioningConfiguration>";
|
||||
const STORED_EXTENSIONS: &[u8] = b"<VersioningConfiguration><ExcludeFolders>true</ExcludeFolders><ExcludedPrefixes><Prefix>archive/</Prefix></ExcludedPrefixes><ExcludedPrefixes></ExcludedPrefixes><Status>Enabled</Status></VersioningConfiguration>";
|
||||
put_versioning_document(&root, OTHER, EXTENSIONS)
|
||||
.await
|
||||
.expect("versioning extensions");
|
||||
assert_eq!(stored_versioning(OTHER).await, STORED_EXTENSIONS);
|
||||
put_versioning_document(&root, OTHER, "Enabled")
|
||||
.await
|
||||
.expect("bare versioning status");
|
||||
assert_eq!(stored_versioning(OTHER).await, VERSIONING_ENABLED.as_bytes());
|
||||
server.shutdown().await;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_put_versioning_preserves_refusal_order_and_stored_bytes() {
|
||||
with_stack(LEGACY, versioning_writes_preserve_refusal_order_and_stored_bytes);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gateway_put_versioning_preserves_refusal_order_and_stored_bytes() {
|
||||
with_stack(GATEWAY, versioning_writes_preserve_refusal_order_and_stored_bytes);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user