Skip to main content

rustfs_notify/rules/
xml_config.rs

1// Copyright 2024 RustFS Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::rules::pattern;
16use hashbrown::HashSet;
17use rustfs_s3_types::EventName;
18use rustfs_targets::arn::{ARN, ArnError, TargetIDError};
19use serde::{Deserialize, Serialize};
20use std::io::Read;
21use thiserror::Error;
22
23#[derive(Debug, Error)]
24pub enum ParseConfigError {
25    #[error("XML parsing error:{0}")]
26    XmlError(#[from] quick_xml::errors::serialize::DeError),
27    #[error("Invalid filter value:{0}")]
28    InvalidFilterValue(String),
29    #[error("Invalid filter name: {0}, only 'prefix' or 'suffix' is allowed")]
30    InvalidFilterName(String),
31    #[error("There can only be one 'prefix' in the filter rule")]
32    DuplicatePrefixFilter,
33    #[error("There can only be one 'suffix' in the filter rule")]
34    DuplicateSuffixFilter,
35    #[error("Missing event name")]
36    MissingEventName,
37    #[error("Duplicate event name:{0}")]
38    DuplicateEventName(String), // EventName is usually an enum, and here String is used to represent its text
39    #[error("Repeated queue configuration: ID={0:?}, ARN={1}")]
40    DuplicateQueueConfiguration(Option<String>, String),
41    #[error("Unsupported configuration types (e.g. Lambda, Topic)")]
42    UnsupportedConfiguration,
43    #[error("ARN not found:{0}")]
44    ArnNotFound(String),
45    #[error("Unknown area:{0}")]
46    UnknownRegion(String),
47    #[error("ARN parsing error:{0}")]
48    ArnParseError(#[from] ArnError),
49    #[error("TargetID parsing error:{0}")]
50    TargetIDParseError(#[from] TargetIDError),
51    #[error("IO Error:{0}")]
52    IoError(#[from] std::io::Error),
53    #[error("Region mismatch: Configure region {config_region}, current region {current_region}")]
54    RegionMismatch { config_region: String, current_region: String },
55    #[error("ARN {0} Not found in the provided list")]
56    ArnValidation(String),
57}
58
59#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
60pub struct FilterRule {
61    #[serde(rename = "Name")]
62    pub name: String,
63    #[serde(rename = "Value")]
64    pub value: String,
65}
66
67impl FilterRule {
68    fn validate(&self) -> Result<(), ParseConfigError> {
69        if !self.name.eq_ignore_ascii_case("prefix") && !self.name.eq_ignore_ascii_case("suffix") {
70            return Err(ParseConfigError::InvalidFilterName(self.name.clone()));
71        }
72        // ValidateFilterRuleValue from Go:
73        // no "." or ".." path segments, <= 1024 characters, no '\'.
74        for segment in self.value.split('/') {
75            if segment == "." || segment == ".." {
76                return Err(ParseConfigError::InvalidFilterValue(self.value.clone()));
77            }
78        }
79        // The limit is 1024 *characters* (runes), matching S3/Go semantics. Using the
80        // byte length (`str::len`) would wrongly reject valid keys whose multi-byte
81        // UTF-8 encoding exceeds 1024 bytes while staying under 1024 characters
82        // (backlog#984). `self.value` is a `String`, so it is already valid UTF-8 —
83        // no separate UTF-8 check is needed.
84        if self.value.chars().count() > 1024 || self.value.contains('\\') {
85            return Err(ParseConfigError::InvalidFilterValue(self.value.clone()));
86        }
87        Ok(())
88    }
89}
90
91#[derive(Debug, Serialize, Clone, Default, PartialEq, Eq)]
92pub struct S3KeyFilter {
93    #[serde(rename = "FilterRule", default, skip_serializing_if = "Vec::is_empty")]
94    pub filter_rule_list: Vec<FilterRule>,
95}
96
97impl S3KeyFilter {
98    /// Validate filter rules for duplicates.
99    /// According to AWS S3 documentation, there can be at most one prefix
100    /// and one suffix filter rule per queue configuration.
101    pub fn validate(&self) -> Result<(), ParseConfigError> {
102        let mut has_prefix = false;
103        let mut has_suffix = false;
104        for rule in &self.filter_rule_list {
105            rule.validate()?;
106            if rule.name.eq_ignore_ascii_case("prefix") {
107                if has_prefix {
108                    return Err(ParseConfigError::DuplicatePrefixFilter);
109                }
110                has_prefix = true;
111            } else if rule.name.eq_ignore_ascii_case("suffix") {
112                if has_suffix {
113                    return Err(ParseConfigError::DuplicateSuffixFilter);
114                }
115                has_suffix = true;
116            }
117        }
118        Ok(())
119    }
120
121    /// Check if filter rule list is empty.
122    pub fn is_empty(&self) -> bool {
123        self.filter_rule_list.is_empty()
124    }
125
126    /// Generate pattern string from filter rules.
127    /// This method extracts prefix and suffix values from filter rules
128    /// and generates a wildcard pattern string for matching object keys.
129    pub fn pattern(&self) -> String {
130        let mut prefix_val: Option<&str> = None;
131        let mut suffix_val: Option<&str> = None;
132
133        for rule in &self.filter_rule_list {
134            if rule.name.eq_ignore_ascii_case("prefix") {
135                prefix_val = Some(&rule.value);
136            } else if rule.name.eq_ignore_ascii_case("suffix") {
137                suffix_val = Some(&rule.value);
138            }
139        }
140        pattern::new_pattern(prefix_val, suffix_val)
141    }
142}
143
144#[derive(Debug, serde::Deserialize)]
145struct S3KeyContent {
146    #[serde(rename = "FilterRule", default)]
147    filter_rule_list: Vec<FilterRule>,
148    #[serde(rename = "FilterRuleList", default)]
149    filter_rule_list_wrapper: Option<S3KeyFilterRuleList>,
150}
151
152#[derive(Debug, serde::Deserialize)]
153struct S3KeyFilterRuleList {
154    #[serde(rename = "FilterRule", default)]
155    filter_rule_list: Vec<FilterRule>,
156}
157
158impl S3KeyContent {
159    /// Get all filter rules from this S3Key content, handling both direct FilterRule
160    /// and FilterRuleList wrapper structures
161    fn get_filter_rules(&self) -> Vec<FilterRule> {
162        // If we have a FilterRuleList wrapper, use that
163        if let Some(wrapper) = &self.filter_rule_list_wrapper
164            && !wrapper.filter_rule_list.is_empty()
165        {
166            return wrapper.filter_rule_list.clone();
167        }
168        // Otherwise use direct FilterRule list
169        self.filter_rule_list.clone()
170    }
171}
172
173/// Custom deserializer for S3KeyFilter to handle Filter element correctly.
174/// AWS S3 XML structure: <Filter><S3Key><FilterRule>...</FilterRule></S3Key></Filter>
175impl<'de> Deserialize<'de> for S3KeyFilter {
176    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
177    where
178        D: serde::Deserializer<'de>,
179    {
180        struct S3KeyFilterVisitor {
181            filter_rules: Vec<FilterRule>,
182        }
183
184        impl S3KeyFilterVisitor {
185            fn new() -> Self {
186                Self {
187                    filter_rules: Vec::new(),
188                }
189            }
190        }
191
192        impl<'de> serde::de::Visitor<'de> for S3KeyFilterVisitor {
193            type Value = S3KeyFilter;
194
195            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
196                write!(
197                    formatter,
198                    "an S3Key filter map with an `S3Key` element containing one or more \
199 `FilterRule` children (e.g. <Filter><S3Key><FilterRule>...</FilterRule></S3Key></Filter>)"
200                )
201            }
202
203            fn visit_map<V>(mut self, mut map: V) -> Result<Self::Value, V::Error>
204            where
205                V: serde::de::MapAccess<'de>,
206            {
207                while let Some(key) = map.next_key::<String>()? {
208                    match key.as_str() {
209                        "S3Key" => {
210                            // Parse S3Key content which contains FilterRule(s)
211                            let s3key_content: S3KeyContent = map.next_value()?;
212                            self.filter_rules = s3key_content.get_filter_rules();
213                        }
214                        _ => {
215                            return Err(serde::de::Error::unknown_field(&key, &["S3Key"]));
216                        }
217                    }
218                }
219
220                Ok(S3KeyFilter {
221                    filter_rule_list: self.filter_rules,
222                })
223            }
224        }
225
226        deserializer.deserialize_map(S3KeyFilterVisitor::new())
227    }
228}
229
230#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
231pub struct QueueConfig {
232    #[serde(rename = "Id", skip_serializing_if = "Option::is_none")]
233    pub id: Option<String>,
234    #[serde(rename = "Queue")] // This is ARN in XML
235    pub arn: ARN,
236    #[serde(rename = "Event", default)] // XML has multiple <Event> tags
237    pub events: Vec<EventName>, // EventName needs to handle XML (de)serialization if not string
238    #[serde(rename = "Filter", default, skip_serializing_if = "s3key_filter_is_empty")]
239    pub filter: S3KeyFilter,
240}
241
242fn s3key_filter_is_empty(f: &S3KeyFilter) -> bool {
243    f.filter_rule_list.is_empty()
244}
245
246impl QueueConfig {
247    pub fn validate(&self, region: &str, arn_list: &[String]) -> Result<(), ParseConfigError> {
248        if self.events.is_empty() {
249            return Err(ParseConfigError::MissingEventName);
250        }
251        let mut event_set = HashSet::new();
252        for event in &self.events {
253            // EventName::to_string() or similar for uniqueness check
254            if !event_set.insert(event.to_string()) {
255                return Err(ParseConfigError::DuplicateEventName(event.to_string()));
256            }
257        }
258        self.filter.validate()?;
259
260        // Validate ARN (similar to Go's Queue.Validate)
261        // The Go code checks targetList.Exists(q.ARN.TargetID)
262        // Here we check against a provided arn_list
263        let _config_arn_str = self.arn.to_string();
264        if !self.arn.region.is_empty() && self.arn.region != region {
265            return Err(ParseConfigError::UnknownRegion(self.arn.region.clone()));
266        }
267
268        // Construct the ARN string that would be in arn_list
269        // The arn_list contains ARNs like "arn:rustfs:sqs:REGION:ID:NAME"
270        // We need to ensure self.arn (potentially with region adjusted) is in arn_list
271        let effective_arn = ARN {
272            target_id: self.arn.target_id.clone(),
273            region: if self.arn.region.is_empty() {
274                region.to_string()
275            } else {
276                self.arn.region.clone()
277            },
278            service: self.arn.service.clone(),     // or default "sqs"
279            partition: self.arn.partition.clone(), // or default "rustfs"
280        };
281
282        if !arn_list.contains(&effective_arn.to_string()) {
283            return Err(ParseConfigError::ArnNotFound(effective_arn.to_string()));
284        }
285        Ok(())
286    }
287
288    /// Sets the region if it's not already set in the ARN.
289    pub fn set_region_if_empty(&mut self, region: &str) {
290        if self.arn.region.is_empty() {
291            self.arn.region = region.to_string();
292        }
293    }
294}
295
296/// Corresponding to the `lambda` structure in the Go code.
297/// Used to parse <CloudFunction> ARN from inside the <CloudFunctionConfiguration> tag.
298#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Default)]
299pub struct LambdaConfigDetail {
300    #[serde(rename = "CloudFunction")]
301    pub arn: String,
302    // According to AWS S3 documentation, <CloudFunctionConfiguration> usually also contains Id, Event, Filter
303    // But in order to strictly correspond to the Go `lambda` structure provided, only ARN is included here.
304    // If full support is required, additional fields can be added.
305    // For example:
306    // #[serde(rename = "Id", skip_serializing_if = "Option::is_none")]
307    // pub id: Option<String>,
308    // #[serde(rename = "Event", default, skip_serializing_if = "Vec::is_empty")]
309    // pub events: Vec<EventName>,
310    // #[serde(rename = "Filter", default, skip_serializing_if = "S3KeyFilterIsEmpty")]
311    // pub filter: S3KeyFilter,
312}
313
314/// Corresponding to the `topic` structure in the Go code.
315/// Used to parse <Topic> ARN from inside the <TopicConfiguration> tag.
316#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Default)]
317pub struct TopicConfigDetail {
318    #[serde(rename = "Topic")]
319    pub arn: String,
320    // Similar to LambdaConfigDetail, it can be extended to include fields such as Id, Event, Filter, etc.
321}
322
323#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq, Eq)]
324#[serde(rename = "NotificationConfiguration")]
325pub struct NotificationConfiguration {
326    #[serde(rename = "xmlns", skip_serializing_if = "Option::is_none")]
327    pub xmlns: Option<String>,
328    #[serde(rename = "QueueConfiguration", default, skip_serializing_if = "Vec::is_empty")]
329    pub queue_list: Vec<QueueConfig>,
330    #[serde(
331        rename = "CloudFunctionConfiguration", // Tags for each lambda configuration item in XML
332        default,
333        skip_serializing_if = "Vec::is_empty"
334    )]
335    pub lambda_list: Vec<LambdaConfigDetail>, // Modify: Use a new structure
336
337    #[serde(
338        rename = "TopicConfiguration", // Tags for each topic configuration item in XML
339        default,
340        skip_serializing_if = "Vec::is_empty"
341    )]
342    pub topic_list: Vec<TopicConfigDetail>, // Modify: Use a new structure
343}
344
345impl NotificationConfiguration {
346    pub fn from_reader<R: Read + std::io::BufRead>(reader: R) -> Result<Self, ParseConfigError> {
347        let config: NotificationConfiguration = quick_xml::de::from_reader(reader)?;
348        Ok(config)
349    }
350
351    pub fn validate(&self, current_region: &str, arn_list: &[String]) -> Result<(), ParseConfigError> {
352        // Verification logic remains the same: if lambda_list or topic_list is not empty, it is considered an unsupported configuration
353        if !self.lambda_list.is_empty() || !self.topic_list.is_empty() {
354            return Err(ParseConfigError::UnsupportedConfiguration);
355        }
356
357        let mut unique_queues = HashSet::new();
358        for queue_config in &self.queue_list {
359            queue_config.validate(current_region, arn_list)?;
360            let queue_key = (
361                queue_config.id.clone(),
362                queue_config.arn.to_string(), // Assuming that the ARN structure implements Display or ToString
363            );
364            if !unique_queues.insert(queue_key.clone()) {
365                return Err(ParseConfigError::DuplicateQueueConfiguration(queue_key.0, queue_key.1));
366            }
367        }
368        Ok(())
369    }
370
371    pub fn set_defaults(&mut self, region: &str) {
372        for queue_config in &mut self.queue_list {
373            queue_config.set_region_if_empty(region);
374        }
375        if self.xmlns.is_none() {
376            self.xmlns = Some("http://s3.amazonaws.com/doc/2006-03-01/".to_string());
377        }
378        // Note: If LambdaConfigDetail and TopicConfigDetail contain information such as regions in the future,
379        // You may also need to set the default value here. But according to the current definition, they only contain ARN strings.
380    }
381}
382
383#[cfg(test)]
384mod filter_rule_length_tests {
385    use super::{FilterRule, ParseConfigError};
386
387    fn prefix(value: String) -> FilterRule {
388        FilterRule {
389            name: "prefix".to_string(),
390            value,
391        }
392    }
393
394    #[test]
395    fn length_limit_counts_characters_not_bytes() {
396        // 1024 multi-byte characters: 3072 bytes but exactly 1024 chars — must pass.
397        // The old byte-length check (`str::len`) would have rejected this valid key
398        // (backlog#984).
399        let multibyte = "中".repeat(1024);
400        assert_eq!(multibyte.chars().count(), 1024);
401        assert!(multibyte.len() > 1024, "test setup: byte length must exceed the char limit");
402        prefix(multibyte).validate().expect("1024-character value must be accepted");
403
404        // 1025 characters must be rejected.
405        let too_long = "a".repeat(1025);
406        assert!(matches!(prefix(too_long).validate(), Err(ParseConfigError::InvalidFilterValue(_))));
407
408        // Exactly 1024 ASCII characters must pass.
409        prefix("a".repeat(1024))
410            .validate()
411            .expect("1024 ASCII chars must be accepted");
412    }
413
414    #[test]
415    fn backslash_is_still_rejected() {
416        assert!(matches!(
417            prefix("bad\\path".to_string()).validate(),
418            Err(ParseConfigError::InvalidFilterValue(_))
419        ));
420    }
421}