1use crate::ReplicationTagFilter;
16use crate::ReplicationType;
17use crate::rule::ReplicationRuleExt as _;
18use s3s::dto::DeleteMarkerReplicationStatus;
19use s3s::dto::DeleteReplicationStatus;
20use s3s::dto::Destination;
21use s3s::dto::{
22 ExistingObjectReplicationStatus, ReplicaModificationsStatus, ReplicationConfiguration, ReplicationRule,
23 ReplicationRuleStatus, ReplicationRules, StorageClass,
24};
25use serde::{Deserialize, Serialize};
26use std::collections::{BTreeMap, HashMap, HashSet};
27use uuid::Uuid;
28
29pub const REPLICATION_CAPABILITY_CONTRACT_VERSION: u32 = 1;
30
31pub const REPLICATION_WRITABLE_FIELDS: &[&str] = &[
42 "Role",
43 "Rule.ID",
44 "Rule.Status",
45 "Rule.Priority",
46 "Rule.Filter.Prefix",
47 "Rule.Filter.Tag",
48 "Rule.Filter.And",
49 "Rule.Destination.Bucket",
50 "Rule.ExistingObjectReplication.Status",
51 "Rule.DeleteMarkerReplication.Status",
52 "Rule.DeleteReplication.Status",
53 "Rule.SourceSelectionCriteria.ReplicaModifications.Status",
54];
55
56pub const REPLICATION_READ_ONLY_HISTORICAL_FIELDS: &[&str] = &[
57 "SourceSelectionCriteria.SseKmsEncryptedObjects",
58 "Destination.EncryptionConfiguration",
59 "Destination.Metrics",
60 "Destination.ReplicationTime",
61];
62
63pub const REMOTE_TARGET_CAPABILITY_CONTRACT_VERSION: u32 = 4;
66
67pub const REMOTE_TARGET_WRITABLE_FIELDS: &[&str] = &[
68 "sourcebucket",
69 "endpoint",
70 "credentials.accessKey",
71 "credentials.secretKey",
72 "credentials.sessionToken",
73 "credentials.expiration",
74 "targetbucket",
75 "secure",
76 "path",
77 "api",
78 "arn",
79 "type",
80 "region",
81 "bandwidth",
82 "replicationSync",
83 "storage_class",
84 "skipTlsVerify",
85 "caCertPem",
86 "healthCheckDuration",
90 "disableProxy",
93];
94
95pub const REMOTE_TARGET_READ_ONLY_HISTORICAL_FIELDS: &[&str] = &[];
101
102pub const REMOTE_TARGET_UNSUPPORTED_FIELDS: &[&str] = &["edge", "edgeSyncBeforeExpiry"];
103
104#[derive(Debug, Clone, Serialize, Deserialize, Default)]
105pub struct ObjectOpts {
106 pub name: String,
107 pub user_tags: String,
108 pub version_id: Option<Uuid>,
109 pub delete_marker: bool,
110 pub ssec: bool,
111 pub op_type: ReplicationType,
112 pub replica: bool,
113 pub existing_object: bool,
114 pub target_arn: String,
115}
116
117pub trait ReplicationConfigurationExt {
118 fn replicate(&self, opts: &ObjectOpts) -> bool;
119 fn has_existing_object_replication(&self, arn: &str) -> (bool, bool);
120 fn filter_actionable_rules(&self, obj: &ObjectOpts) -> ReplicationRules;
121 fn get_destination(&self) -> Destination;
122 fn has_active_rules(&self, prefix: &str, recursive: bool) -> bool;
123 fn filter_target_arns(&self, obj: &ObjectOpts) -> Vec<String>;
124 fn filter_force_delete_target_arns(&self, prefix: &str) -> Vec<String>;
125 fn configured_target_arns(&self) -> HashSet<String>;
129 fn filter_target_replication_decisions(&self, obj: &ObjectOpts) -> Vec<(String, bool)> {
130 self.filter_target_arns(obj)
131 .into_iter()
132 .map(|arn| {
133 let mut target = obj.clone();
134 target.target_arn = arn.clone();
135 (arn, self.replicate(&target))
136 })
137 .collect()
138 }
139}
140
141fn rule_replicates(rule: &ReplicationRule, obj: &ObjectOpts) -> bool {
142 if let Some(status) = &rule.existing_object_replication
143 && obj.existing_object
144 && status.status == ExistingObjectReplicationStatus::from_static(ExistingObjectReplicationStatus::DISABLED)
145 {
146 return false;
147 }
148
149 if obj.op_type != ReplicationType::Delete {
150 return rule.metadata_replicate(obj);
151 }
152
153 if !rule.metadata_replicate(obj) {
154 return false;
155 }
156
157 let version_purge = obj.version_id.is_some();
158 if version_purge {
159 rule.delete_replication
160 .as_ref()
161 .is_some_and(|delete| delete.status == DeleteReplicationStatus::from_static(DeleteReplicationStatus::ENABLED))
162 } else {
163 rule.delete_marker_replication.as_ref().is_some_and(|delete_marker| {
164 delete_marker.status == Some(DeleteMarkerReplicationStatus::from_static(DeleteMarkerReplicationStatus::ENABLED))
165 })
166 }
167}
168
169fn replication_filter_tags_match(filter: &s3s::dto::ReplicationRuleFilter, object_tags: &HashMap<String, String>) -> bool {
170 let tag_matches = |tag: &s3s::dto::Tag| match (&tag.key, &tag.value) {
171 (None, None) => true,
172 (Some(key), _) if key.is_empty() => true,
173 (Some(key), Some(value)) => object_tags.get(key) == Some(value),
174 _ => false,
175 };
176
177 filter
178 .and
179 .as_ref()
180 .and_then(|and| and.tags.as_deref())
181 .into_iter()
182 .flatten()
183 .chain(filter.tag.iter())
184 .all(tag_matches)
185}
186
187#[derive(Debug, Clone, Copy, PartialEq, Eq)]
188pub enum ReplicationTargetValidationError {
189 RoleWithMultipleDestinations,
190 StaleTarget,
191}
192
193pub fn unsupported_replication_config_field(config: &ReplicationConfiguration) -> Option<&'static str> {
194 for rule in &config.rules {
195 if rule
196 .source_selection_criteria
197 .as_ref()
198 .is_some_and(|criteria| criteria.sse_kms_encrypted_objects.is_some())
199 {
200 return Some("SourceSelectionCriteria.SseKmsEncryptedObjects");
201 }
202 if rule.destination.encryption_configuration.is_some() {
203 return Some("Destination.EncryptionConfiguration");
204 }
205 if rule.destination.access_control_translation.is_some() {
206 return Some("Destination.AccessControlTranslation");
207 }
208 if rule.destination.account.is_some() {
209 return Some("Destination.Account");
210 }
211 if rule.destination.metrics.is_some() {
212 return Some("Destination.Metrics");
213 }
214 if rule.destination.replication_time.is_some() {
215 return Some("Destination.ReplicationTime");
216 }
217 if rule
224 .destination
225 .storage_class
226 .as_ref()
227 .is_some_and(|class| class.as_str() != StorageClass::STANDARD)
228 {
229 return Some("Destination.StorageClass");
230 }
231 }
232 None
233}
234
235pub fn invalid_replication_config_status_field(config: &ReplicationConfiguration) -> Option<&'static str> {
236 for rule in &config.rules {
237 if !matches!(rule.status.as_str(), ReplicationRuleStatus::ENABLED | ReplicationRuleStatus::DISABLED) {
238 return Some("Rule.Status");
239 }
240 if rule.existing_object_replication.as_ref().is_some_and(|existing| {
241 !matches!(
242 existing.status.as_str(),
243 ExistingObjectReplicationStatus::ENABLED | ExistingObjectReplicationStatus::DISABLED
244 )
245 }) {
246 return Some("Rule.ExistingObjectReplication.Status");
247 }
248 if rule.delete_replication.as_ref().is_some_and(|delete| {
249 !matches!(
250 delete.status.as_str(),
251 DeleteReplicationStatus::ENABLED | DeleteReplicationStatus::DISABLED
252 )
253 }) {
254 return Some("Rule.DeleteReplication.Status");
255 }
256 if rule
257 .delete_marker_replication
258 .as_ref()
259 .and_then(|delete| delete.status.as_ref())
260 .is_some_and(|status| {
261 !matches!(
262 status.as_str(),
263 DeleteMarkerReplicationStatus::ENABLED | DeleteMarkerReplicationStatus::DISABLED
264 )
265 })
266 {
267 return Some("Rule.DeleteMarkerReplication.Status");
268 }
269 if rule
270 .source_selection_criteria
271 .as_ref()
272 .and_then(|criteria| criteria.replica_modifications.as_ref())
273 .is_some_and(|modifications| {
274 !matches!(
275 modifications.status.as_str(),
276 ReplicaModificationsStatus::ENABLED | ReplicaModificationsStatus::DISABLED
277 )
278 })
279 {
280 return Some("Rule.SourceSelectionCriteria.ReplicaModifications.Status");
281 }
282 }
283
284 None
285}
286
287pub fn active_replication_rule_destination_arns(config: &ReplicationConfiguration) -> HashSet<String> {
288 let mut arns = HashSet::new();
289
290 for rule in &config.rules {
291 if rule.status == ReplicationRuleStatus::from_static(ReplicationRuleStatus::DISABLED) {
292 continue;
293 }
294
295 let arn = rule.destination.bucket.trim();
296 if !arn.is_empty() {
297 arns.insert(arn.to_string());
298 }
299 }
300
301 arns
302}
303
304pub fn replication_target_arn_deployment_id(arn: &str) -> Option<String> {
308 let parts: Vec<_> = arn.split(':').collect();
309 if parts.len() == 6
310 && parts[0] == "arn"
311 && matches!(parts[1], "rustfs" | "minio")
312 && parts[2] == "replication"
313 && !parts[4].is_empty()
314 {
315 return Some(parts[4].to_string());
316 }
317
318 None
319}
320
321pub const SITE_REPLICATION_RULE_ID_PREFIX: &str = "site-repl-";
324
325pub fn is_site_replication_rule(rule: &ReplicationRule) -> bool {
330 rule.id
331 .as_deref()
332 .is_some_and(|id| id.starts_with(SITE_REPLICATION_RULE_ID_PREFIX))
333}
334
335pub fn site_replication_rule_deployment_id(rule: &ReplicationRule) -> Option<&str> {
343 let deployment_id = rule.id.as_deref()?.strip_prefix(SITE_REPLICATION_RULE_ID_PREFIX)?;
344 (!deployment_id.is_empty()
345 && replication_target_arn_deployment_id(&rule.destination.bucket).as_deref() == Some(deployment_id))
346 .then_some(deployment_id)
347}
348
349pub fn is_reconciler_owned_site_replication_rule(rule: &ReplicationRule, peer_deployment_ids: &HashSet<String>) -> bool {
354 site_replication_rule_deployment_id(rule).is_some_and(|deployment_id| peer_deployment_ids.contains(deployment_id))
355}
356
357pub fn is_site_replication_role(role: &str, deployment_ids: &HashSet<String>) -> bool {
364 replication_target_arn_deployment_id(role).is_some_and(|deployment_id| deployment_ids.contains(&deployment_id))
365}
366
367#[derive(Debug, Clone, Copy, PartialEq, Eq)]
373pub enum OperatorRuleContract {
374 Derived,
377 Legacy,
385}
386
387pub fn merge_incoming_replication_config(
402 incoming: Option<ReplicationConfiguration>,
403 local: Option<ReplicationConfiguration>,
404 site_deployment_ids: &HashSet<String>,
405 contract: OperatorRuleContract,
406) -> Option<ReplicationConfiguration> {
407 merge_replication_config_keeping_site_rules(incoming, local, site_deployment_ids, contract)
408}
409
410pub fn merge_user_replication_config(
421 incoming: Option<ReplicationConfiguration>,
422 local: Option<ReplicationConfiguration>,
423 peer_deployment_ids: &HashSet<String>,
424 contract: OperatorRuleContract,
425) -> Option<ReplicationConfiguration> {
426 let incoming = incoming.map(|mut config| {
427 match contract {
428 OperatorRuleContract::Derived => config.rules.retain(|rule| {
429 !rule
430 .id
431 .as_deref()
432 .and_then(|id| id.strip_prefix(SITE_REPLICATION_RULE_ID_PREFIX))
433 .is_some_and(|deployment_id| peer_deployment_ids.contains(deployment_id))
434 }),
435 OperatorRuleContract::Legacy => config.rules.sort_by_key(|rule| rule.priority.unwrap_or(0)),
439 }
440 config
441 });
442 merge_replication_config_keeping_site_rules(incoming, local, peer_deployment_ids, contract)
443}
444
445fn merge_replication_config_keeping_site_rules(
446 incoming: Option<ReplicationConfiguration>,
447 local: Option<ReplicationConfiguration>,
448 deployment_ids: &HashSet<String>,
449 contract: OperatorRuleContract,
450) -> Option<ReplicationConfiguration> {
451 let is_site_rule = |rule: &ReplicationRule| match contract {
452 OperatorRuleContract::Derived => is_reconciler_owned_site_replication_rule(rule, deployment_ids),
453 OperatorRuleContract::Legacy => is_site_replication_rule(rule),
454 };
455 let incoming_role = incoming.as_ref().map(|config| config.role.clone()).unwrap_or_default();
456 let mut rules: Vec<ReplicationRule> = incoming
460 .into_iter()
461 .flat_map(|config| config.rules)
462 .filter(|rule| !is_site_rule(rule))
463 .collect();
464 rules.extend(
465 local
466 .into_iter()
467 .flat_map(|config| config.rules)
468 .filter(|rule| is_site_rule(rule)),
469 );
470
471 if rules.is_empty() {
472 return None;
473 }
474
475 let drop_role = match contract {
476 OperatorRuleContract::Derived => {
477 assign_site_replication_rule_priorities(&mut rules, is_site_rule);
478 is_site_replication_role(&incoming_role, deployment_ids)
479 }
480 OperatorRuleContract::Legacy => {
481 for (index, rule) in rules.iter_mut().enumerate() {
482 rule.priority = Some(i32::try_from(index + 1).unwrap_or(i32::MAX));
483 }
484 replication_target_arn_deployment_id(&incoming_role).is_some()
485 }
486 };
487 let role = if drop_role { String::new() } else { incoming_role };
488
489 Some(ReplicationConfiguration { role, rules })
490}
491
492pub fn assign_site_replication_rule_priorities(rules: &mut [ReplicationRule], is_site_rule: impl Fn(&ReplicationRule) -> bool) {
501 let taken: HashSet<i32> = rules
502 .iter()
503 .filter(|rule| !is_site_rule(rule))
504 .map(|rule| rule.priority.unwrap_or(0))
505 .collect();
506 let mut next = 1;
507 for rule in rules.iter_mut().filter(|rule| is_site_rule(rule)) {
508 while taken.contains(&next) {
509 next += 1;
510 }
511 rule.priority = Some(next);
512 next = next.saturating_add(1);
513 }
514}
515
516pub fn replication_target_arns(config: &ReplicationConfiguration) -> HashSet<String> {
517 let role = config.role.trim();
518 if !role.is_empty() {
519 return HashSet::from([role.to_string()]);
520 }
521
522 active_replication_rule_destination_arns(config)
523}
524
525pub fn validate_replication_config_target_arns<'a>(
526 configured_arns: impl IntoIterator<Item = &'a str>,
527 config: &ReplicationConfiguration,
528) -> std::result::Result<(), ReplicationTargetValidationError> {
529 let configured_arns = configured_arns.into_iter().collect::<HashSet<_>>();
530
531 let role = config.role.trim();
532 let destination_arns = active_replication_rule_destination_arns(config);
533 if !role.is_empty() && destination_arns.len() > 1 {
534 return Err(ReplicationTargetValidationError::RoleWithMultipleDestinations);
535 }
536
537 for configured_arn in replication_target_arns(config) {
538 if !configured_arns.contains(configured_arn.as_str()) {
539 return Err(ReplicationTargetValidationError::StaleTarget);
540 }
541 }
542
543 Ok(())
544}
545
546pub fn should_remove_replication_target(
547 target_arn: &str,
548 is_replication_target: bool,
549 config_target_arns: &HashSet<String>,
550) -> bool {
551 is_replication_target && config_target_arns.contains(target_arn)
552}
553
554pub const REPLICATION_CONFIG_MAX_RULES: usize = 1000;
557
558pub const REPLICATION_CONFIG_MAX_RULE_ID_LEN: usize = 255;
560
561#[derive(Debug, Clone, Copy, PartialEq, Eq)]
564pub enum ReplicationConfigStructureError {
565 NoRules,
566 TooManyRules,
567 NegativeRulePriority,
568 DuplicateRulePriority,
569 RuleIdTooLong,
570 AmbiguousRuleFilter,
571 TagFilterWithDeleteMarkerReplication,
572}
573
574impl ReplicationConfigStructureError {
575 pub fn message(self) -> &'static str {
576 match self {
577 Self::NoRules => "replication configuration must contain at least one rule",
578 Self::TooManyRules => "replication configuration cannot contain more than 1000 rules",
579 Self::NegativeRulePriority => "replication rule Priority must be zero or a positive integer",
580 Self::DuplicateRulePriority => "replication rule Priority must be unique across rules",
581 Self::RuleIdTooLong => "replication rule ID cannot be longer than 255 characters",
582 Self::AmbiguousRuleFilter => "replication rule Filter must specify only one of Prefix, Tag or And",
583 Self::TagFilterWithDeleteMarkerReplication => {
584 "delete marker replication cannot be enabled on a rule with a Tag filter"
585 }
586 }
587 }
588}
589
590fn filter_and_operator_is_set(and: &s3s::dto::ReplicationRuleAndOperator) -> bool {
591 and.prefix.as_ref().is_some_and(|prefix| !prefix.is_empty()) || and.tags.as_ref().is_some_and(|tags| !tags.is_empty())
592}
593
594pub fn validate_replication_config_structure(
612 config: &ReplicationConfiguration,
613) -> std::result::Result<(), ReplicationConfigStructureError> {
614 if config.rules.is_empty() {
615 return Err(ReplicationConfigStructureError::NoRules);
616 }
617 if config.rules.len() > REPLICATION_CONFIG_MAX_RULES {
618 return Err(ReplicationConfigStructureError::TooManyRules);
619 }
620
621 let mut priorities = HashSet::new();
622 for rule in &config.rules {
623 let priority = rule.priority.unwrap_or(0);
624 if priority < 0 {
625 return Err(ReplicationConfigStructureError::NegativeRulePriority);
626 }
627 if !priorities.insert(priority) {
628 return Err(ReplicationConfigStructureError::DuplicateRulePriority);
629 }
630
631 if rule
633 .id
634 .as_ref()
635 .is_some_and(|id| id.len() > REPLICATION_CONFIG_MAX_RULE_ID_LEN)
636 {
637 return Err(ReplicationConfigStructureError::RuleIdTooLong);
638 }
639
640 if let Some(filter) = &rule.filter {
641 let has_and = filter.and.as_ref().is_some_and(filter_and_operator_is_set);
642 let has_prefix = filter.prefix.as_ref().is_some_and(|prefix| !prefix.is_empty());
643 let has_tag = filter
646 .tag
647 .as_ref()
648 .is_some_and(|tag| tag.key.as_ref().is_some_and(|key| !key.is_empty()));
649 if usize::from(has_and) + usize::from(has_prefix) + usize::from(has_tag) > 1 {
650 return Err(ReplicationConfigStructureError::AmbiguousRuleFilter);
651 }
652
653 let delete_marker_replication_enabled = rule
654 .delete_marker_replication
655 .as_ref()
656 .and_then(|delete_marker| delete_marker.status.as_ref())
657 .is_some_and(|status| status.as_str() == DeleteMarkerReplicationStatus::ENABLED);
658 if delete_marker_replication_enabled && has_tag {
659 return Err(ReplicationConfigStructureError::TagFilterWithDeleteMarkerReplication);
660 }
661 }
662 }
663
664 Ok(())
665}
666
667impl ReplicationConfigurationExt for ReplicationConfiguration {
668 fn has_existing_object_replication(&self, arn: &str) -> (bool, bool) {
670 let mut has_arn = false;
671 let arn = arn.trim();
672
673 for rule in &self.rules {
674 if rule.destination.bucket.trim() == arn || self.role.trim() == arn {
675 if !has_arn {
676 has_arn = true;
677 }
678 if let Some(status) = &rule.existing_object_replication
679 && status.status == ExistingObjectReplicationStatus::from_static(ExistingObjectReplicationStatus::ENABLED)
680 {
681 return (true, true);
682 }
683 }
684 }
685 (has_arn, false)
686 }
687
688 fn filter_actionable_rules(&self, obj: &ObjectOpts) -> ReplicationRules {
689 if obj.name.is_empty() && obj.op_type != ReplicationType::Resync && obj.op_type != ReplicationType::All {
690 return vec![];
691 }
692
693 let mut rules = ReplicationRules::default();
694
695 for rule in &self.rules {
696 if rule.status == ReplicationRuleStatus::from_static(ReplicationRuleStatus::DISABLED) {
697 continue;
698 }
699
700 if !obj.target_arn.is_empty()
701 && rule.destination.bucket.trim() != obj.target_arn.trim()
702 && self.role.trim() != obj.target_arn.trim()
703 {
704 continue;
705 }
706
707 if obj.op_type == ReplicationType::Resync || obj.op_type == ReplicationType::All {
708 rules.push(rule.clone());
709 continue;
710 }
711
712 if let Some(status) = &rule.existing_object_replication
713 && obj.existing_object
714 && status.status == ExistingObjectReplicationStatus::from_static(ExistingObjectReplicationStatus::DISABLED)
715 {
716 continue;
717 }
718
719 if !obj.name.starts_with(rule.prefix()) {
720 continue;
721 }
722
723 if let Some(filter) = &rule.filter {
724 let object_tags = ReplicationTagFilter::decode_tags_to_map(&obj.user_tags);
725 if replication_filter_tags_match(filter, &object_tags) {
726 rules.push(rule.clone());
727 }
728 } else {
729 rules.push(rule.clone());
730 }
731 }
732
733 rules.sort_by(|a, b| {
739 b.priority
740 .cmp(&a.priority)
741 .then_with(|| a.destination.bucket.cmp(&b.destination.bucket))
742 .then_with(|| a.id.cmp(&b.id))
743 });
744
745 rules
746 }
747
748 fn get_destination(&self) -> Destination {
750 if !self.rules.is_empty() {
751 self.rules[0].destination.clone()
752 } else {
753 Destination {
754 bucket: String::new(),
755 ..Default::default()
756 }
757 }
758 }
759
760 fn replicate(&self, obj: &ObjectOpts) -> bool {
762 let rules = self.filter_actionable_rules(obj);
763 rules.first().is_some_and(|rule| rule_replicates(rule, obj))
764 }
765
766 fn has_active_rules(&self, prefix: &str, recursive: bool) -> bool {
771 if self.rules.is_empty() {
772 return false;
773 }
774
775 for rule in &self.rules {
776 if rule.status == ReplicationRuleStatus::from_static(ReplicationRuleStatus::DISABLED) {
777 continue;
778 }
779
780 if let Some(filter) = &rule.filter
781 && let Some(filter_prefix) = &filter.prefix
782 {
783 if !prefix.is_empty() && !filter_prefix.is_empty() {
784 if !recursive && !prefix.starts_with(filter_prefix) {
786 continue;
787 }
788 }
789
790 if recursive && !rule.prefix().starts_with(prefix) && !prefix.starts_with(rule.prefix()) {
792 continue;
793 }
794 }
795 return true;
796 }
797 false
798 }
799
800 fn configured_target_arns(&self) -> HashSet<String> {
802 let role = self.role.trim();
803 if !role.is_empty() {
804 return HashSet::from([role.to_string()]);
805 }
806 self.rules
807 .iter()
808 .map(|rule| rule.destination.bucket.trim())
809 .filter(|arn| !arn.is_empty())
810 .map(str::to_string)
811 .collect()
812 }
813
814 fn filter_target_arns(&self, obj: &ObjectOpts) -> Vec<String> {
815 let role = self.role.trim();
816 if !role.is_empty() {
817 return vec![role.to_string()];
818 }
819
820 let mut arns: Vec<String> = Vec::new();
823 for rule in self.filter_actionable_rules(obj) {
824 if rule.status == ReplicationRuleStatus::from_static(ReplicationRuleStatus::DISABLED) {
825 continue;
826 }
827
828 let arn = rule.destination.bucket.trim();
829 if !arn.is_empty() && !arns.iter().any(|seen| seen == arn) {
830 arns.push(arn.to_string());
831 }
832 }
833 arns
834 }
835
836 fn filter_force_delete_target_arns(&self, prefix: &str) -> Vec<String> {
837 let role = self.role.trim();
838 let mut selected = BTreeMap::<String, (&ReplicationRule, bool)>::new();
839
840 for rule in &self.rules {
841 if rule.status == ReplicationRuleStatus::from_static(ReplicationRuleStatus::DISABLED) {
842 continue;
843 }
844
845 let rule_prefix = rule.prefix();
846 if !prefix.starts_with(rule_prefix) && !rule_prefix.starts_with(prefix) {
847 continue;
848 }
849
850 let target = if role.is_empty() {
851 rule.destination.bucket.trim()
852 } else {
853 role
854 };
855 if target.is_empty() {
856 continue;
857 }
858
859 let delete_enabled =
860 rule.delete_replication.as_ref().is_some_and(|delete| {
861 delete.status == DeleteReplicationStatus::from_static(DeleteReplicationStatus::ENABLED)
862 }) || rule.delete_marker_replication.as_ref().is_some_and(|delete_marker| {
863 delete_marker.status
864 == Some(DeleteMarkerReplicationStatus::from_static(DeleteMarkerReplicationStatus::ENABLED))
865 });
866
867 if selected
868 .get(target)
869 .is_none_or(|(current, _)| rule.priority > current.priority)
870 {
871 selected.insert(target.to_string(), (rule, delete_enabled));
872 }
873 }
874
875 selected
876 .into_iter()
877 .filter_map(|(target, (_, enabled))| enabled.then_some(target))
878 .collect()
879 }
880
881 fn filter_target_replication_decisions(&self, obj: &ObjectOpts) -> Vec<(String, bool)> {
882 let rules = self.filter_actionable_rules(obj);
883 let role = self.role.trim();
884 if !role.is_empty() {
885 let mut selected = None;
886 for rule in &rules {
887 if selected.is_none_or(|current: &ReplicationRule| rule.priority > current.priority) {
888 selected = Some(rule);
889 }
890 }
891 return vec![(role.to_string(), selected.is_some_and(|rule| rule_replicates(rule, obj)))];
892 }
893
894 let mut target_indexes: HashMap<&str, usize> = HashMap::new();
895 let mut selected_rules: Vec<(&str, &ReplicationRule)> = Vec::new();
896 for rule in &rules {
897 let arn = rule.destination.bucket.trim();
898 if arn.is_empty() {
899 continue;
900 }
901 if let Some(index) = target_indexes.get(arn).copied() {
902 if rule.priority > selected_rules[index].1.priority {
903 selected_rules[index].1 = rule;
904 }
905 } else {
906 target_indexes.insert(arn, selected_rules.len());
907 selected_rules.push((arn, rule));
908 }
909 }
910 selected_rules
911 .into_iter()
912 .map(|(arn, rule)| (arn.to_string(), rule_replicates(rule, obj)))
913 .collect()
914 }
915}
916
917#[cfg(test)]
918mod tests {
919 use super::*;
920 use s3s::dto::{
921 DeleteMarkerReplication, DeleteReplication, Destination, EncryptionConfiguration, ExistingObjectReplication, Metrics,
922 MetricsStatus, ReplicaModifications, ReplicationRule, ReplicationTime, ReplicationTimeStatus, ReplicationTimeValue,
923 SourceSelectionCriteria, SseKmsEncryptedObjects, SseKmsEncryptedObjectsStatus,
924 };
925 use s3s::xml::{Deserializer, Serializer};
926
927 fn replication_rule(id: &str, arn: &str) -> ReplicationRule {
928 ReplicationRule {
929 delete_marker_replication: Some(DeleteMarkerReplication::default()),
930 delete_replication: None,
931 destination: Destination {
932 bucket: arn.to_string(),
933 ..Default::default()
934 },
935 existing_object_replication: Some(ExistingObjectReplication {
936 status: ExistingObjectReplicationStatus::from_static(ExistingObjectReplicationStatus::ENABLED),
937 }),
938 filter: None,
939 id: Some(id.to_string()),
940 prefix: Some(String::new()),
941 priority: Some(1),
942 source_selection_criteria: None,
943 status: ReplicationRuleStatus::from_static(ReplicationRuleStatus::ENABLED),
944 }
945 }
946
947 fn structure_config(rules: Vec<ReplicationRule>) -> ReplicationConfiguration {
948 ReplicationConfiguration {
949 role: String::new(),
950 rules,
951 }
952 }
953
954 fn tag_filter() -> s3s::dto::ReplicationRuleFilter {
955 s3s::dto::ReplicationRuleFilter {
956 tag: Some(s3s::dto::Tag {
957 key: Some("k".to_string()),
958 value: Some("v".to_string()),
959 }),
960 ..Default::default()
961 }
962 }
963
964 #[test]
965 fn structure_validation_accepts_multi_rule_config_with_unique_priorities() {
966 let mut second = replication_rule("rule-2", "arn:target:a");
967 second.priority = Some(2);
968 second.filter = Some(s3s::dto::ReplicationRuleFilter {
969 and: Some(s3s::dto::ReplicationRuleAndOperator {
970 prefix: Some("photos/".to_string()),
971 tags: Some(vec![s3s::dto::Tag {
972 key: Some("k".to_string()),
973 value: Some("v".to_string()),
974 }]),
975 }),
976 ..Default::default()
977 });
978 let config = structure_config(vec![replication_rule("rule-1", "arn:target:a"), second]);
979
980 assert_eq!(validate_replication_config_structure(&config), Ok(()));
981 }
982
983 #[test]
984 fn structure_validation_rejects_empty_rule_list() {
985 let config = structure_config(Vec::new());
986
987 assert_eq!(
988 validate_replication_config_structure(&config),
989 Err(ReplicationConfigStructureError::NoRules)
990 );
991 }
992
993 #[test]
994 fn structure_validation_rejects_more_than_max_rules() {
995 let rules = (0..=REPLICATION_CONFIG_MAX_RULES as i32)
996 .map(|priority| {
997 let mut rule = replication_rule(&format!("rule-{priority}"), "arn:target:a");
998 rule.priority = Some(priority);
999 rule
1000 })
1001 .collect();
1002
1003 assert_eq!(
1004 validate_replication_config_structure(&structure_config(rules)),
1005 Err(ReplicationConfigStructureError::TooManyRules)
1006 );
1007 }
1008
1009 #[test]
1010 fn structure_validation_rejects_duplicate_priorities() {
1011 let config = structure_config(vec![
1012 replication_rule("rule-1", "arn:target:a"),
1013 replication_rule("rule-2", "arn:target:a"),
1014 ]);
1015
1016 assert_eq!(
1017 validate_replication_config_structure(&config),
1018 Err(ReplicationConfigStructureError::DuplicateRulePriority)
1019 );
1020 }
1021
1022 #[test]
1023 fn structure_validation_treats_missing_priority_as_zero_for_uniqueness() {
1024 let mut first = replication_rule("rule-1", "arn:target:a");
1025 first.priority = None;
1026 let mut second = replication_rule("rule-2", "arn:target:a");
1027 second.priority = None;
1028
1029 assert_eq!(
1030 validate_replication_config_structure(&structure_config(vec![first, second])),
1031 Err(ReplicationConfigStructureError::DuplicateRulePriority)
1032 );
1033 }
1034
1035 #[test]
1036 fn structure_validation_rejects_negative_priority() {
1037 let mut rule = replication_rule("rule-1", "arn:target:a");
1038 rule.priority = Some(-1);
1039
1040 assert_eq!(
1041 validate_replication_config_structure(&structure_config(vec![rule])),
1042 Err(ReplicationConfigStructureError::NegativeRulePriority)
1043 );
1044 }
1045
1046 #[test]
1047 fn structure_validation_rejects_rule_id_longer_than_255_chars() {
1048 let mut rule = replication_rule(&"x".repeat(REPLICATION_CONFIG_MAX_RULE_ID_LEN + 1), "arn:target:a");
1049 rule.priority = Some(1);
1050
1051 assert_eq!(
1052 validate_replication_config_structure(&structure_config(vec![rule])),
1053 Err(ReplicationConfigStructureError::RuleIdTooLong)
1054 );
1055 }
1056
1057 #[test]
1058 fn structure_validation_counts_rule_id_limit_in_bytes() {
1059 let mut within_byte_limit = replication_rule(&"\u{00e9}".repeat(127), "arn:target:a");
1060 within_byte_limit.priority = Some(1);
1061 assert_eq!(
1062 within_byte_limit.id.as_ref().expect("rule id should be present").len(),
1063 REPLICATION_CONFIG_MAX_RULE_ID_LEN - 1
1064 );
1065 assert_eq!(validate_replication_config_structure(&structure_config(vec![within_byte_limit])), Ok(()));
1066
1067 let mut over_byte_limit = replication_rule(&"\u{00e9}".repeat(128), "arn:target:a");
1068 over_byte_limit.priority = Some(1);
1069 assert!(
1070 over_byte_limit
1071 .id
1072 .as_ref()
1073 .expect("rule id should be present")
1074 .chars()
1075 .count()
1076 < REPLICATION_CONFIG_MAX_RULE_ID_LEN
1077 );
1078 assert_eq!(
1079 validate_replication_config_structure(&structure_config(vec![over_byte_limit])),
1080 Err(ReplicationConfigStructureError::RuleIdTooLong)
1081 );
1082 }
1083
1084 #[test]
1085 fn structure_validation_rejects_filter_with_both_prefix_and_tag() {
1086 let mut rule = replication_rule("rule-1", "arn:target:a");
1087 let mut filter = tag_filter();
1088 filter.prefix = Some("photos/".to_string());
1089 rule.filter = Some(filter);
1090
1091 assert_eq!(
1092 validate_replication_config_structure(&structure_config(vec![rule])),
1093 Err(ReplicationConfigStructureError::AmbiguousRuleFilter)
1094 );
1095 }
1096
1097 #[test]
1098 fn structure_validation_rejects_delete_marker_replication_on_tag_filtered_rule() {
1099 let mut rule = replication_rule("rule-1", "arn:target:a");
1100 rule.delete_marker_replication = Some(DeleteMarkerReplication {
1101 status: Some(DeleteMarkerReplicationStatus::from_static(DeleteMarkerReplicationStatus::ENABLED)),
1102 });
1103 rule.filter = Some(tag_filter());
1104
1105 assert_eq!(
1106 validate_replication_config_structure(&structure_config(vec![rule])),
1107 Err(ReplicationConfigStructureError::TagFilterWithDeleteMarkerReplication)
1108 );
1109 }
1110
1111 #[test]
1112 fn structure_validation_treats_empty_tag_element_as_absent() {
1113 let mut rule = replication_rule("rule-1", "arn:target:a");
1117 rule.delete_marker_replication = Some(DeleteMarkerReplication {
1118 status: Some(DeleteMarkerReplicationStatus::from_static(DeleteMarkerReplicationStatus::ENABLED)),
1119 });
1120 rule.filter = Some(s3s::dto::ReplicationRuleFilter {
1121 prefix: Some("photos/".to_string()),
1122 tag: Some(s3s::dto::Tag { key: None, value: None }),
1123 ..Default::default()
1124 });
1125
1126 assert_eq!(validate_replication_config_structure(&structure_config(vec![rule])), Ok(()));
1127 }
1128
1129 #[test]
1130 fn structure_validation_allows_delete_marker_replication_with_and_tags() {
1131 let mut rule = replication_rule("rule-1", "arn:target:a");
1136 rule.delete_marker_replication = Some(DeleteMarkerReplication {
1137 status: Some(DeleteMarkerReplicationStatus::from_static(DeleteMarkerReplicationStatus::ENABLED)),
1138 });
1139 rule.filter = Some(s3s::dto::ReplicationRuleFilter {
1140 and: Some(s3s::dto::ReplicationRuleAndOperator {
1141 prefix: None,
1142 tags: Some(vec![
1143 s3s::dto::Tag {
1144 key: Some("k1".to_string()),
1145 value: Some("v1".to_string()),
1146 },
1147 s3s::dto::Tag {
1148 key: Some("k2".to_string()),
1149 value: Some("v2".to_string()),
1150 },
1151 ]),
1152 }),
1153 ..Default::default()
1154 });
1155
1156 assert_eq!(validate_replication_config_structure(&structure_config(vec![rule])), Ok(()));
1157 }
1158
1159 #[test]
1160 fn actionable_rules_require_every_and_tag_to_match() {
1161 let mut rule = replication_rule("rule-1", "arn:target:a");
1162 rule.filter = Some(s3s::dto::ReplicationRuleFilter {
1163 and: Some(s3s::dto::ReplicationRuleAndOperator {
1164 prefix: None,
1165 tags: Some(vec![
1166 s3s::dto::Tag {
1167 key: Some("env".to_string()),
1168 value: Some("prod".to_string()),
1169 },
1170 s3s::dto::Tag {
1171 key: Some("tier".to_string()),
1172 value: Some("gold".to_string()),
1173 },
1174 ]),
1175 }),
1176 ..Default::default()
1177 });
1178 let config = structure_config(vec![rule]);
1179 let object = |user_tags: &str| ObjectOpts {
1180 name: "object".to_string(),
1181 user_tags: user_tags.to_string(),
1182 ..Default::default()
1183 };
1184
1185 assert!(config.filter_target_arns(&object("env=prod")).is_empty());
1186 assert_eq!(config.filter_target_arns(&object("env=prod&tier=gold")), vec!["arn:target:a"]);
1187 assert!(config.filter_target_arns(&object("")).is_empty());
1188
1189 let mut malformed = config;
1190 malformed.rules[0].filter.as_mut().unwrap().and.as_mut().unwrap().tags = Some(vec![s3s::dto::Tag {
1191 key: Some("env".to_string()),
1192 value: None,
1193 }]);
1194 assert!(
1195 malformed.filter_target_arns(&object("env=prod")).is_empty(),
1196 "a malformed tag filter must fail closed"
1197 );
1198 }
1199
1200 #[test]
1201 fn structure_validation_allows_tag_filter_when_delete_marker_replication_disabled() {
1202 let mut rule = replication_rule("rule-1", "arn:target:a");
1203 rule.delete_marker_replication = Some(DeleteMarkerReplication {
1204 status: Some(DeleteMarkerReplicationStatus::from_static(DeleteMarkerReplicationStatus::DISABLED)),
1205 });
1206 rule.filter = Some(tag_filter());
1207
1208 assert_eq!(validate_replication_config_structure(&structure_config(vec![rule])), Ok(()));
1209 }
1210
1211 #[test]
1212 fn filter_target_arns_uses_role_when_role_is_present() {
1213 let config = ReplicationConfiguration {
1214 role: " arn:legacy:target ".to_string(),
1215 rules: vec![
1216 replication_rule("rule-1", "arn:target:a"),
1217 replication_rule("rule-2", "arn:target:b"),
1218 ],
1219 };
1220
1221 let arns = config.filter_target_arns(&ObjectOpts {
1222 name: "object".to_string(),
1223 op_type: ReplicationType::Object,
1224 ..Default::default()
1225 });
1226
1227 assert_eq!(arns, vec!["arn:legacy:target".to_string()]);
1228 }
1229
1230 #[test]
1231 fn filter_target_arns_falls_back_to_role_when_destination_is_empty() {
1232 let config = ReplicationConfiguration {
1233 role: "arn:legacy:target".to_string(),
1234 rules: vec![ReplicationRule {
1235 delete_marker_replication: Some(DeleteMarkerReplication::default()),
1236 delete_replication: None,
1237 destination: Destination {
1238 bucket: String::new(),
1239 ..Default::default()
1240 },
1241 existing_object_replication: Some(ExistingObjectReplication {
1242 status: ExistingObjectReplicationStatus::from_static(ExistingObjectReplicationStatus::ENABLED),
1243 }),
1244 filter: None,
1245 id: Some("rule-1".to_string()),
1246 prefix: Some(String::new()),
1247 priority: Some(1),
1248 source_selection_criteria: None,
1249 status: ReplicationRuleStatus::from_static(ReplicationRuleStatus::ENABLED),
1250 }],
1251 };
1252
1253 let arns = config.filter_target_arns(&ObjectOpts {
1254 name: "object".to_string(),
1255 op_type: ReplicationType::Object,
1256 ..Default::default()
1257 });
1258
1259 assert_eq!(arns, vec!["arn:legacy:target".to_string()]);
1260 }
1261
1262 fn replication_rule_existing_object_disabled(id: &str, arn: &str) -> ReplicationRule {
1263 ReplicationRule {
1264 delete_marker_replication: Some(DeleteMarkerReplication::default()),
1265 delete_replication: None,
1266 destination: Destination {
1267 bucket: arn.to_string(),
1268 ..Default::default()
1269 },
1270 existing_object_replication: Some(ExistingObjectReplication {
1271 status: ExistingObjectReplicationStatus::from_static(ExistingObjectReplicationStatus::DISABLED),
1272 }),
1273 filter: None,
1274 id: Some(id.to_string()),
1275 prefix: Some(String::new()),
1276 priority: Some(1),
1277 source_selection_criteria: None,
1278 status: ReplicationRuleStatus::from_static(ReplicationRuleStatus::ENABLED),
1279 }
1280 }
1281
1282 #[test]
1286 fn filter_target_arns_excludes_disabled_existing_object_target_for_existing_object_op() {
1287 let config = ReplicationConfiguration {
1288 role: String::new(),
1289 rules: vec![
1290 replication_rule("rule-enabled", "arn:target:enabled"),
1291 replication_rule_existing_object_disabled("rule-disabled", "arn:target:disabled"),
1292 ],
1293 };
1294
1295 let arns = config.filter_target_arns(&ObjectOpts {
1296 name: "object".to_string(),
1297 op_type: ReplicationType::ExistingObject,
1298 existing_object: true,
1299 ..Default::default()
1300 });
1301
1302 assert_eq!(arns.len(), 1, "only the ENABLED target should be returned for ExistingObject ops");
1303 assert!(arns.contains(&"arn:target:enabled".to_string()));
1304 assert!(!arns.contains(&"arn:target:disabled".to_string()));
1305 }
1306
1307 #[test]
1310 fn filter_target_arns_includes_disabled_existing_object_target_for_heal_op() {
1311 let config = ReplicationConfiguration {
1312 role: String::new(),
1313 rules: vec![
1314 replication_rule("rule-enabled", "arn:target:enabled"),
1315 replication_rule_existing_object_disabled("rule-disabled", "arn:target:disabled"),
1316 ],
1317 };
1318
1319 let arns = config.filter_target_arns(&ObjectOpts {
1320 name: "object".to_string(),
1321 op_type: ReplicationType::Heal,
1322 existing_object: false,
1323 ..Default::default()
1324 });
1325
1326 assert_eq!(
1327 arns.len(),
1328 2,
1329 "Heal ops must reach all targets regardless of existing_object_replication setting"
1330 );
1331 assert!(arns.contains(&"arn:target:enabled".to_string()));
1332 assert!(arns.contains(&"arn:target:disabled".to_string()));
1333 }
1334
1335 #[test]
1336 fn replication_target_arns_use_role_when_present() {
1337 let role = "arn:rustfs:replication:us-east-1:source:bucket";
1338 let destination = "arn:rustfs:replication:us-east-1:target:bucket";
1339 let config = ReplicationConfiguration {
1340 role: format!(" {role} "),
1341 rules: vec![replication_rule("rule-1", destination)],
1342 };
1343
1344 let arns = replication_target_arns(&config);
1345
1346 assert!(arns.contains(role));
1347 assert!(!arns.contains(destination));
1348 }
1349
1350 #[test]
1351 fn replication_target_arns_use_rule_destinations_without_role() {
1352 let destination = "arn:rustfs:replication:us-east-1:target:bucket";
1353 let config = ReplicationConfiguration {
1354 role: String::new(),
1355 rules: vec![replication_rule("rule-1", destination)],
1356 };
1357
1358 let arns = replication_target_arns(&config);
1359
1360 assert!(arns.contains(destination));
1361 }
1362
1363 #[test]
1364 fn validate_replication_config_target_arns_accepts_matching_destination_arns() {
1365 let arn = "arn:rustfs:replication:us-east-1:target:bucket";
1366 let config = ReplicationConfiguration {
1367 role: String::new(),
1368 rules: vec![replication_rule("rule-1", arn)],
1369 };
1370
1371 validate_replication_config_target_arns([arn], &config).expect("matching target should pass validation");
1372 }
1373
1374 #[test]
1375 fn validate_replication_config_target_arns_rejects_stale_destination_arns() {
1376 let config = ReplicationConfiguration {
1377 role: String::new(),
1378 rules: vec![replication_rule("rule-1", "arn:rustfs:replication:us-east-1:target-b:bucket")],
1379 };
1380
1381 let err = validate_replication_config_target_arns(["arn:rustfs:replication:us-east-1:target-a:bucket"], &config)
1382 .expect_err("stale target should fail validation");
1383 assert_eq!(err, ReplicationTargetValidationError::StaleTarget);
1384 }
1385
1386 #[test]
1387 fn validate_replication_config_target_arns_rejects_role_with_multiple_destinations() {
1388 let role = "arn:rustfs:replication:us-east-1:role-target:bucket";
1389 let config = ReplicationConfiguration {
1390 role: role.to_string(),
1391 rules: vec![
1392 replication_rule("rule-a", "arn:rustfs:replication:us-east-1:target-a:bucket"),
1393 replication_rule("rule-b", "arn:rustfs:replication:us-east-1:target-b:bucket"),
1394 ],
1395 };
1396
1397 let err = validate_replication_config_target_arns([role], &config)
1398 .expect_err("role plus multiple destinations should be rejected");
1399 assert_eq!(err, ReplicationTargetValidationError::RoleWithMultipleDestinations);
1400 }
1401
1402 #[test]
1403 fn validate_replication_config_target_arns_ignores_disabled_rules() {
1404 let mut rule = replication_rule("rule-1", "arn:rustfs:replication:us-east-1:stale:bucket");
1405 rule.status = ReplicationRuleStatus::from_static(ReplicationRuleStatus::DISABLED);
1406 let config = ReplicationConfiguration {
1407 role: String::new(),
1408 rules: vec![rule],
1409 };
1410
1411 validate_replication_config_target_arns(std::iter::empty::<&str>(), &config)
1412 .expect("disabled rules should not require live targets");
1413 }
1414
1415 #[test]
1416 fn should_remove_replication_target_only_matches_replication_target_arns() {
1417 let target_arns = HashSet::from(["arn:rustfs:replication:us-east-1:removed:bucket".to_string()]);
1418
1419 assert!(should_remove_replication_target(
1420 "arn:rustfs:replication:us-east-1:removed:bucket",
1421 true,
1422 &target_arns
1423 ));
1424 assert!(!should_remove_replication_target(
1425 "arn:rustfs:replication:us-east-1:kept:bucket",
1426 true,
1427 &target_arns
1428 ));
1429 assert!(!should_remove_replication_target(
1430 "arn:rustfs:replication:us-east-1:removed:bucket",
1431 false,
1432 &target_arns
1433 ));
1434 }
1435
1436 fn delete_marker_rule(id: &str, arn: &str, prefix: &str, priority: i32, delete_marker_enabled: bool) -> ReplicationRule {
1437 let status = if delete_marker_enabled {
1438 DeleteMarkerReplicationStatus::from_static(DeleteMarkerReplicationStatus::ENABLED)
1439 } else {
1440 DeleteMarkerReplicationStatus::from_static(DeleteMarkerReplicationStatus::DISABLED)
1441 };
1442 ReplicationRule {
1443 delete_marker_replication: Some(DeleteMarkerReplication { status: Some(status) }),
1444 delete_replication: None,
1445 destination: Destination {
1446 bucket: arn.to_string(),
1447 ..Default::default()
1448 },
1449 existing_object_replication: Some(ExistingObjectReplication {
1450 status: ExistingObjectReplicationStatus::from_static(ExistingObjectReplicationStatus::ENABLED),
1451 }),
1452 filter: None,
1453 id: Some(id.to_string()),
1454 prefix: Some(prefix.to_string()),
1455 priority: Some(priority),
1456 source_selection_criteria: None,
1457 status: ReplicationRuleStatus::from_static(ReplicationRuleStatus::ENABLED),
1458 }
1459 }
1460
1461 #[test]
1466 fn replicate_delete_marker_follows_highest_priority_rule() {
1467 let arn = "arn:rustfs:replication:us-east-1:target:bucket";
1468 let config = ReplicationConfiguration {
1469 role: String::new(),
1470 rules: vec![
1471 delete_marker_rule("low-priority-enabled", arn, "logs/", 1, true),
1472 delete_marker_rule("high-priority-disabled", arn, "logs/2026/", 5, false),
1473 ],
1474 };
1475
1476 let opts = ObjectOpts {
1477 name: "logs/2026/app.log".to_string(),
1478 op_type: ReplicationType::Delete,
1479 delete_marker: true,
1480 version_id: None,
1481 ..Default::default()
1482 };
1483
1484 assert!(
1485 !config.replicate(&opts),
1486 "highest-priority rule disables delete-marker replication, so the delete marker must not replicate"
1487 );
1488 }
1489
1490 #[test]
1491 fn role_delete_decision_follows_highest_priority_rule() {
1492 let role = "arn:rustfs:replication:us-east-1:role-target:bucket";
1493 let destination = "arn:rustfs:replication:us-east-1:target:bucket";
1494 let config = ReplicationConfiguration {
1495 role: role.to_string(),
1496 rules: vec![
1497 delete_marker_rule("low-priority-enabled", destination, "logs/", 1, true),
1498 delete_marker_rule("high-priority-disabled", destination, "logs/2026/", 5, false),
1499 ],
1500 };
1501 let opts = ObjectOpts {
1502 name: "logs/2026/app.log".to_string(),
1503 op_type: ReplicationType::Delete,
1504 delete_marker: true,
1505 ..Default::default()
1506 };
1507
1508 assert_eq!(
1509 config.filter_target_replication_decisions(&opts),
1510 vec![(role.to_string(), false)],
1511 "the role target must use the highest-priority matching rule"
1512 );
1513 }
1514
1515 #[test]
1516 fn version_purge_uses_delete_replication_for_object_and_marker_versions() {
1517 let arn = "arn:rustfs:replication:us-east-1:target:bucket";
1518 let mut rule = delete_marker_rule("delete-switches", arn, "", 1, true);
1519 rule.delete_replication = Some(DeleteReplication {
1520 status: DeleteReplicationStatus::from_static(DeleteReplicationStatus::DISABLED),
1521 });
1522 let mut config = ReplicationConfiguration {
1523 role: String::new(),
1524 rules: vec![rule],
1525 };
1526
1527 for version_id in [Some(Uuid::new_v4()), Some(Uuid::nil())] {
1528 for delete_marker in [false, true] {
1529 assert!(!config.replicate(&ObjectOpts {
1530 name: "object".to_string(),
1531 op_type: ReplicationType::Delete,
1532 version_id,
1533 delete_marker,
1534 ..Default::default()
1535 }));
1536 }
1537 }
1538
1539 let stored_marker = ObjectOpts {
1540 name: "object".to_string(),
1541 op_type: ReplicationType::Delete,
1542 delete_marker: true,
1543 ..Default::default()
1544 };
1545 assert!(config.replicate(&stored_marker), "stored markers must use DeleteMarkerReplication");
1546 assert_eq!(config.filter_target_replication_decisions(&stored_marker), vec![(arn.to_string(), true)]);
1547
1548 config.rules[0].delete_replication = Some(DeleteReplication {
1549 status: DeleteReplicationStatus::from_static(DeleteReplicationStatus::ENABLED),
1550 });
1551 assert!(config.replicate(&ObjectOpts {
1552 name: "object".to_string(),
1553 op_type: ReplicationType::Delete,
1554 version_id: Some(Uuid::nil()),
1555 delete_marker: true,
1556 ..Default::default()
1557 }));
1558 assert!(config.replicate(&stored_marker));
1559 }
1560
1561 #[test]
1562 fn unsupported_replication_fields_are_reported_before_persistence() {
1563 let arn = "arn:rustfs:replication:us-east-1:target:bucket";
1564 let mut config = ReplicationConfiguration {
1565 role: String::new(),
1566 rules: vec![replication_rule("unsupported", arn)],
1567 };
1568
1569 config.rules[0].source_selection_criteria = Some(SourceSelectionCriteria {
1570 replica_modifications: None,
1571 sse_kms_encrypted_objects: Some(SseKmsEncryptedObjects {
1572 status: SseKmsEncryptedObjectsStatus::from_static(SseKmsEncryptedObjectsStatus::ENABLED),
1573 }),
1574 });
1575 assert_eq!(
1576 unsupported_replication_config_field(&config),
1577 Some("SourceSelectionCriteria.SseKmsEncryptedObjects")
1578 );
1579
1580 config.rules[0].source_selection_criteria = None;
1581 config.rules[0].destination.encryption_configuration = Some(EncryptionConfiguration {
1582 replica_kms_key_id: Some("arn:aws:kms:us-east-1:123456789012:key/opaque-key-id".to_string()),
1583 });
1584 assert_eq!(unsupported_replication_config_field(&config), Some("Destination.EncryptionConfiguration"));
1585
1586 config.rules[0].destination.encryption_configuration = None;
1587 config.rules[0].destination.access_control_translation = Some(s3s::dto::AccessControlTranslation {
1588 owner: s3s::dto::OwnerOverride::from_static(s3s::dto::OwnerOverride::DESTINATION),
1589 });
1590 assert_eq!(
1591 unsupported_replication_config_field(&config),
1592 Some("Destination.AccessControlTranslation")
1593 );
1594
1595 config.rules[0].destination.access_control_translation = None;
1596 config.rules[0].destination.account = Some("123456789012".to_string());
1597 assert_eq!(unsupported_replication_config_field(&config), Some("Destination.Account"));
1598
1599 config.rules[0].destination.account = None;
1600 config.rules[0].destination.metrics = Some(Metrics {
1601 event_threshold: None,
1602 status: MetricsStatus::from_static(MetricsStatus::ENABLED),
1603 });
1604 assert_eq!(unsupported_replication_config_field(&config), Some("Destination.Metrics"));
1605
1606 config.rules[0].destination.metrics = None;
1607 config.rules[0].destination.replication_time = Some(ReplicationTime {
1608 status: ReplicationTimeStatus::from_static(ReplicationTimeStatus::ENABLED),
1609 time: ReplicationTimeValue { minutes: Some(15) },
1610 });
1611 assert_eq!(unsupported_replication_config_field(&config), Some("Destination.ReplicationTime"));
1612
1613 config.rules[0].destination.replication_time = None;
1614 config.rules[0].destination.storage_class =
1615 Some(s3s::dto::StorageClass::from_static(s3s::dto::StorageClass::STANDARD_IA));
1616 assert_eq!(unsupported_replication_config_field(&config), Some("Destination.StorageClass"));
1617
1618 config.rules[0].destination.storage_class = Some(StorageClass::from("standard".to_string()));
1621 assert_eq!(unsupported_replication_config_field(&config), Some("Destination.StorageClass"));
1622
1623 config.rules[0].destination.storage_class = Some(StorageClass::from_static(StorageClass::STANDARD));
1627 assert_eq!(unsupported_replication_config_field(&config), None);
1628 }
1629
1630 #[test]
1631 fn explicit_standard_storage_class_is_accepted_from_wire_xml() {
1632 let xml = br#"
1635 <ReplicationConfiguration>
1636 <Role></Role>
1637 <Rule>
1638 <ID>console-rule</ID>
1639 <Status>Enabled</Status>
1640 <Priority>1</Priority>
1641 <DeleteMarkerReplication><Status>Enabled</Status></DeleteMarkerReplication>
1642 <Destination>
1643 <Bucket>arn:aws:s3:::destination</Bucket>
1644 <StorageClass>STANDARD</StorageClass>
1645 </Destination>
1646 </Rule>
1647 </ReplicationConfiguration>
1648 "#;
1649 let mut deserializer = Deserializer::new(xml);
1650 let config = <ReplicationConfiguration as s3s::xml::Deserialize>::deserialize(&mut deserializer)
1651 .expect("console-shaped config should parse");
1652 deserializer
1653 .expect_eof()
1654 .expect("console-shaped config should consume the whole body");
1655
1656 assert_eq!(config.rules[0].destination.storage_class.as_ref().map(|c| c.as_str()), Some("STANDARD"));
1657 assert_eq!(unsupported_replication_config_field(&config), None);
1658 }
1659
1660 #[test]
1661 fn historical_destination_fields_survive_the_s3_xml_round_trip() {
1662 let xml = br#"
1663 <ReplicationConfiguration>
1664 <Role></Role>
1665 <Rule>
1666 <ID>historical</ID>
1667 <Status>Enabled</Status>
1668 <Destination>
1669 <Bucket>arn:aws:s3:::destination</Bucket>
1670 <Account>123456789012</Account>
1671 <AccessControlTranslation><Owner>Destination</Owner></AccessControlTranslation>
1672 <StorageClass>STANDARD_IA</StorageClass>
1673 </Destination>
1674 </Rule>
1675 </ReplicationConfiguration>
1676 "#;
1677 let mut deserializer = Deserializer::new(xml);
1678 let config = <ReplicationConfiguration as s3s::xml::Deserialize>::deserialize(&mut deserializer)
1679 .expect("historical config should parse");
1680 deserializer
1681 .expect_eof()
1682 .expect("historical config should consume the whole body");
1683
1684 let mut encoded = Vec::new();
1685 <ReplicationConfiguration as s3s::xml::Serialize>::serialize(&config, &mut Serializer::new(&mut encoded))
1686 .expect("historical config should serialize");
1687 let encoded = String::from_utf8(encoded).expect("serialized XML should be UTF-8");
1688 for field in [
1689 "<Account>123456789012</Account>",
1690 "<Owner>Destination</Owner>",
1691 "<StorageClass>STANDARD_IA</StorageClass>",
1692 ] {
1693 assert!(encoded.contains(field), "historical field {field} was lost: {encoded}");
1694 }
1695 }
1696
1697 #[test]
1698 fn s3_xml_parser_discards_unknown_replication_elements_before_validation() {
1699 let xml = br#"
1700 <ReplicationConfiguration>
1701 <Role></Role>
1702 <FutureTopLevel>future</FutureTopLevel>
1703 <Rule>
1704 <ID>unknown</ID>
1705 <Status>Enabled</Status>
1706 <Destination>
1707 <Bucket>arn:aws:s3:::destination</Bucket>
1708 </Destination>
1709 </Rule>
1710 </ReplicationConfiguration>
1711 "#;
1712 let mut deserializer = Deserializer::new(xml);
1713 let config = <ReplicationConfiguration as s3s::xml::Deserialize>::deserialize(&mut deserializer)
1714 .expect("s3s should accept unknown elements");
1715 deserializer.expect_eof().expect("unknown elements should still be consumed");
1716
1717 assert!(config.rules[0].destination.encryption_configuration.is_none());
1718 assert_eq!(unsupported_replication_config_field(&config), None);
1719 }
1720
1721 #[test]
1722 fn capability_fields_match_validator_rejections() {
1723 let rejected_fields = [
1724 "SourceSelectionCriteria.SseKmsEncryptedObjects",
1725 "Destination.EncryptionConfiguration",
1726 "Destination.Metrics",
1727 "Destination.ReplicationTime",
1728 ];
1729
1730 for field in rejected_fields {
1731 assert!(
1732 REPLICATION_READ_ONLY_HISTORICAL_FIELDS.contains(&field),
1733 "rejected field {field} must be advertised as readable historical data"
1734 );
1735 assert!(
1736 !REPLICATION_WRITABLE_FIELDS.contains(&field),
1737 "rejected field {field} must not be advertised as writable"
1738 );
1739 }
1740
1741 for field in REPLICATION_WRITABLE_FIELDS {
1742 assert!(
1743 !REPLICATION_READ_ONLY_HISTORICAL_FIELDS.contains(field),
1744 "field {field} cannot be both writable and historical-only"
1745 );
1746 }
1747 }
1748
1749 #[test]
1750 fn replication_writable_fields_bind_to_typed_dto_fields() {
1751 let mut rule = replication_rule("id-marker", "arn:bucket-marker");
1752 rule.priority = Some(37);
1753 rule.filter = Some(s3s::dto::ReplicationRuleFilter {
1754 prefix: Some("prefix-marker/".to_string()),
1755 tag: Some(s3s::dto::Tag {
1756 key: Some("tag-key-marker".to_string()),
1757 value: Some("tag-value-marker".to_string()),
1758 }),
1759 and: Some(s3s::dto::ReplicationRuleAndOperator {
1760 prefix: Some("and-prefix-marker/".to_string()),
1761 tags: Some(vec![s3s::dto::Tag {
1762 key: Some("and-tag-key-marker".to_string()),
1763 value: Some("and-tag-value-marker".to_string()),
1764 }]),
1765 }),
1766 ..Default::default()
1767 });
1768 rule.delete_marker_replication = Some(DeleteMarkerReplication {
1769 status: Some(DeleteMarkerReplicationStatus::from_static(DeleteMarkerReplicationStatus::ENABLED)),
1770 });
1771 rule.delete_replication = Some(DeleteReplication {
1772 status: DeleteReplicationStatus::from_static(DeleteReplicationStatus::ENABLED),
1773 });
1774 rule.source_selection_criteria = Some(SourceSelectionCriteria {
1775 replica_modifications: Some(ReplicaModifications {
1776 status: ReplicaModificationsStatus::from_static(ReplicaModificationsStatus::ENABLED),
1777 }),
1778 sse_kms_encrypted_objects: None,
1779 });
1780 let config = ReplicationConfiguration {
1781 role: "role-marker".to_string(),
1782 rules: vec![rule],
1783 };
1784
1785 let rule = config.rules.first().expect("fixture should contain one rule");
1786 let filter = rule.filter.as_ref().expect("fixture should contain a rule filter");
1787 let field_hits = [
1788 ("Role", config.role == "role-marker"),
1789 ("Rule.ID", rule.id.as_deref() == Some("id-marker")),
1790 ("Rule.Status", rule.status.as_str() == ReplicationRuleStatus::ENABLED),
1791 ("Rule.Priority", rule.priority == Some(37)),
1792 ("Rule.Filter.Prefix", filter.prefix.as_deref() == Some("prefix-marker/")),
1793 (
1794 "Rule.Filter.Tag",
1795 filter.tag.as_ref().and_then(|tag| tag.key.as_deref()) == Some("tag-key-marker"),
1796 ),
1797 (
1798 "Rule.Filter.And",
1799 filter.and.as_ref().and_then(|and| and.prefix.as_deref()) == Some("and-prefix-marker/"),
1800 ),
1801 ("Rule.Destination.Bucket", rule.destination.bucket == "arn:bucket-marker"),
1802 (
1803 "Rule.ExistingObjectReplication.Status",
1804 rule.existing_object_replication
1805 .as_ref()
1806 .is_some_and(|existing| existing.status.as_str() == ExistingObjectReplicationStatus::ENABLED),
1807 ),
1808 (
1809 "Rule.DeleteMarkerReplication.Status",
1810 rule.delete_marker_replication
1811 .as_ref()
1812 .and_then(|delete_marker| delete_marker.status.as_ref())
1813 .is_some_and(|status| status.as_str() == DeleteMarkerReplicationStatus::ENABLED),
1814 ),
1815 (
1816 "Rule.DeleteReplication.Status",
1817 rule.delete_replication
1818 .as_ref()
1819 .is_some_and(|delete| delete.status.as_str() == DeleteReplicationStatus::ENABLED),
1820 ),
1821 (
1822 "Rule.SourceSelectionCriteria.ReplicaModifications.Status",
1823 rule.source_selection_criteria
1824 .as_ref()
1825 .and_then(|criteria| criteria.replica_modifications.as_ref())
1826 .is_some_and(|modifications| modifications.status.as_str() == ReplicaModificationsStatus::ENABLED),
1827 ),
1828 ];
1829 let bound_paths = field_hits.iter().map(|(path, _)| *path).collect::<Vec<_>>();
1830 assert_eq!(bound_paths, REPLICATION_WRITABLE_FIELDS);
1831
1832 for (path, hit) in field_hits {
1833 assert!(hit, "typed field probe did not reach {path}");
1834 }
1835 }
1836
1837 #[test]
1838 fn invalid_replication_status_fields_are_reported_before_persistence() {
1839 let arn = "arn:rustfs:replication:us-east-1:target:bucket";
1840 let mut config = ReplicationConfiguration {
1841 role: String::new(),
1842 rules: vec![replication_rule("invalid-status", arn)],
1843 };
1844
1845 config.rules[0].status = ReplicationRuleStatus::from_static("Invalid");
1846 assert_eq!(invalid_replication_config_status_field(&config), Some("Rule.Status"));
1847
1848 config.rules[0] = replication_rule("invalid-status", arn);
1849 config.rules[0].existing_object_replication = Some(ExistingObjectReplication {
1850 status: ExistingObjectReplicationStatus::from_static("Invalid"),
1851 });
1852 assert_eq!(
1853 invalid_replication_config_status_field(&config),
1854 Some("Rule.ExistingObjectReplication.Status")
1855 );
1856
1857 config.rules[0] = replication_rule("invalid-status", arn);
1858 config.rules[0].delete_replication = Some(DeleteReplication {
1859 status: DeleteReplicationStatus::from_static("Invalid"),
1860 });
1861 assert_eq!(invalid_replication_config_status_field(&config), Some("Rule.DeleteReplication.Status"));
1862
1863 config.rules[0] = replication_rule("invalid-status", arn);
1864 config.rules[0].delete_marker_replication = Some(DeleteMarkerReplication {
1865 status: Some(DeleteMarkerReplicationStatus::from_static("Invalid")),
1866 });
1867 assert_eq!(
1868 invalid_replication_config_status_field(&config),
1869 Some("Rule.DeleteMarkerReplication.Status")
1870 );
1871
1872 config.rules[0] = replication_rule("invalid-status", arn);
1873 config.rules[0].source_selection_criteria = Some(SourceSelectionCriteria {
1874 replica_modifications: Some(ReplicaModifications {
1875 status: ReplicaModificationsStatus::from_static("Invalid"),
1876 }),
1877 sse_kms_encrypted_objects: None,
1878 });
1879 assert_eq!(
1880 invalid_replication_config_status_field(&config),
1881 Some("Rule.SourceSelectionCriteria.ReplicaModifications.Status")
1882 );
1883 }
1884
1885 #[test]
1886 fn target_decisions_choose_highest_priority_rule_per_destination() {
1887 let target_a = "arn:rustfs:replication:us-east-1:target:a";
1888 let target_b = "arn:rustfs:replication:us-east-1:target:b";
1889 let mut a_low = delete_marker_rule("a-low", target_a, "logs/", 1, true);
1890 let b = delete_marker_rule("b", target_b, "logs/", 2, true);
1891 let a_high = delete_marker_rule("a-high", target_a, "logs/2026/", 5, false);
1892 a_low.delete_replication = Some(DeleteReplication {
1893 status: DeleteReplicationStatus::from_static(DeleteReplicationStatus::ENABLED),
1894 });
1895 let config = ReplicationConfiguration {
1896 role: String::new(),
1897 rules: vec![a_low, b, a_high],
1898 };
1899
1900 let decisions = config.filter_target_replication_decisions(&ObjectOpts {
1901 name: "logs/2026/app.log".to_string(),
1902 op_type: ReplicationType::Delete,
1903 delete_marker: true,
1904 ..Default::default()
1905 });
1906
1907 assert_eq!(decisions, vec![(target_a.to_string(), false), (target_b.to_string(), true)]);
1908 }
1909
1910 #[test]
1916 fn actionable_rule_sort_is_a_total_order_across_destinations() {
1917 let targets = ["arn:target:a", "arn:target:b", "arn:target:c"];
1918 let mut seed: u64 = 0x2367;
1919 for _ in 0..200 {
1920 let rule_count = 21 + (seed % 200) as usize;
1921 let rules = (0..rule_count)
1922 .map(|index| {
1923 seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
1924 let target = targets[(seed >> 33) as usize % targets.len()];
1925 delete_marker_rule(&format!("r{index}"), target, "", index as i32, true)
1926 })
1927 .collect();
1928 let config = ReplicationConfiguration {
1929 role: String::new(),
1930 rules,
1931 };
1932 let ordered = config.filter_actionable_rules(&ObjectOpts {
1933 name: "logs/app.log".to_string(),
1934 op_type: ReplicationType::Object,
1935 ..Default::default()
1936 });
1937 assert_eq!(ordered.len(), rule_count);
1938 assert!(
1939 ordered.windows(2).all(|pair| pair[0].priority >= pair[1].priority),
1940 "actionable rules must be ordered by descending priority"
1941 );
1942 }
1943 }
1944
1945 #[test]
1948 fn top_level_rule_prefix_scopes_matching_without_a_filter() {
1949 let arn = "arn:target:a";
1950 let config = ReplicationConfiguration {
1951 role: String::new(),
1952 rules: vec![delete_marker_rule("v1-prefix", arn, "logs/", 1, true)],
1953 };
1954 assert_eq!(config.rules[0].prefix(), "logs/");
1955
1956 let matching = config.filter_actionable_rules(&ObjectOpts {
1957 name: "logs/app.log".to_string(),
1958 op_type: ReplicationType::Object,
1959 ..Default::default()
1960 });
1961 assert_eq!(matching.len(), 1);
1962
1963 let outside = config.filter_actionable_rules(&ObjectOpts {
1964 name: "data/app.log".to_string(),
1965 op_type: ReplicationType::Object,
1966 ..Default::default()
1967 });
1968 assert!(outside.is_empty(), "an object outside the V1 prefix must not match: {outside:?}");
1969 assert!(
1970 config
1971 .filter_target_arns(&ObjectOpts {
1972 name: "data/app.log".to_string(),
1973 op_type: ReplicationType::Object,
1974 ..Default::default()
1975 })
1976 .is_empty()
1977 );
1978
1979 let mut filtered = delete_marker_rule("filtered", arn, "logs/", 1, true);
1981 filtered.filter = Some(s3s::dto::ReplicationRuleFilter {
1982 prefix: Some("photos/".to_string()),
1983 ..Default::default()
1984 });
1985 assert_eq!(filtered.prefix(), "photos/");
1986 }
1987
1988 #[test]
1989 fn force_delete_targets_use_overlapping_rules_and_highest_priority_switch() {
1990 let target_a = "arn:target:a";
1991 let target_b = "arn:target:b";
1992 let mut a_parent = delete_marker_rule("a-parent", target_a, "logs/", 1, true);
1993 a_parent.delete_replication = Some(DeleteReplication {
1994 status: DeleteReplicationStatus::from_static(DeleteReplicationStatus::ENABLED),
1995 });
1996 let a_child_disabled = delete_marker_rule("a-child", target_a, "logs/2026/", 5, false);
1997 let b_child = delete_marker_rule("b-child", target_b, "logs/2026/", 2, true);
1998 let config = ReplicationConfiguration {
1999 role: String::new(),
2000 rules: vec![a_parent, a_child_disabled, b_child],
2001 };
2002
2003 assert_eq!(
2004 config.filter_force_delete_target_arns("logs/2026/app.log"),
2005 vec![target_b.to_string()],
2006 "the child rule must win for target A while the overlapping child target B remains eligible"
2007 );
2008 }
2009
2010 #[test]
2011 fn site_replication_rule_deployment_id_requires_id_and_arn_agreement() {
2012 let reconciler_rule = replication_rule("site-repl-peer-dep", "arn:rustfs:replication::peer-dep:bucket");
2013 assert_eq!(site_replication_rule_deployment_id(&reconciler_rule), Some("peer-dep"));
2014
2015 let operator_named_rule = replication_rule("site-repl-user", "arn:minio:replication:us-east-1:2f1c-remote:bucket");
2018 assert_eq!(site_replication_rule_deployment_id(&operator_named_rule), None);
2019
2020 let foreign_arn = replication_rule("site-repl-peer-dep", "arn:rustfs:replication::other-dep:bucket");
2021 assert_eq!(site_replication_rule_deployment_id(&foreign_arn), None);
2022
2023 let empty_id = replication_rule("site-repl-", "arn:rustfs:replication::peer-dep:bucket");
2024 assert_eq!(site_replication_rule_deployment_id(&empty_id), None);
2025
2026 let peers = HashSet::from(["peer-dep".to_string()]);
2027 assert!(is_reconciler_owned_site_replication_rule(&reconciler_rule, &peers));
2028 assert!(!is_reconciler_owned_site_replication_rule(&reconciler_rule, &HashSet::new()));
2029 let removed_peer = replication_rule("site-repl-gone-dep", "arn:rustfs:replication::gone-dep:bucket");
2030 assert!(!is_reconciler_owned_site_replication_rule(&removed_peer, &peers));
2031 }
2032
2033 #[test]
2037 fn merge_keeps_operator_priorities_and_replication_decision() {
2038 let user_arn = "arn:minio:replication:us-east-1:2f1c-remote:bucket";
2039 let peer_arn = "arn:rustfs:replication::peer-dep:bucket";
2040 let incoming = ReplicationConfiguration {
2041 role: String::new(),
2042 rules: vec![
2043 delete_marker_rule("dm-enabled", user_arn, "logs/", 5, true),
2044 delete_marker_rule("dm-disabled", user_arn, "logs/2026/", 1, false),
2045 ],
2046 };
2047 let mut site_rule = delete_marker_rule("site-repl-peer-dep", peer_arn, "", 7, true);
2048 site_rule.prefix = None;
2049 let local = structure_config(vec![site_rule]);
2050 let opts = ObjectOpts {
2051 name: "logs/2026/app.log".to_string(),
2052 op_type: ReplicationType::Delete,
2053 delete_marker: true,
2054 version_id: None,
2055 ..Default::default()
2056 };
2057 let submitted: Vec<_> = incoming.filter_target_replication_decisions(&opts);
2058
2059 let peers = HashSet::from(["peer-dep".to_string()]);
2060 let merged =
2061 merge_user_replication_config(Some(incoming.clone()), Some(local.clone()), &peers, OperatorRuleContract::Derived)
2062 .expect("rules");
2063
2064 let priorities: Vec<_> = merged
2065 .rules
2066 .iter()
2067 .map(|rule| (rule.id.as_deref().unwrap(), rule.priority))
2068 .collect();
2069 assert_eq!(
2070 priorities,
2071 vec![
2072 ("dm-enabled", Some(5)),
2073 ("dm-disabled", Some(1)),
2074 ("site-repl-peer-dep", Some(2))
2075 ],
2076 "operator priorities are kept verbatim; the site rule takes the lowest free slot"
2077 );
2078 assert!(validate_replication_config_structure(&merged).is_ok());
2079 let mut decisions = merged.filter_target_replication_decisions(&opts);
2080 decisions.retain(|(arn, _)| arn == user_arn);
2081 assert_eq!(decisions, submitted, "the merged config must replicate exactly as the operator submitted");
2082 assert_eq!(decisions, vec![(user_arn.to_string(), true)]);
2083
2084 let merged =
2086 merge_incoming_replication_config(Some(incoming), Some(local), &peers, OperatorRuleContract::Derived).expect("rules");
2087 let priorities: Vec<_> = merged.rules.iter().map(|rule| rule.priority).collect();
2088 assert_eq!(priorities, vec![Some(5), Some(1), Some(2)]);
2089 }
2090
2091 #[test]
2092 fn site_rule_priorities_skip_every_operator_priority() {
2093 let mut rules = vec![
2094 delete_marker_rule("a", "arn:a", "", 2, true),
2095 delete_marker_rule("site-repl-x", "arn:rustfs:replication::x:b", "", 9, true),
2096 delete_marker_rule("b", "arn:a", "", 1, true),
2097 delete_marker_rule("site-repl-y", "arn:rustfs:replication::y:b", "", 9, true),
2098 delete_marker_rule("c", "arn:a", "", 4, true),
2099 ];
2100 assign_site_replication_rule_priorities(&mut rules, is_site_replication_rule);
2101 let priorities: Vec<_> = rules.iter().map(|rule| rule.priority).collect();
2102 assert_eq!(priorities, vec![Some(2), Some(3), Some(1), Some(5), Some(4)]);
2103 assert!(validate_replication_config_structure(&structure_config(rules.clone())).is_ok());
2104
2105 let settled = rules.clone();
2108 assign_site_replication_rule_priorities(&mut rules, is_site_replication_rule);
2109 assert_eq!(rules, settled);
2110 }
2111
2112 fn operator_rule_ids(config: &ReplicationConfiguration) -> Vec<(&str, Option<i32>)> {
2113 config
2114 .rules
2115 .iter()
2116 .filter(|rule| site_replication_rule_deployment_id(rule).is_none())
2117 .map(|rule| (rule.id.as_deref().unwrap(), rule.priority))
2118 .collect()
2119 }
2120
2121 #[test]
2125 fn peer_ingestion_keeps_owner_site_repl_user_rule_and_sites_agree() {
2126 let user_arn = "arn:minio:replication:us-east-1:2f1c-remote:bucket";
2127 let put = structure_config(vec![
2128 delete_marker_rule("site-repl-user", user_arn, "logs/", 3, true),
2129 delete_marker_rule("nightly", user_arn, "", 1, true),
2130 ]);
2131 let a_local = structure_config(vec![replication_rule("site-repl-b-dep", "arn:rustfs:replication::b-dep:bucket")]);
2132 let a_peers = HashSet::from(["b-dep".to_string()]);
2133 let a_merged =
2134 merge_user_replication_config(Some(put), Some(a_local), &a_peers, OperatorRuleContract::Derived).expect("rules");
2135 assert_eq!(operator_rule_ids(&a_merged), vec![("site-repl-user", Some(3)), ("nightly", Some(1))]);
2136
2137 let b_local = structure_config(vec![replication_rule("site-repl-a-dep", "arn:rustfs:replication::a-dep:bucket")]);
2139 let b_sites = HashSet::from(["a-dep".to_string(), "b-dep".to_string()]);
2140 let b_merged =
2141 merge_incoming_replication_config(Some(a_merged.clone()), Some(b_local), &b_sites, OperatorRuleContract::Derived)
2142 .expect("rules");
2143
2144 let ids: Vec<_> = b_merged.rules.iter().map(|rule| rule.id.as_deref().unwrap()).collect();
2145 assert_eq!(ids, vec!["site-repl-user", "nightly", "site-repl-a-dep"]);
2146 assert_eq!(
2147 operator_rule_ids(&b_merged),
2148 operator_rule_ids(&a_merged),
2149 "both sites must persist the same operator rules"
2150 );
2151 }
2152
2153 #[test]
2157 fn merge_keeps_operator_role_target_for_target_selection() {
2158 let role = "arn:minio:replication::operator-dep:bucket";
2159 let peers = HashSet::from(["peer-dep".to_string()]);
2160 let incoming = ReplicationConfiguration {
2161 role: role.to_string(),
2162 rules: vec![delete_marker_rule("nightly", role, "", 1, true)],
2163 };
2164 let local = structure_config(vec![replication_rule(
2165 "site-repl-peer-dep",
2166 "arn:rustfs:replication::peer-dep:bucket",
2167 )]);
2168 let opts = ObjectOpts {
2169 name: "logs/app.log".to_string(),
2170 ..Default::default()
2171 };
2172
2173 let merged =
2174 merge_user_replication_config(Some(incoming.clone()), Some(local.clone()), &peers, OperatorRuleContract::Derived)
2175 .expect("rules");
2176 assert_eq!(merged.role, role);
2177 assert_eq!(replication_target_arns(&merged), HashSet::from([role.to_string()]));
2178 assert_eq!(merged.filter_target_arns(&opts), vec![role.to_string()]);
2179
2180 let ingested =
2181 merge_incoming_replication_config(Some(incoming.clone()), Some(local.clone()), &peers, OperatorRuleContract::Derived)
2182 .expect("rules");
2183 assert_eq!(ingested.role, role);
2184 assert_eq!(ingested.filter_target_arns(&opts), vec![role.to_string()]);
2185
2186 let mut derived_role = incoming;
2188 derived_role.role = "arn:rustfs:replication::peer-dep:bucket".to_string();
2189 let merged =
2190 merge_user_replication_config(Some(derived_role), Some(local), &peers, OperatorRuleContract::Derived).expect("rules");
2191 assert!(merged.role.is_empty());
2192 }
2193
2194 #[test]
2200 fn legacy_contract_matches_pre_contract_peers_and_keeps_the_decision() {
2201 let user_arn = "arn:minio:replication:us-east-1:2f1c-remote:bucket";
2202 let put = ReplicationConfiguration {
2203 role: "arn:minio:replication::operator-dep:bucket".to_string(),
2204 rules: vec![
2205 delete_marker_rule("dm-enabled", user_arn, "logs/", 5, true),
2206 delete_marker_rule("dm-disabled", user_arn, "logs/2026/", 1, false),
2207 delete_marker_rule("site-repl-user", user_arn, "tmp/", 2, true),
2208 ],
2209 };
2210 let a_local = structure_config(vec![replication_rule("site-repl-b-dep", "arn:rustfs:replication::b-dep:bucket")]);
2211 let opts = ObjectOpts {
2212 name: "logs/2026/app.log".to_string(),
2213 op_type: ReplicationType::Delete,
2214 delete_marker: true,
2215 ..Default::default()
2216 };
2217 let submitted: Vec<_> = structure_config(put.rules.clone()).filter_target_replication_decisions(&opts);
2220
2221 let a_peers = HashSet::from(["b-dep".to_string()]);
2222 let a_merged =
2223 merge_user_replication_config(Some(put.clone()), Some(a_local.clone()), &a_peers, OperatorRuleContract::Legacy)
2224 .expect("rules");
2225 let layout: Vec<_> = a_merged
2226 .rules
2227 .iter()
2228 .map(|rule| (rule.id.as_deref().unwrap(), rule.priority))
2229 .collect();
2230 assert_eq!(
2231 layout,
2232 vec![
2233 ("dm-disabled", Some(1)),
2234 ("dm-enabled", Some(2)),
2235 ("site-repl-b-dep", Some(3))
2236 ],
2237 "legacy: operator rules in priority order, every rule renumbered 1..n, `site-repl-*` ids dropped"
2238 );
2239 assert!(a_merged.role.is_empty(), "legacy peers drop any site-replication-shaped role");
2240 let mut decisions = a_merged.filter_target_replication_decisions(&opts);
2241 decisions.retain(|(arn, _)| arn == user_arn);
2242 assert_eq!(decisions, submitted, "the renumbering must not flip the winning rule");
2243
2244 let mut pre_contract = a_merged
2246 .rules
2247 .iter()
2248 .filter(|rule| !is_site_replication_rule(rule))
2249 .cloned()
2250 .collect::<Vec<_>>();
2251 pre_contract.push(replication_rule("site-repl-a-dep", "arn:rustfs:replication::a-dep:bucket"));
2252 for (index, rule) in pre_contract.iter_mut().enumerate() {
2253 rule.priority = Some(index as i32 + 1);
2254 }
2255 let b_local = structure_config(vec![replication_rule("site-repl-a-dep", "arn:rustfs:replication::a-dep:bucket")]);
2257 let b_sites = HashSet::from(["a-dep".to_string(), "b-dep".to_string()]);
2258 let b_merged = merge_incoming_replication_config(Some(a_merged), Some(b_local), &b_sites, OperatorRuleContract::Legacy)
2259 .expect("rules");
2260 assert_eq!(b_merged.rules, pre_contract);
2261
2262 let a_merged =
2264 merge_user_replication_config(Some(put), Some(a_local), &a_peers, OperatorRuleContract::Derived).expect("rules");
2265 let layout: Vec<_> = a_merged
2266 .rules
2267 .iter()
2268 .map(|rule| (rule.id.as_deref().unwrap(), rule.priority))
2269 .collect();
2270 assert_eq!(
2271 layout,
2272 vec![
2273 ("dm-enabled", Some(5)),
2274 ("dm-disabled", Some(1)),
2275 ("site-repl-user", Some(2)),
2276 ("site-repl-b-dep", Some(3))
2277 ]
2278 );
2279 assert_eq!(a_merged.role, "arn:minio:replication::operator-dep:bucket");
2280 }
2281}