forked from speakeasy-api/speakeasy-auth-test-service
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfault.go
More file actions
161 lines (129 loc) · 4.36 KB
/
fault.go
File metadata and controls
161 lines (129 loc) · 4.36 KB
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
package middleware
import (
"encoding/json"
"net/http"
"sync"
"time"
"github.com/lingrino/go-fault"
)
type FaultSession struct {
// RequestCount is the number of requests that have been made on this session.
RequestCount int
// Exhausted is set to true when all faults on this session have been exercised.
Exhausted bool
Settings FaultSettings
}
// Describes the fault injection settings for a session. The fault chain is
// a series of fault injectors that are applied to the request in order. The
// order of faults is:
// - Delay
// - ConnectionClose
// - ConnectionReset
// - Reject
// - Error
type FaultSettings struct {
// Number of times to close the connection.
ConnectionCloseCount int `json:"connection_close_count"`
// ConnectionResetCount is the number of times to reset the connection.
ConnectionResetCount int `json:"connection_reset_count"`
// DelayMS is the number of milliseconds to delay the request.
DelayMS int64 `json:"delay_ms"`
// DelayCount is the number of times to delay the request.
DelayCount int `json:"delay_count"`
// RejectCount is the number of times to reject the request without a response.
// A value greater than 0 enables this fault injector.
RejectCount int `json:"reject_count"`
// ErrorCount is the number of times to return an error status code.
// A value greater than 0 enables this fault injector.
//
// NOTE: Error injection only takes effect after all rejections have
// resolved if both of these injectors are enabled.
ErrorCount int `json:"error_count"`
// ErrorCode is the status code to return when the error injector is enabled.
ErrorCode int `json:"error_code"`
}
func Fault(h http.Handler) http.Handler {
var sessions sync.Map
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
reqid := r.Header.Get("request-id")
if reqid == "" {
h.ServeHTTP(w, r)
return
}
settinghdr := r.Header.Get("fault-settings")
if settinghdr == "" {
h.ServeHTTP(w, r)
return
}
session := &FaultSession{}
asession, found := sessions.Load(reqid)
if found {
session = asession.(*FaultSession)
if session.Exhausted {
h.ServeHTTP(w, r)
return
}
}
var settings FaultSettings
err := json.Unmarshal([]byte(settinghdr), &settings)
if err != nil {
http.Error(w, "Invalid fault settings", http.StatusBadRequest)
return
}
reqCount := session.RequestCount
session.Settings = settings
session.RequestCount++
defer func() {
sessions.Store(reqid, session)
}()
var faults []fault.Injector
// Since multiple injectors can be enabled, need to count the number of
// requests based on prior injector counts
countOffset := 0
if settings.DelayMS > 0 && reqCount < settings.DelayCount {
inj, err := fault.NewSlowInjector(time.Millisecond * time.Duration(settings.DelayMS))
if err != nil {
http.Error(w, "Failed to build slow injector", http.StatusInternalServerError)
return
}
faults = append(faults, inj)
}
// Delay injector does not increase the count offset.
if settings.ConnectionCloseCount > 0 && reqCount < settings.ConnectionCloseCount+countOffset {
faults = append(faults, &ConnectionErrorInjector{})
}
countOffset += settings.ConnectionCloseCount
if settings.ConnectionResetCount > 0 && reqCount < settings.ConnectionResetCount+countOffset {
faults = append(faults, &ConnectionErrorInjector{Reset: true})
}
countOffset += settings.ConnectionResetCount
if settings.RejectCount > 0 && reqCount < settings.RejectCount+countOffset {
inj, err := fault.NewRejectInjector()
if err != nil {
http.Error(w, "Failed to build reject injector", http.StatusInternalServerError)
return
}
faults = append(faults, inj)
}
countOffset += settings.RejectCount
if settings.ErrorCode > 0 && reqCount < (settings.ErrorCount+countOffset) {
inj, err := fault.NewErrorInjector(settings.ErrorCode, fault.WithStatusText("Injected error"))
if err != nil {
http.Error(w, "Failed to build error injector", http.StatusInternalServerError)
return
}
faults = append(faults, inj)
}
if len(faults) == 0 {
session.Exhausted = true
h.ServeHTTP(w, r)
return
}
faultchain, err := fault.NewChainInjector(faults)
if err != nil {
http.Error(w, "Failed to build fault chain injector", http.StatusInternalServerError)
}
w.Header().Set("Faults-Enabled", "true")
faultchain.Handler(h).ServeHTTP(w, r)
})
}