-
Notifications
You must be signed in to change notification settings - Fork 341
/
spec.go
397 lines (346 loc) · 12 KB
/
spec.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package apiusagemonitoring
import (
"encoding/json"
"regexp"
"sort"
"strings"
"sync"
"time"
"github.com/sirupsen/logrus"
"golang.org/x/time/rate"
"github.com/zalando/skipper/filters"
)
const (
// Deprecated, use filters.ApiUsageMonitoringName instead
Name = filters.ApiUsageMonitoringName
unknownPlaceholder = "{unknown}"
noMatchPlaceholder = "{no-match}"
noTagPlaceholder = "{no-tag}"
)
var (
log = logrus.WithField("filter", filters.ApiUsageMonitoringName)
regCache = sync.Map{}
)
func loadOrCompileRegex(pattern string) (*regexp.Regexp, error) {
var err error
var reg *regexp.Regexp
regI, ok := regCache.Load(pattern)
if !ok {
reg, err = regexp.Compile(pattern)
regCache.Store(pattern, reg)
} else {
reg = regI.(*regexp.Regexp)
}
return reg, err
}
// NewApiUsageMonitoring creates a new instance of the API Monitoring filter
// specification (its factory).
func NewApiUsageMonitoring(
enabled bool,
realmKeys string,
clientKeys string,
realmsTrackingPattern string,
) filters.Spec {
if !enabled {
log.Debugf("filter %q is not enabled. spec returns `noop` filters.", filters.ApiUsageMonitoringName)
return &noopSpec{&noopFilter{}}
}
// parse realm keys comma separated list
var realmKeyList []string
for _, key := range strings.Split(realmKeys, ",") {
strippedKey := strings.TrimSpace(key)
if strippedKey != "" {
realmKeyList = append(realmKeyList, strippedKey)
}
}
// parse client keys comma separated list
var clientKeyList []string
for _, key := range strings.Split(clientKeys, ",") {
strippedKey := strings.TrimSpace(key)
if strippedKey != "" {
clientKeyList = append(clientKeyList, strippedKey)
}
}
realmsTrackingMatcher, err := loadOrCompileRegex(realmsTrackingPattern)
if err != nil {
log.Errorf(
"api-usage-monitoring-realmsTrackingPattern-tracking-pattern (global config) ignored: error compiling regular expression %q: %v",
realmsTrackingPattern, err)
realmsTrackingMatcher = regexp.MustCompile("services")
log.Warn("defaulting to 'services' as api-usage-monitoring-realmsTrackingPattern-tracking-pattern (global config)")
}
// Create the filter Spec
var unknownPathClientTracking *clientTrackingInfo = nil // client metrics feature is disabled
if realmKeys != "" {
unknownPathClientTracking = &clientTrackingInfo{
ClientTrackingMatcher: nil, // do not match anything (track `realm.{unknown}`)
RealmsTrackingMatcher: realmsTrackingMatcher,
}
}
unknownPath := newPathInfo(
unknownPlaceholder,
noTagPlaceholder,
unknownPlaceholder,
noMatchPlaceholder,
unknownPathClientTracking,
)
spec := &apiUsageMonitoringSpec{
pathHandler: defaultPathHandler{},
realmKeys: realmKeyList,
clientKeys: clientKeyList,
unknownPath: unknownPath,
realmsTrackingMatcher: realmsTrackingMatcher,
sometimes: rate.Sometimes{First: 3, Interval: 1 * time.Minute},
}
log.Debugf("created filter spec: %+v", spec)
return spec
}
// apiConfig is the structure used to parse the parameters of the filter.
type apiConfig struct {
ApplicationId string `json:"application_id"`
Tag string `json:"tag"`
ApiId string `json:"api_id"`
PathTemplates []string `json:"path_templates"`
ClientTrackingPattern string `json:"client_tracking_pattern"`
}
type apiUsageMonitoringSpec struct {
pathHandler pathHandler
realmKeys []string
clientKeys []string
realmsTrackingMatcher *regexp.Regexp
unknownPath *pathInfo
sometimes rate.Sometimes
}
func (s *apiUsageMonitoringSpec) errorf(format string, args ...interface{}) {
s.sometimes.Do(func() {
log.Errorf(format, args...)
})
}
func (s *apiUsageMonitoringSpec) warnf(format string, args ...interface{}) {
s.sometimes.Do(func() {
log.Warnf(format, args...)
})
}
func (s *apiUsageMonitoringSpec) infof(format string, args ...interface{}) {
s.sometimes.Do(func() {
log.Infof(format, args...)
})
}
func (s *apiUsageMonitoringSpec) debugf(format string, args ...interface{}) {
s.sometimes.Do(func() {
log.Debugf(format, args...)
})
}
func (s *apiUsageMonitoringSpec) Name() string {
return filters.ApiUsageMonitoringName
}
func (s *apiUsageMonitoringSpec) CreateFilter(args []interface{}) (filter filters.Filter, err error) {
apis := s.parseJsonConfiguration(args)
paths := s.buildPathInfoListFromConfiguration(apis)
if len(paths) == 0 {
s.errorf("no valid configurations, creating noop api usage monitoring filter")
return noopFilter{}, nil
}
filter = &apiUsageMonitoringFilter{
realmKeys: s.realmKeys,
clientKeys: s.clientKeys,
Paths: paths,
UnknownPath: s.buildUnknownPathInfo(paths),
}
return
}
func (s *apiUsageMonitoringSpec) parseJsonConfiguration(args []interface{}) []*apiConfig {
apis := make([]*apiConfig, 0, len(args))
for i, a := range args {
rawJsonConfiguration, ok := a.(string)
if !ok {
s.errorf("args[%d] ignored: expecting a string, was %t", i, a)
continue
}
config := &apiConfig{
ClientTrackingPattern: ".*", // track all clients per default
}
decoder := json.NewDecoder(strings.NewReader(rawJsonConfiguration))
decoder.DisallowUnknownFields()
err := decoder.Decode(config)
if err != nil {
s.errorf("args[%d] ignored: error reading JSON configuration: %s", i, err)
continue
}
apis = append(apis, config)
}
return apis
}
func (s *apiUsageMonitoringSpec) buildUnknownPathInfo(paths []*pathInfo) *pathInfo {
var applicationId *string
for i := range paths {
path := paths[i]
if applicationId != nil && *applicationId != path.ApplicationId {
return s.unknownPath
}
applicationId = &path.ApplicationId
}
if applicationId != nil && *applicationId != "" {
return newPathInfo(
*applicationId,
s.unknownPath.Tag,
s.unknownPath.ApiId,
s.unknownPath.PathTemplate,
s.unknownPath.ClientTracking)
}
return s.unknownPath
}
func (s *apiUsageMonitoringSpec) buildPathInfoListFromConfiguration(apis []*apiConfig) []*pathInfo {
var paths []*pathInfo
existingPathTemplates := make(map[string]*pathInfo)
existingPathPattern := make(map[string]*pathInfo)
for apiIndex, api := range apis {
applicationId := api.ApplicationId
if applicationId == "" {
s.warnf("args[%d] ignored: does not specify an application_id", apiIndex)
continue
}
apiId := api.ApiId
if apiId == "" {
s.warnf("args[%d] ignored: does not specify an api_id", apiIndex)
continue
}
if api.PathTemplates == nil || len(api.PathTemplates) == 0 {
s.warnf("args[%d] ignored: does not specify any path template", apiIndex)
continue
}
clientTrackingInfo := s.buildClientTrackingInfo(apiIndex, api, s.realmsTrackingMatcher)
for templateIndex, template := range api.PathTemplates {
// Path Template validation
if template == "" {
s.warnf(
"args[%d].path_templates[%d] ignored: empty",
apiIndex, templateIndex)
continue
}
// Normalize path template and get regular expression path pattern
pathTemplate := s.pathHandler.normalizePathTemplate(template)
pathPattern := s.pathHandler.createPathPattern(template)
// Create new `pathInfo` with normalized PathTemplate
info := newPathInfo(applicationId, api.Tag, apiId, pathTemplate, clientTrackingInfo)
// Detect path template duplicates
if _, ok := existingPathTemplates[info.PathTemplate]; ok {
s.warnf(
"args[%d].path_templates[%d] ignored: duplicate path template %q",
apiIndex, templateIndex, info.PathTemplate)
continue
}
existingPathTemplates[info.PathTemplate] = info
// Detect regular expression duplicates
if existingMatcher, ok := existingPathPattern[pathPattern]; ok {
s.warnf(
"args[%d].path_templates[%d] ignored: two path templates yielded the same regular expression %q (%q and %q)",
apiIndex, templateIndex, pathPattern, info.PathTemplate, existingMatcher.PathTemplate)
continue
}
existingPathPattern[pathPattern] = info
pathPatternMatcher, err := loadOrCompileRegex(pathPattern)
if err != nil {
s.warnf(
"args[%d].path_templates[%d] ignored: error compiling regular expression %q for path %q: %v",
apiIndex, templateIndex, pathPattern, info.PathTemplate, err)
continue
}
if pathPatternMatcher == nil {
continue
}
info.Matcher = pathPatternMatcher
// Add valid entry to the results
paths = append(paths, info)
}
}
// Sort the paths by their matcher's RegEx
sort.Sort(pathInfoByRegExRev(paths))
return paths
}
func (s *apiUsageMonitoringSpec) buildClientTrackingInfo(apiIndex int, api *apiConfig, realmsTrackingMatcher *regexp.Regexp) *clientTrackingInfo {
if len(s.realmKeys) == 0 {
s.infof(
`args[%d]: skipper wide configuration "api-usage-monitoring-realm-keys" not provided, not tracking client metrics`,
apiIndex)
return nil
}
if len(s.clientKeys) == 0 {
s.infof(
`args[%d]: skipper wide configuration "api-usage-monitoring-client-keys" not provided, not tracking client metrics`,
apiIndex)
return nil
}
if api.ClientTrackingPattern == "" {
s.debugf(
`args[%d]: empty client_tracking_pattern disables the client metrics for its endpoints`,
apiIndex)
return nil
}
clientTrackingMatcher, err := loadOrCompileRegex(api.ClientTrackingPattern)
if err != nil {
s.errorf(
"args[%d].client_tracking_pattern ignored (no client tracking): error compiling regular expression %q: %v",
apiIndex, api.ClientTrackingPattern, err)
return nil
}
if clientTrackingMatcher == nil {
return nil
}
return &clientTrackingInfo{
ClientTrackingMatcher: clientTrackingMatcher,
RealmsTrackingMatcher: realmsTrackingMatcher,
}
}
var (
regexpMultipleSlashes = regexp.MustCompile(`/+`)
regexpLeadingSlashes = regexp.MustCompile(`^/*`)
regexpTrailingSlashes = regexp.MustCompile(`/*$`)
regexpMiddleSlashes = regexp.MustCompile(`([^/^])/+([^/*])`)
rexexpSlashColumnVar = regexp.MustCompile(`/:([^:{}/]*)`)
rexexpCurlyBracketVar = regexp.MustCompile(`{([^{}]*?)([?]?)}`)
regexpEscapeBeforeChars = regexp.MustCompile(`([.*+?\\])`)
regexpEscapeAfterChars = regexp.MustCompile(`([{}[\]()|])`)
)
// pathHandler path handler interface.
type pathHandler interface {
normalizePathTemplate(path string) string
createPathPattern(path string) string
}
// defaultPathHandler default path handler implementation.
type defaultPathHandler struct{}
// normalizePathTemplate normalize path template removing the leading and
// trailing slashes, substituting multiple adjacent slashes with a single
// one, and replacing column based variable declarations by curly bracked
// based.
func (ph defaultPathHandler) normalizePathTemplate(path string) string {
path = regexpLeadingSlashes.ReplaceAllString(path, "")
path = regexpTrailingSlashes.ReplaceAllString(path, "")
path = regexpMultipleSlashes.ReplaceAllString(path, "/")
path = rexexpSlashColumnVar.ReplaceAllString(path, "/{$1}")
path = rexexpCurlyBracketVar.ReplaceAllString(path, "{$1}")
return path
}
// createPathPattern create a regular expression path pattern for a path
// template by escaping regular specific characters, add optional matching
// of leading and trailing slashes, accept adjacent slashes as if a single
// slash was given, and allow free matching of content on variable locations.
func (ph defaultPathHandler) createPathPattern(path string) string {
path = regexpEscapeBeforeChars.ReplaceAllString(path, "\\$1")
path = rexexpSlashColumnVar.ReplaceAllString(path, "/.+")
path = rexexpCurlyBracketVar.ReplaceAllStringFunc(path, selectPathVarPattern)
path = regexpLeadingSlashes.ReplaceAllString(path, "^/*")
path = regexpTrailingSlashes.ReplaceAllString(path, "/*$")
path = regexpMiddleSlashes.ReplaceAllString(path, "$1/+$2")
path = regexpEscapeAfterChars.ReplaceAllString(path, "\\$1")
return path
}
// selectPathVarPattern select the correct path variable pattern depending
// on the path variable syntax. A trailing question mark is interpreted as
// a path variable that is allowed to be empty.
func selectPathVarPattern(match string) string {
if strings.HasSuffix(match, "\\?}") {
return ".*"
}
return ".+"
}