forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex.go
141 lines (128 loc) · 3.31 KB
/
regex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2016 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package topdown
import (
"fmt"
"regexp"
"sync"
"github.com/yashtewari/glob-intersection"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/topdown/builtins"
)
var regexpCacheLock = sync.Mutex{}
var regexpCache map[string]*regexp.Regexp
func builtinRegexMatch(a, b ast.Value) (ast.Value, error) {
s1, err := builtins.StringOperand(a, 1)
if err != nil {
return nil, err
}
s2, err := builtins.StringOperand(b, 2)
if err != nil {
return nil, err
}
re, err := getRegexp(string(s1))
if err != nil {
return nil, err
}
return ast.Boolean(re.Match([]byte(s2))), nil
}
func builtinRegexMatchTemplate(a, b, c, d ast.Value) (ast.Value, error) {
pattern, err := builtins.StringOperand(a, 1)
if err != nil {
return nil, err
}
match, err := builtins.StringOperand(b, 2)
if err != nil {
return nil, err
}
start, err := builtins.StringOperand(c, 3)
if err != nil {
return nil, err
}
end, err := builtins.StringOperand(d, 4)
if err != nil {
return nil, err
}
if len(start) != 1 {
return nil, fmt.Errorf("start delimiter has to be exactly one character long but is %d long", len(start))
}
if len(end) != 1 {
return nil, fmt.Errorf("end delimiter has to be exactly one character long but is %d long", len(start))
}
re, err := getRegexpTemplate(string(pattern), string(start)[0], string(end)[0])
if err != nil {
return nil, err
}
return ast.Boolean(re.MatchString(string(match))), nil
}
func builtinRegexSplit(a, b ast.Value) (ast.Value, error) {
s1, err := builtins.StringOperand(a, 1)
if err != nil {
return nil, err
}
s2, err := builtins.StringOperand(b, 2)
if err != nil {
return nil, err
}
re, err := getRegexp(string(s1))
if err != nil {
return nil, err
}
elems := re.Split(string(s2), -1)
arr := make(ast.Array, len(elems))
for i := range arr {
arr[i] = ast.StringTerm(elems[i])
}
return arr, nil
}
func getRegexp(pat string) (*regexp.Regexp, error) {
regexpCacheLock.Lock()
defer regexpCacheLock.Unlock()
re, ok := regexpCache[pat]
if !ok {
var err error
re, err = regexp.Compile(string(pat))
if err != nil {
return nil, err
}
regexpCache[pat] = re
}
return re, nil
}
func getRegexpTemplate(pat string, delimStart, delimEnd byte) (*regexp.Regexp, error) {
regexpCacheLock.Lock()
defer regexpCacheLock.Unlock()
re, ok := regexpCache[pat]
if !ok {
var err error
re, err = compileRegexTemplate(string(pat), delimStart, delimEnd)
if err != nil {
return nil, err
}
regexpCache[pat] = re
}
return re, nil
}
func builtinGlobsMatch(a, b ast.Value) (ast.Value, error) {
s1, err := builtins.StringOperand(a, 1)
if err != nil {
return nil, err
}
s2, err := builtins.StringOperand(b, 2)
if err != nil {
return nil, err
}
ne, err := gintersect.NonEmpty(string(s1), string(s2))
if err != nil {
return nil, err
}
return ast.Boolean(ne), nil
}
func init() {
regexpCache = map[string]*regexp.Regexp{}
RegisterFunctionalBuiltin2(ast.RegexMatch.Name, builtinRegexMatch)
RegisterFunctionalBuiltin2(ast.RegexSplit.Name, builtinRegexSplit)
RegisterFunctionalBuiltin2(ast.GlobsMatch.Name, builtinGlobsMatch)
RegisterFunctionalBuiltin4(ast.RegexTemplateMatch.Name, builtinRegexMatchTemplate)
}