1
// Copyright Istio Authors
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
7
// http://www.apache.org/licenses/LICENSE-2.0
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.
20
matcher "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3"
23
// StringMatcher creates a string matcher for v.
24
func StringMatcher(v string) *matcher.StringMatcher {
25
return StringMatcherWithPrefix(v, "")
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),
38
return OrMatcher(matchers)
41
// OrMatcher creates an OR matcher for a list of matchers.
42
func OrMatcher(matchers []*matcher.ValueMatcher) *matcher.ValueMatcher {
43
if len(matchers) == 1 {
46
return &matcher.ValueMatcher{
47
MatchPattern: &matcher.ValueMatcher_OrMatch{
48
OrMatch: &matcher.OrMatcher{
49
ValueMatchers: matchers,
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{
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{
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{
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{
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 {
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.
104
return StringMatcherRegex(".+")
105
case strings.HasPrefix(v, "*"):
107
return StringMatcherSuffix(strings.TrimPrefix(v, "*"), false)
109
return StringMatcherRegex(prefix + ".*" + strings.TrimPrefix(v, "*"))
110
case strings.HasSuffix(v, "*"):
111
return StringMatcherPrefix(prefix+strings.TrimSuffix(v, "*"), false)
113
return StringMatcherExact(prefix+v, false)