feat(gateway): serve PutObjectTagging through the shared use case

Share XML tag authorization and storage execution across both stacks.
Preserve the requested version, replication-target proxy, and metadata
completion order.

The dual-stack tests exercise 37 refusal and 13 success scenarios.
The restored integration run passed 148 tests on 26db9b19a, and the
proxy cleanup verification passed 134 run, 134 passed.
Both feature configurations passed Clippy, and script tests passed.

Fault injection caught native and shared authorization bypasses,
missing XML facts, and incorrect action, resource and tag key facts.
It also caught duplicate counting, incorrect persisted tags, missing
or duplicate success events, and event or dirty publication on a miss.

Live replication targets and ECStore write faults remain unverified.
This commit is contained in:
overtrue committed 2026-11-16 00:29:49 +08:00
1 parent 26db9b19af
commit 92050cdca6
9 files changed
+675 -168

No files matched your search

+152 -9
View File
@@ -12,20 +12,21 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//! GetObjectTagging and DeleteObjectTagging: the tag set an object version stores.
//! GetObjectTagging, PutObjectTagging and DeleteObjectTagging for an object version.
//!
//! Responsible for: reading the stored tags (as stored), deleting them with metadata replication,
//! and, for an object missing locally, serving the request from a replication target that has it.
//! NOT responsible for: authorization (and so not the version swap of its action), or
//! PutObjectTagging, which still runs on the legacy edge.
//! Reads stored tags and applies validated mutations with metadata replication. For an object
//! missing locally, it can serve the request from a replication target that has it. The edges
//! authorize requests before invoking these operations.
//! Upstream: both edges. Downstream: the object store, metadata replication and the tagging proxy.
use super::ObjectMetaUsecase;
use super::tagging_proxy::{proxy_delete_object_tagging, proxy_get_object_tagging};
use super::tagging_proxy::{proxy_delete_object_tagging, proxy_get_object_tagging, proxy_put_object_tagging};
use crate::app::RequestEnvelope;
use crate::app::object::shared::{reject_veeam_sosapi_mutation, s3_response_version_id, validate_table_catalog_object_mutation};
use crate::app::storage_api::object_meta_usecase::contract::ObjectOperations as _;
use crate::app::storage_api::object_meta_usecase::{VersioningApi as _, decode_stored_tags, parse_object_version_id};
use crate::app::storage_api::object_meta_usecase::{
VersioningApi as _, decode_stored_tags, encode_stored_tags, parse_object_version_id, validate_tag_set_entries,
};
use crate::app::storage_api::object_usecase::ECStore;
use crate::app::storage_api::object_usecase::StorageObjectOptions as ObjectOptions;
use crate::app::storage_api::object_usecase::bucket::replication::{must_replicate_metadata, schedule_metadata_replication};
@@ -36,9 +37,12 @@ use crate::app::storage_api::object_usecase::options::get_opts;
use crate::error::ApiError;
use http::{HeaderMap, StatusCode};
use metrics::{counter, histogram};
use rustfs_gateway_types::cors_tagging::PersistedTag;
#[cfg(feature = "gateway-stack")]
use rustfs_gateway_types::dto::PutObjectTaggingInput;
#[cfg(any(feature = "gateway-stack", test))]
use rustfs_gateway_types::dto::{DeleteObjectTaggingInput, GetObjectTaggingInput};
use rustfs_gateway_types::dto::{DeleteObjectTaggingOutput, GetObjectTaggingOutput, Tag};
use rustfs_gateway_types::dto::{DeleteObjectTaggingOutput, GetObjectTaggingOutput, PutObjectTaggingOutput, Tag};
use rustfs_s3_ops::S3Operation;
use rustfs_s3_types::{EventName, S3Error, S3ErrorCode, S3Result};
use rustfs_utils::http::{
@@ -160,6 +164,130 @@ impl ObjectMetaUsecase {
})
}
/// Replaces the tag set of the named object version and replicates the change when configured.
///
/// # Errors
///
/// Returns `InvalidTag` for an invalid tag set, a refusal for reserved objects, or the
/// storage error when the tags cannot be written.
#[cfg(feature = "gateway-stack")]
pub async fn execute_put_object_tagging(
&self,
envelope: RequestEnvelope,
input: PutObjectTaggingInput,
) -> S3Result<PutObjectTaggingOutput> {
self.put_object_tagging_fields(
envelope,
input.bucket.as_str(),
input.key.as_str(),
input.version_id,
input.tagging.tag_set,
)
.await
}
pub(crate) async fn put_object_tagging_fields(
&self,
envelope: RequestEnvelope,
bucket: &str,
object: &str,
version_id: Option<String>,
tag_set: Vec<Tag>,
) -> S3Result<PutObjectTaggingOutput> {
let start_time = Instant::now();
let helper =
OperationHelper::from_view(&envelope.audit_view(), EventName::ObjectTaggingPut, S3Operation::PutObjectTagging);
let (bucket, object) = (bucket.to_owned(), object.to_owned());
reject_veeam_sosapi_mutation(|| self.object_store().ok(), &bucket, &object).await?;
validate_table_catalog_object_mutation(&bucket, &object).await?;
let (bucket, object) = (bucket.as_str(), object.as_str());
let persisted_tags = validate_put_tags(&tag_set)?;
let store = self.object_store()?;
let tags = encode_stored_tags(persisted_tags);
let mut opts = get_opts(bucket, object, version_id.clone(), None, envelope.headers())
.await
.map_err(ApiError::from)?;
let existing = match store.get_object_info(bucket, object, &opts).await {
Ok(info) => info,
Err(error) => {
// A write on a remote target emits no local object event.
if (is_err_object_not_found(&error) || is_err_version_not_found(&error))
&& proxy_allowed(&envelope)
&& proxy_put_object_tagging(bucket, object, version_id.clone(), envelope.headers(), &tag_set)
.await
.is_some()
{
counter!("rustfs_put_object_tagging_success").increment(1);
record_tagging_duration("put", start_time);
return Ok(PutObjectTaggingOutput { version_id });
}
return Err(ApiError::from(error).into());
}
};
let decision = must_replicate_metadata(
bucket,
object,
&existing.user_defined,
tags.clone(),
existing.replication_status,
opts.clone(),
)
.await;
if decision.replicate_any() {
let mut eval_metadata = HashMap::new();
insert_str(&mut eval_metadata, SUFFIX_REPLICATION_GENERATION, Uuid::new_v4().to_string());
insert_str(&mut eval_metadata, SUFFIX_REPLICATION_TIMESTAMP, jiff::Zoned::now().to_string());
insert_str(
&mut eval_metadata,
SUFFIX_REPLICATION_STATUS,
decision.pending_status().unwrap_or_default(),
);
insert_str(
&mut eval_metadata,
SUFFIX_TAGGING_TIMESTAMP,
OffsetDateTime::now_utc().format(&Rfc3339).unwrap_or_default(),
);
opts.eval_metadata = Some(eval_metadata);
}
let info = store.put_object_tags(bucket, object, &tags, &opts).await.map_err(|error| {
error!(
component = LOG_COMPONENT_STORAGE,
subsystem = LOG_SUBSYSTEM_TAGGING,
event = "object_tagging_put_failed",
bucket = %bucket,
object = %object,
error = %error,
"Failed to put object tags"
);
counter!("rustfs_put_object_tagging_failure").increment(1);
ApiError::from(error)
})?;
if decision.replicate_any() {
schedule_metadata_replication(info.clone(), Arc::clone(&store), decision).await;
}
counter!("rustfs_put_object_tagging_success").increment(1);
let event_version_id = version_id
.as_deref()
.filter(|value| !value.is_empty())
.map(str::to_owned)
.or_else(|| info.version_id.map(|version| version.to_string()))
.unwrap_or_default();
let result = Ok(PutObjectTaggingOutput { version_id });
let _ = helper
.object(info)
.version_id(event_version_id)
.complete_owned(&result, StatusCode::OK, &HeaderMap::new());
rustfs_scanner::record_dirty_usage_object_from_producer(
bucket,
object,
rustfs_scanner::SegmentInvalidationProducerIdentity::ObjectMetadata,
);
record_tagging_duration("put", start_time);
result
}
/// Deletes the tag set of the object version `input` names, replicating the change where the
/// bucket's rules ask for it. An object missing locally is deleted on a replication target that
/// has it, without a local event.
@@ -294,7 +422,7 @@ impl ObjectMetaUsecase {
/// A proxy may answer only when no tag-conditioned authorization ran: the remote's tags were never
/// shown to the policy that admitted the request.
fn proxy_allowed(envelope: &RequestEnvelope) -> bool {
pub(crate) fn proxy_allowed(envelope: &RequestEnvelope) -> bool {
envelope.object_tag_conditions().is_none()
}
@@ -313,3 +441,18 @@ fn stored_tags(encoded: &str) -> Vec<Tag> {
})
.collect()
}
fn validate_put_tags(tags: &[Tag]) -> S3Result<Vec<PersistedTag>> {
if tags.len() > 10 {
return Err(S3Error::with_message(S3ErrorCode::InvalidTag, "Cannot have more than 10 tags per object"));
}
let tags: Vec<_> = tags
.iter()
.map(|tag| PersistedTag {
key: tag.key.clone(),
value: tag.value.clone(),
})
.collect();
validate_tag_set_entries(&tags).map_err(|error| S3Error::with_message(S3ErrorCode::InvalidTag, error))?;
Ok(tags)
}
+1 -1
View File
@@ -19,7 +19,7 @@
//! target through the AWS SDK until one has the object, and the proxy metrics.
//! NOT responsible for: deciding whether proxying is allowed for the request (the caller checks
//! that no tag-conditioned authorization ran), or any local tagging.
//! Upstream: the tagging use cases, and the legacy PutObjectTagging edge until it moves here.
//! Upstream: the tagging use cases.
//! Downstream: the replication target clients.
use crate::app::storage_api::object_usecase::StorageObjectOptions as ObjectOptions;
+4 -1
View File
@@ -1845,7 +1845,10 @@ pub(crate) mod object_meta_usecase {
}
pub(crate) use crate::storage::storage_api::ecfs_consumer::object_lock::validate_bucket_object_lock_enabled;
pub(crate) use crate::storage::storage_api::ecstore_bucket::tagging::decode_tags as decode_stored_tags;
pub(crate) use crate::storage::storage_api::ecstore_bucket::metadata::validate_tag_set_entries;
pub(crate) use crate::storage::storage_api::ecstore_bucket::tagging::{
decode_tags as decode_stored_tags, encode_tags as encode_stored_tags,
};
pub(crate) use crate::storage::storage_api::ecstore_bucket::versioning::VersioningApi;
pub(crate) use crate::storage::storage_api::parse_object_version_id;
}
@@ -20,7 +20,9 @@ use crate::app::RequestEnvelope;
use crate::app::object_meta::ObjectMetaUsecase;
use crate::app::object_meta::torrent::execute_get_object_torrent;
use crate::runtime_sources::AppContext;
use crate::storage_api::server::http::access::access_owned::object_meta::authorize_object_tagging_request;
use crate::storage_api::server::http::access::access_owned::object_meta::{
RequestedObjectTags, authorize_object_tagging_request, authorize_put_object_tagging,
};
use crate::storage_api::server::http::access::authorize_object_request;
use rustfs_gateway::{Req, Resp, dto};
use rustfs_gateway_types::{BucketName, ObjectKey};
@@ -37,6 +39,7 @@ pub(super) const ROWS: &[OpRow] = &[
row::<dto::GetObjectRetention>(),
row::<dto::GetObjectTagging>(),
row::<dto::DeleteObjectTagging>(),
row::<dto::PutObjectTagging>(),
];
/// What the legacy hooks of the ACL, legal-hold and retention reads make: the decoded object, its
@@ -169,3 +172,31 @@ impl Served for dto::DeleteObjectTagging {
.map(Resp::new)
}
}
impl Served for dto::PutObjectTagging {
const METRIC: S3Operation = S3Operation::PutObjectTagging;
async fn authorize(envelope: &mut RequestEnvelope, request: &Req<Self>) -> S3Result<()> {
let input = request.input();
let tags = RequestedObjectTags(
input
.tagging
.tag_set
.iter()
.filter_map(|tag| Some((tag.key.clone()?, tag.value.clone()?)))
.collect(),
);
Ok(
authorize_put_object_tagging(envelope, input.bucket.as_str(), input.key.as_str(), input.version_id.as_deref(), tags)
.await?,
)
}
async fn execute(context: Option<Arc<AppContext>>, envelope: RequestEnvelope, request: Req<Self>) -> S3Result<Resp<Self>> {
let usecase = ObjectMetaUsecase::with_context(context);
usecase
.execute_put_object_tagging(envelope, request.into_input())
.await
.map(Resp::new)
}
}
+19 -28
View File
@@ -935,12 +935,6 @@ pub(crate) mod envelope_payloads {
}
}
/// Local tag evidence cannot authorize an object that only exists on a replica.
/// Keep the local miss whenever authorization needed existing-object tags.
pub(crate) fn replication_proxy_allowed(extensions: &http::Extensions) -> bool {
extensions.get::<ObjectTagConditions>().is_none()
}
const AMZ_WRITE_OFFSET_BYTES_HEADER: &str = "x-amz-write-offset-bytes";
fn has_write_offset_bytes_header(headers: &http::HeaderMap) -> bool {
@@ -4247,17 +4241,10 @@ impl S3Access for FS {
///
/// This method returns `Ok(())` by default.
async fn put_object_tagging(&self, req: &mut S3Request<PutObjectTaggingInput>) -> S3Result<()> {
req.install_requested_object_tags(RequestedObjectTags::from(&req.input.tagging));
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());
req_info.version_id = req.input.version_id.clone();
authorize_request(
req,
versioned_tagging_action(S3Action::PutObjectTaggingAction, req.input.version_id.as_deref()),
)
.await
let input = &req.input;
let tags = RequestedObjectTags::from(&input.tagging);
let (bucket, key, version_id) = (input.bucket.clone(), input.key.clone(), input.version_id.clone());
access_owned::object_meta::authorize_put_object_tagging(req, &bucket, &key, version_id.as_deref(), tags).await
}
/// Checks whether the PutPublicAccessBlock request has accesses to the resources.
@@ -6468,21 +6455,28 @@ mod tests {
assert!(!conditions.contains_key("RequestObjectTagKeys"));
}
fn tagging_proxy_allowed<T>(request: &S3Request<T>) -> bool {
let envelope = crate::app::RequestEnvelope::from_s3s(request);
crate::app::object_meta::tagging::proxy_allowed(&envelope)
}
#[test]
fn replication_proxy_requires_authorization_independent_of_local_tags() {
let mut extensions = http::Extensions::new();
assert!(super::replication_proxy_allowed(&extensions));
let mut request = build_request((), Method::PUT);
assert!(tagging_proxy_allowed(&request));
// A local miss produces an empty tag map, but still establishes that the
// policy needs tags. Neither anonymous nor authenticated callers may proxy.
extensions.insert(ObjectTagConditions::new("bucket", "missing", None, HashMap::new()));
assert!(!super::replication_proxy_allowed(&extensions));
extensions.insert(ObjectTagConditions::new(
request
.extensions
.insert(ObjectTagConditions::new("bucket", "missing", None, HashMap::new()));
assert!(!tagging_proxy_allowed(&request));
request.extensions.insert(ObjectTagConditions::new(
"bucket",
"missing",
Some("version"),
HashMap::from([("ExistingObjectTag/protected".to_string(), vec!["yes".to_string()])]),
));
assert!(!super::replication_proxy_allowed(&extensions));
assert!(!tagging_proxy_allowed(&request));
}
#[tokio::test]
@@ -6517,7 +6511,7 @@ mod tests {
.expect("merge required existing object tags into authorization conditions");
assert_eq!(conditions.get("ExistingObjectTag/security"), Some(&vec!["restricted".to_string()]));
assert!(!super::replication_proxy_allowed(&request.extensions));
assert!(!tagging_proxy_allowed(&request));
assert_eq!(conditions.get("ExistingObjectTag/project"), Some(&vec!["webapp".to_string()]));
assert_eq!(conditions.get("delimiter"), Some(&vec!["/".to_string()]));
}
@@ -7463,10 +7457,7 @@ mod tests {
.get::<ObjectTagConditions>()
.expect("authorization must retain tag dependency");
assert!(evidence.values.is_empty(), "missing local object has no tags");
assert!(
!super::replication_proxy_allowed(&req.extensions),
"local absence cannot authorize a tagged replica"
);
assert!(!tagging_proxy_allowed(&req), "local absence cannot authorize a tagged replica");
}};
}
check_hook!(
@@ -21,6 +21,7 @@
//! Upstream: the legacy hook in `storage/access.rs` and the batch's `server/gateway_handlers/object_meta.rs`.
//! Downstream: the shared shapes in `storage/access.rs`.
pub(crate) use super::super::RequestedObjectTags;
use super::super::{AuthorizationRequest, S3Result, authorize_object_request, versioned_tagging_action};
use rustfs_policy::policy::action::S3Action;
@@ -35,3 +36,15 @@ pub(crate) async fn authorize_object_tagging_request<R: AuthorizationRequest + ?
) -> S3Result<()> {
authorize_object_request(req, bucket, key, version_id, versioned_tagging_action(action, version_id)).await
}
/// Authorize the version and tag conditions using the original query and XML tag set.
pub(crate) async fn authorize_put_object_tagging<R: AuthorizationRequest + ?Sized>(
req: &mut R,
bucket: &str,
key: &str,
version_id: Option<&str>,
tags: RequestedObjectTags,
) -> S3Result<()> {
req.install_requested_object_tags(tags);
authorize_object_tagging_request(req, bucket, key, version_id, S3Action::PutObjectTaggingAction).await
}
+17 -127
View File
@@ -20,13 +20,13 @@
//! owns this file and is the only one that edits it.
use crate::app::object_meta::ObjectMetaUsecase;
use crate::app::object_meta::tagging_proxy::proxy_put_object_tagging;
use crate::app::object_meta::torrent::answer_get_object_torrent;
use crate::app::storage_api::object_usecase::bucket::replication::{
ReplicateDecision, must_replicate_metadata, schedule_metadata_replication,
};
use crate::error::ApiError;
use crate::storage::access::{apply_bucket_generation_guard, has_bypass_governance_header, replication_proxy_allowed};
use crate::storage::access::{apply_bucket_generation_guard, has_bypass_governance_header};
use crate::storage::check_retention_for_modification;
use crate::storage::ecfs::*;
use crate::storage::helper::OperationHelper;
use crate::storage::options::get_opts;
@@ -39,9 +39,7 @@ use crate::storage::storage_api::ecfs_consumer::contract::{
use crate::storage::storage_api::ecfs_consumer::object_lock::{
parse_object_lock_legal_hold, parse_object_lock_retention, validate_bucket_object_lock_enabled,
};
use crate::storage::{check_retention_for_modification, encode_tags, is_err_object_not_found, is_err_version_not_found};
use crate::table_catalog;
use metrics::{counter, histogram};
use rustfs_gateway_types::compat::s3s_5761ddfe::generated::ops::{
delete_object_tagging, get_object_acl, get_object_legal_hold, get_object_retention, get_object_tagging, get_object_torrent,
put_object_acl,
@@ -49,15 +47,12 @@ use rustfs_gateway_types::compat::s3s_5761ddfe::generated::ops::{
use rustfs_io_metrics::record_s3_op;
use rustfs_s3_ops::S3Operation;
use rustfs_targets::EventName;
use rustfs_utils::http::{
SUFFIX_REPLICATION_GENERATION, SUFFIX_REPLICATION_STATUS, SUFFIX_REPLICATION_TIMESTAMP, SUFFIX_TAGGING_TIMESTAMP, insert_str,
};
use std::collections::HashMap;
use time::{OffsetDateTime, format_description::well_known::Rfc3339};
use tracing::{debug, error, instrument};
use rustfs_utils::http::{SUFFIX_REPLICATION_GENERATION, SUFFIX_REPLICATION_STATUS, SUFFIX_REPLICATION_TIMESTAMP, insert_str};
use time::OffsetDateTime;
use tracing::{error, instrument};
use uuid::Uuid;
/// The legacy PutObjectTagging tag set as the tagging proxy takes it, until that operation moves.
/// Preserve every tag and its optional fields for validation by the shared use case.
fn gateway_tags(tag_set: &TagSet) -> Vec<rustfs_gateway_types::dto::Tag> {
tag_set
.iter()
@@ -410,125 +405,20 @@ impl FS {
&self,
req: S3Request<PutObjectTaggingInput>,
) -> S3Result<S3Response<PutObjectTaggingOutput>> {
let start_time = std::time::Instant::now();
let mut helper = OperationHelper::new(&req, EventName::ObjectTaggingPut, S3Operation::PutObjectTagging);
record_s3_op(S3Operation::PutObjectTagging);
let envelope = crate::app::RequestEnvelope::from_s3s(&req);
let PutObjectTaggingInput {
bucket,
key: object,
key,
version_id,
tagging,
..
} = req.input.clone();
reject_veeam_sosapi_mutation(self, &bucket, &object).await?;
validate_table_catalog_object_mutation(&bucket, &object).await?;
crate::storage::s3_api::tagging::validate_object_tag_set(&tagging.tag_set)?;
let Some(store) = self.server_ctx.object_store() else {
return Err(S3Error::with_message(S3ErrorCode::InternalError, "Not init".to_string()));
};
let tags = encode_tags(tagging.tag_set.clone());
debug!("Encoded tags: {}", tags);
let version_id = req.input.version_id.clone();
let mut opts = get_opts(&bucket, &object, version_id.clone(), None, &req.headers)
.await
.map_err(ApiError::from)?;
let existing_object_info = match store.get_object_info(&bucket, &object, &opts).await {
Ok(info) => info,
Err(e) => {
// Replication lag window: apply the tagging update on a
// replication target that already has the object
// (backlog#1675 P1-5). No local object exists, so no bucket
// notification event is emitted for the proxied write.
if (is_err_object_not_found(&e) || is_err_version_not_found(&e))
&& replication_proxy_allowed(&req.extensions)
&& proxy_put_object_tagging(
&bucket,
&object,
version_id.clone(),
&req.headers,
&gateway_tags(&tagging.tag_set),
)
.await
.is_some()
{
counter!("rustfs_put_object_tagging_success").increment(1);
let duration = start_time.elapsed();
histogram!("rustfs_object_tagging_operation_duration_seconds", "operation" => "put")
.record(duration.as_secs_f64());
return Ok(S3Response::new(PutObjectTaggingOutput {
version_id: req.input.version_id.clone(),
}));
}
return Err(ApiError::from(e).into());
}
};
let dsc = must_replicate_metadata(
&bucket,
&object,
&existing_object_info.user_defined,
tags.clone(),
existing_object_info.replication_status.clone(),
opts.clone(),
)
.await;
if dsc.replicate_any() {
let mut eval_metadata = HashMap::new();
insert_str(&mut eval_metadata, SUFFIX_REPLICATION_GENERATION, Uuid::new_v4().to_string());
insert_str(&mut eval_metadata, SUFFIX_REPLICATION_TIMESTAMP, jiff::Zoned::now().to_string());
insert_str(&mut eval_metadata, SUFFIX_REPLICATION_STATUS, dsc.pending_status().unwrap_or_default());
insert_str(
&mut eval_metadata,
SUFFIX_TAGGING_TIMESTAMP,
OffsetDateTime::now_utc().format(&Rfc3339).unwrap_or_default(),
);
opts.eval_metadata = Some(eval_metadata);
}
let put_tags_result = store.put_object_tags(&bucket, &object, &tags, &opts).await;
let object_info = put_tags_result.map_err(|e| {
error!("Failed to put object tags: {}", e);
counter!("rustfs_put_object_tagging_failure").increment(1);
ApiError::from(e)
})?;
let event_object_info = Some(object_info.clone());
if dsc.replicate_any() {
schedule_metadata_replication(object_info, store.clone(), dsc).await;
}
counter!("rustfs_put_object_tagging_success").increment(1);
let event_version_id = req
.input
.version_id
.as_deref()
.filter(|version_id| !version_id.is_empty())
.map(str::to_string)
.or_else(|| {
event_object_info
.as_ref()
.and_then(|info| info.version_id.map(|version_id| version_id.to_string()))
})
.unwrap_or_default();
if let Some(event_object_info) = event_object_info {
helper = helper.object(event_object_info);
}
helper = helper.version_id(event_version_id);
let result = Ok(S3Response::new(PutObjectTaggingOutput {
version_id: req.input.version_id.clone(),
}));
let _ = helper.complete(&result);
rustfs_scanner::record_dirty_usage_object_from_producer(
&bucket,
&object,
rustfs_scanner::SegmentInvalidationProducerIdentity::ObjectMetadata,
);
let duration = start_time.elapsed();
histogram!("rustfs_object_tagging_operation_duration_seconds", "operation" => "put").record(duration.as_secs_f64());
result
} = req.input;
let output = object_meta_usecase(self)
.put_object_tagging_fields(envelope, &bucket, &key, version_id, gateway_tags(&tagging.tag_set))
.await?;
Ok(S3Response::new(PutObjectTaggingOutput {
version_id: output.version_id,
}))
}
}
@@ -166,6 +166,31 @@ async fn test_put_object_tagging_rejects_empty_tag_key_before_store_lookup() {
assert!(err.to_string().contains("Tag key cannot be empty"));
}
#[tokio::test]
async fn test_put_object_tagging_preserves_missing_tag_fields_before_version_validation() {
let fs = FS::new();
for (key, value, message) in [
(None, Some("value"), "Tag key cannot be empty"),
(Some("key"), None, "Tag value cannot be null"),
] {
let input = PutObjectTaggingInput::builder()
.bucket("test-bucket".to_owned())
.key("test-key".to_owned())
.version_id(Some("invalid".to_owned()))
.tagging(Tagging {
tag_set: vec![Tag {
key: key.map(str::to_owned),
value: value.map(str::to_owned),
}],
})
.build()
.unwrap();
let error = fs.put_object_tagging(build_request(input, Method::PUT)).await.unwrap_err();
assert_eq!(error.code(), &S3ErrorCode::InvalidTag);
assert!(error.to_string().contains(message), "{error}");
}
}
#[tokio::test]
async fn test_put_object_tagging_returns_internal_error_when_store_uninitialized() {
if !store_uninitialized_premise_holds() {
@@ -206,3 +231,72 @@ async fn test_delete_object_tagging_returns_internal_error_when_store_uninitiali
.unwrap_err();
assert_eq!(err.code(), &S3ErrorCode::InternalError);
}
#[test]
#[serial_test::serial]
fn put_object_tagging_completes_once_for_committed_metadata() {
use crate::app::{RequestEnvelope, gating_test_env, object_meta::ObjectMetaUsecase};
use crate::storage::helper::MutationCompletionProbe;
use crate::storage::storage_api::StoragePutObjReader;
use crate::storage::storage_api::contract::bucket::{BucketOperations, MakeBucketOptions};
use crate::storage::storage_api::contract::object::ObjectIO;
use rustfs_scanner::{SegmentInvalidationProducerIdentity, set_scanner_dirty_usage_mutation_observer};
use std::sync::{Arc, Mutex};
gating_test_env::run_large_stack_test("put-tagging-completion", || async {
let context = gating_test_env::shared_gating_ambient().await;
let store = context.object_store();
let usecase = ObjectMetaUsecase::with_context(Some(context));
let bucket = format!("put-tagging-{}", uuid::Uuid::new_v4().simple());
store.make_bucket(&bucket, &MakeBucketOptions::default()).await.unwrap();
let mut body = StoragePutObjReader::from_vec(b"preserved".to_vec());
store
.put_object(&bucket, "present", &mut body, &Default::default())
.await
.unwrap();
let probe = MutationCompletionProbe::install(&bucket);
let dirty = Arc::new(Mutex::new(Vec::new()));
let observe = {
let dirty = Arc::clone(&dirty);
let bucket = bucket.clone();
Arc::new(move |observed_bucket: &str, key: &str, producer| {
if observed_bucket == bucket && producer == SegmentInvalidationProducerIdentity::ObjectMetadata {
dirty.lock().unwrap().push(key.to_owned());
}
})
};
struct RestoreObserver(Option<rustfs_scanner::ScannerDirtyUsageMutationObserver>);
impl Drop for RestoreObserver {
fn drop(&mut self) {
set_scanner_dirty_usage_mutation_observer(self.0.take());
}
}
let _observer = RestoreObserver(set_scanner_dirty_usage_mutation_observer(Some(observe)));
let put = |key: &'static str| {
let mut req = build_request((), Method::PUT);
req.uri = format!("/{bucket}/{key}?tagging").parse().unwrap();
usecase.put_object_tagging_fields(
RequestEnvelope::from_s3s(&req),
&bucket,
key,
None,
vec![rustfs_gateway_types::dto::Tag {
key: Some("project".into()),
value: Some("approved".into()),
}],
)
};
let missing = put("missing").await.unwrap_err();
assert_eq!(missing.code(), &rustfs_s3_types::S3ErrorCode::NoSuchKey);
assert!(probe.events.lock().unwrap().is_empty(), "missing object emitted a tagging event");
assert!(dirty.lock().unwrap().is_empty(), "missing object marked usage dirty");
put("present").await.unwrap();
let events = probe.events.lock().unwrap();
assert_eq!(events.len(), 1, "one committed tagging event");
assert_eq!(events[0].event_name, rustfs_s3_types::EventName::ObjectTaggingPut);
assert_eq!(events[0].object.name, "present");
assert_eq!(*dirty.lock().unwrap(), ["present"], "one committed dirty marker");
});
}
@@ -23,7 +23,7 @@ use super::{
};
use aws_sdk_s3::Client;
use aws_sdk_s3::error::{ProvideErrorMetadata, SdkError};
use aws_sdk_s3::types::{AccessControlPolicy, Grant, Grantee, ObjectCannedAcl, Owner, Permission, Type};
use aws_sdk_s3::types::{AccessControlPolicy, Grant, Grantee, ObjectCannedAcl, Owner, Permission, Tag, Tagging, Type};
use futures::future::BoxFuture;
use rustfs::app::context::get_global_app_context;
use rustfs_ecstore::api::object::{ObjectOptions, PutObjReader};
@@ -66,6 +66,12 @@ pub(crate) const MATRIX: &[MatrixRow] = &[
has_error_body: true,
call: get_object_tagging,
},
MatrixRow {
operation: "PutObjectTagging",
metric: S3Operation::PutObjectTagging,
has_error_body: true,
call: put_object_tagging,
},
MatrixRow {
operation: "DeleteObjectTagging",
metric: S3Operation::DeleteObjectTagging,
@@ -119,6 +125,13 @@ fn get_object_tagging<'a>(client: &'a Client, _bucket: &'a str) -> BoxFuture<'a,
})
}
fn put_object_tagging<'a>(client: &'a Client, _bucket: &'a str) -> BoxFuture<'a, Outcome> {
Box::pin(async move {
put_object_in_the_store(MATRIX_BUCKET, KEY, HashMap::new()).await;
outcome(put_tags(client, MATRIX_BUCKET, KEY, None, &[]).send().await)
})
}
fn delete_object_tagging<'a>(client: &'a Client, _bucket: &'a str) -> BoxFuture<'a, Outcome> {
let bucket = MATRIX_BUCKET;
Box::pin(async move {
@@ -343,6 +356,335 @@ fn the_legacy_stack_answers_object_metadata_as_stored() {
with_stack(LEGACY, object_metadata_answers_as_stored);
}
fn put_tags(
client: &Client,
bucket: &str,
key: &str,
version: Option<&str>,
pairs: &[(&str, &str)],
) -> aws_sdk_s3::operation::put_object_tagging::builders::PutObjectTaggingFluentBuilder {
let tag_set = pairs
.iter()
.map(|(key, value)| Tag::builder().key(*key).value(*value).build().unwrap())
.collect();
let tagging = Tagging::builder().set_tag_set(Some(tag_set)).build().unwrap();
client
.put_object_tagging()
.bucket(bucket)
.key(key)
.set_version_id(version.map(str::to_owned))
.tagging(tagging)
}
async fn tagged_object(client: &Client, bucket: &str, key: &str, payload: &'static [u8], tagging: &str) -> Option<String> {
client
.put_object()
.bucket(bucket)
.key(key)
.tagging(tagging)
.body(aws_sdk_s3::primitives::ByteStream::from_static(payload))
.send()
.await
.unwrap()
.version_id()
.map(str::to_owned)
}
async fn object_payload(client: &Client, bucket: &str, key: &str, version: Option<&str>) -> (Option<String>, Vec<u8>) {
let output = client
.get_object()
.bucket(bucket)
.key(key)
.set_version_id(version.map(str::to_owned))
.send()
.await
.unwrap();
(
output.version_id().map(str::to_owned),
output.body.collect().await.unwrap().into_bytes().to_vec(),
)
}
fn tagging_operation_count() -> u64 {
rustfs_io_metrics::s3_op_metrics_snapshot()
.into_iter()
.find(|row| row.op == S3Operation::PutObjectTagging.as_str())
.map_or(0, |row| row.total)
}
async fn read_tags(client: &Client, bucket: &str, key: &str, version: Option<&str>) -> Vec<(String, String)> {
client
.get_object_tagging()
.bucket(bucket)
.key(key)
.set_version_id(version.map(str::to_owned))
.send()
.await
.unwrap()
.tag_set()
.iter()
.map(|tag| (tag.key().to_owned(), tag.value().to_owned()))
.collect()
}
async fn attach_tagging_policy(user: &str, action: &str, resource: &str, condition: serde_json::Value) {
super::create_user_without_a_policy(user).await;
let document = serde_json::json!({
"Version": "2012-10-17",
"Statement": [{"Effect": "Allow", "Action": [action], "Resource": [resource], "Condition": condition}]
})
.to_string();
let policy = rustfs_policy::policy::Policy::parse_config(document.as_bytes()).unwrap();
let iam = get_global_app_context().unwrap().iam().handle();
iam.set_policy(user, policy).await.unwrap();
iam.policy_db_set(user, rustfs_iam::store::UserType::Reg, false, user)
.await
.unwrap();
}
async fn put_object_tagging_authorization(server: rustfs::embedded::RustFSServer) {
const BUCKET: &str = "put-object-tagging-policy";
const OTHER: &str = "alternate";
const CURRENT: &str = "tagging-current";
const KEYS: &str = "tagging-keys";
const VERSION: &str = "tagging-version";
const WRONG: &str = "tagging-wrong-action";
let endpoint = server.endpoint();
let root = s3_client(&endpoint, server.access_key(), server.secret_key());
create_bucket_in_the_store(BUCKET).await;
for key in [KEY, OTHER] {
tagged_object(&root, BUCKET, key, b"preserved", "old=before").await;
}
let resource = format!("arn:aws:s3:::{BUCKET}/{KEY}");
attach_tagging_policy(
CURRENT,
"s3:PutObjectTagging",
&resource,
serde_json::json!({
"StringEquals": {"s3:RequestObjectTag/project": "approved"}
}),
)
.await;
attach_tagging_policy(
KEYS,
"s3:PutObjectTagging",
&resource,
serde_json::json!({
"ForAllValues:StringLike": {"s3:RequestObjectTagKeys": ["project"]}
}),
)
.await;
create_user_allowed_on_one_bucket(VERSION, "s3:PutObjectVersionTagging", &format!("{BUCKET}/{KEY}")).await;
create_user_allowed_on_one_bucket(WRONG, "s3:GetObjectTagging", &format!("{BUCKET}/{KEY}")).await;
let current = s3_client(&endpoint, CURRENT, USER_SECRET_KEY);
let keys = s3_client(&endpoint, KEYS, USER_SECRET_KEY);
let version = s3_client(&endpoint, VERSION, USER_SECRET_KEY);
let wrong = s3_client(&endpoint, WRONG, USER_SECRET_KEY);
let anonymous = super::anonymous_client(&endpoint).await;
let approved = [("project", "approved")];
let denied = [("project", "denied")];
let unexpected = [("forbidden", "approved")];
let old = [("old".to_owned(), "before".to_owned())];
let stored = [("project".to_owned(), "approved".to_owned())];
assert_eq!(read_tags(&root, BUCKET, KEY, None).await, old);
let grant = "project=approved";
let refuse = "project=denied";
for (label, client, key, named_version, pairs, header, allowed) in [
("XML value", &current, KEY, None, approved.as_slice(), refuse, true),
("spoofed value", &current, KEY, None, &denied, grant, false),
("empty XML set", &current, KEY, None, &[], grant, false),
("empty version", &current, KEY, Some(""), &approved, grant, false),
("null version", &current, KEY, Some("null"), &approved, grant, false),
("wrong resource", &current, OTHER, None, &approved, grant, false),
("XML key", &keys, KEY, None, &approved, "forbidden=approved", true),
("spoofed key", &keys, KEY, None, &unexpected, grant, false),
("version-only policy", &version, KEY, None, &denied, refuse, false),
("null version allowed", &version, KEY, Some("null"), &approved, refuse, true),
("wrong action", &wrong, KEY, None, &denied, refuse, false),
("anonymous", &anonymous, KEY, None, &denied, refuse, false),
] {
let before = tagging_operation_count();
let result = put_tags(client, BUCKET, key, named_version, pairs)
.customize()
.mutate_request(move |req| {
req.headers_mut().insert("x-amz-tagging", header);
})
.send()
.await;
if allowed {
assert_eq!(
result.unwrap_or_else(|error| panic!("{label}: {error:?}")).version_id(),
named_version,
"{label}"
);
} else {
assert_eq!(
refusal(&result.expect_err(label)),
(Some("AccessDenied".to_owned()), Some(403)),
"{label}"
);
}
assert_eq!(tagging_operation_count() - before, u64::from(allowed), "{label}");
for (key, expected) in [(KEY, &stored), (OTHER, &old)] {
assert_eq!(&read_tags(&root, BUCKET, key, None).await, expected, "{label}: {key}");
assert_eq!(
object_payload(&root, BUCKET, key, None).await,
(None, b"preserved".to_vec()),
"{label}: {key}"
);
}
}
server.shutdown().await;
}
#[test]
fn gateway_put_object_tagging_authorizes_body_version_and_resource() {
with_stack(GATEWAY, put_object_tagging_authorization);
}
#[test]
fn legacy_put_object_tagging_authorizes_body_version_and_resource() {
with_stack(LEGACY, put_object_tagging_authorization);
}
async fn put_object_tagging_validation(server: rustfs::embedded::RustFSServer) {
const BUCKET: &str = "put-object-tagging-validation";
create_bucket_in_the_store(BUCKET).await;
put_object_in_the_store(BUCKET, KEY, HashMap::new()).await;
let client = s3_client(&server.endpoint(), server.access_key(), server.secret_key());
let keys: Vec<_> = (0..11).map(|key| key.to_string()).collect();
let too_many: Vec<_> = keys.iter().map(|key| (key.as_str(), "")).collect();
let (long_key, long_value) = ("𐐀".repeat(65), "𐐀".repeat(129));
let duplicates = [("same", "one"), ("same", "two")];
for (pairs, version, code) in [
(too_many.as_slice(), None, "InvalidTag"),
(&[("", "value")], None, "InvalidTag"),
(&[("tag", "invalid?")], None, "InvalidTag"),
(&[(long_key.as_str(), "value")], None, "InvalidTag"),
(&[("tag", long_value.as_str())], None, "InvalidTag"),
(&duplicates, Some("bad"), "InvalidTag"),
(&[("tag", "value")], Some("bad"), "InvalidArgument"),
(&[("tag", "value")], Some(""), "InvalidArgument"),
] {
let result = put_tags(&client, BUCKET, KEY, version, pairs).send().await;
assert_eq!(refusal(&result.unwrap_err()), (Some(code.to_owned()), Some(400)), "{pairs:?} {version:?}");
assert!(read_tags(&client, BUCKET, KEY, None).await.is_empty(), "{pairs:?}");
}
let (key, value) = ("𐐀".repeat(64), "𐐀".repeat(128));
put_tags(&client, BUCKET, KEY, None, &[(&key, &value)]).send().await.unwrap();
assert_eq!(read_tags(&client, BUCKET, KEY, None).await, [(key, value)]);
server.shutdown().await;
}
#[test]
fn gateway_put_object_tagging_validates_tags_before_version() {
with_stack(GATEWAY, put_object_tagging_validation);
}
#[test]
fn legacy_put_object_tagging_validates_tags_before_version() {
with_stack(LEGACY, put_object_tagging_validation);
}
fn put_object_tagging_survives_restart(stack: Option<&'static str>) {
temp_env::with_var(super::ENV_S3_STACK, stack, || {
super::common::run_embedded_test(|| async {
use aws_sdk_s3::types::{BucketVersioningStatus, VersioningConfiguration};
use rustfs::embedded::{RustFSServerBuilder, find_available_port};
const BUCKET: &str = "put-object-tagging-history";
let volume = tempfile::tempdir().unwrap();
let start = || {
RustFSServerBuilder::new()
.address(format!("127.0.0.1:{}", find_available_port().unwrap()))
.access_key(super::ROOT_ACCESS_KEY)
.secret_key(super::ROOT_SECRET_KEY)
.volume(volume.path().to_string_lossy())
.build()
};
let server = start().await.unwrap();
let root = s3_client(&server.endpoint(), server.access_key(), server.secret_key());
create_bucket_in_the_store(BUCKET).await;
root.put_bucket_versioning()
.bucket(BUCKET)
.versioning_configuration(
VersioningConfiguration::builder()
.status(BucketVersioningStatus::Enabled)
.build(),
)
.send()
.await
.unwrap();
let first = tagged_object(&root, BUCKET, KEY, b"first", "old=first").await.unwrap();
let second = tagged_object(&root, BUCKET, KEY, b"second", "old=second").await.unwrap();
let first_tags = [
("project".to_owned(), "approved".to_owned()),
("with space".to_owned(), String::new()),
];
let current_tags = [("old".to_owned(), "second".to_owned())];
let notify = rustfs_notify::notification_system().expect("embedded notification runtime");
let mut events = notify.subscribe_live_events();
let resource = format!("arn:aws:s3:::{BUCKET}/{KEY}");
attach_tagging_policy(
"history-writer",
"s3:PutObjectVersionTagging",
&resource,
serde_json::json!({"StringEquals":{"s3:VersionId": first}}),
)
.await;
let writer = s3_client(&server.endpoint(), "history-writer", USER_SECRET_KEY);
let result = put_tags(&writer, BUCKET, KEY, Some(&first), &[("project", "approved"), ("with space", "")])
.send()
.await
.unwrap();
assert_eq!(result.version_id(), Some(first.as_str()));
let event = tokio::time::timeout(std::time::Duration::from_secs(5), async {
loop {
let event = events.recv().await.unwrap();
if event.event_name == rustfs_s3_types::EventName::ObjectTaggingPut {
break event;
}
}
})
.await
.expect("committed tagging event");
assert_eq!(event.s3.bucket.name, BUCKET);
assert_eq!(event.s3.object.key, KEY);
assert_eq!(event.s3.object.version_id.as_deref(), Some(first.as_str()));
server.shutdown().await;
let server = start().await.unwrap();
let root = s3_client(&server.endpoint(), server.access_key(), server.secret_key());
for (version, payload, expected) in [
(Some(first.as_str()), b"first".as_slice(), first_tags.as_slice()),
(None, b"second".as_slice(), current_tags.as_slice()),
] {
assert_eq!(read_tags(&root, BUCKET, KEY, version).await, expected);
let (seen_version, seen_payload) = object_payload(&root, BUCKET, KEY, version).await;
assert_eq!(seen_version.as_deref(), version.or(Some(second.as_str())));
assert_eq!(seen_payload, payload);
}
let cleared = put_tags(&root, BUCKET, KEY, None, &[]).send().await.unwrap();
assert!(cleared.version_id().is_none());
assert!(read_tags(&root, BUCKET, KEY, None).await.is_empty());
assert_eq!(read_tags(&root, BUCKET, KEY, Some(&first)).await, first_tags);
server.shutdown().await;
});
});
}
#[test]
fn gateway_put_object_tagging_replaces_stored_tags() {
put_object_tagging_survives_restart(GATEWAY);
}
#[test]
fn legacy_put_object_tagging_replaces_stored_tags() {
put_object_tagging_survives_restart(LEGACY);
}
/// The tagging actions swap to their version actions when a version is named: a user allowed only
/// the unversioned action is refused with a version, and one allowed only the version action is
/// refused without one.