mirror of
https://github.com/rustfs/rustfs.git
synced 2026-11-16 04:52:36 +00:00
feat(gateway): serve PutBucketNotificationConfiguration
Share one owned write path across the gateway and legacy edges. Project runtime destinations at each edge and defer consumption of the persisted conversion result until after filter validation. Keep IAM scope, the incarnation fence, and the existing validate, write, reload, clear, and add order. Count the request at its edge and audit it in the use case. Add three refusal tests and four tests that exercise success and failure. The legacy HTTP baseline passed on97a7639f. On6f93ac5fthe 38-test control passed. Six fault injections exposed wrong IAM action, wrong IAM resource, a missing incarnation fence, duplicate counting, the wrong audit status, and missing reload callbacks. The first three changed the stored bytes guarded by their refusal tests. The restored tree rebuilt and passed all 38 scoped tests. Both package Clippy configurations, script tests, test wiring, and prepush passed. Cross-node reload delivery and remote audit sink delivery were outside the local test scope.
This commit is contained in:
@@ -61,10 +61,7 @@ use super::storage_api::bucket_usecase::s3_api::bucket::{
|
||||
ListObjectVersionsParams, ListObjectsV2Params, encode_list_objects_v2_continuation_token, parse_list_object_versions_params,
|
||||
parse_list_objects_v2_params, rustfs_owner,
|
||||
};
|
||||
use super::storage_api::bucket_usecase::{
|
||||
get_validated_store, process_lambda_configurations, process_queue_configurations, process_topic_configurations,
|
||||
request_context, validate_list_object_unordered_with_delimiter,
|
||||
};
|
||||
use super::storage_api::bucket_usecase::{get_validated_store, request_context, validate_list_object_unordered_with_delimiter};
|
||||
#[cfg(feature = "diagnostic-read-version-caller")]
|
||||
use super::storage_api::diagnostic;
|
||||
use crate::app::RequestEnvelope;
|
||||
@@ -138,6 +135,7 @@ pub(crate) mod list_objects;
|
||||
mod listing;
|
||||
mod logging;
|
||||
mod notification;
|
||||
pub(crate) use notification::NotificationDestination;
|
||||
mod object_lock;
|
||||
mod policy;
|
||||
mod public_access_block;
|
||||
@@ -275,6 +273,23 @@ pub(crate) async fn notify_table_bucket_metadata_reload(
|
||||
notify_bucket_metadata_reload_with_context(bucket, operation, None, false, app_context).await;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
type BucketMetadataReloadObserver = Box<dyn Fn(bool, &'static str, bool) + Send + Sync>;
|
||||
|
||||
#[cfg(test)]
|
||||
tokio::task_local! {
|
||||
static BUCKET_METADATA_RELOAD_PROBE: (String, BucketMetadataReloadObserver);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn record_bucket_metadata_reload(bucket: &str, completed: bool, operation: &'static str, scanner_maintenance_change: bool) {
|
||||
let _ = BUCKET_METADATA_RELOAD_PROBE.try_with(|(observed_bucket, record)| {
|
||||
if observed_bucket == bucket {
|
||||
record(completed, operation, scanner_maintenance_change);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async fn notify_bucket_metadata_reload_with_context(
|
||||
bucket: String,
|
||||
operation: &'static str,
|
||||
@@ -282,6 +297,11 @@ async fn notify_bucket_metadata_reload_with_context(
|
||||
scanner_maintenance_change: bool,
|
||||
app_context: Option<Arc<AppContext>>,
|
||||
) {
|
||||
#[cfg(test)]
|
||||
record_bucket_metadata_reload(&bucket, false, operation, scanner_maintenance_change);
|
||||
#[cfg(test)]
|
||||
let observed_bucket = bucket.clone();
|
||||
|
||||
record_local_scanner_maintenance_reload(&bucket, scanner_maintenance_change);
|
||||
// Keep reload detached across request cancellation, but wait before a healthy peer can serve the previous config.
|
||||
let (completed_tx, completed_rx) = tokio::sync::oneshot::channel();
|
||||
@@ -303,7 +323,12 @@ async fn notify_bucket_metadata_reload_with_context(
|
||||
}
|
||||
let _ = completed_tx.send(());
|
||||
});
|
||||
let _ = completed_rx.await;
|
||||
let completed = completed_rx.await;
|
||||
#[cfg(test)]
|
||||
if completed.is_ok() {
|
||||
record_bucket_metadata_reload(&observed_bucket, true, operation, scanner_maintenance_change);
|
||||
}
|
||||
let _ = completed;
|
||||
}
|
||||
|
||||
fn record_local_scanner_maintenance_reload(bucket: &str, scanner_maintenance_change: bool) {
|
||||
|
||||
@@ -15,13 +15,19 @@
|
||||
//! Bucket notification configuration use-case operations.
|
||||
|
||||
use super::*;
|
||||
use crate::app::storage_api::s3::dto::{
|
||||
NotificationConfiguration, NotificationConfigurationFilter, PutBucketNotificationConfigurationInput,
|
||||
PutBucketNotificationConfigurationOutput,
|
||||
};
|
||||
use crate::app::storage_api::s3::region::Region;
|
||||
use crate::app::storage_api::s3::{S3Error, S3ErrorCode, S3Request, S3Response, S3Result, s3_error};
|
||||
use rustfs_gateway_types::persistence::parse_notification_dto;
|
||||
#[cfg(test)]
|
||||
use rustfs_s3_types::S3ErrorCode;
|
||||
use rustfs_s3_types::{S3Result, s3_error};
|
||||
|
||||
/// Runtime rules input retained alongside the prepared persisted configuration.
|
||||
pub(crate) struct NotificationDestination {
|
||||
pub(crate) kind: &'static str,
|
||||
pub(crate) id: Option<String>,
|
||||
pub(crate) events: Vec<String>,
|
||||
pub(crate) target_arn: String,
|
||||
pub(crate) filters: Vec<(Option<String>, Option<String>)>,
|
||||
}
|
||||
|
||||
fn is_valid_notification_filter_value(value: &str) -> bool {
|
||||
if value.len() > 1024 || value.contains('\\') {
|
||||
@@ -41,30 +47,24 @@ fn invalid_filter_name_message(cfg_scope: &str, name: &str) -> String {
|
||||
)
|
||||
}
|
||||
|
||||
fn validate_notification_filter_rules(
|
||||
filter: Option<&NotificationConfigurationFilter>,
|
||||
cfg_kind: &str,
|
||||
cfg_id: Option<&str>,
|
||||
) -> S3Result<()> {
|
||||
let Some(filter) = filter else {
|
||||
fn validate_notification_filter_rules(destination: &NotificationDestination) -> S3Result<()> {
|
||||
if destination.filters.is_empty() {
|
||||
return Ok(());
|
||||
};
|
||||
let Some(s3key_filter) = filter.key.as_ref() else {
|
||||
return Ok(());
|
||||
};
|
||||
let Some(rules) = s3key_filter.filter_rules.as_ref() else {
|
||||
return Ok(());
|
||||
};
|
||||
}
|
||||
|
||||
let mut has_prefix = false;
|
||||
let mut has_suffix = false;
|
||||
let cfg_scope = cfg_id.map_or_else(|| cfg_kind.to_string(), |id| format!("{cfg_kind} id={id}"));
|
||||
let kind = destination.kind;
|
||||
let cfg_scope = destination
|
||||
.id
|
||||
.as_ref()
|
||||
.map_or_else(|| kind.to_owned(), |id| format!("{kind} id={id}"));
|
||||
|
||||
for rule in rules {
|
||||
let Some(name) = rule.name.as_ref() else {
|
||||
for (name, value) in &destination.filters {
|
||||
let Some(name) = name.as_deref() else {
|
||||
return Err(s3_error!(InvalidArgument, "invalid notification filter rule: missing Name ({cfg_scope})"));
|
||||
};
|
||||
let Some(value) = rule.value.as_ref() else {
|
||||
let Some(value) = value.as_deref() else {
|
||||
return Err(s3_error!(
|
||||
InvalidArgument,
|
||||
"invalid notification filter rule: missing Value ({cfg_scope})"
|
||||
@@ -75,43 +75,24 @@ fn validate_notification_filter_rules(
|
||||
return Err(s3_error!(InvalidArgument, "{}", invalid_filter_value_message(&cfg_scope, value)));
|
||||
}
|
||||
|
||||
if name.as_str().eq_ignore_ascii_case("prefix") {
|
||||
if name.eq_ignore_ascii_case("prefix") {
|
||||
if has_prefix {
|
||||
return Err(s3_error!(InvalidArgument, "duplicate notification filter name 'prefix' ({cfg_scope})"));
|
||||
}
|
||||
has_prefix = true;
|
||||
} else if name.as_str().eq_ignore_ascii_case("suffix") {
|
||||
} else if name.eq_ignore_ascii_case("suffix") {
|
||||
if has_suffix {
|
||||
return Err(s3_error!(InvalidArgument, "duplicate notification filter name 'suffix' ({cfg_scope})"));
|
||||
}
|
||||
has_suffix = true;
|
||||
} else {
|
||||
return Err(s3_error!(InvalidArgument, "{}", invalid_filter_name_message(&cfg_scope, name.as_str())));
|
||||
return Err(s3_error!(InvalidArgument, "{}", invalid_filter_name_message(&cfg_scope, name)));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_notification_configuration_filters(notification_configuration: &NotificationConfiguration) -> S3Result<()> {
|
||||
if let Some(queue_configs) = notification_configuration.queue_configurations.as_ref() {
|
||||
for cfg in queue_configs {
|
||||
validate_notification_filter_rules(cfg.filter.as_ref(), "QueueConfiguration", cfg.id.as_deref())?;
|
||||
}
|
||||
}
|
||||
if let Some(topic_configs) = notification_configuration.topic_configurations.as_ref() {
|
||||
for cfg in topic_configs {
|
||||
validate_notification_filter_rules(cfg.filter.as_ref(), "TopicConfiguration", cfg.id.as_deref())?;
|
||||
}
|
||||
}
|
||||
if let Some(lambda_configs) = notification_configuration.lambda_function_configurations.as_ref() {
|
||||
for cfg in lambda_configs {
|
||||
validate_notification_filter_rules(cfg.filter.as_ref(), "LambdaFunctionConfiguration", cfg.id.as_deref())?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_notification_target_id(arn_str: &str) -> Result<TargetID, TargetIDError> {
|
||||
ARN::parse(arn_str)
|
||||
.map(|arn| arn.target_id)
|
||||
@@ -120,43 +101,34 @@ fn parse_notification_target_id(arn_str: &str) -> Result<TargetID, TargetIDError
|
||||
|
||||
type NotificationEventRule = (Vec<EventName>, String, String, Vec<TargetID>);
|
||||
|
||||
/// Builds the notify runtime rules for a bucket notification configuration
|
||||
/// without touching the store or the runtime rule state.
|
||||
fn build_notification_event_rules(
|
||||
notification_configuration: &NotificationConfiguration,
|
||||
) -> S3Result<Vec<NotificationEventRule>> {
|
||||
let mut event_rules = Vec::new();
|
||||
let invalid_arn = |e: TargetIDError| {
|
||||
S3Error::with_message(S3ErrorCode::InvalidArgument, format!("Invalid ARN in notification configuration: {e}"))
|
||||
};
|
||||
|
||||
process_queue_configurations(
|
||||
&mut event_rules,
|
||||
notification_configuration.queue_configurations.clone(),
|
||||
parse_notification_target_id,
|
||||
)
|
||||
.map_err(invalid_arn)?;
|
||||
process_topic_configurations(
|
||||
&mut event_rules,
|
||||
notification_configuration.topic_configurations.clone(),
|
||||
parse_notification_target_id,
|
||||
)
|
||||
.map_err(invalid_arn)?;
|
||||
process_lambda_configurations(
|
||||
&mut event_rules,
|
||||
notification_configuration.lambda_function_configurations.clone(),
|
||||
parse_notification_target_id,
|
||||
)
|
||||
.map_err(invalid_arn)?;
|
||||
|
||||
Ok(event_rules)
|
||||
fn build_notification_event_rules(destinations: Vec<NotificationDestination>) -> S3Result<Vec<NotificationEventRule>> {
|
||||
destinations
|
||||
.into_iter()
|
||||
.map(|destination| {
|
||||
let events = destination
|
||||
.events
|
||||
.iter()
|
||||
.filter_map(|event| EventName::parse(event).ok())
|
||||
.collect();
|
||||
let (mut prefix, mut suffix) = (String::new(), String::new());
|
||||
for (name, value) in destination.filters {
|
||||
if let (Some(name), Some(value)) = (name, value) {
|
||||
if name.eq_ignore_ascii_case("prefix") {
|
||||
prefix = value;
|
||||
} else if name.eq_ignore_ascii_case("suffix") {
|
||||
suffix = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
let target = parse_notification_target_id(&destination.target_arn)
|
||||
.map_err(|err| s3_error!(InvalidArgument, "Invalid ARN in notification configuration: {err}"))?;
|
||||
Ok((events, prefix, suffix, vec![target]))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn resolve_notification_region(global_region: Option<Region>, request_region: Option<Region>) -> String {
|
||||
global_region
|
||||
.or(request_region)
|
||||
.map(|region| region.to_string())
|
||||
.unwrap_or_else(|| RUSTFS_REGION.to_string())
|
||||
fn resolve_notification_region(global_region: Option<String>, request_region: Option<&str>) -> String {
|
||||
global_region.unwrap_or_else(|| request_region.unwrap_or(RUSTFS_REGION).to_owned())
|
||||
}
|
||||
|
||||
impl DefaultBucketUsecase {
|
||||
@@ -211,70 +183,74 @@ impl DefaultBucketUsecase {
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn execute_put_bucket_notification_configuration(
|
||||
/// Persists notification configuration, then updates the active rules for the bucket.
|
||||
pub(crate) async fn execute_put_bucket_notification_configuration(
|
||||
&self,
|
||||
req: S3Request<PutBucketNotificationConfigurationInput>,
|
||||
envelope: RequestEnvelope,
|
||||
bucket: &str,
|
||||
destinations: Vec<NotificationDestination>,
|
||||
prepared: S3Result<PersistedNotificationConfiguration>,
|
||||
) -> S3Result<S3Response<PutBucketNotificationConfigurationOutput>> {
|
||||
let expected_incarnation_id = bucket_config_mutation_incarnation(&req, &req.input.bucket)?;
|
||||
let request_region = req.region.clone();
|
||||
let request_context = req.extensions.get::<request_context::RequestContext>().cloned();
|
||||
) -> S3Result<gateway_dto::PutBucketNotificationConfigurationOutput> {
|
||||
RequestEnvelope::audit_only_operation(
|
||||
envelope,
|
||||
S3Operation::PutBucketNotificationConfiguration,
|
||||
StatusCode::OK,
|
||||
|envelope| async move {
|
||||
let expected_incarnation_id = bucket_config_mutation_incarnation(&envelope, bucket)?;
|
||||
let request_context = envelope.request_context().cloned();
|
||||
|
||||
let PutBucketNotificationConfigurationInput {
|
||||
bucket,
|
||||
notification_configuration,
|
||||
..
|
||||
} = req.input;
|
||||
for destination in &destinations {
|
||||
validate_notification_filter_rules(destination)?;
|
||||
}
|
||||
let persisted_notification_configuration = prepared?;
|
||||
|
||||
validate_notification_configuration_filters(¬ification_configuration)?;
|
||||
let persisted_notification_configuration = prepared?;
|
||||
let Some(store) = self.object_store() else {
|
||||
return Err(s3_error!(InternalError, "Not init"));
|
||||
};
|
||||
store
|
||||
.get_bucket_info(bucket, &BucketOptions::default())
|
||||
.await
|
||||
.map_err(ApiError::from)?;
|
||||
|
||||
let Some(store) = self.object_store() else {
|
||||
return Err(S3Error::with_message(S3ErrorCode::InternalError, "Not init".to_string()));
|
||||
};
|
||||
let region =
|
||||
resolve_notification_region(self.global_region().map(|region| region.to_string()), envelope.region());
|
||||
let notify = current_notify_interface_for_context(self.context.as_deref());
|
||||
let event_rules = build_notification_event_rules(destinations)?;
|
||||
|
||||
store
|
||||
.get_bucket_info(&bucket, &BucketOptions::default())
|
||||
.await
|
||||
.map_err(ApiError::from)?;
|
||||
// Validate before the write. Clear and add still run after the durable update.
|
||||
notify
|
||||
.validate_event_specific_rules(bucket, ®ion, &event_rules)
|
||||
.await
|
||||
.map_err(|e| s3_error!(InternalError, "Failed to add rules: {e}"))?;
|
||||
|
||||
let region = resolve_notification_region(self.global_region(), request_region);
|
||||
let notify = current_notify_interface_for_context(self.context.as_deref());
|
||||
let event_rules = build_notification_event_rules(¬ification_configuration)?;
|
||||
let data = serialize_config(&persisted_notification_configuration)?;
|
||||
update_bucket_config_for_incarnation(bucket, BUCKET_NOTIFICATION_CONFIG, data, expected_incarnation_id)
|
||||
.await
|
||||
.map_err(ApiError::from)?;
|
||||
|
||||
// Reject the request before the store write so a failure cannot leave a
|
||||
// persisted configuration that the notify runtime refused to activate.
|
||||
notify
|
||||
.validate_event_specific_rules(&bucket, region.as_str(), &event_rules)
|
||||
.await
|
||||
.map_err(|e| s3_error!(InternalError, "Failed to add rules: {e}"))?;
|
||||
notify_bucket_metadata_reload(bucket.to_owned(), "put bucket notification", request_context, false).await;
|
||||
|
||||
let data = serialize_config(&persisted_notification_configuration)?;
|
||||
update_bucket_config_for_incarnation(&bucket, BUCKET_NOTIFICATION_CONFIG, data, expected_incarnation_id)
|
||||
.await
|
||||
.map_err(ApiError::from)?;
|
||||
notify
|
||||
.clear_bucket_notification_rules(bucket)
|
||||
.await
|
||||
.map_err(|e| s3_error!(InternalError, "Failed to clear rules: {e}"))?;
|
||||
warn!("notify event rules: {:?}", &event_rules);
|
||||
notify
|
||||
.add_event_specific_rules(bucket, ®ion, &event_rules)
|
||||
.await
|
||||
.map_err(|e| s3_error!(InternalError, "Failed to add rules: {e}"))?;
|
||||
|
||||
notify_bucket_metadata_reload(bucket.clone(), "put bucket notification", request_context, false).await;
|
||||
|
||||
notify
|
||||
.clear_bucket_notification_rules(&bucket)
|
||||
.await
|
||||
.map_err(|e| s3_error!(InternalError, "Failed to clear rules: {e}"))?;
|
||||
warn!("notify event rules: {:?}", &event_rules);
|
||||
notify
|
||||
.add_event_specific_rules(&bucket, region.as_str(), &event_rules)
|
||||
.await
|
||||
.map_err(|e| s3_error!(InternalError, "Failed to add rules: {e}"))?;
|
||||
|
||||
Ok(S3Response::new(PutBucketNotificationConfigurationOutput {}))
|
||||
Ok(gateway_dto::PutBucketNotificationConfigurationOutput::default())
|
||||
},
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::app::bucket_usecase::test_support::build_request;
|
||||
use crate::app::storage_api::s3::dto::{FilterRule, FilterRuleName, QueueConfiguration, S3KeyFilter};
|
||||
use crate::app::bucket_usecase::test_support::{build_request, owned_envelope};
|
||||
use http::Method;
|
||||
|
||||
#[tokio::test]
|
||||
@@ -294,43 +270,40 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn resolve_notification_region_prefers_global_region() {
|
||||
let binding = resolve_notification_region(Some("us-east-1".parse().unwrap()), Some("ap-southeast-1".parse().unwrap()));
|
||||
assert_eq!(binding, "us-east-1");
|
||||
assert_eq!(
|
||||
resolve_notification_region(Some("us-east-1".to_owned()), Some("ap-southeast-1")),
|
||||
"us-east-1"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_notification_region_falls_back_to_request_region() {
|
||||
let binding = resolve_notification_region(None, Some("ap-southeast-1".parse().unwrap()));
|
||||
assert_eq!(binding, "ap-southeast-1");
|
||||
assert_eq!(resolve_notification_region(None, Some("ap-southeast-1")), "ap-southeast-1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_notification_region_defaults_value() {
|
||||
let binding = resolve_notification_region(None, None);
|
||||
assert_eq!(binding, RUSTFS_REGION);
|
||||
assert_eq!(resolve_notification_region(None, None), RUSTFS_REGION);
|
||||
}
|
||||
|
||||
fn destination(kind: &'static str, filters: &[(Option<&str>, Option<&str>)]) -> NotificationDestination {
|
||||
NotificationDestination {
|
||||
kind,
|
||||
id: Some("fixture".to_owned()),
|
||||
events: vec!["s3:ObjectCreated:*".to_owned()],
|
||||
target_arn: "arn:rustfs:sqs:us-east-1:1:webhook".to_owned(),
|
||||
filters: filters
|
||||
.iter()
|
||||
.map(|&(name, value)| (name.map(str::to_owned), value.map(str::to_owned)))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_notification_configuration_filters_rejects_invalid_filter_name() {
|
||||
let raw_name = "unsupported".repeat(100);
|
||||
let cfg = NotificationConfiguration {
|
||||
queue_configurations: Some(vec![QueueConfiguration {
|
||||
id: Some("q1".to_string()),
|
||||
queue_arn: "arn:rustfs:sqs:us-east-1:1:webhook".to_string(),
|
||||
events: vec!["s3:ObjectCreated:*".to_string().into()],
|
||||
filter: Some(NotificationConfigurationFilter {
|
||||
key: Some(S3KeyFilter {
|
||||
filter_rules: Some(vec![FilterRule {
|
||||
name: Some(FilterRuleName::from(raw_name.clone())),
|
||||
value: Some("uploads/".to_string()),
|
||||
}]),
|
||||
}),
|
||||
}),
|
||||
}]),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let err = validate_notification_configuration_filters(&cfg).unwrap_err();
|
||||
let cfg = destination("QueueConfiguration", &[(Some(&raw_name), Some("uploads/"))]);
|
||||
let err = validate_notification_filter_rules(&cfg).unwrap_err();
|
||||
assert_eq!(err.code(), &S3ErrorCode::InvalidArgument);
|
||||
let msg = err.message().unwrap_or_default();
|
||||
assert!(msg.contains("len="), "error message should include summarized length");
|
||||
@@ -339,81 +312,47 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn validate_notification_configuration_filters_accepts_case_insensitive_filter_names() {
|
||||
let cfg = NotificationConfiguration {
|
||||
queue_configurations: Some(vec![QueueConfiguration {
|
||||
id: Some("q1".to_string()),
|
||||
queue_arn: "arn:rustfs:sqs:us-east-1:1:webhook".to_string(),
|
||||
events: vec!["s3:ObjectCreated:*".to_string().into()],
|
||||
filter: Some(NotificationConfigurationFilter {
|
||||
key: Some(S3KeyFilter {
|
||||
filter_rules: Some(vec![
|
||||
FilterRule {
|
||||
name: Some(FilterRuleName::from("Prefix".to_string())),
|
||||
value: Some("uploads/".to_string()),
|
||||
},
|
||||
FilterRule {
|
||||
name: Some(FilterRuleName::from("Suffix".to_string())),
|
||||
value: Some(".csv".to_string()),
|
||||
},
|
||||
]),
|
||||
}),
|
||||
}),
|
||||
}]),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
validate_notification_configuration_filters(&cfg).expect("capitalized filter names should be accepted");
|
||||
let cfg = destination(
|
||||
"QueueConfiguration",
|
||||
&[(Some("Prefix"), Some("uploads/")), (Some("Suffix"), Some(".csv"))],
|
||||
);
|
||||
validate_notification_filter_rules(&cfg).expect("capitalized filter names should be accepted");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_notification_configuration_filters_rejects_duplicate_prefix_rules() {
|
||||
let cfg = NotificationConfiguration {
|
||||
queue_configurations: Some(vec![QueueConfiguration {
|
||||
id: Some("q1".to_string()),
|
||||
queue_arn: "arn:rustfs:sqs:us-east-1:1:webhook".to_string(),
|
||||
events: vec!["s3:ObjectCreated:*".to_string().into()],
|
||||
filter: Some(NotificationConfigurationFilter {
|
||||
key: Some(S3KeyFilter {
|
||||
filter_rules: Some(vec![
|
||||
FilterRule {
|
||||
name: Some(FilterRuleName::from_static(FilterRuleName::PREFIX)),
|
||||
value: Some("uploads/".to_string()),
|
||||
},
|
||||
FilterRule {
|
||||
name: Some(FilterRuleName::from_static(FilterRuleName::PREFIX)),
|
||||
value: Some("images/".to_string()),
|
||||
},
|
||||
]),
|
||||
}),
|
||||
}),
|
||||
}]),
|
||||
..Default::default()
|
||||
};
|
||||
for kind in ["QueueConfiguration"] {
|
||||
for duplicate in ["prefix", "PREFIX"] {
|
||||
let cfg = destination(kind, &[(Some("prefix"), Some("uploads/")), (Some(duplicate), Some("images/"))]);
|
||||
let err = validate_notification_filter_rules(&cfg).unwrap_err();
|
||||
assert_eq!(err.code(), &S3ErrorCode::InvalidArgument);
|
||||
assert_eq!(
|
||||
err.message(),
|
||||
Some(format!("duplicate notification filter name 'prefix' ({kind} id=fixture)").as_str())
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let err = validate_notification_configuration_filters(&cfg).unwrap_err();
|
||||
assert_eq!(err.code(), &S3ErrorCode::InvalidArgument);
|
||||
#[test]
|
||||
fn validate_notification_configuration_filters_rejects_duplicate_suffix_rules() {
|
||||
for kind in ["QueueConfiguration", "TopicConfiguration"] {
|
||||
for duplicate in ["suffix", "SUFFIX"] {
|
||||
let cfg = destination(kind, &[(Some("suffix"), Some(".csv")), (Some(duplicate), Some(".log"))]);
|
||||
let err = validate_notification_filter_rules(&cfg).unwrap_err();
|
||||
assert_eq!(err.code(), &S3ErrorCode::InvalidArgument);
|
||||
assert_eq!(
|
||||
err.message(),
|
||||
Some(format!("duplicate notification filter name 'suffix' ({kind} id=fixture)").as_str())
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_notification_configuration_filters_rejects_invalid_filter_value() {
|
||||
let cfg = NotificationConfiguration {
|
||||
queue_configurations: Some(vec![QueueConfiguration {
|
||||
id: Some("q1".to_string()),
|
||||
queue_arn: "arn:rustfs:sqs:us-east-1:1:webhook".to_string(),
|
||||
events: vec!["s3:ObjectCreated:*".to_string().into()],
|
||||
filter: Some(NotificationConfigurationFilter {
|
||||
key: Some(S3KeyFilter {
|
||||
filter_rules: Some(vec![FilterRule {
|
||||
name: Some(FilterRuleName::from_static(FilterRuleName::SUFFIX)),
|
||||
value: Some("../secret".to_string()),
|
||||
}]),
|
||||
}),
|
||||
}),
|
||||
}]),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let err = validate_notification_configuration_filters(&cfg).unwrap_err();
|
||||
let cfg = destination("QueueConfiguration", &[(Some("suffix"), Some("../secret"))]);
|
||||
let err = validate_notification_filter_rules(&cfg).unwrap_err();
|
||||
assert_eq!(err.code(), &S3ErrorCode::InvalidArgument);
|
||||
let msg = err.message().unwrap_or_default();
|
||||
assert!(msg.contains("len="), "error message should include summarized length");
|
||||
@@ -422,109 +361,127 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn validate_notification_configuration_filters_rejects_missing_filter_name() {
|
||||
let cfg = NotificationConfiguration {
|
||||
queue_configurations: Some(vec![QueueConfiguration {
|
||||
id: Some("q1".to_string()),
|
||||
queue_arn: "arn:rustfs:sqs:us-east-1:1:webhook".to_string(),
|
||||
events: vec!["s3:ObjectCreated:*".to_string().into()],
|
||||
filter: Some(NotificationConfigurationFilter {
|
||||
key: Some(S3KeyFilter {
|
||||
filter_rules: Some(vec![FilterRule {
|
||||
name: None,
|
||||
value: Some("uploads/".to_string()),
|
||||
}]),
|
||||
}),
|
||||
}),
|
||||
}]),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let err = validate_notification_configuration_filters(&cfg).unwrap_err();
|
||||
assert_eq!(err.code(), &S3ErrorCode::InvalidArgument);
|
||||
for (kind, value) in [
|
||||
("QueueConfiguration", Some("uploads/")),
|
||||
("LambdaFunctionConfiguration", None),
|
||||
] {
|
||||
let cfg = destination(kind, &[(None, value)]);
|
||||
let err = validate_notification_filter_rules(&cfg).unwrap_err();
|
||||
assert_eq!(err.code(), &S3ErrorCode::InvalidArgument);
|
||||
assert_eq!(
|
||||
err.message(),
|
||||
Some(format!("invalid notification filter rule: missing Name ({kind} id=fixture)").as_str())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_notification_configuration_filters_rejects_missing_filter_value() {
|
||||
let cfg = NotificationConfiguration {
|
||||
queue_configurations: Some(vec![QueueConfiguration {
|
||||
id: Some("q1".to_string()),
|
||||
queue_arn: "arn:rustfs:sqs:us-east-1:1:webhook".to_string(),
|
||||
events: vec!["s3:ObjectCreated:*".to_string().into()],
|
||||
filter: Some(NotificationConfigurationFilter {
|
||||
key: Some(S3KeyFilter {
|
||||
filter_rules: Some(vec![FilterRule {
|
||||
name: Some(FilterRuleName::from_static(FilterRuleName::PREFIX)),
|
||||
value: None,
|
||||
}]),
|
||||
}),
|
||||
}),
|
||||
}]),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let err = validate_notification_configuration_filters(&cfg).unwrap_err();
|
||||
let cfg = destination("QueueConfiguration", &[(Some("prefix"), None)]);
|
||||
let err = validate_notification_filter_rules(&cfg).unwrap_err();
|
||||
assert_eq!(err.code(), &S3ErrorCode::InvalidArgument);
|
||||
assert_eq!(
|
||||
err.message(),
|
||||
Some("invalid notification filter rule: missing Value (QueueConfiguration id=fixture)")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_notification_configuration_filters_rejects_duplicate_suffix_rules() {
|
||||
let cfg = NotificationConfiguration {
|
||||
queue_configurations: Some(vec![QueueConfiguration {
|
||||
id: Some("q1".to_string()),
|
||||
queue_arn: "arn:rustfs:sqs:us-east-1:1:webhook".to_string(),
|
||||
events: vec!["s3:ObjectCreated:*".to_string().into()],
|
||||
filter: Some(NotificationConfigurationFilter {
|
||||
key: Some(S3KeyFilter {
|
||||
filter_rules: Some(vec![
|
||||
FilterRule {
|
||||
name: Some(FilterRuleName::from_static(FilterRuleName::SUFFIX)),
|
||||
value: Some(".csv".to_string()),
|
||||
},
|
||||
FilterRule {
|
||||
name: Some(FilterRuleName::from_static(FilterRuleName::SUFFIX)),
|
||||
value: Some(".log".to_string()),
|
||||
},
|
||||
]),
|
||||
}),
|
||||
}),
|
||||
}]),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let err = validate_notification_configuration_filters(&cfg).unwrap_err();
|
||||
assert_eq!(err.code(), &S3ErrorCode::InvalidArgument);
|
||||
fn notification_filter_values_preserve_byte_and_path_boundaries() {
|
||||
let limit = "é".repeat(512);
|
||||
for value in ["", "a.b/..suffix/.prefix", &limit] {
|
||||
let cfg = destination("QueueConfiguration", &[(Some("prefix"), Some(value))]);
|
||||
validate_notification_filter_rules(&cfg).unwrap();
|
||||
}
|
||||
let too_long = format!("{limit}a");
|
||||
for value in ["a\\b", "a/./b", "a/../b", &too_long] {
|
||||
let cfg = destination("QueueConfiguration", &[(Some("unknown"), Some(value))]);
|
||||
let error = validate_notification_filter_rules(&cfg).unwrap_err();
|
||||
assert_eq!(error.code(), &S3ErrorCode::InvalidArgument);
|
||||
assert_eq!(
|
||||
error.message(),
|
||||
Some(format!("invalid notification filter value (len={}) (QueueConfiguration id=fixture)", value.len()).as_str())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn execute_put_bucket_notification_configuration_rejects_invalid_filter_before_store_lookup() {
|
||||
let input = PutBucketNotificationConfigurationInput::builder()
|
||||
.bucket("test-bucket".to_string())
|
||||
.notification_configuration(NotificationConfiguration {
|
||||
queue_configurations: Some(vec![QueueConfiguration {
|
||||
id: Some("q1".to_string()),
|
||||
queue_arn: "arn:rustfs:sqs:us-east-1:1:webhook".to_string(),
|
||||
events: vec!["s3:ObjectCreated:*".to_string().into()],
|
||||
filter: Some(NotificationConfigurationFilter {
|
||||
key: Some(S3KeyFilter {
|
||||
filter_rules: Some(vec![FilterRule {
|
||||
name: Some(FilterRuleName::from("unsupported".to_string())),
|
||||
value: Some("uploads/".to_string()),
|
||||
}]),
|
||||
}),
|
||||
}),
|
||||
}]),
|
||||
..Default::default()
|
||||
})
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let req = build_request(input, Method::PUT);
|
||||
let usecase = DefaultBucketUsecase::without_context();
|
||||
|
||||
let err = usecase
|
||||
.execute_put_bucket_notification_configuration(req, Ok(PersistedNotificationConfiguration::default()))
|
||||
let bucket = "test-bucket";
|
||||
let error = usecase
|
||||
.execute_put_bucket_notification_configuration(
|
||||
owned_envelope(Method::PUT),
|
||||
bucket,
|
||||
vec![destination("QueueConfiguration", &[(Some("unsupported"), Some("uploads/"))])],
|
||||
Ok(PersistedNotificationConfiguration::default()),
|
||||
)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(err.code(), &S3ErrorCode::InvalidArgument);
|
||||
assert_eq!(error.code(), &S3ErrorCode::InvalidArgument);
|
||||
|
||||
let invalid = [
|
||||
destination("QueueConfiguration", &[(Some("unsupported"), Some("uploads/"))]),
|
||||
destination("TopicConfiguration", &[(None, None)]),
|
||||
];
|
||||
let error = usecase
|
||||
.execute_put_bucket_notification_configuration(
|
||||
owned_envelope(Method::PUT),
|
||||
bucket,
|
||||
invalid.into(),
|
||||
Err(s3_error!(MalformedXML, "persistence conversion")),
|
||||
)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(error.code(), &S3ErrorCode::InvalidArgument);
|
||||
assert_eq!(
|
||||
error.message(),
|
||||
Some(
|
||||
"invalid notification filter name (len=11) (only 'prefix'/'suffix' are supported) (QueueConfiguration id=fixture)"
|
||||
)
|
||||
);
|
||||
|
||||
let error = usecase
|
||||
.execute_put_bucket_notification_configuration(
|
||||
owned_envelope(Method::PUT),
|
||||
bucket,
|
||||
Vec::new(),
|
||||
Err(s3_error!(MalformedXML, "persistence conversion")),
|
||||
)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(error.code(), &S3ErrorCode::MalformedXML);
|
||||
assert_eq!(error.message(), Some("persistence conversion"));
|
||||
|
||||
let mut request = build_request((), Method::PUT);
|
||||
request.extensions.insert(crate::runtime_sources::ServerContextSlot::new());
|
||||
let error = usecase
|
||||
.execute_put_bucket_notification_configuration(
|
||||
RequestEnvelope::from_s3s(&request),
|
||||
bucket,
|
||||
vec![destination("QueueConfiguration", &[(None, None)])],
|
||||
Err(s3_error!(MalformedXML, "persistence conversion")),
|
||||
)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(error.code(), &S3ErrorCode::InternalError);
|
||||
assert_eq!(error.message(), Some("bucket config mutation snapshot is missing"));
|
||||
|
||||
let mut invalid_arn = destination("QueueConfiguration", &[]);
|
||||
invalid_arn.target_arn = "invalid-arn".to_owned();
|
||||
let error = usecase
|
||||
.execute_put_bucket_notification_configuration(
|
||||
owned_envelope(Method::PUT),
|
||||
bucket,
|
||||
vec![invalid_arn],
|
||||
Ok(PersistedNotificationConfiguration::default()),
|
||||
)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(error.code(), &S3ErrorCode::InternalError);
|
||||
assert_eq!(error.message(), Some("Not init"));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod side_effect_tests;
|
||||
|
||||
@@ -0,0 +1,459 @@
|
||||
// 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.
|
||||
|
||||
//! Store and callback observations through the legacy Put entrypoint and shared use case.
|
||||
|
||||
use super::*;
|
||||
use crate::app::bucket_usecase::test_support::build_request_with_req_info;
|
||||
use crate::app::gating_test_env::{app_context_from_current_environment, run_large_stack_test, shared_gating_ambient};
|
||||
use crate::app::storage_api::bucket_usecase::access::ReqInfo;
|
||||
use crate::app::storage_api::bucket_usecase::helper::MutationCompletionProbe;
|
||||
use crate::app::storage_api::s3::dto::{
|
||||
FilterRule, LambdaFunctionConfiguration, NotificationConfiguration, NotificationConfigurationFilter,
|
||||
PutBucketNotificationConfigurationInput, QueueConfiguration, S3KeyFilter, TopicConfiguration,
|
||||
};
|
||||
use crate::app::storage_api::s3::{S3, S3Access, S3ErrorCode, S3Request};
|
||||
use crate::app::storage_api::test::ecfs::FS;
|
||||
use crate::runtime_sources::{NotifyInterface, ServerContextSlot};
|
||||
use http::Method;
|
||||
use rustfs_notify::{EventArgs, NotificationError};
|
||||
use std::sync::Mutex;
|
||||
|
||||
const EMPTY_XML: &[u8] = b"<NotificationConfiguration></NotificationConfiguration>";
|
||||
const OLD_XML: &[u8] =
|
||||
b"<NotificationConfiguration><EventBridgeConfiguration></EventBridgeConfiguration></NotificationConfiguration>";
|
||||
const NEW_XML: &[u8] = concat!(
|
||||
"<NotificationConfiguration>",
|
||||
"<CloudFunctionConfiguration><Event>s3:Future:Unrecognized</Event><Id>lambda</Id><CloudFunction>arn:rustfs:sqs:us-east-1:3:webhook</CloudFunction></CloudFunctionConfiguration>",
|
||||
"<QueueConfiguration><Event>s3:ObjectCreated:*</Event><Event>s3:Future:Unrecognized</Event>",
|
||||
"<Filter><S3Key><FilterRule><Name>Prefix</Name><Value>logs/%2F</Value></FilterRule>",
|
||||
"<FilterRule><Name>Suffix</Name><Value>.csv</Value></FilterRule></S3Key></Filter>",
|
||||
"<Id>queue</Id><Queue>arn:rustfs:sqs:us-east-1:1:webhook</Queue></QueueConfiguration>",
|
||||
"<TopicConfiguration><Event>s3:ObjectRemoved:*</Event><Id>topic</Id><Topic>arn:rustfs:sqs:us-east-1:2:webhook</Topic></TopicConfiguration>",
|
||||
"</NotificationConfiguration>"
|
||||
).as_bytes();
|
||||
|
||||
type RuntimeRule = (Vec<EventName>, String, String, Vec<TargetID>);
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum Callback {
|
||||
Validate,
|
||||
ReloadStarted,
|
||||
ReloadCompleted,
|
||||
Clear,
|
||||
Add,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
enum Observation {
|
||||
Rules(Callback, String, Vec<RuntimeRule>, Vec<u8>),
|
||||
Reload(Callback, &'static str, bool),
|
||||
Clear(Vec<u8>),
|
||||
}
|
||||
|
||||
struct RecordingNotify {
|
||||
bucket: String,
|
||||
fail_at: Option<Callback>,
|
||||
calls: Arc<Mutex<Vec<Observation>>>,
|
||||
active_rules: Mutex<Vec<RuntimeRule>>,
|
||||
}
|
||||
|
||||
async fn read_notification_xml(bucket: &str) -> Vec<u8> {
|
||||
crate::app::storage_api::test::get_global_bucket_metadata_sys()
|
||||
.expect("test bucket metadata")
|
||||
.read()
|
||||
.await
|
||||
.get_config_from_disk(bucket)
|
||||
.await
|
||||
.expect("load notification bytes from disk")
|
||||
.notification_config_xml
|
||||
}
|
||||
|
||||
impl RecordingNotify {
|
||||
fn maybe_fail(&self, callback: Callback) -> Result<(), NotificationError> {
|
||||
if self.fail_at == Some(callback) {
|
||||
return Err(NotificationError::RuleConfiguration("fixture failure".into()));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn record_rules(&self, callback: Callback, bucket: &str, region: &str, rules: &[RuntimeRule]) {
|
||||
assert_eq!(bucket, self.bucket);
|
||||
let xml = read_notification_xml(bucket).await;
|
||||
self.calls
|
||||
.lock()
|
||||
.unwrap()
|
||||
.push(Observation::Rules(callback, region.to_owned(), rules.to_vec(), xml));
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl NotifyInterface for RecordingNotify {
|
||||
async fn notify(&self, _args: EventArgs) {
|
||||
panic!("bucket notification configuration must not emit an object event");
|
||||
}
|
||||
|
||||
async fn validate_event_specific_rules(
|
||||
&self,
|
||||
bucket: &str,
|
||||
region: &str,
|
||||
rules: &[RuntimeRule],
|
||||
) -> Result<(), NotificationError> {
|
||||
self.record_rules(Callback::Validate, bucket, region, rules).await;
|
||||
self.maybe_fail(Callback::Validate)
|
||||
}
|
||||
|
||||
async fn add_event_specific_rules(&self, bucket: &str, region: &str, rules: &[RuntimeRule]) -> Result<(), NotificationError> {
|
||||
self.record_rules(Callback::Add, bucket, region, rules).await;
|
||||
self.maybe_fail(Callback::Add)?;
|
||||
*self.active_rules.lock().unwrap() = rules.to_vec();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn clear_bucket_notification_rules(&self, bucket: &str) -> Result<(), NotificationError> {
|
||||
assert_eq!(bucket, self.bucket);
|
||||
let xml = read_notification_xml(bucket).await;
|
||||
self.calls.lock().unwrap().push(Observation::Clear(xml));
|
||||
self.maybe_fail(Callback::Clear)?;
|
||||
self.active_rules.lock().unwrap().clear();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn configuration() -> NotificationConfiguration {
|
||||
NotificationConfiguration {
|
||||
queue_configurations: Some(vec![QueueConfiguration {
|
||||
id: Some("queue".into()),
|
||||
events: vec![
|
||||
"s3:ObjectCreated:*".to_owned().into(),
|
||||
"s3:Future:Unrecognized".to_owned().into(),
|
||||
],
|
||||
queue_arn: "arn:rustfs:sqs:us-east-1:1:webhook".into(),
|
||||
filter: Some(NotificationConfigurationFilter {
|
||||
key: Some(S3KeyFilter {
|
||||
filter_rules: Some(vec![
|
||||
FilterRule {
|
||||
name: Some("Prefix".to_owned().into()),
|
||||
value: Some("logs/%2F".into()),
|
||||
},
|
||||
FilterRule {
|
||||
name: Some("Suffix".to_owned().into()),
|
||||
value: Some(".csv".into()),
|
||||
},
|
||||
]),
|
||||
}),
|
||||
}),
|
||||
}]),
|
||||
topic_configurations: Some(vec![TopicConfiguration {
|
||||
id: Some("topic".into()),
|
||||
events: vec!["s3:ObjectRemoved:*".to_owned().into()],
|
||||
topic_arn: "arn:rustfs:sqs:us-east-1:2:webhook".into(),
|
||||
filter: None,
|
||||
}]),
|
||||
lambda_function_configurations: Some(vec![LambdaFunctionConfiguration {
|
||||
id: Some("lambda".into()),
|
||||
events: vec!["s3:Future:Unrecognized".to_owned().into()],
|
||||
lambda_function_arn: "arn:rustfs:sqs:us-east-1:3:webhook".into(),
|
||||
filter: None,
|
||||
}]),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn expected_rules() -> Vec<RuntimeRule> {
|
||||
[
|
||||
(vec![EventName::parse("s3:ObjectCreated:*").unwrap()], "logs/%2F", ".csv", "1"),
|
||||
(vec![EventName::parse("s3:ObjectRemoved:*").unwrap()], "", "", "2"),
|
||||
(Vec::new(), "", "", "3"),
|
||||
]
|
||||
.map(|(events, prefix, suffix, id)| {
|
||||
(
|
||||
events,
|
||||
prefix.to_owned(),
|
||||
suffix.to_owned(),
|
||||
vec![TargetID {
|
||||
id: id.to_owned(),
|
||||
name: "webhook".into(),
|
||||
}],
|
||||
)
|
||||
})
|
||||
.into()
|
||||
}
|
||||
|
||||
fn notification_request(
|
||||
bucket: &str,
|
||||
configuration: NotificationConfiguration,
|
||||
slot: &Arc<ServerContextSlot>,
|
||||
) -> S3Request<PutBucketNotificationConfigurationInput> {
|
||||
let input = PutBucketNotificationConfigurationInput::builder()
|
||||
.bucket(bucket.to_owned())
|
||||
.notification_configuration(configuration)
|
||||
.build()
|
||||
.unwrap();
|
||||
let mut request = build_request_with_req_info(
|
||||
input,
|
||||
Method::PUT,
|
||||
ReqInfo {
|
||||
bucket: Some(bucket.to_owned()),
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
request.extensions.insert(slot.clone());
|
||||
request.uri = format!("/{bucket}?notification").parse().unwrap();
|
||||
request.region = Some("eu-west-2".parse().unwrap());
|
||||
request
|
||||
}
|
||||
|
||||
fn check_notification_write_order() {
|
||||
run_large_stack_test("notification-write-order", || async {
|
||||
let ambient = shared_gating_ambient().await;
|
||||
let store = ambient.object_store();
|
||||
for fail_at in [None, Some(Callback::Validate), Some(Callback::Clear), Some(Callback::Add)] {
|
||||
let bucket = format!("notify-order-{}", uuid::Uuid::new_v4().simple());
|
||||
store.make_bucket(&bucket, &MakeBucketOptions::default()).await.unwrap();
|
||||
store
|
||||
.update_bucket_metadata_config(&bucket, BUCKET_NOTIFICATION_CONFIG, OLD_XML.to_vec())
|
||||
.await
|
||||
.unwrap();
|
||||
let policy = serde_json::json!({
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [{"Effect": "Allow", "Principal": {"AWS": "*"}, "Action": ["s3:PutBucketNotification"],
|
||||
"Resource": [format!("arn:aws:s3:::{bucket}")]}]
|
||||
});
|
||||
store
|
||||
.update_bucket_metadata_config(&bucket, BUCKET_POLICY_CONFIG, serde_json::to_vec(&policy).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let calls = Arc::new(Mutex::new(Vec::new()));
|
||||
let recorder = Arc::new(RecordingNotify {
|
||||
bucket: bucket.clone(),
|
||||
fail_at,
|
||||
calls: calls.clone(),
|
||||
active_rules: Mutex::new(expected_rules()),
|
||||
});
|
||||
let context = Arc::new(app_context_from_current_environment(&ambient).with_test_notify_interface(recorder.clone()));
|
||||
let region = context
|
||||
.region()
|
||||
.get()
|
||||
.map_or_else(|| "eu-west-2".to_owned(), |region| region.to_string());
|
||||
let slot = ServerContextSlot::new();
|
||||
assert!(slot.install(context.clone()));
|
||||
let fs = FS::with_server_ctx(slot.clone());
|
||||
let audit = MutationCompletionProbe::install(&bucket);
|
||||
let mut request = notification_request(&bucket, configuration(), &slot);
|
||||
S3Access::put_bucket_notification_configuration(&fs, &mut request)
|
||||
.await
|
||||
.unwrap();
|
||||
let result = observe_reload(&bucket, calls.clone(), S3::put_bucket_notification_configuration(&fs, request)).await;
|
||||
let expected = expected_rules();
|
||||
let mut timeline = vec![Observation::Rules(
|
||||
Callback::Validate,
|
||||
region.clone(),
|
||||
expected.clone(),
|
||||
OLD_XML.to_vec(),
|
||||
)];
|
||||
if fail_at != Some(Callback::Validate) {
|
||||
timeline.extend([
|
||||
Observation::Reload(Callback::ReloadStarted, "put bucket notification", false),
|
||||
Observation::Reload(Callback::ReloadCompleted, "put bucket notification", false),
|
||||
Observation::Clear(NEW_XML.to_vec()),
|
||||
]);
|
||||
if fail_at != Some(Callback::Clear) {
|
||||
timeline.push(Observation::Rules(Callback::Add, region.clone(), expected.clone(), NEW_XML.to_vec()));
|
||||
}
|
||||
}
|
||||
assert_eq!(*calls.lock().unwrap(), timeline);
|
||||
let stored = if fail_at == Some(Callback::Validate) {
|
||||
OLD_XML
|
||||
} else {
|
||||
NEW_XML
|
||||
};
|
||||
assert_eq!(read_notification_xml(&bucket).await, stored);
|
||||
let error_message = match fail_at {
|
||||
Some(Callback::Clear) => Some("Failed to clear rules: Rule configuration error: fixture failure"),
|
||||
Some(_) => Some("Failed to add rules: Rule configuration error: fixture failure"),
|
||||
None => None,
|
||||
};
|
||||
if let Some(message) = error_message {
|
||||
let error = result.expect_err("injected callback failure");
|
||||
assert_eq!(error.code(), &S3ErrorCode::InternalError);
|
||||
assert_eq!(error.message(), Some(message));
|
||||
} else {
|
||||
result.expect("configuration written");
|
||||
}
|
||||
{
|
||||
let audits = audit.audits.lock().unwrap();
|
||||
let [entry] = audits.as_slice() else { panic!("one notification audit: {audits:?}") };
|
||||
assert_eq!(entry.api.name.as_deref(), Some(S3Operation::PutBucketNotificationConfiguration.as_str()));
|
||||
assert_eq!(entry.api.bucket.as_deref(), Some(bucket.as_str()));
|
||||
assert_eq!(entry.api.status_code, Some(if fail_at.is_some() { 500 } else { 200 }));
|
||||
assert_eq!(entry.error.as_deref().filter(|message| !message.is_empty()), error_message);
|
||||
}
|
||||
assert!(audit.events.lock().unwrap().is_empty());
|
||||
|
||||
if fail_at.is_none() {
|
||||
assert_eq!(*recorder.active_rules.lock().unwrap(), expected);
|
||||
let mut invalid = configuration();
|
||||
invalid.lambda_function_configurations.as_mut().unwrap()[0].lambda_function_arn = "invalid-arn".into();
|
||||
let mut invalid = notification_request(&bucket, invalid, &slot);
|
||||
S3Access::put_bucket_notification_configuration(&fs, &mut invalid)
|
||||
.await
|
||||
.unwrap();
|
||||
calls.lock().unwrap().clear();
|
||||
let err = observe_reload(&bucket, calls.clone(), S3::put_bucket_notification_configuration(&fs, invalid))
|
||||
.await
|
||||
.expect_err("the last destination has an invalid ARN");
|
||||
assert_eq!(err.code(), &S3ErrorCode::InvalidArgument);
|
||||
assert!(
|
||||
err.message()
|
||||
.unwrap()
|
||||
.starts_with("Invalid ARN in notification configuration: ")
|
||||
);
|
||||
assert!(
|
||||
calls.lock().unwrap().is_empty(),
|
||||
"validation happens after all destinations have been built"
|
||||
);
|
||||
assert_eq!(read_notification_xml(&bucket).await, NEW_XML);
|
||||
let usecase = DefaultBucketUsecase::with_context(Some(context));
|
||||
let output = usecase
|
||||
.execute_get_bucket_notification_configuration(gateway_dto::GetBucketNotificationConfigurationInput {
|
||||
bucket: super::super::test_support::gateway_bucket(&bucket),
|
||||
..Default::default()
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(output.queue_configurations[0].events[1].as_str(), "s3:Future:Unrecognized");
|
||||
|
||||
calls.lock().unwrap().clear();
|
||||
let mut empty = notification_request(&bucket, NotificationConfiguration::default(), &slot);
|
||||
S3Access::put_bucket_notification_configuration(&fs, &mut empty)
|
||||
.await
|
||||
.unwrap();
|
||||
observe_reload(&bucket, calls.clone(), S3::put_bucket_notification_configuration(&fs, empty))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
*calls.lock().unwrap(),
|
||||
[
|
||||
Observation::Rules(Callback::Validate, region.clone(), Vec::new(), NEW_XML.to_vec()),
|
||||
Observation::Reload(Callback::ReloadStarted, "put bucket notification", false),
|
||||
Observation::Reload(Callback::ReloadCompleted, "put bucket notification", false),
|
||||
Observation::Clear(EMPTY_XML.to_vec()),
|
||||
Observation::Rules(Callback::Add, region, Vec::new(), EMPTY_XML.to_vec()),
|
||||
]
|
||||
);
|
||||
assert_eq!(read_notification_xml(&bucket).await, EMPTY_XML);
|
||||
assert!(recorder.active_rules.lock().unwrap().is_empty());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial_test::serial]
|
||||
fn legacy_put_notification_keeps_callback_order_and_durable_bytes() {
|
||||
temp_env::with_var(rustfs_config::ENV_AUDIT_ENABLE, Some("true"), || {
|
||||
crate::server::refresh_audit_module_enabled();
|
||||
check_notification_write_order();
|
||||
});
|
||||
crate::server::refresh_audit_module_enabled();
|
||||
}
|
||||
|
||||
async fn observe_reload<T>(bucket: &str, calls: Arc<Mutex<Vec<Observation>>>, operation: impl Future<Output = T>) -> T {
|
||||
super::super::BUCKET_METADATA_RELOAD_PROBE
|
||||
.scope(
|
||||
(
|
||||
bucket.to_owned(),
|
||||
Box::new(move |completed, operation, scanner| {
|
||||
let callback = if completed {
|
||||
Callback::ReloadCompleted
|
||||
} else {
|
||||
Callback::ReloadStarted
|
||||
};
|
||||
calls.lock().unwrap().push(Observation::Reload(callback, operation, scanner));
|
||||
}),
|
||||
),
|
||||
operation,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_put_notification_does_not_write_after_the_bucket_is_recreated() {
|
||||
run_large_stack_test("notification-stale-incarnation", || async {
|
||||
let ambient = shared_gating_ambient().await;
|
||||
let store = ambient.object_store();
|
||||
let bucket = format!("notify-stale-{}", uuid::Uuid::new_v4().simple());
|
||||
store.make_bucket(&bucket, &MakeBucketOptions::default()).await.unwrap();
|
||||
let first = store.bucket_incarnation_id(&bucket).await.unwrap();
|
||||
let policy = serde_json::json!({
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [{"Effect": "Allow", "Principal": {"AWS": "*"}, "Action": ["s3:PutBucketNotification"],
|
||||
"Resource": [format!("arn:aws:s3:::{bucket}")]}]
|
||||
});
|
||||
store
|
||||
.update_bucket_metadata_config(&bucket, BUCKET_POLICY_CONFIG, serde_json::to_vec(&policy).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
let calls = Arc::new(Mutex::new(Vec::new()));
|
||||
let recorder = Arc::new(RecordingNotify {
|
||||
bucket: bucket.clone(),
|
||||
fail_at: None,
|
||||
calls: calls.clone(),
|
||||
active_rules: Mutex::new(Vec::new()),
|
||||
});
|
||||
let context = Arc::new(app_context_from_current_environment(&ambient).with_test_notify_interface(recorder));
|
||||
let slot = ServerContextSlot::new();
|
||||
assert!(slot.install(context));
|
||||
let fs = FS::with_server_ctx(slot.clone());
|
||||
let mut request = notification_request(&bucket, configuration(), &slot);
|
||||
S3Access::put_bucket_notification_configuration(&fs, &mut request)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(bucket_config_mutation_incarnation(&request, &bucket).unwrap(), Some(first));
|
||||
|
||||
let mismatch = DefaultBucketUsecase::without_context()
|
||||
.execute_put_bucket_notification_configuration(
|
||||
RequestEnvelope::from_s3s(&request),
|
||||
"other-bucket",
|
||||
vec![NotificationDestination {
|
||||
kind: "QueueConfiguration",
|
||||
id: None,
|
||||
events: Vec::new(),
|
||||
target_arn: String::new(),
|
||||
filters: vec![(None, None)],
|
||||
}],
|
||||
Err(rustfs_s3_types::s3_error!(MalformedXML, "persistence conversion")),
|
||||
)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(mismatch.code(), &rustfs_s3_types::S3ErrorCode::InternalError);
|
||||
assert_eq!(mismatch.message(), Some("bucket config mutation snapshot does not match request bucket"));
|
||||
store.delete_bucket(&bucket, &DeleteBucketOptions::default()).await.unwrap();
|
||||
store.make_bucket(&bucket, &MakeBucketOptions::default()).await.unwrap();
|
||||
assert_ne!(first, store.bucket_incarnation_id(&bucket).await.unwrap());
|
||||
store
|
||||
.update_bucket_metadata_config(&bucket, BUCKET_NOTIFICATION_CONFIG, OLD_XML.to_vec())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let result = observe_reload(&bucket, calls.clone(), S3::put_bucket_notification_configuration(&fs, request)).await;
|
||||
assert_eq!(read_notification_xml(&bucket).await, OLD_XML);
|
||||
assert_eq!(result.unwrap_err().code(), &S3ErrorCode::NoSuchBucket);
|
||||
assert!(
|
||||
matches!(calls.lock().unwrap().as_slice(), [Observation::Rules(Callback::Validate, _, _, xml)] if xml == OLD_XML)
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -377,6 +377,11 @@ impl AppContext {
|
||||
self
|
||||
}
|
||||
|
||||
pub(crate) fn with_test_notify_interface(mut self, notify: Arc<dyn NotifyInterface>) -> Self {
|
||||
self.notify = notify;
|
||||
self
|
||||
}
|
||||
|
||||
pub(crate) fn with_test_notification_system_interface(
|
||||
mut self,
|
||||
notification_system: Arc<dyn NotificationSystemInterface>,
|
||||
|
||||
@@ -1752,8 +1752,7 @@ pub(crate) mod bucket_usecase {
|
||||
|
||||
pub(crate) use super::{access, bucket, error, helper, object_utils, request_context, s3, s3_api, sse};
|
||||
pub(crate) use crate::storage::storage_api::{
|
||||
ECStore, StorageObjectInfo, StorageObjectOptions, get_validated_store, process_lambda_configurations,
|
||||
process_queue_configurations, process_topic_configurations, validate_list_object_unordered_with_delimiter,
|
||||
ECStore, StorageObjectInfo, StorageObjectOptions, get_validated_store, validate_list_object_unordered_with_delimiter,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -17,11 +17,13 @@
|
||||
use super::access::authorize_bucket_config_mutation;
|
||||
use super::{OpRow, Served, row};
|
||||
use crate::app::RequestEnvelope;
|
||||
use crate::app::bucket_usecase::DefaultBucketUsecase;
|
||||
use crate::app::bucket_usecase::{DefaultBucketUsecase, NotificationDestination};
|
||||
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_gateway_types::persistence::{
|
||||
parse_notification, parse_versioning, serialize_notification_dto, serialize_versioning_dto,
|
||||
};
|
||||
use rustfs_policy::policy::action::{Action, S3Action};
|
||||
use rustfs_s3_ops::S3Operation;
|
||||
use rustfs_s3_types::S3Result;
|
||||
@@ -34,6 +36,7 @@ pub(super) const ROWS: &[OpRow] = &[
|
||||
row::<dto::GetBucketPolicyStatus>(),
|
||||
row::<dto::GetBucketAcl>(),
|
||||
row::<dto::GetBucketNotificationConfiguration>(),
|
||||
row::<dto::PutBucketNotificationConfiguration>(),
|
||||
];
|
||||
|
||||
impl Served for dto::GetBucketVersioning {
|
||||
@@ -134,3 +137,64 @@ impl Served for dto::GetBucketNotificationConfiguration {
|
||||
Ok(Resp::new(output))
|
||||
}
|
||||
}
|
||||
|
||||
impl Served for dto::PutBucketNotificationConfiguration {
|
||||
const METRIC: S3Operation = S3Operation::PutBucketNotificationConfiguration;
|
||||
|
||||
async fn authorize(envelope: &mut RequestEnvelope, request: &Req<Self>) -> S3Result<()> {
|
||||
let action = Action::S3Action(S3Action::PutBucketNotificationAction);
|
||||
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_notification(&serialize_notification_dto(&input.notification_configuration))
|
||||
.map_err(|err| rustfs_s3_types::s3_error!(MalformedXML, "{err}"));
|
||||
let destinations = notification_destinations(input.notification_configuration);
|
||||
let output = DefaultBucketUsecase::with_context(context)
|
||||
.execute_put_bucket_notification_configuration(envelope, input.bucket.as_str(), destinations, prepared)
|
||||
.await?;
|
||||
Ok(Resp::new(output))
|
||||
}
|
||||
}
|
||||
|
||||
fn notification_destinations(configuration: dto::NotificationConfiguration) -> Vec<NotificationDestination> {
|
||||
let mut destinations = Vec::new();
|
||||
for cfg in configuration.queue_configurations {
|
||||
destinations.push(NotificationDestination {
|
||||
kind: "QueueConfiguration",
|
||||
id: cfg.id,
|
||||
events: cfg.events.into_iter().map(|event| event.as_str().to_owned()).collect(),
|
||||
target_arn: cfg.queue_arn,
|
||||
filters: notification_filters(cfg.filter),
|
||||
});
|
||||
}
|
||||
for cfg in configuration.topic_configurations {
|
||||
destinations.push(NotificationDestination {
|
||||
kind: "TopicConfiguration",
|
||||
id: cfg.id,
|
||||
events: cfg.events.into_iter().map(|event| event.as_str().to_owned()).collect(),
|
||||
target_arn: cfg.topic_arn,
|
||||
filters: notification_filters(cfg.filter),
|
||||
});
|
||||
}
|
||||
for cfg in configuration.lambda_function_configurations {
|
||||
destinations.push(NotificationDestination {
|
||||
kind: "LambdaFunctionConfiguration",
|
||||
id: cfg.id,
|
||||
events: cfg.events.into_iter().map(|event| event.as_str().to_owned()).collect(),
|
||||
target_arn: cfg.lambda_function_arn,
|
||||
filters: notification_filters(cfg.filter),
|
||||
});
|
||||
}
|
||||
destinations
|
||||
}
|
||||
|
||||
fn notification_filters(filter: Option<dto::NotificationConfigurationFilter>) -> Vec<(Option<String>, Option<String>)> {
|
||||
filter
|
||||
.and_then(|filter| filter.key)
|
||||
.into_iter()
|
||||
.flat_map(|key| key.filter_rules)
|
||||
.map(|rule| (rule.name.map(|name| name.as_str().to_owned()), rule.value))
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ use crate::error::ApiError;
|
||||
use crate::storage::ecfs::*;
|
||||
use crate::storage::helper::audit_only_operation;
|
||||
use crate::storage::persisted_from_s3s;
|
||||
use crate::storage::s3_api;
|
||||
use crate::storage::s3_api::{self, NotificationDestination};
|
||||
use crate::storage::storage_api::ecfs_consumer::contract::bucket::{BucketOperations, BucketOptions};
|
||||
use rustfs_gateway_types::compat::s3s_5761ddfe::generated::ops::{
|
||||
get_bucket_acl, get_bucket_notification_configuration, get_bucket_policy, get_bucket_policy_status, get_bucket_versioning,
|
||||
@@ -152,12 +152,14 @@ impl FS {
|
||||
&self,
|
||||
req: S3Request<PutBucketNotificationConfigurationInput>,
|
||||
) -> S3Result<S3Response<PutBucketNotificationConfigurationOutput>> {
|
||||
let prepared = persisted_from_s3s(&req.input.notification_configuration);
|
||||
let usecase = s3_api::bucket_usecase_for(self);
|
||||
audit_only_operation(req, S3Operation::PutBucketNotificationConfiguration, |req| {
|
||||
usecase.execute_put_bucket_notification_configuration(req, prepared)
|
||||
})
|
||||
.await
|
||||
record_s3_op(S3Operation::PutBucketNotificationConfiguration);
|
||||
let prepared = persisted_from_s3s(&req.input.notification_configuration).map_err(rustfs_s3_types::S3Error::from);
|
||||
let envelope = RequestEnvelope::from_s3s(&req);
|
||||
let destinations = notification_destinations(req.input.notification_configuration);
|
||||
s3_api::bucket_usecase_for(self)
|
||||
.execute_put_bucket_notification_configuration(envelope, &req.input.bucket, destinations, prepared)
|
||||
.await?;
|
||||
Ok(S3Response::new(PutBucketNotificationConfigurationOutput {}))
|
||||
}
|
||||
|
||||
pub(in crate::storage::ecfs) async fn put_bucket_policy_edge(
|
||||
@@ -182,3 +184,45 @@ impl FS {
|
||||
Ok(S3Response::new(PutBucketVersioningOutput::default()))
|
||||
}
|
||||
}
|
||||
|
||||
fn notification_destinations(configuration: NotificationConfiguration) -> Vec<NotificationDestination> {
|
||||
let mut destinations = Vec::new();
|
||||
for cfg in configuration.queue_configurations.into_iter().flatten() {
|
||||
destinations.push(NotificationDestination {
|
||||
kind: "QueueConfiguration",
|
||||
id: cfg.id,
|
||||
events: cfg.events.into_iter().map(|event| event.as_ref().to_owned()).collect(),
|
||||
target_arn: cfg.queue_arn,
|
||||
filters: notification_filters(cfg.filter),
|
||||
});
|
||||
}
|
||||
for cfg in configuration.topic_configurations.into_iter().flatten() {
|
||||
destinations.push(NotificationDestination {
|
||||
kind: "TopicConfiguration",
|
||||
id: cfg.id,
|
||||
events: cfg.events.into_iter().map(|event| event.as_ref().to_owned()).collect(),
|
||||
target_arn: cfg.topic_arn,
|
||||
filters: notification_filters(cfg.filter),
|
||||
});
|
||||
}
|
||||
for cfg in configuration.lambda_function_configurations.into_iter().flatten() {
|
||||
destinations.push(NotificationDestination {
|
||||
kind: "LambdaFunctionConfiguration",
|
||||
id: cfg.id,
|
||||
events: cfg.events.into_iter().map(|event| event.as_ref().to_owned()).collect(),
|
||||
target_arn: cfg.lambda_function_arn,
|
||||
filters: notification_filters(cfg.filter),
|
||||
});
|
||||
}
|
||||
destinations
|
||||
}
|
||||
|
||||
fn notification_filters(filter: Option<NotificationConfigurationFilter>) -> Vec<(Option<String>, Option<String>)> {
|
||||
filter
|
||||
.and_then(|filter| filter.key)
|
||||
.and_then(|key| key.filter_rules)
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.map(|rule| (rule.name.map(|name| name.as_str().to_owned()), rule.value))
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -18,7 +18,9 @@
|
||||
//! until each helper is moved with dedicated small refactor steps.
|
||||
|
||||
use crate::app::bucket_usecase::DefaultBucketUsecase;
|
||||
pub(crate) use crate::app::bucket_usecase::{PreparedLifecycleConfiguration, validate_lifecycle_rule_status};
|
||||
pub(crate) use crate::app::bucket_usecase::{
|
||||
NotificationDestination, PreparedLifecycleConfiguration, validate_lifecycle_rule_status,
|
||||
};
|
||||
pub(crate) use crate::app::multipart_usecase::{
|
||||
AbortMultipartUploadTarget, CreateMultipartUploadFields, DefaultMultipartUsecase, ListPartsFields, ListPartsTarget,
|
||||
UploadPartTarget, UploadPartWriteInput, UploadPartWriteOutput, reject_abort_capabilities,
|
||||
|
||||
@@ -63,6 +63,12 @@ pub(crate) const MATRIX: &[MatrixRow] = &[
|
||||
has_error_body: true,
|
||||
call: get_bucket_notification,
|
||||
},
|
||||
MatrixRow {
|
||||
operation: "PutBucketNotificationConfiguration",
|
||||
metric: S3Operation::PutBucketNotificationConfiguration,
|
||||
has_error_body: true,
|
||||
call: put_empty_bucket_notification,
|
||||
},
|
||||
];
|
||||
|
||||
fn get_bucket_versioning<'a>(client: &'a Client, bucket: &'a str) -> BoxFuture<'a, Outcome> {
|
||||
@@ -565,3 +571,282 @@ fn legacy_put_versioning_preserves_refusal_order_and_stored_bytes() {
|
||||
fn gateway_put_versioning_preserves_refusal_order_and_stored_bytes() {
|
||||
with_stack(GATEWAY, versioning_writes_preserve_refusal_order_and_stored_bytes);
|
||||
}
|
||||
|
||||
pub(super) fn with_notification_stack<F, Fut>(stack: Option<&'static str>, test: F)
|
||||
where
|
||||
F: FnOnce(RustFSServer) -> Fut + Send + 'static,
|
||||
Fut: std::future::Future<Output = ()> + 'static,
|
||||
{
|
||||
let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("bind webhook fixture");
|
||||
listener.set_nonblocking(true).expect("set webhook listener nonblocking");
|
||||
let endpoint = format!("http://{}", listener.local_addr().expect("webhook fixture address"));
|
||||
temp_env::with_var(rustfs_utils::egress::ENV_OUTBOUND_ALLOW_ORIGINS, Some(endpoint.clone()), || {
|
||||
with_stack(stack, move |server| async move {
|
||||
let listener = tokio::net::TcpListener::from_std(listener).expect("register webhook listener");
|
||||
let router = axum::Router::new().route("/", axum::routing::get(|| async { http::StatusCode::OK }));
|
||||
let health = tokio::spawn(async move { axum::serve(listener, router).await.expect("serve webhook fixture") });
|
||||
let queue = tempfile::tempdir().expect("webhook queue");
|
||||
let mut target = rustfs_config::server_config::KVS::new();
|
||||
target.insert(rustfs_config::ENABLE_KEY.to_owned(), "on".to_owned());
|
||||
target.insert(rustfs_config::WEBHOOK_ENDPOINT.to_owned(), endpoint);
|
||||
target.insert(
|
||||
rustfs_config::WEBHOOK_QUEUE_DIR.to_owned(),
|
||||
queue.path().to_str().expect("valid queue path").to_owned(),
|
||||
);
|
||||
let notify = rustfs_notify::notification_system().expect("embedded notification runtime");
|
||||
let mut config = notify.config_snapshot().await;
|
||||
config
|
||||
.0
|
||||
.entry(rustfs_config::notify::NOTIFY_WEBHOOK_SUB_SYS.to_owned())
|
||||
.or_default()
|
||||
.insert("notification-http".to_owned(), target);
|
||||
notify
|
||||
.set_targets_enabled(true, Some(config))
|
||||
.await
|
||||
.expect("register loopback webhook target");
|
||||
assert!(
|
||||
notify
|
||||
.get_active_targets()
|
||||
.await
|
||||
.iter()
|
||||
.any(|target| target.id == "notification-http")
|
||||
);
|
||||
test(server).await;
|
||||
health.abort();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn put_empty_bucket_notification<'a>(client: &'a Client, bucket: &'a str) -> BoxFuture<'a, Outcome> {
|
||||
Box::pin(async move { outcome(put_notification(client, bucket).send().await) })
|
||||
}
|
||||
|
||||
const EMPTY_NOTIFICATION: &str = "<NotificationConfiguration></NotificationConfiguration>";
|
||||
const EVENT_BRIDGE_NOTIFICATION: &str =
|
||||
"<NotificationConfiguration><EventBridgeConfiguration></EventBridgeConfiguration></NotificationConfiguration>";
|
||||
|
||||
fn put_notification(
|
||||
client: &Client,
|
||||
bucket: &str,
|
||||
) -> aws_sdk_s3::operation::put_bucket_notification_configuration::builders::PutBucketNotificationConfigurationFluentBuilder {
|
||||
client
|
||||
.put_bucket_notification_configuration()
|
||||
.bucket(bucket)
|
||||
.notification_configuration(aws_sdk_s3::types::NotificationConfiguration::builder().build())
|
||||
}
|
||||
|
||||
async fn put_notification_document(
|
||||
client: &Client,
|
||||
bucket: &str,
|
||||
document: &'static str,
|
||||
) -> Result<
|
||||
aws_sdk_s3::operation::put_bucket_notification_configuration::PutBucketNotificationConfigurationOutput,
|
||||
Box<SdkError<aws_sdk_s3::operation::put_bucket_notification_configuration::PutBucketNotificationConfigurationError>>,
|
||||
> {
|
||||
use md5::{Digest as _, Md5};
|
||||
|
||||
let content_md5 = base64_simd::STANDARD.encode_to_string(Md5::digest(document.as_bytes()));
|
||||
put_notification(client, bucket)
|
||||
.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");
|
||||
headers.insert("content-md5", content_md5.clone());
|
||||
})
|
||||
.send()
|
||||
.await
|
||||
.map_err(Box::new)
|
||||
}
|
||||
|
||||
async fn stored_notification(bucket: &str) -> Vec<u8> {
|
||||
rustfs_ecstore::api::bucket::metadata_sys::get_config_from_disk(bucket)
|
||||
.await
|
||||
.expect("bucket metadata on disk")
|
||||
.notification_config_xml
|
||||
}
|
||||
|
||||
async fn notification_writes_preserve_authorization_and_stored_bytes(server: RustFSServer) {
|
||||
use aws_sdk_s3::error::ProvideErrorMetadata as _;
|
||||
|
||||
const ALLOWED: &str = "notification-put-allowed";
|
||||
const READABLE: &str = "notification-put-readable";
|
||||
const MISSING: &str = "notification-put-missing";
|
||||
const WRITER: &str = "notificationwriter";
|
||||
const READER: &str = "notificationreader";
|
||||
const NO_POLICY: &str = "notificationnopolicy";
|
||||
const DENIED: &str = "notificationexplicitdeny";
|
||||
|
||||
for bucket in [ALLOWED, READABLE, "sthree-x", "a-s3alias"] {
|
||||
create_bucket_in_the_store(bucket).await;
|
||||
store_bucket_config(bucket, "notification.xml", EMPTY_NOTIFICATION).await;
|
||||
}
|
||||
super::create_user_allowed_on_one_bucket(WRITER, "s3:PutBucketNotification", ALLOWED).await;
|
||||
super::create_user_allowed_on_one_bucket(READER, "s3:GetBucketNotification", READABLE).await;
|
||||
super::create_user_without_a_policy(NO_POLICY).await;
|
||||
super::create_user_without_a_policy(DENIED).await;
|
||||
let policy = rustfs_policy::policy::Policy::parse_config(
|
||||
br#"{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["s3:PutBucketNotification"],"Resource":["arn:aws:s3:::notification-put-readable"]},{"Effect":"Deny","Action":["s3:PutBucketNotification"],"Resource":["arn:aws:s3:::notification-put-readable"]}]}"#,
|
||||
).expect("parse explicit deny");
|
||||
let iam = get_global_app_context().expect("embedded server context").iam().handle();
|
||||
iam.set_policy(DENIED, policy).await.expect("store policy");
|
||||
iam.policy_db_set(DENIED, rustfs_iam::store::UserType::Reg, false, DENIED)
|
||||
.await
|
||||
.expect("attach policy");
|
||||
|
||||
let endpoint = server.endpoint();
|
||||
let root = s3_client(&endpoint, server.access_key(), server.secret_key());
|
||||
let writer = s3_client(&endpoint, WRITER, super::USER_SECRET_KEY);
|
||||
let reader = s3_client(&endpoint, READER, super::USER_SECRET_KEY);
|
||||
let unprivileged = s3_client(&endpoint, NO_POLICY, super::USER_SECRET_KEY);
|
||||
let denied = s3_client(&endpoint, DENIED, super::USER_SECRET_KEY);
|
||||
|
||||
let metric = S3Operation::PutBucketNotificationConfiguration.as_str();
|
||||
let handled = || {
|
||||
rustfs_io_metrics::s3_op_metrics_snapshot()
|
||||
.iter()
|
||||
.find(|counter| counter.op == metric)
|
||||
.map_or(0, |counter| counter.total)
|
||||
};
|
||||
let handled_before = handled();
|
||||
let before = super::s3_request_counts();
|
||||
put_notification_document(&writer, ALLOWED, EVENT_BRIDGE_NOTIFICATION)
|
||||
.await
|
||||
.expect("authorized notification update");
|
||||
assert_eq!(stored_notification(ALLOWED).await, EVENT_BRIDGE_NOTIFICATION.as_bytes());
|
||||
assert_eq!(super::counted_since(&before), [(("PUT", metric, "2xx"), 1)].into());
|
||||
assert_eq!(handled() - handled_before, 1, "the admitted edge counts once");
|
||||
let stored = root
|
||||
.get_bucket_notification_configuration()
|
||||
.bucket(ALLOWED)
|
||||
.send()
|
||||
.await
|
||||
.expect("Get after Put");
|
||||
assert!(stored.event_bridge_configuration().is_some());
|
||||
|
||||
reader
|
||||
.get_bucket_notification_configuration()
|
||||
.bucket(READABLE)
|
||||
.send()
|
||||
.await
|
||||
.expect("reader can Get");
|
||||
const BAD_FILTER: &str = "<NotificationConfiguration><QueueConfiguration><Id>queue</Id><Queue>arn:rustfs:sqs:us-east-1:1:webhook</Queue><Event>s3:ObjectCreated:*</Event><Filter><S3Key><FilterRule><Name>prefix</Name><Value>../secret</Value></FilterRule></S3Key></Filter></QueueConfiguration></NotificationConfiguration>";
|
||||
for (client, bucket, document) in [
|
||||
(&reader, READABLE, EVENT_BRIDGE_NOTIFICATION),
|
||||
(&reader, READABLE, BAD_FILTER),
|
||||
(&unprivileged, READABLE, EVENT_BRIDGE_NOTIFICATION),
|
||||
(&denied, READABLE, EVENT_BRIDGE_NOTIFICATION),
|
||||
(&reader, MISSING, EVENT_BRIDGE_NOTIFICATION),
|
||||
] {
|
||||
let before = super::s3_request_counts();
|
||||
let handled_before = handled();
|
||||
let result = put_notification_document(client, bucket, document).await;
|
||||
if bucket == READABLE {
|
||||
assert_eq!(stored_notification(bucket).await, EMPTY_NOTIFICATION.as_bytes());
|
||||
}
|
||||
assert_eq!(
|
||||
refusal(result.expect_err(bucket).as_ref()),
|
||||
(Some("AccessDenied".into()), Some(403)),
|
||||
"{bucket}"
|
||||
);
|
||||
let counts = super::counted_since(&before);
|
||||
assert_eq!(counts.len(), 1, "{bucket}: {counts:?}");
|
||||
assert_eq!(counts.values().sum::<u64>(), 1, "{bucket}: {counts:?}");
|
||||
assert!(
|
||||
counts
|
||||
.keys()
|
||||
.all(|(method, _, outcome)| *method == "PUT" && *outcome == "4xx")
|
||||
);
|
||||
assert_eq!(handled(), handled_before);
|
||||
}
|
||||
|
||||
let before = super::s3_request_counts();
|
||||
let handled_before = handled();
|
||||
let invalid = put_notification_document(&root, READABLE, BAD_FILTER)
|
||||
.await
|
||||
.expect_err("invalid filter");
|
||||
assert_eq!(refusal(invalid.as_ref()), (Some("InvalidArgument".into()), Some(400)));
|
||||
assert_eq!(
|
||||
invalid.message(),
|
||||
Some("invalid notification filter value (len=9) (QueueConfiguration id=queue)")
|
||||
);
|
||||
assert_eq!(stored_notification(READABLE).await, EMPTY_NOTIFICATION.as_bytes());
|
||||
assert_eq!(super::counted_since(&before), [(("PUT", metric, "4xx"), 1)].into());
|
||||
assert_eq!(handled() - handled_before, 1, "the admitted edge counts once");
|
||||
|
||||
for bucket in ["sthree-x", "a-s3alias"] {
|
||||
put_notification_document(&root, bucket, EVENT_BRIDGE_NOTIFICATION)
|
||||
.await
|
||||
.expect(bucket);
|
||||
assert_eq!(stored_notification(bucket).await, EVENT_BRIDGE_NOTIFICATION.as_bytes());
|
||||
}
|
||||
server.shutdown().await;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_put_notification_preserves_authorization_and_stored_bytes() {
|
||||
with_notification_stack(LEGACY, notification_writes_preserve_authorization_and_stored_bytes);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gateway_put_notification_preserves_authorization_and_stored_bytes() {
|
||||
with_notification_stack(GATEWAY, notification_writes_preserve_authorization_and_stored_bytes);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gateway_put_notification_rejects_get_only_before_writing() {
|
||||
with_notification_stack(GATEWAY, |server| async move {
|
||||
const BUCKET: &str = "notification-get-only";
|
||||
const READER: &str = "notificationgetonly";
|
||||
create_bucket_in_the_store(BUCKET).await;
|
||||
store_bucket_config(BUCKET, "notification.xml", EMPTY_NOTIFICATION).await;
|
||||
super::create_user_allowed_on_one_bucket(READER, "s3:GetBucketNotification", BUCKET).await;
|
||||
let reader = s3_client(&server.endpoint(), READER, super::USER_SECRET_KEY);
|
||||
reader
|
||||
.get_bucket_notification_configuration()
|
||||
.bucket(BUCKET)
|
||||
.send()
|
||||
.await
|
||||
.expect("the Get-only principal can read this bucket");
|
||||
let result = put_notification_document(&reader, BUCKET, EVENT_BRIDGE_NOTIFICATION).await;
|
||||
assert_eq!(
|
||||
stored_notification(BUCKET).await,
|
||||
EMPTY_NOTIFICATION.as_bytes(),
|
||||
"a Get-only principal must not change stored notification bytes"
|
||||
);
|
||||
assert_eq!(
|
||||
refusal(result.expect_err("Get-only Put must be denied").as_ref()),
|
||||
(Some("AccessDenied".into()), Some(403))
|
||||
);
|
||||
server.shutdown().await;
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gateway_put_notification_rejects_other_bucket_writer_before_writing() {
|
||||
with_notification_stack(GATEWAY, |server| async move {
|
||||
const ALLOWED: &str = "notification-scope-allowed";
|
||||
const DENIED: &str = "notification-scope-denied";
|
||||
const WRITER: &str = "notificationscopewriter";
|
||||
for bucket in [ALLOWED, DENIED] {
|
||||
create_bucket_in_the_store(bucket).await;
|
||||
store_bucket_config(bucket, "notification.xml", EMPTY_NOTIFICATION).await;
|
||||
}
|
||||
super::create_user_allowed_on_one_bucket(WRITER, "s3:PutBucketNotification", ALLOWED).await;
|
||||
let writer = s3_client(&server.endpoint(), WRITER, super::USER_SECRET_KEY);
|
||||
let result = put_notification_document(&writer, DENIED, EVENT_BRIDGE_NOTIFICATION).await;
|
||||
assert_eq!(
|
||||
stored_notification(DENIED).await,
|
||||
EMPTY_NOTIFICATION.as_bytes(),
|
||||
"permission on another bucket must not allow changing this bucket"
|
||||
);
|
||||
assert_eq!(
|
||||
refusal(result.expect_err("another bucket's writer must be denied").as_ref()),
|
||||
(Some("AccessDenied".into()), Some(403))
|
||||
);
|
||||
assert_eq!(stored_notification(ALLOWED).await, EMPTY_NOTIFICATION.as_bytes());
|
||||
server.shutdown().await;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -430,12 +430,12 @@ async fn every_served_operation_serves_root_and_refuses_the_callers_it_cannot_ad
|
||||
|
||||
#[test]
|
||||
fn the_refusal_matrix_holds_on_the_gateway_stack() {
|
||||
with_stack(GATEWAY, every_served_operation_serves_root_and_refuses_the_callers_it_cannot_admit);
|
||||
bucket_access::with_notification_stack(GATEWAY, every_served_operation_serves_root_and_refuses_the_callers_it_cannot_admit);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_refusal_matrix_holds_on_the_legacy_stack() {
|
||||
with_stack(LEGACY, every_served_operation_serves_root_and_refuses_the_callers_it_cannot_admit);
|
||||
bucket_access::with_notification_stack(LEGACY, every_served_operation_serves_root_and_refuses_the_callers_it_cannot_admit);
|
||||
}
|
||||
|
||||
/// Names RustFS creates and the AWS bucket grammar refuses. An IP-shaped name such as `256.1.1.1`
|
||||
|
||||
Reference in New Issue
Block a user