forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_definition.go
1113 lines (943 loc) · 34.5 KB
/
api_definition.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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"encoding/base64"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"path/filepath"
"regexp"
"strings"
"sync"
textTemplate "text/template"
"time"
"github.com/rubyist/circuitbreaker"
"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/config"
)
const (
LDAPStorageEngine apidef.StorageEngineCode = "ldap"
RPCStorageEngine apidef.StorageEngineCode = "rpc"
)
// URLStatus is a custom enum type to avoid collisions
type URLStatus int
// Enums representing the various statuses for a VersionInfo Path match during a
// proxy request
const (
_ URLStatus = iota
Ignored
WhiteList
BlackList
Cached
Transformed
HeaderInjected
HeaderInjectedResponse
TransformedResponse
HardTimeout
CircuitBreaker
URLRewrite
VirtualPath
RequestSizeLimit
MethodTransformed
RequestTracked
RequestNotTracked
)
// RequestStatus is a custom type to avoid collisions
type RequestStatus string
// Statuses of the request, all are false-y except StatusOk and StatusOkAndIgnore
const (
VersionNotFound RequestStatus = "Version information not found"
VersionDoesNotExist RequestStatus = "This API version does not seem to exist"
VersionWhiteListStatusNotFound RequestStatus = "WhiteListStatus for path not found"
VersionExpired RequestStatus = "Api Version has expired, please check documentation or contact administrator"
EndPointNotAllowed RequestStatus = "Requested endpoint is forbidden"
StatusOkAndIgnore RequestStatus = "Everything OK, passing and not filtering"
StatusOk RequestStatus = "Everything OK, passing"
StatusCached RequestStatus = "Cached path"
StatusTransform RequestStatus = "Transformed path"
StatusTransformResponse RequestStatus = "Transformed response"
StatusHeaderInjected RequestStatus = "Header injected"
StatusMethodTransformed RequestStatus = "Method Transformed"
StatusHeaderInjectedResponse RequestStatus = "Header injected on response"
StatusRedirectFlowByReply RequestStatus = "Exceptional action requested, redirecting flow!"
StatusHardTimeout RequestStatus = "Hard Timeout enforced on path"
StatusCircuitBreaker RequestStatus = "Circuit breaker enforced"
StatusURLRewrite RequestStatus = "URL Rewritten"
StatusVirtualPath RequestStatus = "Virtual Endpoint"
StatusRequestSizeControlled RequestStatus = "Request Size Limited"
StatusRequesTracked RequestStatus = "Request Tracked"
StatusRequestNotTracked RequestStatus = "Request Not Tracked"
)
// URLSpec represents a flattened specification for URLs, used to check if a proxy URL
// path is on any of the white, plack or ignored lists. This is generated as part of the
// configuration init
type URLSpec struct {
Spec *regexp.Regexp
Status URLStatus
MethodActions map[string]apidef.EndpointMethodMeta
TransformAction TransformSpec
TransformResponseAction TransformSpec
InjectHeaders apidef.HeaderInjectionMeta
InjectHeadersResponse apidef.HeaderInjectionMeta
HardTimeout apidef.HardTimeoutMeta
CircuitBreaker ExtendedCircuitBreakerMeta
URLRewrite apidef.URLRewriteMeta
VirtualPathSpec apidef.VirtualMeta
RequestSize apidef.RequestSizeMeta
MethodTransform apidef.MethodTransformMeta
TrackEndpoint apidef.TrackEndpointMeta
DoNotTrackEndpoint apidef.TrackEndpointMeta
}
type TransformSpec struct {
apidef.TemplateMeta
Template *textTemplate.Template
}
type ExtendedCircuitBreakerMeta struct {
apidef.CircuitBreakerMeta
CB *circuit.Breaker
}
// APISpec represents a path specification for an API, to avoid enumerating multiple nested lists, a single
// flattened URL list is checked for matching paths and then it's status evaluated if found.
type APISpec struct {
*apidef.APIDefinition
RxPaths map[string][]URLSpec
WhiteListEnabled map[string]bool
target *url.URL
AuthManager AuthorisationHandler
SessionManager SessionHandler
OAuthManager *OAuthManager
OrgSessionManager SessionHandler
EventPaths map[apidef.TykEvent][]config.TykEventHandler
Health HealthChecker
JSVM *JSVM
ResponseChain []TykResponseHandler
RoundRobin RoundRobin
URLRewriteEnabled bool
CircuitBreakerEnabled bool
EnforcedTimeoutEnabled bool
ResponseHandlersActive bool
LastGoodHostList *apidef.HostList
HasRun bool
ServiceRefreshInProgress bool
}
// APIDefinitionLoader will load an Api definition from a storage
// system.
type APIDefinitionLoader struct{}
// Nonce to use when interacting with the dashboard service
var ServiceNonce string
// MakeSpec will generate a flattened URLSpec from and APIDefinitions' VersionInfo data. paths are
// keyed to the Api version name, which is determined during routing to speed up lookups
func (a APIDefinitionLoader) MakeSpec(def *apidef.APIDefinition) *APISpec {
spec := &APISpec{}
spec.APIDefinition = def
// We'll push the default HealthChecker:
spec.Health = &DefaultHealthChecker{
APIID: spec.APIID,
}
// Add any new session managers or auth handlers here
spec.AuthManager = &DefaultAuthorisationManager{}
spec.SessionManager = &DefaultSessionManager{}
spec.OrgSessionManager = &DefaultSessionManager{}
// Create and init the virtual Machine
if globalConf.EnableJSVM {
spec.JSVM = &JSVM{}
spec.JSVM.Init()
}
// Set up Event Handlers
log.Debug("INITIALISING EVENT HANDLERS")
spec.EventPaths = make(map[apidef.TykEvent][]config.TykEventHandler)
for eventName, eventHandlerConfs := range def.EventHandlers.Events {
log.Debug("FOUND EVENTS TO INIT")
for _, handlerConf := range eventHandlerConfs {
log.Debug("CREATING EVENT HANDLERS")
eventHandlerInstance, err := GetEventHandlerByName(handlerConf, spec)
if err != nil {
log.Error("Failed to init event handler: ", err)
} else {
log.Debug("Init Event Handler: ", eventName)
spec.EventPaths[eventName] = append(spec.EventPaths[eventName], eventHandlerInstance)
}
}
}
spec.RxPaths = make(map[string][]URLSpec, len(def.VersionData.Versions))
spec.WhiteListEnabled = make(map[string]bool, len(def.VersionData.Versions))
for _, v := range def.VersionData.Versions {
var pathSpecs []URLSpec
var whiteListSpecs bool
// If we have transitioned to extended path specifications, we should use these now
if v.UseExtendedPaths {
pathSpecs, whiteListSpecs = a.getExtendedPathSpecs(v, spec)
} else {
log.Warning("Legacy path detected! Upgrade to extended.")
pathSpecs, whiteListSpecs = a.getPathSpecs(v)
}
spec.RxPaths[v.Name] = pathSpecs
spec.WhiteListEnabled[v.Name] = whiteListSpecs
}
return spec
}
func (a APIDefinitionLoader) readBody(response *http.Response) ([]byte, error) {
defer response.Body.Close()
return ioutil.ReadAll(response.Body)
}
// FromDashboardService will connect and download ApiDefintions from a Tyk Dashboard instance.
func (a APIDefinitionLoader) FromDashboardService(endpoint, secret string) []*APISpec {
// Get the definitions
log.Debug("Calling: ", endpoint)
newRequest, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
log.Error("Failed to create request: ", err)
}
newRequest.Header.Set("authorization", secret)
log.Debug("Using: NodeID: ", NodeID)
newRequest.Header.Set("x-tyk-nodeid", NodeID)
newRequest.Header.Set("x-tyk-nonce", ServiceNonce)
c := &http.Client{
Timeout: 120 * time.Second,
}
response, err := c.Do(newRequest)
if err != nil {
log.Error("Request failed: ", err)
return nil
}
retBody, err := a.readBody(response)
if err != nil {
log.Error("Failed to read body: ", err)
return nil
}
if response.StatusCode == 403 {
log.Error("Login failure, Response was: ", string(retBody))
reLogin()
return nil
}
// Extract tagged APIs#
type ResponseStruct struct {
ApiDefinition *apidef.APIDefinition `bson:"api_definition" json:"api_definition"`
}
type NodeResponseOK struct {
Status string
Message []ResponseStruct
Nonce string
}
list := NodeResponseOK{}
if err := json.Unmarshal(retBody, &list); err != nil {
log.Error("Failed to decode body: ", err, "Response was: ", string(retBody))
log.Info("--> Retrying in 5s")
return nil
}
// Extract tagged entries only
apiDefs := make([]*apidef.APIDefinition, 0)
if globalConf.DBAppConfOptions.NodeIsSegmented {
tagList := make(map[string]bool, len(globalConf.DBAppConfOptions.Tags))
toLoad := make(map[string]*apidef.APIDefinition)
for _, mt := range globalConf.DBAppConfOptions.Tags {
tagList[mt] = true
}
for _, apiEntry := range list.Message {
for _, t := range apiEntry.ApiDefinition.Tags {
if tagList[t] {
toLoad[apiEntry.ApiDefinition.APIID] = apiEntry.ApiDefinition
}
}
}
for _, apiDef := range toLoad {
apiDefs = append(apiDefs, apiDef)
}
} else {
for _, apiEntry := range list.Message {
apiDefs = append(apiDefs, apiEntry.ApiDefinition)
}
}
// Process
var apiSpecs []*APISpec
for _, def := range apiDefs {
spec := a.MakeSpec(def)
apiSpecs = append(apiSpecs, spec)
}
// Set the nonce
ServiceNonce = list.Nonce
log.Debug("Loading APIS Finished: Nonce Set: ", ServiceNonce)
return apiSpecs
}
// FromCloud will connect and download ApiDefintions from a Mongo DB instance.
func (a APIDefinitionLoader) FromRPC(orgId string) []*APISpec {
store := RPCStorageHandler{UserKey: globalConf.SlaveOptions.APIKey, Address: globalConf.SlaveOptions.ConnectionString}
store.Connect()
// enable segments
var tags []string
if globalConf.DBAppConfOptions.NodeIsSegmented {
log.Info("Segmented node, loading: ", globalConf.DBAppConfOptions.Tags)
tags = globalConf.DBAppConfOptions.Tags
}
apiCollection := store.GetApiDefinitions(orgId, tags)
//store.Disconnect()
if RPC_LoadCount > 0 {
saveRPCDefinitionsBackup(apiCollection)
}
return a.processRPCDefinitions(apiCollection)
}
func (a APIDefinitionLoader) processRPCDefinitions(apiCollection string) []*APISpec {
var apiDefs []*apidef.APIDefinition
if err := json.Unmarshal([]byte(apiCollection), &apiDefs); err != nil {
log.Error("Failed decode: ", err)
return nil
}
var apiSpecs []*APISpec
for _, def := range apiDefs {
def.DecodeFromDB()
if globalConf.SlaveOptions.BindToSlugsInsteadOfListenPaths {
newListenPath := "/" + def.Slug //+ "/"
log.Warning("Binding to ",
newListenPath,
" instead of ",
def.Proxy.ListenPath)
def.Proxy.ListenPath = newListenPath
}
spec := a.MakeSpec(def)
apiSpecs = append(apiSpecs, spec)
}
return apiSpecs
}
func (a APIDefinitionLoader) ParseDefinition(apiDef []byte) *apidef.APIDefinition {
def := &apidef.APIDefinition{}
if err := json.Unmarshal(apiDef, def); err != nil {
log.Error("[RPC] --> Couldn't unmarshal api configuration: ", err)
}
return def
}
// FromDir will load APIDefinitions from a directory on the filesystem. Definitions need
// to be the JSON representation of APIDefinition object
func (a APIDefinitionLoader) FromDir(dir string) []*APISpec {
var apiSpecs []*APISpec
// Grab json files from directory
files, _ := ioutil.ReadDir(dir)
for _, f := range files {
if strings.Contains(f.Name(), ".json") {
filePath := filepath.Join(dir, f.Name())
log.Info("Loading API Specification from ", filePath)
defBody, err := ioutil.ReadFile(filePath)
if err != nil {
log.Error("Couldn't load app configuration file: ", err)
}
def := a.ParseDefinition(defBody)
spec := a.MakeSpec(def)
apiSpecs = append(apiSpecs, spec)
}
}
return apiSpecs
}
func (a APIDefinitionLoader) getPathSpecs(apiVersionDef apidef.VersionInfo) ([]URLSpec, bool) {
ignoredPaths := a.compilePathSpec(apiVersionDef.Paths.Ignored, Ignored)
blackListPaths := a.compilePathSpec(apiVersionDef.Paths.BlackList, BlackList)
whiteListPaths := a.compilePathSpec(apiVersionDef.Paths.WhiteList, WhiteList)
combinedPath := []URLSpec{}
combinedPath = append(combinedPath, ignoredPaths...)
combinedPath = append(combinedPath, blackListPaths...)
combinedPath = append(combinedPath, whiteListPaths...)
return combinedPath, len(whiteListPaths) > 0
}
func (a APIDefinitionLoader) generateRegex(stringSpec string, newSpec *URLSpec, specType URLStatus) {
apiLangIDsRegex := regexp.MustCompile(`{(.*?)}`)
asRegexStr := apiLangIDsRegex.ReplaceAllString(stringSpec, `(.*?)`)
asRegex := regexp.MustCompile(asRegexStr)
newSpec.Status = specType
newSpec.Spec = asRegex
}
func (a APIDefinitionLoader) compilePathSpec(paths []string, specType URLStatus) []URLSpec {
// transform a configuration URL into an array of URLSpecs
// This way we can iterate the whole array once, on match we break with status
urlSpec := []URLSpec{}
for _, stringSpec := range paths {
newSpec := URLSpec{}
a.generateRegex(stringSpec, &newSpec, specType)
urlSpec = append(urlSpec, newSpec)
}
return urlSpec
}
func (a APIDefinitionLoader) compileExtendedPathSpec(paths []apidef.EndPointMeta, specType URLStatus) []URLSpec {
// transform an extended configuration URL into an array of URLSpecs
// This way we can iterate the whole array once, on match we break with status
urlSpec := []URLSpec{}
for _, stringSpec := range paths {
newSpec := URLSpec{}
a.generateRegex(stringSpec.Path, &newSpec, specType)
// Extend with method actions
newSpec.MethodActions = stringSpec.MethodActions
urlSpec = append(urlSpec, newSpec)
}
return urlSpec
}
func (a APIDefinitionLoader) compileCachedPathSpec(paths []string) []URLSpec {
// transform an extended configuration URL into an array of URLSpecs
// This way we can iterate the whole array once, on match we break with status
urlSpec := []URLSpec{}
for _, stringSpec := range paths {
newSpec := URLSpec{}
a.generateRegex(stringSpec, &newSpec, Cached)
// Extend with method actions
urlSpec = append(urlSpec, newSpec)
}
return urlSpec
}
func (a APIDefinitionLoader) loadFileTemplate(path string) (*textTemplate.Template, error) {
log.Debug("-- Loading template: ", path)
return textTemplate.ParseFiles(path)
}
func (a APIDefinitionLoader) loadBlobTemplate(blob string) (*textTemplate.Template, error) {
log.Debug("-- Loading blob")
uDec, err := base64.StdEncoding.DecodeString(blob)
if err != nil {
return nil, err
}
return textTemplate.New("blob").Parse(string(uDec))
}
func (a APIDefinitionLoader) compileTransformPathSpec(paths []apidef.TemplateMeta, stat URLStatus) []URLSpec {
// transform an extended configuration URL into an array of URLSpecs
// This way we can iterate the whole array once, on match we break with status
urlSpec := []URLSpec{}
log.Debug("Checking for transform paths...")
for _, stringSpec := range paths {
log.Debug("-- Generating path")
newSpec := URLSpec{}
a.generateRegex(stringSpec.Path, &newSpec, stat)
// Extend with template actions
newTransformSpec := TransformSpec{TemplateMeta: stringSpec}
// Load the templates
var err error
switch stringSpec.TemplateData.Mode {
case apidef.UseFile:
log.Debug("-- Using File mode")
newTransformSpec.Template, err = a.loadFileTemplate(stringSpec.TemplateData.TemplateSource)
case apidef.UseBlob:
log.Debug("-- Blob mode")
newTransformSpec.Template, err = a.loadBlobTemplate(stringSpec.TemplateData.TemplateSource)
default:
log.Warning("[Transform Templates] No template mode defined! Found: ", stringSpec.TemplateData.Mode)
err = errors.New("No valid template mode defined, must be either 'file' or 'blob'")
}
if stat == Transformed {
newSpec.TransformAction = newTransformSpec
} else {
newSpec.TransformResponseAction = newTransformSpec
}
if err == nil {
urlSpec = append(urlSpec, newSpec)
log.Debug("-- Loaded")
} else {
log.Error("Template load failure! Skipping transformation: ", err)
}
}
return urlSpec
}
func (a APIDefinitionLoader) compileInjectedHeaderSpec(paths []apidef.HeaderInjectionMeta, stat URLStatus) []URLSpec {
// transform an extended configuration URL into an array of URLSpecs
// This way we can iterate the whole array once, on match we break with status
urlSpec := []URLSpec{}
for _, stringSpec := range paths {
newSpec := URLSpec{}
a.generateRegex(stringSpec.Path, &newSpec, stat)
// Extend with method actions
if stat == HeaderInjected {
newSpec.InjectHeaders = stringSpec
} else {
newSpec.InjectHeadersResponse = stringSpec
}
urlSpec = append(urlSpec, newSpec)
}
return urlSpec
}
func (a APIDefinitionLoader) compileMethodTransformSpec(paths []apidef.MethodTransformMeta, stat URLStatus) []URLSpec {
// transform an extended configuration URL into an array of URLSpecs
// This way we can iterate the whole array once, on match we break with status
urlSpec := []URLSpec{}
for _, stringSpec := range paths {
newSpec := URLSpec{}
a.generateRegex(stringSpec.Path, &newSpec, stat)
newSpec.MethodTransform = stringSpec
urlSpec = append(urlSpec, newSpec)
}
return urlSpec
}
func (a APIDefinitionLoader) compileTimeoutPathSpec(paths []apidef.HardTimeoutMeta, stat URLStatus) []URLSpec {
// transform an extended configuration URL into an array of URLSpecs
// This way we can iterate the whole array once, on match we break with status
urlSpec := []URLSpec{}
for _, stringSpec := range paths {
newSpec := URLSpec{}
a.generateRegex(stringSpec.Path, &newSpec, stat)
// Extend with method actions
newSpec.HardTimeout = stringSpec
urlSpec = append(urlSpec, newSpec)
}
return urlSpec
}
func (a APIDefinitionLoader) compileRequestSizePathSpec(paths []apidef.RequestSizeMeta, stat URLStatus) []URLSpec {
// transform an extended configuration URL into an array of URLSpecs
// This way we can iterate the whole array once, on match we break with status
urlSpec := []URLSpec{}
for _, stringSpec := range paths {
newSpec := URLSpec{}
a.generateRegex(stringSpec.Path, &newSpec, stat)
// Extend with method actions
newSpec.RequestSize = stringSpec
urlSpec = append(urlSpec, newSpec)
}
return urlSpec
}
func (a APIDefinitionLoader) compileCircuitBreakerPathSpec(paths []apidef.CircuitBreakerMeta, stat URLStatus, apiSpec *APISpec) []URLSpec {
// transform an extended configuration URL into an array of URLSpecs
// This way we can iterate the whole array once, on match we break with status
urlSpec := []URLSpec{}
for _, stringSpec := range paths {
newSpec := URLSpec{}
a.generateRegex(stringSpec.Path, &newSpec, stat)
// Extend with method actions
newSpec.CircuitBreaker = ExtendedCircuitBreakerMeta{CircuitBreakerMeta: stringSpec}
log.Debug("Initialising circuit breaker for: ", stringSpec.Path)
newSpec.CircuitBreaker.CB = circuit.NewRateBreaker(stringSpec.ThresholdPercent, stringSpec.Samples)
events := newSpec.CircuitBreaker.CB.Subscribe()
go func(path string, spec *APISpec, breakerPtr *circuit.Breaker) {
timerActive := false
for e := range events {
switch e {
case circuit.BreakerTripped:
log.Warning("[PROXY] [CIRCUIT BREKER] Breaker tripped for path: ", path)
log.Debug("Breaker tripped: ", e)
// Start a timer function
if !timerActive {
go func(timeout int, breaker *circuit.Breaker) {
log.Debug("-- Sleeping for (s): ", timeout)
time.Sleep(time.Duration(timeout) * time.Second)
log.Debug("-- Resetting breaker")
breaker.Reset()
timerActive = false
}(newSpec.CircuitBreaker.ReturnToServiceAfter, breakerPtr)
timerActive = true
}
if spec.Proxy.ServiceDiscovery.UseDiscoveryService {
if ServiceCache != nil {
log.Warning("[PROXY] [CIRCUIT BREKER] Refreshing host list")
ServiceCache.Delete(spec.APIID)
}
}
spec.FireEvent(EventBreakerTriggered, EventCurcuitBreakerMeta{
EventMetaDefault: EventMetaDefault{Message: "Breaker Tripped"},
CircuitEvent: e,
Path: path,
APIID: spec.APIID,
})
case circuit.BreakerReset:
spec.FireEvent(EventBreakerTriggered, EventCurcuitBreakerMeta{
EventMetaDefault: EventMetaDefault{Message: "Breaker Reset"},
CircuitEvent: e,
Path: path,
APIID: spec.APIID,
})
}
}
}(stringSpec.Path, apiSpec, newSpec.CircuitBreaker.CB)
urlSpec = append(urlSpec, newSpec)
}
return urlSpec
}
func (a APIDefinitionLoader) compileURLRewritesPathSpec(paths []apidef.URLRewriteMeta, stat URLStatus) []URLSpec {
// transform an extended configuration URL into an array of URLSpecs
// This way we can iterate the whole array once, on match we break with status
urlSpec := []URLSpec{}
for _, stringSpec := range paths {
newSpec := URLSpec{}
a.generateRegex(stringSpec.Path, &newSpec, stat)
// Extend with method actions
newSpec.URLRewrite = stringSpec
urlSpec = append(urlSpec, newSpec)
}
return urlSpec
}
func (a APIDefinitionLoader) compileVirtualPathspathSpec(paths []apidef.VirtualMeta, stat URLStatus, apiSpec *APISpec) []URLSpec {
if !globalConf.EnableJSVM {
return nil
}
// transform an extended configuration URL into an array of URLSpecs
// This way we can iterate the whole array once, on match we break with status
urlSpec := []URLSpec{}
for _, stringSpec := range paths {
newSpec := URLSpec{}
a.generateRegex(stringSpec.Path, &newSpec, stat)
// Extend with method actions
newSpec.VirtualPathSpec = stringSpec
PreLoadVirtualMetaCode(&newSpec.VirtualPathSpec, apiSpec.JSVM)
urlSpec = append(urlSpec, newSpec)
}
return urlSpec
}
func (a APIDefinitionLoader) compileTrackedEndpointPathspathSpec(paths []apidef.TrackEndpointMeta, stat URLStatus) []URLSpec {
urlSpec := []URLSpec{}
for _, stringSpec := range paths {
newSpec := URLSpec{}
a.generateRegex(stringSpec.Path, &newSpec, stat)
// Extend with method actions
newSpec.TrackEndpoint = stringSpec
urlSpec = append(urlSpec, newSpec)
}
return urlSpec
}
func (a APIDefinitionLoader) compileUnTrackedEndpointPathspathSpec(paths []apidef.TrackEndpointMeta, stat URLStatus) []URLSpec {
urlSpec := []URLSpec{}
for _, stringSpec := range paths {
newSpec := URLSpec{}
a.generateRegex(stringSpec.Path, &newSpec, stat)
// Extend with method actions
newSpec.DoNotTrackEndpoint = stringSpec
urlSpec = append(urlSpec, newSpec)
}
return urlSpec
}
func (a APIDefinitionLoader) getExtendedPathSpecs(apiVersionDef apidef.VersionInfo, apiSpec *APISpec) ([]URLSpec, bool) {
// TODO: New compiler here, needs to put data into a different structure
ignoredPaths := a.compileExtendedPathSpec(apiVersionDef.ExtendedPaths.Ignored, Ignored)
blackListPaths := a.compileExtendedPathSpec(apiVersionDef.ExtendedPaths.BlackList, BlackList)
whiteListPaths := a.compileExtendedPathSpec(apiVersionDef.ExtendedPaths.WhiteList, WhiteList)
cachedPaths := a.compileCachedPathSpec(apiVersionDef.ExtendedPaths.Cached)
transformPaths := a.compileTransformPathSpec(apiVersionDef.ExtendedPaths.Transform, Transformed)
transformResponsePaths := a.compileTransformPathSpec(apiVersionDef.ExtendedPaths.TransformResponse, TransformedResponse)
headerTransformPaths := a.compileInjectedHeaderSpec(apiVersionDef.ExtendedPaths.TransformHeader, HeaderInjected)
headerTransformPathsOnResponse := a.compileInjectedHeaderSpec(apiVersionDef.ExtendedPaths.TransformResponseHeader, HeaderInjectedResponse)
hardTimeouts := a.compileTimeoutPathSpec(apiVersionDef.ExtendedPaths.HardTimeouts, HardTimeout)
circuitBreakers := a.compileCircuitBreakerPathSpec(apiVersionDef.ExtendedPaths.CircuitBreaker, CircuitBreaker, apiSpec)
urlRewrites := a.compileURLRewritesPathSpec(apiVersionDef.ExtendedPaths.URLRewrite, URLRewrite)
virtualPaths := a.compileVirtualPathspathSpec(apiVersionDef.ExtendedPaths.Virtual, VirtualPath, apiSpec)
requestSizes := a.compileRequestSizePathSpec(apiVersionDef.ExtendedPaths.SizeLimit, RequestSizeLimit)
methodTransforms := a.compileMethodTransformSpec(apiVersionDef.ExtendedPaths.MethodTransforms, MethodTransformed)
trackedPaths := a.compileTrackedEndpointPathspathSpec(apiVersionDef.ExtendedPaths.TrackEndpoints, RequestTracked)
unTrackedPaths := a.compileUnTrackedEndpointPathspathSpec(apiVersionDef.ExtendedPaths.DoNotTrackEndpoints, RequestNotTracked)
combinedPath := []URLSpec{}
combinedPath = append(combinedPath, ignoredPaths...)
combinedPath = append(combinedPath, blackListPaths...)
combinedPath = append(combinedPath, whiteListPaths...)
combinedPath = append(combinedPath, cachedPaths...)
combinedPath = append(combinedPath, transformPaths...)
combinedPath = append(combinedPath, transformResponsePaths...)
combinedPath = append(combinedPath, headerTransformPaths...)
combinedPath = append(combinedPath, headerTransformPathsOnResponse...)
combinedPath = append(combinedPath, hardTimeouts...)
combinedPath = append(combinedPath, circuitBreakers...)
combinedPath = append(combinedPath, urlRewrites...)
combinedPath = append(combinedPath, requestSizes...)
combinedPath = append(combinedPath, virtualPaths...)
combinedPath = append(combinedPath, methodTransforms...)
combinedPath = append(combinedPath, trackedPaths...)
combinedPath = append(combinedPath, unTrackedPaths...)
return combinedPath, len(whiteListPaths) > 0
}
func (a *APISpec) Init(authStore, sessionStore, healthStore, orgStore StorageHandler) {
a.AuthManager.Init(authStore)
a.SessionManager.Init(sessionStore)
a.Health.Init(healthStore)
a.OrgSessionManager.Init(orgStore)
}
func (a *APISpec) getURLStatus(stat URLStatus) RequestStatus {
switch stat {
case Ignored:
return StatusOkAndIgnore
case BlackList:
return EndPointNotAllowed
case WhiteList:
return StatusOk
case Cached:
return StatusCached
case Transformed:
return StatusTransform
case HeaderInjected:
return StatusHeaderInjected
case HeaderInjectedResponse:
return StatusHeaderInjectedResponse
case TransformedResponse:
return StatusTransformResponse
case HardTimeout:
return StatusHardTimeout
case CircuitBreaker:
return StatusCircuitBreaker
case URLRewrite:
return StatusURLRewrite
case VirtualPath:
return StatusVirtualPath
case RequestSizeLimit:
return StatusRequestSizeControlled
case MethodTransformed:
return StatusMethodTransformed
case RequestTracked:
return StatusRequesTracked
case RequestNotTracked:
return StatusRequestNotTracked
default:
log.Error("URL Status was not one of Ignored, Blacklist or WhiteList! Blocking.")
return EndPointNotAllowed
}
}
// IsURLAllowedAndIgnored checks if a url is allowed and ignored.
func (a *APISpec) IsURLAllowedAndIgnored(r *http.Request, rxPaths []URLSpec, whiteListStatus bool) (RequestStatus, interface{}) {
// Check if ignored
for _, v := range rxPaths {
if !v.Spec.MatchString(strings.ToLower(r.URL.Path)) {
continue
}
if v.MethodActions != nil {
// We are using an extended path set, check for the method
methodMeta, matchMethodOk := v.MethodActions[r.Method]
if matchMethodOk {
// Matched the method, check what status it is:
if methodMeta.Action == apidef.NoAction {
// NoAction status means we're not treating this request in any special or exceptional way
return a.getURLStatus(v.Status), nil
}
// TODO: Extend here for additional reply options
switch methodMeta.Action {
case apidef.Reply:
return StatusRedirectFlowByReply, &methodMeta
default:
log.Error("URL Method Action was not set to NoAction, blocking.")
return EndPointNotAllowed, nil
}
}
if whiteListStatus {
// We have a whitelist, nothing gets through unless specifically defined
return EndPointNotAllowed, nil
}
// Method not matched in an extended set, means it can be passed through
return StatusOk, nil
}
if v.TransformAction.Template != nil {
return a.getURLStatus(v.Status), &v.TransformAction
}
// TODO: Fix, Not a great detection method
if len(v.InjectHeaders.Path) > 0 {
return a.getURLStatus(v.Status), &v.InjectHeaders
}
// Using a legacy path, handle it raw.
return a.getURLStatus(v.Status), nil
}
// Nothing matched - should we still let it through?
if whiteListStatus {
// We have a whitelist, nothing gets through unless specifically defined
return EndPointNotAllowed, nil
}
// No whitelist, but also not in any of the other lists, let it through and filter
return StatusOk, nil
}
// CheckSpecMatchesStatus checks if a url spec has a specific status
func (a *APISpec) CheckSpecMatchesStatus(r *http.Request, rxPaths []URLSpec, mode URLStatus) (bool, interface{}) {
// Check if ignored
for _, v := range rxPaths {
match := v.Spec.MatchString(r.URL.Path)
// only return it it's what we are looking for
if !match || mode != v.Status {
continue
}
switch v.Status {
case Ignored, BlackList, WhiteList, Cached:
return true, nil
case Transformed:
if r.Method == v.TransformAction.Method {
return true, &v.TransformAction
}
case HeaderInjected:
if r.Method == v.InjectHeaders.Method {
return true, &v.InjectHeaders
}
case HeaderInjectedResponse:
if r.Method == v.InjectHeadersResponse.Method {
return true, &v.InjectHeadersResponse
}
case TransformedResponse:
if r.Method == v.TransformResponseAction.Method {
return true, &v.TransformResponseAction
}
case HardTimeout:
if r.Method == v.HardTimeout.Method {
return true, &v.HardTimeout.TimeOut
}
case CircuitBreaker:
if r.Method == v.CircuitBreaker.Method {
return true, &v.CircuitBreaker
}
case URLRewrite:
if r.Method == v.URLRewrite.Method {
return true, &v.URLRewrite
}
case VirtualPath:
if r.Method == v.VirtualPathSpec.Method {
return true, &v.VirtualPathSpec
}
case RequestSizeLimit:
if r.Method == v.RequestSize.Method {
return true, &v.RequestSize
}
case MethodTransformed:
if r.Method == v.MethodTransform.Method {
return true, &v.MethodTransform
}
case RequestTracked:
if r.Method == v.TrackEndpoint.Method {
return true, &v.TrackEndpoint
}
case RequestNotTracked:
if r.Method == v.DoNotTrackEndpoint.Method {
return true, &v.DoNotTrackEndpoint
}
}
}
return false, nil
}
func (a *APISpec) getVersionFromRequest(r *http.Request) string {
switch a.VersionDefinition.Location {
case "header":
return r.Header.Get(a.VersionDefinition.Key)
case "url-param":
return r.URL.Query().Get(a.VersionDefinition.Key)
case "url":
url := strings.Replace(r.URL.Path, a.Proxy.ListenPath, "", 1)
if len(url) == 0 {
return ""
}
if url[:1] == "/" {
url = url[1:]
}
// Assume first param is the version ID
firstParamEndsAt := strings.Index(url, "/")
if firstParamEndsAt == -1 {
return ""
}
return url[:firstParamEndsAt]
}
return ""
}
// IsThisAPIVersionExpired checks if an API version (during a proxied
// request) is expired. If it isn't and the configured time was valid,
// it also returns the expiration time.
func (a *APISpec) IsThisAPIVersionExpired(versionDef *apidef.VersionInfo) (bool, *time.Time) {
// Never expires
if versionDef.Expires == "" || versionDef.Expires == "-1" {
return false, nil
}
// otherwise - calculate the time
t, err := time.Parse("2006-01-02 15:04", versionDef.Expires)
if err != nil {
log.Error("Could not parse expiry date for API, dissallow: ", err)
return true, nil
}
// It's in the past, expire
// It's in the future, keep going
return time.Since(t) >= 0, &t
}
// IsRequestValid will check if an incoming request has valid version
// data and return a RequestStatus that describes the status of the
// request
func (a *APISpec) IsRequestValid(r *http.Request) (bool, RequestStatus, interface{}) {
versionMetaData, versionPaths, whiteListStatus, stat := a.GetVersionData(r)
// Screwed up version info - fail and pass through
if stat != StatusOk {
return false, stat, nil
}
// Is the API version expired?
// TODO: Don't abuse the interface{} return value for both
// *apidef.EndpointMethodMeta and *time.Time. Probably need to
// redesign or entirely remove IsRequestValid. See discussion on
// https://github.com/TykTechnologies/tyk/pull/776
expired, expTime := a.IsThisAPIVersionExpired(versionMetaData)
if expired {