forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
externs.go
323 lines (274 loc) · 8.42 KB
/
externs.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright 2017 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package runtime
import (
"errors"
"fmt"
"net"
"net/mail"
"net/url"
"regexp"
"strings"
"time"
"golang.org/x/net/idna"
config "istio.io/api/policy/v1beta1"
"istio.io/istio/mixer/pkg/expr"
"istio.io/istio/mixer/pkg/il/interpreter"
)
// Externs contains the list of standard external functions used during evaluation.
var Externs = map[string]interpreter.Extern{
"ip": interpreter.ExternFromFn("ip", externIP),
"ip_equal": interpreter.ExternFromFn("ip_equal", externIPEqual),
"timestamp": interpreter.ExternFromFn("timestamp", externTimestamp),
"timestamp_equal": interpreter.ExternFromFn("timestamp_equal", externTimestampEqual),
"dnsName": interpreter.ExternFromFn("dnsName", externDNSName),
"dnsName_equal": interpreter.ExternFromFn("dnsName_equal", externDNSNameEqual),
"email": interpreter.ExternFromFn("email", externEmail),
"email_equal": interpreter.ExternFromFn("email_equal", externEmailEqual),
"uri": interpreter.ExternFromFn("uri", externURI),
"uri_equal": interpreter.ExternFromFn("uri_equal", externURIEqual),
"match": interpreter.ExternFromFn("match", externMatch),
"matches": interpreter.ExternFromFn("matches", externMatches),
"startsWith": interpreter.ExternFromFn("startsWith", externStartsWith),
"endsWith": interpreter.ExternFromFn("endsWith", externEndsWith),
"emptyStringMap": interpreter.ExternFromFn("emptyStringMap", externEmptyStringMap),
}
// ExternFunctionMetadata is the type-metadata about externs. It gets used during compilations.
var ExternFunctionMetadata = []expr.FunctionMetadata{
{
Name: "ip",
ReturnType: config.IP_ADDRESS,
ArgumentTypes: []config.ValueType{config.STRING},
},
{
Name: "timestamp",
ReturnType: config.TIMESTAMP,
ArgumentTypes: []config.ValueType{config.STRING},
},
{
Name: "dnsName",
ReturnType: config.DNS_NAME,
ArgumentTypes: []config.ValueType{config.STRING},
},
{
Name: "email",
ReturnType: config.EMAIL_ADDRESS,
ArgumentTypes: []config.ValueType{config.STRING},
},
{
Name: "uri",
ReturnType: config.URI,
ArgumentTypes: []config.ValueType{config.STRING},
},
{
Name: "match",
ReturnType: config.BOOL,
ArgumentTypes: []config.ValueType{config.STRING, config.STRING},
},
{
Name: "matches",
Instance: true,
TargetType: config.STRING,
ReturnType: config.BOOL,
ArgumentTypes: []config.ValueType{config.STRING},
},
{
Name: "startsWith",
Instance: true,
TargetType: config.STRING,
ReturnType: config.BOOL,
ArgumentTypes: []config.ValueType{config.STRING},
},
{
Name: "endsWith",
Instance: true,
TargetType: config.STRING,
ReturnType: config.BOOL,
ArgumentTypes: []config.ValueType{config.STRING},
},
{
Name: "emptyStringMap",
ReturnType: config.STRING_MAP,
ArgumentTypes: []config.ValueType{},
},
}
func externIP(in string) ([]byte, error) {
if ip := net.ParseIP(in); ip != nil {
return []byte(ip), nil
}
return []byte{}, fmt.Errorf("could not convert %s to IP_ADDRESS", in)
}
func externIPEqual(a []byte, b []byte) bool {
// net.IP is an alias for []byte, so these are safe to convert
ip1 := net.IP(a)
ip2 := net.IP(b)
return ip1.Equal(ip2)
}
func externTimestamp(in string) (time.Time, error) {
layout := time.RFC3339
t, err := time.Parse(layout, in)
if err != nil {
return time.Time{}, fmt.Errorf("could not convert '%s' to TIMESTAMP. expected format: '%s'", in, layout)
}
return t, nil
}
func externTimestampEqual(t1 time.Time, t2 time.Time) bool {
return t1.Equal(t2)
}
// This IDNA profile is for performing validations, but does not otherwise modify the string.
var externDNSNameProfile = idna.New(
idna.StrictDomainName(true),
idna.ValidateLabels(true),
idna.VerifyDNSLength(true),
idna.BidiRule())
func externDNSName(in string) (string, error) {
s, err := externDNSNameProfile.ToUnicode(in)
if err != nil {
err = fmt.Errorf("error converting '%s' to dns name: '%v'", in, err)
}
return s, err
}
// This IDNA profile converts the string for lookup, which ends up canonicalizing the dns name, for the most
// part.
var externDNSNameEqualProfile = idna.New(idna.MapForLookup(),
idna.BidiRule())
func externDNSNameEqual(n1 string, n2 string) (bool, error) {
var err error
if n1, err = externDNSNameEqualProfile.ToUnicode(n1); err != nil {
return false, err
}
if n2, err = externDNSNameEqualProfile.ToUnicode(n2); err != nil {
return false, err
}
if n1[len(n1)-1] == '.' && n2[len(n2)-1] != '.' {
n1 = n1[:len(n1)-1]
}
if n2[len(n2)-1] == '.' && n1[len(n1)-1] != '.' {
n2 = n2[:len(n2)-1]
}
return n1 == n2, nil
}
func externEmail(in string) (string, error) {
a, err := mail.ParseAddress(in)
if err != nil {
return "", fmt.Errorf("error converting '%s' to e-mail: '%v'", in, err)
}
if a.Name != "" {
return "", fmt.Errorf("error converting '%s' to e-mail: display names are not allowed", in)
}
// Also check through the dns name logic to ensure that this will not cause any breaks there, when used for
// comparison.
_, domain := getEmailParts(a.Address)
_, err = externDNSName(domain)
if err != nil {
return "", fmt.Errorf("error converting '%s' to e-mail: '%v'", in, err)
}
return in, nil
}
func externEmailEqual(e1 string, e2 string) (bool, error) {
a1, err := mail.ParseAddress(e1)
if err != nil {
return false, err
}
a2, err := mail.ParseAddress(e2)
if err != nil {
return false, err
}
local1, domain1 := getEmailParts(a1.Address)
local2, domain2 := getEmailParts(a2.Address)
domainEq, err := externDNSNameEqual(domain1, domain2)
if err != nil {
return false, fmt.Errorf("error comparing e-mails '%s' and '%s': %v", e1, e2, err)
}
if !domainEq {
return false, nil
}
return local1 == local2, nil
}
func externURI(in string) (string, error) {
if in == "" {
return "", errors.New("error converting string to uri: empty string")
}
if _, err := url.Parse(in); err != nil {
return "", fmt.Errorf("error converting string to uri '%s': '%v'", in, err)
}
return in, nil
}
func externURIEqual(u1 string, u2 string) (bool, error) {
url1, err := url.Parse(u1)
if err != nil {
return false, fmt.Errorf("error converting string to uri '%s': '%v'", u1, err)
}
url2, err := url.Parse(u2)
if err != nil {
return false, fmt.Errorf("error converting string to uri '%s': '%v'", u2, err)
}
// Try to apply as much normalization logic as possible.
scheme1 := strings.ToLower(url1.Scheme)
scheme2 := strings.ToLower(url2.Scheme)
if scheme1 != scheme2 {
return false, nil
}
// normalize schemes
url1.Scheme = scheme1
url2.Scheme = scheme1
if scheme1 == "http" || scheme1 == "https" {
// Special case http(s) URLs
dnsEq, err := externDNSNameEqual(url1.Hostname(), url2.Hostname())
if err != nil {
return false, err
}
if !dnsEq {
return false, nil
}
if url1.Port() != url2.Port() {
return false, nil
}
// normalize host names
url1.Host = url2.Host
}
return url1.String() == url2.String(), nil
}
func getEmailParts(email string) (local string, domain string) {
idx := strings.IndexByte(email, '@')
if idx == -1 {
local = email
domain = ""
return
}
local = email[:idx]
domain = email[idx+1:]
return
}
func externMatch(str string, pattern string) bool {
if strings.HasSuffix(pattern, "*") {
return strings.HasPrefix(str, pattern[:len(pattern)-1])
}
if strings.HasPrefix(pattern, "*") {
return strings.HasSuffix(str, pattern[1:])
}
return str == pattern
}
func externMatches(pattern string, str string) (bool, error) {
return regexp.MatchString(pattern, str)
}
func externStartsWith(str string, prefix string) bool {
return strings.HasPrefix(str, prefix)
}
func externEndsWith(str string, suffix string) bool {
return strings.HasSuffix(str, suffix)
}
func externEmptyStringMap() map[string]string {
return map[string]string{}
}