2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2020-07-21 20:49:56 -04:00
|
|
|
|
|
|
|
package replication
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"io"
|
|
|
|
"sort"
|
2020-08-19 17:25:21 -04:00
|
|
|
"strconv"
|
2020-07-21 20:49:56 -04:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-09-18 16:31:35 -04:00
|
|
|
errReplicationTooManyRules = Errorf("Replication configuration allows a maximum of 1000 rules")
|
|
|
|
errReplicationNoRule = Errorf("Replication configuration should have at least one rule")
|
|
|
|
errReplicationUniquePriority = Errorf("Replication configuration has duplicate priority")
|
|
|
|
errRoleArnMissingLegacy = Errorf("Missing required parameter `Role` in ReplicationConfiguration")
|
|
|
|
errDestinationArnMissing = Errorf("Missing required parameter `Destination` in Replication rule")
|
|
|
|
errInvalidSourceSelectionCriteria = Errorf("Invalid ReplicaModification status")
|
|
|
|
errRoleArnPresentForMultipleTargets = Errorf("`Role` should be empty in ReplicationConfiguration for multiple targets")
|
2020-07-21 20:49:56 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config - replication configuration specified in
|
|
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/dev/replication-add-config.html
|
|
|
|
type Config struct {
|
|
|
|
XMLName xml.Name `xml:"ReplicationConfiguration" json:"-"`
|
|
|
|
Rules []Rule `xml:"Rule" json:"Rules"`
|
2020-08-06 20:10:21 -04:00
|
|
|
// RoleArn is being reused for MinIO replication ARN
|
|
|
|
RoleArn string `xml:"Role" json:"Role"`
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Maximum 2MiB size per replication config.
|
|
|
|
const maxReplicationConfigSize = 2 << 20
|
|
|
|
|
|
|
|
// ParseConfig parses ReplicationConfiguration from xml
|
|
|
|
func ParseConfig(reader io.Reader) (*Config, error) {
|
|
|
|
config := Config{}
|
|
|
|
if err := xml.NewDecoder(io.LimitReader(reader, maxReplicationConfigSize)).Decode(&config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-13 22:20:45 -04:00
|
|
|
// By default, set replica modification to enabled if unset.
|
|
|
|
for i := range config.Rules {
|
|
|
|
if len(config.Rules[i].SourceSelectionCriteria.ReplicaModifications.Status) == 0 {
|
|
|
|
config.Rules[i].SourceSelectionCriteria = SourceSelectionCriteria{
|
|
|
|
ReplicaModifications: ReplicaModifications{
|
|
|
|
Status: Enabled,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-07-14 19:27:09 -04:00
|
|
|
// Default DeleteReplication to disabled if unset.
|
|
|
|
if len(config.Rules[i].DeleteReplication.Status) == 0 {
|
|
|
|
config.Rules[i].DeleteReplication = DeleteReplication{
|
|
|
|
Status: Disabled,
|
|
|
|
}
|
|
|
|
}
|
2021-05-13 22:20:45 -04:00
|
|
|
}
|
2020-07-21 20:49:56 -04:00
|
|
|
return &config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate - validates the replication configuration
|
|
|
|
func (c Config) Validate(bucket string, sameTarget bool) error {
|
|
|
|
// replication config can't have more than 1000 rules
|
|
|
|
if len(c.Rules) > 1000 {
|
|
|
|
return errReplicationTooManyRules
|
|
|
|
}
|
|
|
|
// replication config should have at least one rule
|
|
|
|
if len(c.Rules) == 0 {
|
|
|
|
return errReplicationNoRule
|
|
|
|
}
|
2021-09-18 16:31:35 -04:00
|
|
|
|
2020-07-21 20:49:56 -04:00
|
|
|
// Validate all the rules in the replication config
|
|
|
|
targetMap := make(map[string]struct{})
|
|
|
|
priorityMap := make(map[string]struct{})
|
2021-09-18 16:31:35 -04:00
|
|
|
var legacyArn bool
|
2020-07-21 20:49:56 -04:00
|
|
|
for _, r := range c.Rules {
|
|
|
|
if _, ok := targetMap[r.Destination.Bucket]; !ok {
|
2021-09-18 16:31:35 -04:00
|
|
|
targetMap[r.Destination.Bucket] = struct{}{}
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
|
|
|
if err := r.Validate(bucket, sameTarget); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-19 17:25:21 -04:00
|
|
|
if _, ok := priorityMap[strconv.Itoa(r.Priority)]; ok {
|
2020-07-21 20:49:56 -04:00
|
|
|
return errReplicationUniquePriority
|
|
|
|
}
|
2020-08-19 17:25:21 -04:00
|
|
|
priorityMap[strconv.Itoa(r.Priority)] = struct{}{}
|
2021-09-18 16:31:35 -04:00
|
|
|
|
|
|
|
if r.Destination.LegacyArn() {
|
|
|
|
legacyArn = true
|
|
|
|
}
|
|
|
|
if c.RoleArn == "" && !r.Destination.TargetArn() {
|
|
|
|
return errDestinationArnMissing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// disallow combining old replication configuration which used RoleArn as target ARN with multiple
|
|
|
|
// destination replication
|
|
|
|
if c.RoleArn != "" && len(targetMap) > 1 {
|
|
|
|
return errRoleArnPresentForMultipleTargets
|
|
|
|
}
|
|
|
|
// validate RoleArn if destination used legacy ARN format.
|
|
|
|
if c.RoleArn == "" && legacyArn {
|
|
|
|
return errRoleArnMissingLegacy
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-13 13:28:35 -05:00
|
|
|
// Type - replication type enum
|
|
|
|
type Type int
|
|
|
|
|
|
|
|
// Types of replication
|
|
|
|
const (
|
2021-06-01 22:59:11 -04:00
|
|
|
UnsetReplicationType Type = 0 + iota
|
|
|
|
ObjectReplicationType
|
2021-03-13 13:28:35 -05:00
|
|
|
DeleteReplicationType
|
|
|
|
MetadataReplicationType
|
2021-04-15 19:32:00 -04:00
|
|
|
HealReplicationType
|
2021-06-01 22:59:11 -04:00
|
|
|
ExistingObjectReplicationType
|
2021-09-18 16:31:35 -04:00
|
|
|
ResyncReplicationType
|
2022-05-24 22:40:45 -04:00
|
|
|
AllReplicationType
|
2021-03-13 13:28:35 -05:00
|
|
|
)
|
|
|
|
|
2021-06-01 22:59:11 -04:00
|
|
|
// Valid returns true if replication type is set
|
|
|
|
func (t Type) Valid() bool {
|
|
|
|
return t > 0
|
|
|
|
}
|
|
|
|
|
2022-09-12 15:40:02 -04:00
|
|
|
// IsDataReplication returns true if content being replicated
|
|
|
|
func (t Type) IsDataReplication() bool {
|
|
|
|
switch t {
|
|
|
|
case ObjectReplicationType, HealReplicationType, ExistingObjectReplicationType:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-21 20:49:56 -04:00
|
|
|
// ObjectOpts provides information to deduce whether replication
|
|
|
|
// can be triggered on the resultant object.
|
|
|
|
type ObjectOpts struct {
|
2021-06-01 22:59:11 -04:00
|
|
|
Name string
|
|
|
|
UserTags string
|
|
|
|
VersionID string
|
|
|
|
DeleteMarker bool
|
|
|
|
SSEC bool
|
|
|
|
OpType Type
|
|
|
|
Replica bool
|
|
|
|
ExistingObject bool
|
2021-09-18 16:31:35 -04:00
|
|
|
TargetArn string
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
|
|
|
|
2022-06-06 05:54:39 -04:00
|
|
|
// HasExistingObjectReplication returns true if any of the rule returns 'ExistingObjects' replication.
|
|
|
|
func (c Config) HasExistingObjectReplication(arn string) bool {
|
|
|
|
for _, rule := range c.Rules {
|
|
|
|
if rule.Destination.ARN == arn || c.RoleArn == arn {
|
|
|
|
if rule.ExistingObjectReplication.Status == Enabled {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-21 20:49:56 -04:00
|
|
|
// FilterActionableRules returns the rules actions that need to be executed
|
|
|
|
// after evaluating prefix/tag filtering
|
|
|
|
func (c Config) FilterActionableRules(obj ObjectOpts) []Rule {
|
2022-05-24 22:40:45 -04:00
|
|
|
if obj.Name == "" && !(obj.OpType == ResyncReplicationType || obj.OpType == AllReplicationType) {
|
2020-07-21 20:49:56 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var rules []Rule
|
|
|
|
for _, rule := range c.Rules {
|
|
|
|
if rule.Status == Disabled {
|
|
|
|
continue
|
|
|
|
}
|
2021-09-18 16:31:35 -04:00
|
|
|
|
|
|
|
if obj.TargetArn != "" && rule.Destination.ARN != obj.TargetArn && c.RoleArn != obj.TargetArn {
|
|
|
|
continue
|
|
|
|
}
|
2022-05-24 22:40:45 -04:00
|
|
|
// Ignore other object level and prefix filters for resyncing target/listing bucket targets
|
|
|
|
if obj.OpType == ResyncReplicationType || obj.OpType == AllReplicationType {
|
2021-09-18 16:31:35 -04:00
|
|
|
rules = append(rules, rule)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if obj.ExistingObject && rule.ExistingObjectReplication.Status == Disabled {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-21 20:49:56 -04:00
|
|
|
if !strings.HasPrefix(obj.Name, rule.Prefix()) {
|
|
|
|
continue
|
|
|
|
}
|
2022-09-28 14:25:46 -04:00
|
|
|
if rule.Filter.TestTags(obj.UserTags) {
|
2020-07-21 20:49:56 -04:00
|
|
|
rules = append(rules, rule)
|
|
|
|
}
|
|
|
|
}
|
2021-11-16 12:28:29 -05:00
|
|
|
sort.Slice(rules, func(i, j int) bool {
|
2021-09-18 16:31:35 -04:00
|
|
|
return rules[i].Priority > rules[j].Priority && rules[i].Destination.String() == rules[j].Destination.String()
|
2020-07-21 20:49:56 -04:00
|
|
|
})
|
2021-09-18 16:31:35 -04:00
|
|
|
|
2020-07-21 20:49:56 -04:00
|
|
|
return rules
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDestination returns destination bucket and storage class.
|
|
|
|
func (c Config) GetDestination() Destination {
|
2020-08-05 23:01:20 -04:00
|
|
|
if len(c.Rules) > 0 {
|
|
|
|
return c.Rules[0].Destination
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
|
|
|
return Destination{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replicate returns true if the object should be replicated.
|
|
|
|
func (c Config) Replicate(obj ObjectOpts) bool {
|
2021-03-13 13:28:35 -05:00
|
|
|
if obj.SSEC {
|
|
|
|
return false
|
|
|
|
}
|
2020-07-21 20:49:56 -04:00
|
|
|
for _, rule := range c.FilterActionableRules(obj) {
|
|
|
|
if rule.Status == Disabled {
|
|
|
|
continue
|
|
|
|
}
|
2021-06-01 22:59:11 -04:00
|
|
|
if obj.ExistingObject && rule.ExistingObjectReplication.Status == Disabled {
|
|
|
|
return false
|
|
|
|
}
|
2021-03-13 13:28:35 -05:00
|
|
|
if obj.OpType == DeleteReplicationType {
|
|
|
|
switch {
|
|
|
|
case obj.VersionID != "":
|
2021-09-18 16:31:35 -04:00
|
|
|
// check MinIO extension for versioned deletes
|
2021-03-13 13:28:35 -05:00
|
|
|
return rule.DeleteReplication.Status == Enabled
|
|
|
|
default:
|
|
|
|
return rule.DeleteMarkerReplication.Status == Enabled
|
|
|
|
}
|
2021-05-13 22:20:45 -04:00
|
|
|
} // regular object/metadata replication
|
|
|
|
return rule.MetadataReplicate(obj)
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasActiveRules - returns whether replication policy has active rules
|
|
|
|
// Optionally a prefix can be supplied.
|
|
|
|
// If recursive is specified the function will also return true if any level below the
|
|
|
|
// prefix has active rules. If no prefix is specified recursive is effectively true.
|
|
|
|
func (c Config) HasActiveRules(prefix string, recursive bool) bool {
|
|
|
|
if len(c.Rules) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, rule := range c.Rules {
|
|
|
|
if rule.Status == Disabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(prefix) > 0 && len(rule.Filter.Prefix) > 0 {
|
|
|
|
// incoming prefix must be in rule prefix
|
|
|
|
if !recursive && !strings.HasPrefix(prefix, rule.Filter.Prefix) {
|
|
|
|
continue
|
|
|
|
}
|
2021-03-13 13:28:35 -05:00
|
|
|
// If recursive, we can skip this rule if it doesn't match the tested prefix or level below prefix
|
|
|
|
// does not match
|
|
|
|
if recursive && !strings.HasPrefix(rule.Prefix(), prefix) && !strings.HasPrefix(prefix, rule.Prefix()) {
|
2020-07-21 20:49:56 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2021-09-18 16:31:35 -04:00
|
|
|
|
|
|
|
// FilterTargetArns returns a slice of distinct target arns in the config
|
|
|
|
func (c Config) FilterTargetArns(obj ObjectOpts) []string {
|
|
|
|
var arns []string
|
|
|
|
|
|
|
|
tgtsMap := make(map[string]struct{})
|
|
|
|
rules := c.FilterActionableRules(obj)
|
|
|
|
for _, rule := range rules {
|
|
|
|
if rule.Status == Disabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if c.RoleArn != "" {
|
|
|
|
arns = append(arns, c.RoleArn) // use legacy RoleArn if present
|
|
|
|
return arns
|
|
|
|
}
|
|
|
|
if _, ok := tgtsMap[rule.Destination.ARN]; !ok {
|
|
|
|
tgtsMap[rule.Destination.ARN] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k := range tgtsMap {
|
|
|
|
arns = append(arns, k)
|
|
|
|
}
|
|
|
|
return arns
|
|
|
|
}
|