github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/event/rules.go (about)

     1  // Copyright (c) 2015-2021 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package event
    19  
    20  import (
    21  	"strings"
    22  
    23  	"github.com/minio/pkg/v2/wildcard"
    24  )
    25  
    26  // NewPattern - create new pattern for prefix/suffix.
    27  func NewPattern(prefix, suffix string) (pattern string) {
    28  	if prefix != "" {
    29  		if !strings.HasSuffix(prefix, "*") {
    30  			prefix += "*"
    31  		}
    32  
    33  		pattern = prefix
    34  	}
    35  
    36  	if suffix != "" {
    37  		if !strings.HasPrefix(suffix, "*") {
    38  			suffix = "*" + suffix
    39  		}
    40  
    41  		pattern += suffix
    42  	}
    43  
    44  	pattern = strings.ReplaceAll(pattern, "**", "*")
    45  
    46  	return pattern
    47  }
    48  
    49  // Rules - event rules
    50  type Rules map[string]TargetIDSet
    51  
    52  // Add - adds pattern and target ID.
    53  func (rules Rules) Add(pattern string, targetID TargetID) {
    54  	rules[pattern] = NewTargetIDSet(targetID).Union(rules[pattern])
    55  }
    56  
    57  // MatchSimple - returns true one of the matching object name in rules.
    58  func (rules Rules) MatchSimple(objectName string) bool {
    59  	for pattern := range rules {
    60  		if wildcard.MatchSimple(pattern, objectName) {
    61  			return true
    62  		}
    63  	}
    64  	return false
    65  }
    66  
    67  // Match - returns TargetIDSet matching object name in rules.
    68  func (rules Rules) Match(objectName string) TargetIDSet {
    69  	targetIDs := NewTargetIDSet()
    70  
    71  	for pattern, targetIDSet := range rules {
    72  		if wildcard.MatchSimple(pattern, objectName) {
    73  			targetIDs = targetIDs.Union(targetIDSet)
    74  		}
    75  	}
    76  
    77  	return targetIDs
    78  }
    79  
    80  // Clone - returns copy of this rules.
    81  func (rules Rules) Clone() Rules {
    82  	rulesCopy := make(Rules)
    83  
    84  	for pattern, targetIDSet := range rules {
    85  		rulesCopy[pattern] = targetIDSet.Clone()
    86  	}
    87  
    88  	return rulesCopy
    89  }
    90  
    91  // Union - returns union with given rules as new rules.
    92  func (rules Rules) Union(rules2 Rules) Rules {
    93  	nrules := rules.Clone()
    94  
    95  	for pattern, targetIDSet := range rules2 {
    96  		nrules[pattern] = nrules[pattern].Union(targetIDSet)
    97  	}
    98  
    99  	return nrules
   100  }
   101  
   102  // Difference - returns difference with given rules as new rules.
   103  func (rules Rules) Difference(rules2 Rules) Rules {
   104  	nrules := make(Rules)
   105  
   106  	for pattern, targetIDSet := range rules {
   107  		if nv := targetIDSet.Difference(rules2[pattern]); len(nv) > 0 {
   108  			nrules[pattern] = nv
   109  		}
   110  	}
   111  
   112  	return nrules
   113  }