feat(gateway): serve bucket notification configuration reads

Use native gateway DTOs for stored notification reads, register the handler,
and keep both edges on the shared bucket authorizer. The legacy edge now
uses the compatibility seam. Notification writes and the default stack are
unchanged. Non-test changes: +53/-36 lines.

Add three refusal-oriented tests and two configuration-read tests, including
successful IAM controls. Cover absent and stored configurations, reserved
bucket shapes, unknown stored XML, event/filter values, bucket-bound IAM
allow/implicit deny/explicit deny, and one handled-operation count.

Mutation controls on da5414c: bypassing each edge's authorization made its
notification_authorization_binds_the_bucket_on_the_*_stack test fail because
the wrong bucket was served. Dropping the stored queue made both
*_stack_answers_the_stored_notification_configuration tests fail. Restore
hashes were checked and sources were touched before a real rebuild.

Validation: 279 scoped tests passed on da5414c; 70 scoped tests passed after
rebasing to fd8dc41. Both rustfs all-target Clippy builds passed on fd8dc41,
as did fmt, test wiring and prepush-quick. Script tests passed on da5414c.
The earlier broad run reported one existing CORS test as leaky with exit 0.
CI and the full workspace suite were not run.
This commit is contained in:
overtrue
2026-11-15 01:20:32 +08:00
parent df4c4916c2
commit b7e7a6156c
5 changed files with 208 additions and 36 deletions
+40 -10
View File
@@ -16,11 +16,12 @@
use super::*;
use crate::app::storage_api::s3::dto::{
GetBucketNotificationConfigurationInput, NotificationConfiguration, NotificationConfigurationFilter,
PutBucketNotificationConfigurationInput, PutBucketNotificationConfigurationOutput,
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;
fn is_valid_notification_filter_value(value: &str) -> bool {
if value.len() > 1024 || value.contains('\\') {
@@ -159,22 +160,22 @@ fn resolve_notification_region(global_region: Option<Region>, request_region: Op
}
impl DefaultBucketUsecase {
/// The stored notification configuration, or an empty answer when none is stored.
pub async fn execute_get_bucket_notification_configuration(
&self,
req: S3Request<GetBucketNotificationConfigurationInput>,
) -> S3Result<Option<PersistedNotificationConfiguration>> {
let GetBucketNotificationConfigurationInput { bucket, .. } = req.input;
input: gateway_dto::GetBucketNotificationConfigurationInput,
) -> rustfs_s3_types::S3Result<gateway_dto::GetBucketNotificationConfigurationOutput> {
let bucket = input.bucket.as_str();
let Some(store) = self.object_store() else {
return Err(S3Error::with_message(S3ErrorCode::InternalError, "Not init".to_string()));
return Err(rustfs_s3_types::s3_error!(InternalError, "Not init"));
};
store
.get_bucket_info(&bucket, &BucketOptions::default())
.get_bucket_info(bucket, &BucketOptions::default())
.await
.map_err(ApiError::from)?;
let has_notification_config = match metadata_sys::get_notification_config(&bucket).await {
let stored = match metadata_sys::get_notification_config(bucket).await {
Ok(config) => config,
// An unreadable config is not "no notifications configured".
Err(err) if crate::storage_api::error::is_unreadable_config_error(&err) => {
@@ -192,8 +193,22 @@ impl DefaultBucketUsecase {
None
}
};
let Some(stored) = stored else {
return Ok(gateway_dto::GetBucketNotificationConfigurationOutput::default());
};
Ok(has_notification_config)
let gateway_dto::NotificationConfiguration {
event_bridge_configuration,
lambda_function_configurations,
queue_configurations,
topic_configurations,
} = dto_values::stored_config(&stored, parse_notification_dto)?;
Ok(gateway_dto::GetBucketNotificationConfigurationOutput {
event_bridge_configuration,
lambda_function_configurations,
queue_configurations,
topic_configurations,
})
}
pub async fn execute_put_bucket_notification_configuration(
@@ -262,6 +277,21 @@ mod tests {
use crate::app::storage_api::s3::dto::{FilterRule, FilterRuleName, QueueConfiguration, S3KeyFilter};
use http::Method;
#[tokio::test]
async fn execute_get_bucket_notification_configuration_returns_internal_error_when_store_uninitialized() {
let input = gateway_dto::GetBucketNotificationConfigurationInput {
bucket: rustfs_gateway_types::BucketName::new("test-bucket").expect("valid bucket name"),
..Default::default()
};
let usecase = DefaultBucketUsecase::without_context();
let err = usecase
.execute_get_bucket_notification_configuration(input)
.await
.unwrap_err();
assert_eq!(err.code(), &rustfs_s3_types::S3ErrorCode::InternalError);
}
#[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()));
@@ -31,6 +31,7 @@ pub(super) const ROWS: &[OpRow] = &[
row::<dto::GetBucketPolicy>(),
row::<dto::GetBucketPolicyStatus>(),
row::<dto::GetBucketAcl>(),
row::<dto::GetBucketNotificationConfiguration>(),
];
impl Served for dto::GetBucketVersioning {
@@ -96,3 +97,19 @@ impl Served for dto::GetBucketAcl {
Ok(Resp::new(output))
}
}
impl Served for dto::GetBucketNotificationConfiguration {
const METRIC: S3Operation = S3Operation::GetBucketNotificationConfiguration;
async fn authorize(envelope: &mut RequestEnvelope, request: &Req<Self>) -> S3Result<()> {
let action = Action::S3Action(S3Action::GetBucketNotificationAction);
Ok(authorize_bucket_request(envelope, request.input().bucket.as_str(), action).await?)
}
async fn execute(context: Option<Arc<AppContext>>, _envelope: RequestEnvelope, request: Req<Self>) -> S3Result<Resp<Self>> {
let output = DefaultBucketUsecase::with_context(context)
.execute_get_bucket_notification_configuration(request.into_input())
.await?;
Ok(Resp::new(output))
}
}
+2 -4
View File
@@ -3758,10 +3758,8 @@ impl S3Access for FS {
&self,
req: &mut S3Request<GetBucketNotificationConfigurationInput>,
) -> S3Result<()> {
let req_info = ext_req_info_mut(&mut req.extensions)?;
req_info.bucket = Some(req.input.bucket.clone());
authorize_request(req, Action::S3Action(S3Action::GetBucketNotificationAction)).await
let bucket = req.input.bucket.clone();
authorize_bucket_request(req, &bucket, Action::S3Action(S3Action::GetBucketNotificationAction)).await
}
/// Checks whether the GetBucketOwnershipControls request has accesses to the resources.
@@ -22,11 +22,11 @@
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::storage_api::ecfs_consumer::contract::bucket::{BucketOperations, BucketOptions};
use crate::storage::{persisted_from_s3s, persisted_to_s3s};
use rustfs_gateway_types::compat::s3s_5761ddfe::generated::ops::{
get_bucket_acl, get_bucket_policy, get_bucket_policy_status, get_bucket_versioning,
get_bucket_acl, get_bucket_notification_configuration, get_bucket_policy, get_bucket_policy_status, get_bucket_versioning,
};
use rustfs_io_metrics::record_s3_op;
use rustfs_s3_ops::S3Operation;
@@ -89,26 +89,13 @@ impl FS {
) -> S3Result<S3Response<GetBucketNotificationConfigurationOutput>> {
record_s3_op(S3Operation::GetBucketNotificationConfiguration);
let usecase = s3_api::bucket_usecase_for(self);
let notification_configuration = usecase
.execute_get_bucket_notification_configuration(req)
.await?
.as_ref()
.map(persisted_to_s3s::<_, NotificationConfiguration>)
.transpose()?;
match notification_configuration {
Some(NotificationConfiguration {
event_bridge_configuration,
lambda_function_configurations,
queue_configurations,
topic_configurations,
}) => Ok(S3Response::new(GetBucketNotificationConfigurationOutput {
event_bridge_configuration,
lambda_function_configurations,
queue_configurations,
topic_configurations,
})),
None => Ok(S3Response::new(GetBucketNotificationConfigurationOutput::default())),
}
through_gateway_seam(
req,
get_bucket_notification_configuration::input_from_s3s,
get_bucket_notification_configuration::output_to_s3s,
|_, input| usecase.execute_get_bucket_notification_configuration(input),
)
.await
}
pub(in crate::storage::ecfs) async fn get_bucket_policy_edge(
@@ -47,6 +47,12 @@ pub(crate) const MATRIX: &[MatrixRow] = &[
has_error_body: true,
call: get_bucket_acl,
},
MatrixRow {
operation: "GetBucketNotificationConfiguration",
metric: S3Operation::GetBucketNotificationConfiguration,
has_error_body: true,
call: get_bucket_notification,
},
];
fn get_bucket_versioning<'a>(client: &'a Client, bucket: &'a str) -> BoxFuture<'a, Outcome> {
@@ -72,6 +78,10 @@ fn get_bucket_acl<'a>(client: &'a Client, bucket: &'a str) -> BoxFuture<'a, Outc
Box::pin(async move { outcome(client.get_bucket_acl().bucket(bucket).send().await) })
}
fn get_bucket_notification<'a>(client: &'a Client, bucket: &'a str) -> BoxFuture<'a, Outcome> {
Box::pin(async move { outcome(client.get_bucket_notification_configuration().bucket(bucket).send().await) })
}
/// Stores a bucket configuration document the way the metadata system holds it, so a read is
/// tested without the operation that writes it.
async fn store_bucket_config(bucket: &str, file: &str, document: &str) {
@@ -239,3 +249,133 @@ fn the_gateway_stack_answers_the_bucket_acl() {
fn the_legacy_stack_answers_the_bucket_acl() {
with_stack(LEGACY, get_bucket_acl_answers_the_owner_with_full_control);
}
const QUEUE_NOTIFICATION: &str = r#"<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><QueueConfiguration><Id>a2-queue</Id><Queue>arn:rustfs:sqs:us-east-1:1:webhook</Queue><Event>s3:ObjectCreated:*</Event><Filter><S3Key><FilterRule><Name>prefix</Name><Value>logs/</Value></FilterRule></S3Key></Filter></QueueConfiguration></NotificationConfiguration>"#;
/// GetBucketNotificationConfiguration answers an empty configuration when none is stored, the
/// stored queue rule (event, filter and id) when one is, for any bucket name the store holds, and
/// refuses a bucket the store does not hold.
async fn get_bucket_notification_answers_the_stored_configuration(server: RustFSServer) {
let client = s3_client(&server.endpoint(), server.access_key(), server.secret_key());
for name in ["a2-notification", "sthree-x", "a-s3alias"] {
create_bucket_in_the_store(name).await;
let empty = client
.get_bucket_notification_configuration()
.bucket(name)
.send()
.await
.expect(name);
assert!(
empty.queue_configurations().is_empty() && empty.topic_configurations().is_empty(),
"{name}"
);
}
store_bucket_config(SDK_REACHABLE_NAMES[1], "notification.xml", QUEUE_NOTIFICATION).await;
let stored = client
.get_bucket_notification_configuration()
.bucket(SDK_REACHABLE_NAMES[1])
.send()
.await
.expect("stored configuration");
let [queue] = stored.queue_configurations() else { panic!("one queue rule: {stored:?}") };
assert_eq!((queue.id(), queue.queue_arn()), (Some("a2-queue"), "arn:rustfs:sqs:us-east-1:1:webhook"));
assert_eq!(queue.events(), &[aws_sdk_s3::types::Event::S3ObjectCreated]);
let rules = queue.filter().and_then(|filter| filter.key()).map(|key| key.filter_rules());
let [rule] = rules.expect("stored filter") else { panic!("one filter rule") };
assert_eq!(rule.name(), Some(&aws_sdk_s3::types::FilterRuleName::Prefix));
assert_eq!(rule.value(), Some("logs/"));
let extended =
QUEUE_NOTIFICATION.replace("<QueueConfiguration>", "<Future><Nested>ignored</Nested></Future><QueueConfiguration>");
store_bucket_config("a2-notification", "notification.xml", &extended).await;
let unknown = client
.get_bucket_notification_configuration()
.bucket("a2-notification")
.send()
.await
.expect("unknown stored element");
assert_eq!(unknown.queue_configurations(), stored.queue_configurations());
let missing = client
.get_bucket_notification_configuration()
.bucket("a2-no-such-bucket")
.send()
.await;
let missing = missing.expect_err("a missing bucket");
assert_eq!(refusal(&missing), (Some("NoSuchBucket".to_owned()), Some(404)), "{missing:?}");
server.shutdown().await;
}
#[test]
fn the_gateway_stack_answers_the_stored_notification_configuration() {
with_stack(GATEWAY, get_bucket_notification_answers_the_stored_configuration);
}
#[test]
fn the_legacy_stack_answers_the_stored_notification_configuration() {
with_stack(LEGACY, get_bucket_notification_answers_the_stored_configuration);
}
async fn notification_authorization_is_bound_to_the_requested_bucket(server: RustFSServer) {
const USER: &str = "notification-reader";
const BUCKET: &str = "notification-allowed";
const OTHER: &str = "notification-other";
create_bucket_in_the_store(BUCKET).await;
create_bucket_in_the_store(OTHER).await;
super::create_user_allowed_on_one_bucket(USER, "s3:GetBucketNotification", BUCKET).await;
let client = s3_client(&server.endpoint(), USER, super::USER_SECRET_KEY);
let handled_count = || {
rustfs_io_metrics::s3_op_metrics_snapshot()
.into_iter()
.find(|metric| metric.op == S3Operation::GetBucketNotificationConfiguration.as_str())
.map_or(0, |metric| metric.total)
};
let before = handled_count();
client
.get_bucket_notification_configuration()
.bucket(BUCKET)
.send()
.await
.expect("allowed read");
assert_eq!(handled_count() - before, 1, "the admitted edge counts once");
let wrong = client
.get_bucket_notification_configuration()
.bucket(OTHER)
.send()
.await
.expect_err("wrong bucket");
assert_eq!(refusal(&wrong), (Some("AccessDenied".into()), Some(403)));
super::create_user_without_a_policy("notification-no-policy").await;
let unprivileged = s3_client(&server.endpoint(), "notification-no-policy", super::USER_SECRET_KEY);
let implicit = unprivileged
.get_bucket_notification_configuration()
.bucket(BUCKET)
.send()
.await
.expect_err("implicit deny");
assert_eq!(refusal(&implicit), (Some("AccessDenied".into()), Some(403)));
let deny = format!(
r#"{{"Version":"2012-10-17","Statement":[{{"Effect":"Deny","Principal":{{"AWS":["*"]}},"Action":["s3:GetBucketNotification"],"Resource":["arn:aws:s3:::{BUCKET}"]}}]}}"#
);
store_bucket_config(BUCKET, "policy.json", &deny).await;
let explicit = client
.get_bucket_notification_configuration()
.bucket(BUCKET)
.send()
.await
.expect_err("explicit deny overrides user allow");
assert_eq!(refusal(&explicit), (Some("AccessDenied".into()), Some(403)));
assert_eq!(handled_count() - before, 1, "authorization refusals do not reach the edge counter");
server.shutdown().await;
}
#[test]
fn notification_authorization_binds_the_bucket_on_the_gateway_stack() {
with_stack(GATEWAY, notification_authorization_is_bound_to_the_requested_bucket);
}
#[test]
fn notification_authorization_binds_the_bucket_on_the_legacy_stack() {
with_stack(LEGACY, notification_authorization_is_bound_to_the_requested_bucket);
}