istio

Форк
0
60 строк · 2.0 Кб
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 trustdomain
16

17
import "strings"
18

19
// stringMatch checks if a string is in a list, it supports four types of string matches:
20
// 1. Exact match.
21
// 2. Wild character match. "*" matches any string.
22
// 3. Prefix match. For example, "book*" matches "bookstore", "bookshop", etc.
23
// 4. Suffix match. For example, "*/review" matches "/bookstore/review", "/products/review", etc.
24
// This is an extensive version of model.stringMatch(). The pattern can be in the string or the list.
25
func stringMatch(a string, list []string) bool {
26
	for _, s := range list {
27
		if a == s || s == "*" || prefixMatch(a, s) || prefixMatch(s, a) || suffixMatch(a, s) || suffixMatch(s, a) {
28
			return true
29
		}
30
	}
31
	return false
32
}
33

34
// prefixMatch checks if pattern is a prefix match and if string a has the given prefix.
35
func prefixMatch(a string, pattern string) bool {
36
	if !strings.HasSuffix(pattern, "*") {
37
		return false
38
	}
39
	pattern = strings.TrimSuffix(pattern, "*")
40
	return strings.HasPrefix(a, pattern)
41
}
42

43
// suffixMatch checks if pattern is a suffix match and if string a has the given suffix.
44
func suffixMatch(a string, pattern string) bool {
45
	if !strings.HasPrefix(pattern, "*") {
46
		return false
47
	}
48
	pattern = strings.TrimPrefix(pattern, "*")
49
	return strings.HasSuffix(a, pattern)
50
}
51

52
// isKeyInList it's fine to use this naive implementation for searching in a very short list.
53
func isKeyInList(key string, list []string) bool {
54
	for _, l := range list {
55
		if key == l {
56
			return true
57
		}
58
	}
59
	return false
60
}
61

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.