-
Notifications
You must be signed in to change notification settings - Fork 343
/
eskip.go
399 lines (326 loc) · 8.99 KB
/
eskip.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
398
399
package eskip
//go:generate goyacc -o parser.go -p eskip parser.y
import (
"errors"
"fmt"
"regexp"
"strings"
"github.com/zalando/skipper/filters/flowid"
)
const duplicateHeaderPredicateErrorFmt = "duplicate header predicate: %s"
var (
invalidPredicateArgError = errors.New("invalid predicate arg")
invalidPredicateArgCountError = errors.New("invalid predicate count arg")
duplicatePathTreePredicateError = errors.New("duplicate path tree predicate")
duplicateMethodPredicateError = errors.New("duplicate method predicate")
errInvalidBackend = errors.New("invalid backend")
)
// Represents a matcher condition for incoming requests.
type matcher struct {
// The name of the matcher, e.g. Path or Header
name string
// The args of the matcher, e.g. the path to be matched.
args []interface{}
}
// BackendType indicates whether a route is a network backend, a shunt or a loopback.
type BackendType int
const (
NetworkBackend = iota
ShuntBackend
LoopBackend
)
// Route definition used during the parser processes the raw routing
// document.
type parsedRoute struct {
id string
matchers []*matcher
filters []*Filter
shunt bool
loopback bool
backend string
}
// A Predicate object represents a parsed, in-memory, route matching predicate
// that is defined by extensions.
type Predicate struct {
// The name of the custom predicate as referenced
// in the route definition. E.g. 'Foo'.
Name string `json:"name"`
// The arguments of the predicate as defined in the
// route definition. The arguments can be of type
// float64 or string (string for both strings and
// regular expressions).
Args []interface{} `json:"args"`
}
// A Filter object represents a parsed, in-memory filter expression.
type Filter struct {
// name of the filter specification
Name string `json:"name"`
// filter args applied withing a particular route
Args []interface{} `json:"args"`
}
// A Route object represents a parsed, in-memory route definition.
type Route struct {
// Id of the route definition.
// E.g. route1: ...
Id string
// Deprecated, use Predicate instances with the name "Path".
//
// Exact path to be matched.
// E.g. Path("/some/path")
Path string
// Host regular expressions to match.
// E.g. Host(/[.]example[.]org/)
HostRegexps []string
// Path regular expressions to match.
// E.g. PathRegexp(/\/api\//)
PathRegexps []string
// Method to match.
// E.g. Method("HEAD")
Method string
// Exact header definitions to match.
// E.g. Header("Accept", "application/json")
Headers map[string]string
// Header regular expressions to match.
// E.g. HeaderRegexp("Accept", /\Wapplication\/json\W/)
HeaderRegexps map[string][]string
// Custom predicates to match.
// E.g. Traffic(.3)
Predicates []*Predicate
// Set of filters in a particular route.
// E.g. redirect(302, "https://www.example.org/hello")
Filters []*Filter
// Indicates that the parsed route has a shunt backend.
// (<shunt>, no forwarding to a backend)
//
// Deprecated, use the BackendType field instead.
Shunt bool
// Indicates that the parsed route is a shunt, loopback or
// it is forwarding to a network backend.
BackendType BackendType
// The address of a backend for a parsed route.
// E.g. "https://www.example.org"
Backend string
}
type RoutePredicate func(*Route) bool
// RouteInfo contains a route id, plus the loaded and parsed route or
// the parse error in case of failure.
type RouteInfo struct {
// The route id plus the route data or if parsing was successful.
Route
// The parsing error if the parsing failed.
ParseError error
}
func (t BackendType) String() string {
switch t {
case NetworkBackend:
return "network"
case ShuntBackend:
return "shunt"
case LoopBackend:
return "loopback"
default:
return "unknown"
}
}
// Expects exactly n arguments of type string, or fails.
func getStringArgs(n int, args []interface{}) ([]string, error) {
if len(args) != n {
return nil, invalidPredicateArgCountError
}
sargs := make([]string, n)
for i, a := range args {
if sa, ok := a.(string); ok {
sargs[i] = sa
} else {
return nil, invalidPredicateArgError
}
}
return sargs, nil
}
// Checks and sets the different predicates taken from the yacc result.
// As the syntax is getting stabilized, this logic soon should be defined as
// yacc rules. (https://github.com/zalando/skipper/issues/89)
func applyPredicates(route *Route, proute *parsedRoute) error {
var (
err error
args []string
pathSet bool
methodSet bool
)
for _, m := range proute.matchers {
if err != nil {
return err
}
switch m.name {
case "Path":
if pathSet {
return duplicatePathTreePredicateError
}
if args, err = getStringArgs(1, m.args); err == nil {
route.Path = args[0]
pathSet = true
}
case "Host":
if args, err = getStringArgs(1, m.args); err == nil {
route.HostRegexps = append(route.HostRegexps, args[0])
}
case "PathRegexp":
if args, err = getStringArgs(1, m.args); err == nil {
route.PathRegexps = append(route.PathRegexps, args[0])
}
case "Method":
if methodSet {
return duplicateMethodPredicateError
}
if args, err = getStringArgs(1, m.args); err == nil {
route.Method = args[0]
methodSet = true
}
case "HeaderRegexp":
if args, err = getStringArgs(2, m.args); err == nil {
if route.HeaderRegexps == nil {
route.HeaderRegexps = make(map[string][]string)
}
route.HeaderRegexps[args[0]] = append(route.HeaderRegexps[args[0]], args[1])
}
case "Header":
if args, err = getStringArgs(2, m.args); err == nil {
if route.Headers == nil {
route.Headers = make(map[string]string)
}
if _, ok := route.Headers[args[0]]; ok {
return fmt.Errorf(duplicateHeaderPredicateErrorFmt, args[0])
}
route.Headers[args[0]] = args[1]
}
case "*", "Any":
// void
default:
route.Predicates = append(
route.Predicates,
&Predicate{m.name, m.args})
}
}
return err
}
// Converts a parsing route objects to the exported route definition with
// pre-processed but not validated matchers.
func newRouteDefinition(r *parsedRoute) (*Route, error) {
rd := &Route{}
rd.Id = r.id
rd.Filters = r.filters
rd.Shunt = r.shunt
rd.Backend = r.backend
bt, err := backendType(r.shunt, r.loopback)
if err != nil {
return nil, err
}
rd.BackendType = bt
err = applyPredicates(rd, r)
return rd, err
}
func backendType(shunt, loopback bool) (bt BackendType, err error) {
if shunt && loopback {
err = errInvalidBackend
return
}
if shunt {
bt = ShuntBackend
} else if loopback {
bt = LoopBackend
} else {
bt = NetworkBackend
}
return
}
// executes the parser.
func parse(code string) ([]*parsedRoute, error) {
l := newLexer(code)
eskipParse(l)
return l.routes, l.err
}
func partialRouteToRoute(format, p string) string {
p = strings.TrimSpace(p)
if p == "" {
return ""
}
return fmt.Sprintf(format, p)
}
// hacks a filter expression into a route expression for parsing.
func filtersToRoute(f string) string {
return partialRouteToRoute("* -> %s -> <shunt>", f)
}
// hacks a predicate expression into a route expression for parsing.
func predicatesToRoute(p string) string {
return partialRouteToRoute("%s -> <shunt>", p)
}
// Parses a route expression or a routing document to a set of route definitions.
func Parse(code string) ([]*Route, error) {
parsedRoutes, err := parse(code)
if err != nil {
return nil, err
}
routeDefinitions := make([]*Route, len(parsedRoutes))
for i, r := range parsedRoutes {
rd, err := newRouteDefinition(r)
if err != nil {
return nil, err
}
routeDefinitions[i] = rd
}
return routeDefinitions, nil
}
func partialParse(f string, partialToRoute func(string) string) (*parsedRoute, error) {
rs, err := parse(partialToRoute(f))
if err != nil {
return nil, err
}
if len(rs) == 0 {
return nil, nil
}
return rs[0], nil
}
// Parses a filter chain into a list of parsed filter definitions.
func ParseFilters(f string) ([]*Filter, error) {
r, err := partialParse(f, filtersToRoute)
if r == nil || err != nil {
return nil, err
}
return r.filters, nil
}
// ParsePredicates parses a set of predicates (combined by '&&') into
// a list of parsed predicate definitions.
func ParsePredicates(p string) ([]*Predicate, error) {
r, err := partialParse(p, predicatesToRoute)
if r == nil || err != nil {
return nil, err
}
var ps []*Predicate
for i := range r.matchers {
if r.matchers[i].name != "*" {
ps = append(ps, &Predicate{
Name: r.matchers[i].name,
Args: r.matchers[i].args,
})
}
}
return ps, nil
}
const randomIdLength = 16
var routeIdRx = regexp.MustCompile("\\W")
// generate weak random id for a route if
// it doesn't have one.
func GenerateIfNeeded(existingId string) string {
if existingId != "" {
return existingId
}
// using this to avoid adding a new dependency.
id, err := flowid.NewFlowId(randomIdLength)
if err != nil {
return existingId
}
// replace characters that are not allowed
// for eskip route ids.
id = routeIdRx.ReplaceAllString(id, "x")
return "route" + id
}