forked from anexia-it/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperation.go
661 lines (540 loc) · 18.2 KB
/
operation.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
package oas
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/getkin/kin-openapi/openapi3"
"github.com/TykTechnologies/tyk/apidef"
)
// Operations holds Operation definitions.
type Operations map[string]*Operation
// Operation holds a request operation configuration, allowances, tranformations, caching, timeouts and validation.
type Operation struct {
// Allow request by allowance.
Allow *Allowance `bson:"allow,omitempty" json:"allow,omitempty"`
// Block request by allowance.
Block *Allowance `bson:"block,omitempty" json:"block,omitempty"`
// IgnoreAuthentication ignores authentication on request by allowance.
IgnoreAuthentication *Allowance `bson:"ignoreAuthentication,omitempty" json:"ignoreAuthentication,omitempty"`
// TransformRequestMethod allows you to transform the method of a request.
TransformRequestMethod *TransformRequestMethod `bson:"transformRequestMethod,omitempty" json:"transformRequestMethod,omitempty"`
// TransformRequestBody allows you to transform request body.
// When both `path` and `body` are provided, body would take precedence.
TransformRequestBody *TransformRequestBody `bson:"transformRequestBody,omitempty" json:"transformRequestBody,omitempty"`
// Cache contains the caching plugin configuration.
Cache *CachePlugin `bson:"cache,omitempty" json:"cache,omitempty"`
// EnforceTimeout contains the request timeout configuration.
EnforceTimeout *EnforceTimeout `bson:"enforceTimeout,omitempty" json:"enforceTimeout,omitempty"`
// ValidateRequest contains the request validation configuration.
ValidateRequest *ValidateRequest `bson:"validateRequest,omitempty" json:"validateRequest,omitempty"`
// MockResponse contains the mock response configuration.
MockResponse *MockResponse `bson:"mockResponse,omitempty" json:"mockResponse,omitempty"`
// VirtualEndpoint contains virtual endpoint configuration.
VirtualEndpoint *VirtualEndpoint `bson:"virtualEndpoint,omitempty" json:"virtualEndpoint,omitempty"`
// PostPlugins contains endpoint level post plugins configuration.
PostPlugins EndpointPostPlugins `bson:"postPlugins,omitempty" json:"postPlugins,omitempty"`
}
// AllowanceType holds the valid allowance types values.
type AllowanceType int
const (
allow AllowanceType = 0
block AllowanceType = 1
ignoreAuthentication AllowanceType = 2
contentTypeJSON = "application/json"
)
// Import takes the arguments and populates the receiver *Operation values.
func (o *Operation) Import(oasOperation *openapi3.Operation, overRideValues TykExtensionConfigParams) {
if overRideValues.AllowList != nil {
allow := o.Allow
if allow == nil {
allow = &Allowance{}
}
allow.Import(*overRideValues.AllowList)
if block := o.Block; block != nil && block.Enabled && *overRideValues.AllowList {
block.Enabled = false
}
o.Allow = allow
}
if overRideValues.ValidateRequest != nil {
validate := o.ValidateRequest
if validate == nil {
validate = &ValidateRequest{}
}
if ok := validate.shouldImport(oasOperation); ok {
validate.Import(*overRideValues.ValidateRequest)
o.ValidateRequest = validate
}
}
if overRideValues.MockResponse != nil {
mock := o.MockResponse
if mock == nil {
mock = &MockResponse{}
}
if ok := mock.shouldImport(oasOperation); ok {
mock.Import(*overRideValues.MockResponse)
o.MockResponse = mock
}
}
}
func (s *OAS) fillPathsAndOperations(ep apidef.ExtendedPathsSet) {
// Regardless if `ep` is a zero value, we need a non-nil paths
// to produce a valid OAS document
if s.Paths == nil {
s.Paths = make(openapi3.Paths)
}
s.fillAllowance(ep.WhiteList, allow)
s.fillAllowance(ep.BlackList, block)
s.fillAllowance(ep.Ignored, ignoreAuthentication)
s.fillTransformRequestMethod(ep.MethodTransforms)
s.fillTransformRequestBody(ep.Transform)
s.fillCache(ep.AdvanceCacheConfig)
s.fillEnforceTimeout(ep.HardTimeouts)
s.fillOASValidateRequest(ep.ValidateJSON)
s.fillVirtualEndpoint(ep.Virtual)
s.fillEndpointPostPlugins(ep.GoPlugin)
}
func (s *OAS) extractPathsAndOperations(ep *apidef.ExtendedPathsSet) {
tykOperations := s.getTykOperations()
if len(tykOperations) == 0 {
return
}
for id, tykOp := range tykOperations {
found:
for path, pathItem := range s.Paths {
for method, operation := range pathItem.Operations() {
if id == operation.OperationID {
tykOp.extractAllowanceTo(ep, path, method, allow)
tykOp.extractAllowanceTo(ep, path, method, block)
tykOp.extractAllowanceTo(ep, path, method, ignoreAuthentication)
tykOp.extractTransformRequestMethodTo(ep, path, method)
tykOp.extractTransformRequestBodyTo(ep, path, method)
tykOp.extractCacheTo(ep, path, method)
tykOp.extractEnforceTimeoutTo(ep, path, method)
tykOp.extractVirtualEndpointTo(ep, path, method)
tykOp.extractEndpointPostPluginTo(ep, path, method)
break found
}
}
}
}
}
func (s *OAS) fillAllowance(endpointMetas []apidef.EndPointMeta, typ AllowanceType) {
for _, em := range endpointMetas {
operationID := s.getOperationID(em.Path, em.Method)
operation := s.GetTykExtension().getOperation(operationID)
var allowance *Allowance
switch typ {
case block:
allowance = newAllowance(&operation.Block)
case ignoreAuthentication:
allowance = newAllowance(&operation.IgnoreAuthentication)
default:
allowance = newAllowance(&operation.Allow)
}
allowance.Fill(em)
if ShouldOmit(allowance) {
allowance = nil
}
}
}
func newAllowance(prev **Allowance) *Allowance {
if *prev == nil {
*prev = &Allowance{}
}
return *prev
}
func (s *OAS) fillTransformRequestMethod(metas []apidef.MethodTransformMeta) {
for _, meta := range metas {
operationID := s.getOperationID(meta.Path, meta.Method)
operation := s.GetTykExtension().getOperation(operationID)
if operation.TransformRequestMethod == nil {
operation.TransformRequestMethod = &TransformRequestMethod{}
}
operation.TransformRequestMethod.Fill(meta)
if ShouldOmit(operation.TransformRequestMethod) {
operation.TransformRequestMethod = nil
}
}
}
func (s *OAS) fillTransformRequestBody(metas []apidef.TemplateMeta) {
for _, meta := range metas {
operationID := s.getOperationID(meta.Path, meta.Method)
operation := s.GetTykExtension().getOperation(operationID)
if operation.TransformRequestBody == nil {
operation.TransformRequestBody = &TransformRequestBody{}
}
operation.TransformRequestBody.Fill(meta)
if ShouldOmit(operation.TransformRequestBody) {
operation.TransformRequestBody = nil
}
}
}
func (s *OAS) fillCache(metas []apidef.CacheMeta) {
for _, meta := range metas {
operationID := s.getOperationID(meta.Path, meta.Method)
operation := s.GetTykExtension().getOperation(operationID)
if operation.Cache == nil {
operation.Cache = &CachePlugin{}
}
operation.Cache.Fill(meta)
if ShouldOmit(operation.Cache) {
operation.Cache = nil
}
}
}
func (s *OAS) fillEnforceTimeout(metas []apidef.HardTimeoutMeta) {
for _, meta := range metas {
operationID := s.getOperationID(meta.Path, meta.Method)
operation := s.GetTykExtension().getOperation(operationID)
if operation.EnforceTimeout == nil {
operation.EnforceTimeout = &EnforceTimeout{}
}
operation.EnforceTimeout.Fill(meta)
if ShouldOmit(operation.EnforceTimeout) {
operation.EnforceTimeout = nil
}
}
}
func (o *Operation) extractAllowanceTo(ep *apidef.ExtendedPathsSet, path string, method string, typ AllowanceType) {
allowance := o.Allow
endpointMetas := &ep.WhiteList
switch typ {
case block:
allowance = o.Block
endpointMetas = &ep.BlackList
case ignoreAuthentication:
allowance = o.IgnoreAuthentication
endpointMetas = &ep.Ignored
}
if allowance == nil {
return
}
endpointMeta := apidef.EndPointMeta{Path: path, Method: method}
allowance.ExtractTo(&endpointMeta)
*endpointMetas = append(*endpointMetas, endpointMeta)
}
func (o *Operation) extractTransformRequestMethodTo(ep *apidef.ExtendedPathsSet, path string, method string) {
if o.TransformRequestMethod == nil {
return
}
meta := apidef.MethodTransformMeta{Path: path, Method: method}
o.TransformRequestMethod.ExtractTo(&meta)
ep.MethodTransforms = append(ep.MethodTransforms, meta)
}
func (o *Operation) extractTransformRequestBodyTo(ep *apidef.ExtendedPathsSet, path string, method string) {
if o.TransformRequestBody == nil {
return
}
meta := apidef.TemplateMeta{Path: path, Method: method}
o.TransformRequestBody.ExtractTo(&meta)
ep.Transform = append(ep.Transform, meta)
}
func (o *Operation) extractCacheTo(ep *apidef.ExtendedPathsSet, path string, method string) {
if o.Cache == nil {
return
}
newCacheMeta := apidef.CacheMeta{
Method: method,
Path: path,
}
o.Cache.ExtractTo(&newCacheMeta)
ep.AdvanceCacheConfig = append(ep.AdvanceCacheConfig, newCacheMeta)
}
func (o *Operation) extractEnforceTimeoutTo(ep *apidef.ExtendedPathsSet, path string, method string) {
if o.EnforceTimeout == nil {
return
}
meta := apidef.HardTimeoutMeta{Path: path, Method: method}
o.EnforceTimeout.ExtractTo(&meta)
ep.HardTimeouts = append(ep.HardTimeouts, meta)
}
// detect possible regex pattern:
// - character match ([a-z])
// - greedy match (.*)
// - ungreedy match (.+)
// - end of string ($).
var regexPatterns = []string{
".+", ".*", "[", "]", "$",
}
type pathPart struct {
name string
value string
isRegex bool
}
func (p pathPart) String() string {
if p.isRegex {
return "{" + p.name + "}"
}
return p.value
}
// isRegex checks if value has expected regular expression patterns.
func isRegex(value string) bool {
for _, pattern := range regexPatterns {
if strings.Contains(value, pattern) {
return true
}
}
return false
}
// splitPath splits url into folder parts, detecting regex patterns.
func splitPath(inPath string) ([]pathPart, bool) {
// Each url fragment can contain a regex, but the whole
// url isn't just a regex (`/a/.*/foot` => `/a/{param1}/foot`)
parts := strings.Split(strings.Trim(inPath, "/"), "/")
result := make([]pathPart, len(parts))
found := 0
for k, value := range parts {
name := value
isRegex := isRegex(value)
if isRegex {
found++
name = fmt.Sprintf("customRegex%d", found)
}
result[k] = pathPart{
name: name,
value: value,
isRegex: isRegex,
}
}
return result, found > 0
}
// buildPath converts the url paths with regex to named parameters
// e.g. ["a", ".*"] becomes /a/{customRegex1}.
func buildPath(parts []pathPart, appendSlash bool) string {
newPath := ""
for _, part := range parts {
newPath += "/" + part.String()
}
if appendSlash {
return newPath + "/"
}
return newPath
}
func (s *OAS) getOperationID(inPath, method string) string {
operationID := strings.TrimPrefix(inPath, "/") + method
createOrGetPathItem := func(item string) *openapi3.PathItem {
if s.Paths[item] == nil {
s.Paths[item] = &openapi3.PathItem{}
}
return s.Paths[item]
}
createOrUpdateOperation := func(p *openapi3.PathItem) *openapi3.Operation {
operation := p.GetOperation(method)
if operation == nil {
operation = &openapi3.Operation{
Responses: openapi3.NewResponses(),
}
p.SetOperation(method, operation)
}
if operation.OperationID == "" {
operation.OperationID = operationID
}
return operation
}
var p *openapi3.PathItem
parts, hasRegex := splitPath(inPath)
if hasRegex {
newPath := buildPath(parts, strings.HasSuffix(inPath, "/"))
p = createOrGetPathItem(newPath)
p.Parameters = []*openapi3.ParameterRef{}
for _, part := range parts {
if part.isRegex {
schema := &openapi3.SchemaRef{
Value: &openapi3.Schema{
Type: "string",
Pattern: part.value,
},
}
param := &openapi3.ParameterRef{
Value: &openapi3.Parameter{
Name: part.name,
In: "path",
Required: true,
Schema: schema,
},
}
p.Parameters = append(p.Parameters, param)
}
}
} else {
p = createOrGetPathItem(inPath)
}
operation := createOrUpdateOperation(p)
return operation.OperationID
}
func (x *XTykAPIGateway) getOperation(operationID string) *Operation {
if x.Middleware == nil {
x.Middleware = &Middleware{}
}
middleware := x.Middleware
if middleware.Operations == nil {
middleware.Operations = make(Operations)
}
operations := middleware.Operations
if operations[operationID] == nil {
operations[operationID] = &Operation{}
}
return operations[operationID]
}
// ValidateRequest holds configuration required for validating requests.
type ValidateRequest struct {
// Enabled is a boolean flag, if set to `true`, it enables request validation.
Enabled bool `bson:"enabled" json:"enabled"`
// ErrorResponseCode is the error code emitted when the request fails validation.
// If unset or zero, the response will returned with http status 422 Unprocessable Entity.
ErrorResponseCode int `bson:"errorResponseCode,omitempty" json:"errorResponseCode,omitempty"`
}
// Fill fills *ValidateRequest receiver from apidef.ValidateRequestMeta.
func (v *ValidateRequest) Fill(meta apidef.ValidatePathMeta) {
v.Enabled = !meta.Disabled
v.ErrorResponseCode = meta.ErrorResponseCode
}
func (*ValidateRequest) shouldImport(operation *openapi3.Operation) bool {
if len(operation.Parameters) > 0 {
return true
}
reqBody := operation.RequestBody
if reqBody == nil {
return false
}
reqBodyVal := reqBody.Value
if reqBodyVal == nil {
return false
}
media := reqBodyVal.Content.Get("application/json")
return media != nil
}
// Import populates *ValidateRequest with enabled argument and a default error response code.
func (v *ValidateRequest) Import(enabled bool) {
v.Enabled = enabled
v.ErrorResponseCode = http.StatusUnprocessableEntity
}
func convertSchema(mapSchema map[string]interface{}) (*openapi3.Schema, error) {
bytes, err := json.Marshal(mapSchema)
if err != nil {
return nil, err
}
schema := openapi3.NewSchema()
err = schema.UnmarshalJSON(bytes)
if err != nil {
return nil, err
}
return schema, nil
}
func (s *OAS) fillOASValidateRequest(metas []apidef.ValidatePathMeta) {
for _, meta := range metas {
operationID := s.getOperationID(meta.Path, meta.Method)
operation := s.Paths.Find(meta.Path).GetOperation(meta.Method)
requestBodyRef := operation.RequestBody
if operation.RequestBody == nil {
requestBodyRef = &openapi3.RequestBodyRef{}
operation.RequestBody = requestBodyRef
}
if requestBodyRef.Value == nil {
requestBodyRef.Value = openapi3.NewRequestBody()
}
schema, err := convertSchema(meta.Schema)
if err != nil {
log.WithError(err).Error("Couldn't convert classic API validate JSON schema to OAS")
} else {
requestBodyRef.Value.WithJSONSchema(schema)
}
tykOp := s.GetTykExtension().getOperation(operationID)
if tykOp.ValidateRequest == nil {
tykOp.ValidateRequest = &ValidateRequest{}
}
tykOp.ValidateRequest.Fill(meta)
if ShouldOmit(tykOp.ValidateRequest) {
tykOp.ValidateRequest = nil
}
}
}
// MockResponse configures the mock responses.
type MockResponse struct {
// Enabled enables the mock response middleware.
Enabled bool `bson:"enabled" json:"enabled"`
// Code is the HTTP response code that will be returned.
Code int `bson:"code,omitempty" json:"code,omitempty"`
// Body is the HTTP response body that will be returned.
Body string `bson:"body,omitempty" json:"body,omitempty"`
// Headers are the HTTP response headers that will be returned.
Headers []Header `bson:"headers,omitempty" json:"headers,omitempty"`
// FromOASExamples is the configuration to extract a mock response from OAS documentation.
FromOASExamples *FromOASExamples `bson:"fromOASExamples,omitempty" json:"fromOASExamples,omitempty"`
}
// FromOASExamples configures mock responses should be returned from OAS example responses.
type FromOASExamples struct {
// Enabled enables getting a mock response from OAS examples or schemas documented in OAS.
Enabled bool `bson:"enabled" json:"enabled"`
// Code is the default HTTP response code that the gateway reads from the path responses documented in OAS.
Code int `bson:"code,omitempty" json:"code,omitempty"`
// ContentType is the default HTTP response body type that the gateway reads from the path responses documented in OAS.
ContentType string `bson:"contentType,omitempty" json:"contentType,omitempty"`
// ExampleName is the default example name among multiple path response examples documented in OAS.
ExampleName string `bson:"exampleName,omitempty" json:"exampleName,omitempty"`
}
func (*MockResponse) shouldImport(operation *openapi3.Operation) bool {
for _, response := range operation.Responses {
for _, content := range response.Value.Content {
if content.Example != nil || content.Schema != nil {
return true
}
for _, example := range content.Examples {
if example.Value != nil {
return true
}
}
}
}
return false
}
// Import populates *MockResponse with enabled argument for FromOASExamples.
func (m *MockResponse) Import(enabled bool) {
m.Enabled = enabled
m.FromOASExamples = &FromOASExamples{
Enabled: enabled,
}
}
func (s *OAS) fillVirtualEndpoint(endpointMetas []apidef.VirtualMeta) {
for _, em := range endpointMetas {
operationID := s.getOperationID(em.Path, em.Method)
operation := s.GetTykExtension().getOperation(operationID)
if operation.VirtualEndpoint == nil {
operation.VirtualEndpoint = &VirtualEndpoint{}
}
operation.VirtualEndpoint.Fill(em)
if ShouldOmit(operation.VirtualEndpoint) {
operation.VirtualEndpoint = nil
}
}
}
func (o *Operation) extractVirtualEndpointTo(ep *apidef.ExtendedPathsSet, path string, method string) {
if o.VirtualEndpoint == nil {
return
}
meta := apidef.VirtualMeta{Path: path, Method: method}
o.VirtualEndpoint.ExtractTo(&meta)
ep.Virtual = append(ep.Virtual, meta)
}
func (s *OAS) fillEndpointPostPlugins(endpointMetas []apidef.GoPluginMeta) {
for _, em := range endpointMetas {
operationID := s.getOperationID(em.Path, em.Method)
operation := s.GetTykExtension().getOperation(operationID)
if operation.PostPlugins == nil {
operation.PostPlugins = make(EndpointPostPlugins, 1)
}
operation.PostPlugins.Fill(em)
if ShouldOmit(operation.PostPlugins) {
operation.PostPlugins = nil
}
}
}
func (o *Operation) extractEndpointPostPluginTo(ep *apidef.ExtendedPathsSet, path string, method string) {
if o.PostPlugins == nil {
return
}
meta := apidef.GoPluginMeta{Path: path, Method: method}
o.PostPlugins.ExtractTo(&meta)
ep.GoPlugin = append(ep.GoPlugin, meta)
}