forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_system.go
258 lines (221 loc) · 7.72 KB
/
event_system.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
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
"github.com/rubyist/circuitbreaker"
"gopkg.in/mgo.v2/bson"
"github.com/TykTechnologies/tyk/apidef"
)
// The name for event handlers as defined in the API Definition JSON/BSON format
const EH_LogHandler apidef.TykEventHandlerName = "eh_log_handler"
// Register new event types here, the string is the code used to hook at the Api Deifnititon JSON/BSON level
const (
EventQuotaExceeded apidef.TykEvent = "QuotaExceeded"
EventRateLimitExceeded apidef.TykEvent = "RatelimitExceeded"
EventAuthFailure apidef.TykEvent = "AuthFailure"
EventKeyExpired apidef.TykEvent = "KeyExpired"
EventVersionFailure apidef.TykEvent = "VersionFailure"
EventOrgQuotaExceeded apidef.TykEvent = "OrgQuotaExceeded"
EventTriggerExceeded apidef.TykEvent = "TriggerExceeded"
EventBreakerTriggered apidef.TykEvent = "BreakerTriggered"
EventHOSTDOWN apidef.TykEvent = "HostDown"
EventHOSTUP apidef.TykEvent = "HostUp"
EventTokenCreated apidef.TykEvent = "TokenCreated"
EventTokenUpdated apidef.TykEvent = "TokenUpdated"
EventTokenDeleted apidef.TykEvent = "TokenDeleted"
)
// EventMetaDefault is a standard embedded struct to be used with custom event metadata types, gives an interface for
// easily extending event metadata objects
type EventMetaDefault struct {
Message string
OriginatingRequest string
}
type EventHostStatusMeta struct {
EventMetaDefault
HostInfo HostHealthReport
}
// EventQuotaExceededMeta is the metadata structure for a quota exceeded event (EventQuotaExceeded)
type EventQuotaExceededMeta struct {
EventMetaDefault
Path string
Origin string
Key string
}
// EventRateLimitExceededMeta is the metadata structure for a rate limit exceeded event (EventRateLimitExceeded)
type EventRateLimitExceededMeta struct {
EventMetaDefault
Path string
Origin string
Key string
}
// EventAuthFailureMeta is the metadata structure for an auth failure (EventAuthFailure)
type EventAuthFailureMeta struct {
EventMetaDefault
Path string
Origin string
Key string
}
// EventCurcuitBreakerMeta is the event status for a circuit breaker tripping
type EventCurcuitBreakerMeta struct {
EventMetaDefault
Path string
APIID string
CircuitEvent circuit.BreakerEvent
}
// EventKeyExpiredMeta is the metadata structure for an auth failure (EventKeyExpired)
type EventKeyExpiredMeta struct {
EventMetaDefault
Path string
Origin string
Key string
}
// EventVersionFailureMeta is the metadata structure for an auth failure (EventKeyExpired)
type EventVersionFailureMeta struct {
EventMetaDefault
Path string
Origin string
Key string
Reason string
}
// EventVersionFailureMeta is the metadata structure for an auth failure (EventKeyExpired)
type EventTriggerExceededMeta struct {
EventMetaDefault
Org string
Key string
TriggerLimit int64
}
// EventVersionFailureMeta is the metadata structure for an auth failure (EventKeyExpired)
type EventTokenMeta struct {
EventMetaDefault
Org string
Key string
}
// EventMessage is a standard form to send event data to handlers
type EventMessage struct {
Type apidef.TykEvent
Meta interface{}
TimeStamp string
}
// TykEventHandler defines an event handler, e.g. LogMessageEventHandler will handle an event by logging it to stdout.
type TykEventHandler interface {
New(interface{}) (TykEventHandler, error)
HandleEvent(EventMessage)
}
// EncodeRequestToEvent will write the request out in wire protocol and
// encode it to base64 and store it in an Event object
func EncodeRequestToEvent(r *http.Request) string {
var asBytes bytes.Buffer
r.Write(&asBytes)
return base64.StdEncoding.EncodeToString(asBytes.Bytes())
}
// GetEventHandlerByName is a convenience function to get event handler instances from an API Definition
func GetEventHandlerByName(handlerConf apidef.EventHandlerTriggerConfig, spec *APISpec) (TykEventHandler, error) {
var conf interface{}
switch x := handlerConf.HandlerMeta.(type) {
case bson.M:
asByte, ok := json.Marshal(x)
if ok != nil {
log.Error("Failed to unmarshal handler meta! ", ok)
}
if err := json.Unmarshal(asByte, &conf); err != nil {
log.Error("Return conversion failed, ", err)
}
default:
conf = x
}
switch handlerConf.Handler {
case EH_LogHandler:
return (&LogMessageEventHandler{}).New(conf)
case EH_WebHook:
return (&WebHookHandler{}).New(conf)
case EH_JSVMHandler:
// Load the globals and file here
if spec != nil {
jsVmEventHandler, err := (&JSVMEventHandler{Spec: spec}).New(conf)
if err == nil {
GlobalEventsJSVM.LoadJSPaths([]string{conf.(map[string]interface{})["path"].(string)}, "")
}
return jsVmEventHandler, err
}
case EH_CoProcessHandler:
if spec != nil {
var coprocessEventHandler TykEventHandler
var err error
if GlobalDispatcher == nil {
err = errors.New("no CP available")
} else {
coprocessEventHandler, err = CoProcessEventHandler{Spec: spec}.New(conf)
}
return coprocessEventHandler, err
}
}
return nil, errors.New("Handler not found")
}
// FireEvent is added to the tykMiddleware object so it is available across the entire stack
func (t *TykMiddleware) FireEvent(name apidef.TykEvent, meta interface{}) {
fireEvent(name, meta, t.Spec.EventPaths)
}
func fireEvent(name apidef.TykEvent, meta interface{}, handlers map[apidef.TykEvent][]TykEventHandler) {
if handlers, e := handlers[name]; e {
eventMessage := EventMessage{
Meta: meta,
Type: name,
TimeStamp: time.Now().Local().String(),
}
for _, handler := range handlers {
go handler.HandleEvent(eventMessage)
}
}
}
func (s *APISpec) FireEvent(name apidef.TykEvent, meta interface{}) {
fireEvent(name, meta, s.EventPaths)
}
func FireSystemEvent(name apidef.TykEvent, meta interface{}) {
fireEvent(name, meta, config.EventTriggers)
}
// LogMessageEventHandler is a sample Event Handler
type LogMessageEventHandler struct {
conf map[string]interface{}
}
// New enables the intitialisation of event handler instances when they are created on ApiSpec creation
func (l *LogMessageEventHandler) New(handlerConf interface{}) (TykEventHandler, error) {
handler := &LogMessageEventHandler{}
handler.conf = handlerConf.(map[string]interface{})
return handler, nil
}
// HandleEvent will be fired when the event handler instance is found in an APISpec EventPaths object during a request chain
func (l *LogMessageEventHandler) HandleEvent(em EventMessage) {
formattedMsgString := fmt.Sprintf("%s:%s", l.conf["prefix"].(string), em.Type)
// We can handle specific event types easily
if em.Type == EventQuotaExceeded {
msgConf := em.Meta.(EventQuotaExceededMeta)
formattedMsgString = fmt.Sprintf("%s:%s:%s:%s", formattedMsgString, msgConf.Key, msgConf.Origin, msgConf.Path)
}
if em.Type == EventBreakerTriggered {
msgConf := em.Meta.(EventCurcuitBreakerMeta)
formattedMsgString = fmt.Sprintf("%s:%s:%s: [STATUS] %v", formattedMsgString, msgConf.APIID, msgConf.Path, msgConf.CircuitEvent)
}
log.Warning(formattedMsgString)
}
func InitGenericEventHandlers(theseEvents apidef.EventHandlerMetaConfig) map[apidef.TykEvent][]TykEventHandler {
actualEventHandlers := make(map[apidef.TykEvent][]TykEventHandler)
for eventName, eventHandlerConfs := range theseEvents.Events {
log.Debug("FOUND EVENTS TO INIT")
for _, handlerConf := range eventHandlerConfs {
log.Debug("CREATING EVENT HANDLERS")
eventHandlerInstance, err := GetEventHandlerByName(handlerConf, nil)
if err != nil {
log.Error("Failed to init event handler: ", err)
} else {
log.Debug("Init Event Handler: ", eventName)
actualEventHandlers[eventName] = append(actualEventHandlers[eventName], eventHandlerInstance)
}
}
}
return actualEventHandlers
}