istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/security/authz/matcher/string.go (about)

     1  // Copyright Istio Authors
     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  
    15  package matcher
    16  
    17  import (
    18  	"strings"
    19  
    20  	matcher "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3"
    21  )
    22  
    23  // StringMatcher creates a string matcher for v.
    24  func StringMatcher(v string) *matcher.StringMatcher {
    25  	return StringMatcherWithPrefix(v, "")
    26  }
    27  
    28  // StringOrMatcher creates an OR string matcher for a list of values.
    29  func StringOrMatcher(vs []string) *matcher.ValueMatcher {
    30  	matchers := []*matcher.ValueMatcher{}
    31  	for _, v := range vs {
    32  		matchers = append(matchers, &matcher.ValueMatcher{
    33  			MatchPattern: &matcher.ValueMatcher_StringMatch{
    34  				StringMatch: StringMatcher(v),
    35  			},
    36  		})
    37  	}
    38  	return OrMatcher(matchers)
    39  }
    40  
    41  // OrMatcher creates an OR matcher for a list of matchers.
    42  func OrMatcher(matchers []*matcher.ValueMatcher) *matcher.ValueMatcher {
    43  	if len(matchers) == 1 {
    44  		return matchers[0]
    45  	}
    46  	return &matcher.ValueMatcher{
    47  		MatchPattern: &matcher.ValueMatcher_OrMatch{
    48  			OrMatch: &matcher.OrMatcher{
    49  				ValueMatchers: matchers,
    50  			},
    51  		},
    52  	}
    53  }
    54  
    55  // StringMatcherRegex creates a regex string matcher for regex.
    56  func StringMatcherRegex(regex string) *matcher.StringMatcher {
    57  	return &matcher.StringMatcher{
    58  		MatchPattern: &matcher.StringMatcher_SafeRegex{
    59  			SafeRegex: &matcher.RegexMatcher{
    60  				Regex: regex,
    61  			},
    62  		},
    63  	}
    64  }
    65  
    66  // StringMatcherPrefix create a string matcher for prefix matching.
    67  func StringMatcherPrefix(prefix string, ignoreCase bool) *matcher.StringMatcher {
    68  	return &matcher.StringMatcher{
    69  		IgnoreCase: ignoreCase,
    70  		MatchPattern: &matcher.StringMatcher_Prefix{
    71  			Prefix: prefix,
    72  		},
    73  	}
    74  }
    75  
    76  // StringMatcherSuffix create a string matcher for suffix matching.
    77  func StringMatcherSuffix(suffix string, ignoreCase bool) *matcher.StringMatcher {
    78  	return &matcher.StringMatcher{
    79  		IgnoreCase: ignoreCase,
    80  		MatchPattern: &matcher.StringMatcher_Suffix{
    81  			Suffix: suffix,
    82  		},
    83  	}
    84  }
    85  
    86  // StringMatcherExact create a string matcher for exact matching.
    87  func StringMatcherExact(exact string, ignoreCase bool) *matcher.StringMatcher {
    88  	return &matcher.StringMatcher{
    89  		IgnoreCase: ignoreCase,
    90  		MatchPattern: &matcher.StringMatcher_Exact{
    91  			Exact: exact,
    92  		},
    93  	}
    94  }
    95  
    96  // StringMatcherWithPrefix creates a string matcher for v with the extra prefix inserted to the
    97  // created string matcher, note the prefix is ignored if v is wildcard ("*").
    98  // The wildcard "*" will be generated as ".+" instead of ".*".
    99  func StringMatcherWithPrefix(v, prefix string) *matcher.StringMatcher {
   100  	switch {
   101  	// Check if v is "*" first to make sure we won't generate an empty prefix/suffix StringMatcher,
   102  	// the Envoy StringMatcher doesn't allow empty prefix/suffix.
   103  	case v == "*":
   104  		return StringMatcherRegex(".+")
   105  	case strings.HasPrefix(v, "*"):
   106  		if prefix == "" {
   107  			return StringMatcherSuffix(strings.TrimPrefix(v, "*"), false)
   108  		}
   109  		return StringMatcherRegex(prefix + ".*" + strings.TrimPrefix(v, "*"))
   110  	case strings.HasSuffix(v, "*"):
   111  		return StringMatcherPrefix(prefix+strings.TrimSuffix(v, "*"), false)
   112  	default:
   113  		return StringMatcherExact(prefix+v, false)
   114  	}
   115  }