1use crate::{
16 BucketNotificationConfig, NotificationError, config_manager::notify_configuration_hint,
17 notification_system_subscriber::NotificationSystemSubscriberView, notifier::EventNotifier, rule_engine::NotifyRuleEngine,
18 rules::ParseConfigError,
19};
20use rustfs_s3_types::EventName;
21use std::sync::Arc;
22use tracing::{debug, info, warn};
23
24const LOG_COMPONENT_NOTIFY: &str = "notify";
25const LOG_SUBSYSTEM_BUCKET_CONFIG: &str = "bucket_config";
26const EVENT_NOTIFY_BUCKET_CONFIG_VALIDATION: &str = "notify_bucket_config_validation";
27const EVENT_NOTIFY_BUCKET_CONFIG_LOADED: &str = "notify_bucket_config_loaded";
28
29#[derive(Clone)]
30pub struct NotifyBucketConfigManager {
31 notifier: Arc<EventNotifier>,
32 rule_engine: NotifyRuleEngine,
33 subscriber_view: Arc<NotificationSystemSubscriberView>,
34}
35
36impl NotifyBucketConfigManager {
37 pub fn new(
38 notifier: Arc<EventNotifier>,
39 rule_engine: NotifyRuleEngine,
40 subscriber_view: Arc<NotificationSystemSubscriberView>,
41 ) -> Self {
42 Self {
43 notifier,
44 rule_engine,
45 subscriber_view,
46 }
47 }
48
49 pub async fn has_subscriber(&self, bucket: &str, event: &EventName) -> bool {
50 if !self.subscriber_view.has_subscriber(bucket, event) {
51 return false;
52 }
53 self.rule_engine.has_subscriber(bucket, event).await
54 }
55
56 pub async fn validate_bucket_notification_config(
59 &self,
60 bucket: &str,
61 cfg: &BucketNotificationConfig,
62 ) -> Result<(), NotificationError> {
63 let arn_list = self.notifier.get_arn_list(&cfg.region).await;
64 if arn_list.is_empty() {
65 return Err(NotificationError::Configuration(notify_configuration_hint()));
66 }
67 debug!(
68 event = EVENT_NOTIFY_BUCKET_CONFIG_VALIDATION,
69 component = LOG_COMPONENT_NOTIFY,
70 subsystem = LOG_SUBSYSTEM_BUCKET_CONFIG,
71 bucket = %bucket,
72 region = %cfg.region,
73 available_arn_count = arn_list.len(),
74 "notify bucket config validation"
75 );
76
77 if let Err(e) = cfg.validate(&cfg.region, &arn_list) {
78 debug!(
79 event = EVENT_NOTIFY_BUCKET_CONFIG_VALIDATION,
80 component = LOG_COMPONENT_NOTIFY,
81 subsystem = LOG_SUBSYSTEM_BUCKET_CONFIG,
82 bucket = %bucket,
83 region = %cfg.region,
84 error = %e,
85 result = "validation_failed",
86 "notify bucket config validation"
87 );
88 if !matches!(e, ParseConfigError::ArnNotFound(_)) {
89 return Err(NotificationError::BucketNotification(e.to_string()));
90 }
91 warn!(
92 event = EVENT_NOTIFY_BUCKET_CONFIG_VALIDATION,
93 component = LOG_COMPONENT_NOTIFY,
94 subsystem = LOG_SUBSYSTEM_BUCKET_CONFIG,
95 bucket = %bucket,
96 region = %cfg.region,
97 error = %e,
98 result = "missing_target_arn",
99 "notify bucket config validation"
100 );
101 }
102
103 Ok(())
104 }
105
106 pub async fn apply_bucket_notification_config(&self, bucket: &str, cfg: &BucketNotificationConfig) {
108 self.subscriber_view.apply_bucket_config(bucket, cfg);
109 self.rule_engine.set_bucket_rules(bucket, cfg.get_rules_map().clone()).await;
110 info!(
111 event = EVENT_NOTIFY_BUCKET_CONFIG_LOADED,
112 component = LOG_COMPONENT_NOTIFY,
113 subsystem = LOG_SUBSYSTEM_BUCKET_CONFIG,
114 bucket = %bucket,
115 region = %cfg.region,
116 rule_count = cfg.get_rules_map().inner().len(),
117 "notify bucket config state"
118 );
119 }
120
121 pub async fn load_bucket_notification_config(
122 &self,
123 bucket: &str,
124 cfg: &BucketNotificationConfig,
125 ) -> Result<(), NotificationError> {
126 self.validate_bucket_notification_config(bucket, cfg).await?;
127 self.apply_bucket_notification_config(bucket, cfg).await;
128 Ok(())
129 }
130
131 pub async fn remove_bucket_notification_config(&self, bucket: &str) {
132 self.subscriber_view.clear_bucket(bucket);
133 self.rule_engine.clear_bucket_rules(bucket).await;
134 }
135}
136
137#[cfg(test)]
138mod tests {
139 use super::NotifyBucketConfigManager;
140 use crate::{
141 BucketNotificationConfig, NotificationError, integration::NotificationMetrics,
142 notification_system_subscriber::NotificationSystemSubscriberView, notifier::EventNotifier, rule_engine::NotifyRuleEngine,
143 };
144 use rustfs_s3_types::EventName;
145 use rustfs_targets::arn::TargetID;
146 use std::sync::Arc;
147
148 fn build_manager() -> NotifyBucketConfigManager {
149 let metrics = Arc::new(NotificationMetrics::new());
150 let rule_engine = NotifyRuleEngine::new();
151 let notifier = Arc::new(EventNotifier::new(metrics, rule_engine.clone()));
152 let subscriber_view = Arc::new(NotificationSystemSubscriberView::new());
153 NotifyBucketConfigManager::new(notifier, rule_engine, subscriber_view)
154 }
155
156 #[tokio::test]
157 async fn bucket_config_manager_reports_no_subscriber_for_empty_state() {
158 let manager = build_manager();
159 assert!(!manager.has_subscriber("bucket", &EventName::ObjectCreatedPut).await);
160 }
161
162 #[tokio::test]
163 async fn bucket_config_manager_clears_bucket_snapshot() {
164 let manager = build_manager();
165 let target_id = TargetID::new("primary".to_string(), "webhook".to_string());
166 let mut cfg = BucketNotificationConfig::new("us-east-1");
167 cfg.add_rule(&[EventName::ObjectCreatedPut], "*".to_string(), target_id);
168
169 manager.subscriber_view.apply_bucket_config("bucket", &cfg);
170 assert!(manager.subscriber_view.has_subscriber("bucket", &EventName::ObjectCreatedPut));
171
172 manager.remove_bucket_notification_config("bucket").await;
173 assert!(!manager.subscriber_view.has_subscriber("bucket", &EventName::ObjectCreatedPut));
174 }
175
176 #[tokio::test]
177 async fn validate_bucket_notification_config_rejects_missing_targets_without_applying_rules() {
178 let manager = build_manager();
179 let target_id = TargetID::new("primary".to_string(), "webhook".to_string());
180 let mut cfg = BucketNotificationConfig::new("us-east-1");
181 cfg.add_rule(&[EventName::ObjectCreatedPut], "*".to_string(), target_id);
182
183 let err = manager
184 .validate_bucket_notification_config("bucket", &cfg)
185 .await
186 .expect_err("validation must fail when the runtime has no notify targets");
187 assert!(matches!(err, NotificationError::Configuration(_)));
188 assert!(!manager.has_subscriber("bucket", &EventName::ObjectCreatedPut).await);
189 assert!(!manager.subscriber_view.has_subscriber("bucket", &EventName::ObjectCreatedPut));
190 }
191
192 #[tokio::test]
193 async fn failed_validation_keeps_previous_bucket_rules() {
194 let manager = build_manager();
195 let target_id = TargetID::new("primary".to_string(), "webhook".to_string());
196 let mut cfg = BucketNotificationConfig::new("us-east-1");
197 cfg.add_rule(&[EventName::ObjectCreatedPut], "*".to_string(), target_id);
198 manager.apply_bucket_notification_config("bucket", &cfg).await;
199 assert!(manager.has_subscriber("bucket", &EventName::ObjectCreatedPut).await);
200
201 let err = manager
202 .validate_bucket_notification_config("bucket", &cfg)
203 .await
204 .expect_err("validation must fail when the runtime has no notify targets");
205 assert!(matches!(err, NotificationError::Configuration(_)));
206 assert!(manager.has_subscriber("bucket", &EventName::ObjectCreatedPut).await);
207 }
208}