forked from SpectoLabs/hoverfly
/
admin.go
417 lines (330 loc) · 9.76 KB
/
admin.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
package hoverfly
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
// static assets
_ "github.com/SpectoLabs/hoverfly/statik"
"github.com/rakyll/statik/fs"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/negroni"
"github.com/go-zoo/bone"
"github.com/gorilla/websocket"
"github.com/meatballhat/negroni-logrus"
)
// recordedRequests struct encapsulates payload data
type recordedRequests struct {
Data []Payload `json:"data"`
}
type recordsCount struct {
Count int `json:"count"`
}
type statsResponse struct {
Stats Stats `json:"stats"`
RecordsCount int `json:"recordsCount"`
}
type stateRequest struct {
Mode string `json:"mode"`
Destination string `json:"destination"`
}
type messageResponse struct {
Message string `json:"message"`
}
// StartAdminInterface - starts admin interface web server
func (d *DBClient) StartAdminInterface() {
go func() {
// starting admin interface
mux := getBoneRouter(*d)
n := negroni.Classic()
logLevel := log.ErrorLevel
if d.Cfg.Verbose {
logLevel = log.DebugLevel
}
n.Use(negronilogrus.NewCustomMiddleware(logLevel, &log.JSONFormatter{}, "admin"))
n.UseHandler(mux)
// admin interface starting message
log.WithFields(log.Fields{
"AdminPort": d.Cfg.AdminPort,
}).Info("Admin interface is starting...")
n.Run(fmt.Sprintf(":%s", d.Cfg.AdminPort))
}()
}
// getBoneRouter returns mux for admin interface
func getBoneRouter(d DBClient) *bone.Mux {
mux := bone.New()
mux.Get("/records", http.HandlerFunc(d.AllRecordsHandler))
mux.Delete("/records", http.HandlerFunc(d.DeleteAllRecordsHandler))
mux.Post("/records", http.HandlerFunc(d.ImportRecordsHandler))
mux.Get("/count", http.HandlerFunc(d.RecordsCount))
mux.Get("/stats", http.HandlerFunc(d.StatsHandler))
mux.Get("/statsws", http.HandlerFunc(d.StatsWSHandler))
mux.Get("/state", http.HandlerFunc(d.CurrentStateHandler))
mux.Post("/state", http.HandlerFunc(d.StateHandler))
if d.Cfg.Development {
// since hoverfly is not started from cmd/hoverfly/hoverfly
// we have to target to that directory
log.Warn("Hoverfly is serving files from /static/dist instead of statik binary!")
mux.Handle("/*", http.FileServer(http.Dir("../../static/dist")))
} else {
// preparing static assets for embedded admin
statikFS, err := fs.New()
if err != nil {
log.WithFields(log.Fields{
"Error": err.Error(),
}).Error("Failed to load statikFS, admin UI might not work :(")
}
mux.Handle("/*", http.FileServer(statikFS))
}
return mux
}
// AllRecordsHandler returns JSON content type http response
func (d *DBClient) AllRecordsHandler(w http.ResponseWriter, req *http.Request) {
records, err := d.Cache.GetAllRequests()
if err == nil {
w.Header().Set("Content-Type", "application/json")
var response recordedRequests
response.Data = records
b, err := json.Marshal(response)
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
w.Write(b)
return
}
} else {
log.WithFields(log.Fields{
"Error": err.Error(),
}).Error("Failed to get data from cache!")
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(500) // can't process this entity
return
}
}
// RecordsCount returns number of captured requests as a JSON payload
func (d *DBClient) RecordsCount(w http.ResponseWriter, req *http.Request) {
count, err := d.Cache.RecordsCount()
if err == nil {
w.Header().Set("Content-Type", "application/json")
var response recordsCount
response.Count = count
b, err := json.Marshal(response)
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
w.Write(b)
return
}
} else {
log.WithFields(log.Fields{
"Error": err.Error(),
}).Error("Failed to get data from cache!")
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(500) // can't process this entity
return
}
}
// StatsHandler - returns current stats about Hoverfly (request counts, record count)
func (d *DBClient) StatsHandler(w http.ResponseWriter, req *http.Request) {
stats := d.Counter.Flush()
count, err := d.Cache.RecordsCount()
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
var sr statsResponse
sr.Stats = stats
sr.RecordsCount = count
w.Header().Set("Content-Type", "application/json")
b, err := json.Marshal(sr)
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
w.Write(b)
return
}
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
// StatsWSHandler - returns current stats about Hoverfly (request counts, record count) through the websocket
func (d *DBClient) StatsWSHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
for {
messageType, p, err := conn.ReadMessage()
if err != nil {
return
}
log.WithFields(log.Fields{
"message": string(p),
}).Info("Got message...")
for _ = range time.Tick(1 * time.Second) {
count, err := d.Cache.RecordsCount()
if err != nil {
log.WithFields(log.Fields{
"message": p,
"error": err.Error(),
}).Error("got error while trying to get records count")
return
}
stats := d.Counter.Flush()
var sr statsResponse
sr.Stats = stats
sr.RecordsCount = count
b, err := json.Marshal(sr)
if err = conn.WriteMessage(messageType, b); err != nil {
log.WithFields(log.Fields{
"message": p,
"error": err.Error(),
}).Debug("Got error when writing message...")
return
}
}
}
}
// ImportRecordsHandler - accepts JSON payload and saves it to cache
func (d *DBClient) ImportRecordsHandler(w http.ResponseWriter, req *http.Request) {
var requests recordedRequests
defer req.Body.Close()
body, err := ioutil.ReadAll(req.Body)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
var response messageResponse
if err != nil {
// failed to read response body
log.WithFields(log.Fields{
"error": err.Error(),
}).Error("Could not read response body!")
response.Message = "Bad request. Nothing to import!"
http.Error(w, "Failed to read request body.", 400)
return
}
err = json.Unmarshal(body, &requests)
if err != nil {
w.WriteHeader(422) // can't process this entity
return
}
err = d.ImportPayloads(requests.Data)
if err != nil {
response.Message = err.Error()
w.WriteHeader(400)
} else {
response.Message = fmt.Sprintf("%d payloads import complete.", len(requests.Data))
}
b, err := json.Marshal(response)
w.Write(b)
}
// DeleteAllRecordsHandler - deletes all captured requests
func (d *DBClient) DeleteAllRecordsHandler(w http.ResponseWriter, req *http.Request) {
err := d.Cache.DeleteData()
var en Entry
en.ActionType = ActionTypeWipeDB
en.Message = "wipe"
en.Time = time.Now()
if err := d.Hooks.Fire(ActionTypeWipeDB, &en); err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
"message": en.Message,
"actionType": ActionTypeWipeDB,
}).Error("failed to fire hook")
}
w.Header().Set("Content-Type", "application/json")
var response messageResponse
if err != nil {
if err.Error() == "bucket not found" {
response.Message = fmt.Sprintf("No records found")
w.WriteHeader(200)
} else {
response.Message = fmt.Sprintf("Something went wrong: %s", err.Error())
w.WriteHeader(500)
}
} else {
response.Message = "Proxy cache deleted successfuly"
w.WriteHeader(200)
}
b, err := json.Marshal(response)
w.Write(b)
return
}
// CurrentStateHandler returns current state
func (d *DBClient) CurrentStateHandler(w http.ResponseWriter, req *http.Request) {
var resp stateRequest
resp.Mode = d.Cfg.GetMode()
resp.Destination = d.Cfg.Destination
b, _ := json.Marshal(resp)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.Write(b)
}
// StateHandler handles current proxy state
func (d *DBClient) StateHandler(w http.ResponseWriter, r *http.Request) {
var sr stateRequest
// this is mainly for testing, since when you create
if r.Body == nil {
r.Body = ioutil.NopCloser(bytes.NewBuffer([]byte("")))
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
// failed to read response body
log.WithFields(log.Fields{
"error": err.Error(),
}).Error("Could not read response body!")
http.Error(w, "Failed to read request body.", 400)
return
}
err = json.Unmarshal(body, &sr)
if err != nil {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(400) // can't process this entity
return
}
availableModes := map[string]bool{
"virtualize": true,
"capture": true,
"modify": true,
"synthesize": true,
}
if !availableModes[sr.Mode] {
log.WithFields(log.Fields{
"suppliedMode": sr.Mode,
}).Error("Wrong mode found, can't change state")
http.Error(w, "Bad mode supplied, available modes: virtualize, capture, modify, synthesize.", 400)
return
}
log.WithFields(log.Fields{
"newState": sr.Mode,
"body": string(body),
}).Info("Handling state change request!")
// setting new state
d.Cfg.SetMode(sr.Mode)
var en Entry
en.ActionType = ActionTypeConfigurationChanged
en.Message = "changed"
en.Time = time.Now()
en.Data = []byte("sr.Mode")
if err := d.Hooks.Fire(ActionTypeConfigurationChanged, &en); err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
"message": en.Message,
"actionType": ActionTypeConfigurationChanged,
}).Error("failed to fire hook")
}
var resp stateRequest
resp.Mode = d.Cfg.GetMode()
resp.Destination = d.Cfg.Destination
b, _ := json.Marshal(resp)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.Write(b)
}