-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontroller.go
266 lines (245 loc) · 10.1 KB
/
controller.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
package speakeasy
import (
"context"
"net/http"
)
const (
DefaultStringMask = "__masked__"
DefaultNumberMask = "-12321"
)
type MaskingOption func(c *controller)
// WithQueryStringMask will mask the specified query strings with an optional mask string.
// If no mask is provided, the value will be masked with the default mask.
// If a single mask is provided, it will be used for all query strings.
// If the number of masks provided is equal to the number of query strings, masks will be used in order.
// Otherwise, the masks will be used in order until it they are exhausted. If the masks are exhausted, the default mask will be used.
// (defaults to "__masked__").
func WithQueryStringMask(keys []string, masks ...string) MaskingOption {
return func(c *controller) {
for i, key := range keys {
switch {
case len(masks) == 1:
c.queryStringMasks[key] = masks[0]
case len(masks) > i:
c.queryStringMasks[key] = masks[i]
default:
c.queryStringMasks[key] = DefaultStringMask
}
}
}
}
// WithRequestHeaderMask will mask the specified request headers with an optional mask string.
// If no mask is provided, the value will be masked with the default mask.
// If a single mask is provided, it will be used for all headers.
// If the number of masks provided is equal to the number of headers, masks will be used in order.
// Otherwise, the masks will be used in order until it they are exhausted. If the masks are exhausted, the default mask will be used.
// (defaults to "__masked__").
func WithRequestHeaderMask(headers []string, masks ...string) MaskingOption {
return func(c *controller) {
for i, header := range headers {
switch {
case len(masks) == 1:
c.requestHeaderMasks[header] = masks[0]
case len(masks) > i:
c.requestHeaderMasks[header] = masks[i]
default:
c.requestHeaderMasks[header] = DefaultStringMask
}
}
}
}
// WithResponseHeaderMask will mask the specified response headers with an optional mask string.
// If no mask is provided, the value will be masked with the default mask.
// If a single mask is provided, it will be used for all headers.
// If the number of masks provided is equal to the number of headers, masks will be used in order.
// Otherwise, the masks will be used in order until it they are exhausted. If the masks are exhausted, the default mask will be used.
// (defaults to "__masked__").
func WithResponseHeaderMask(headers []string, masks ...string) MaskingOption {
return func(c *controller) {
for i, header := range headers {
switch {
case len(masks) == 1:
c.responseHeaderMasks[header] = masks[0]
case len(masks) > i:
c.responseHeaderMasks[header] = masks[i]
default:
c.responseHeaderMasks[header] = DefaultStringMask
}
}
}
}
// WithRequestCookieMask will mask the specified request cookies with an optional mask string.
// If no mask is provided, the value will be masked with the default mask.
// If a single mask is provided, it will be used for all cookies.
// If the number of masks provided is equal to the number of cookies, masks will be used in order.
// Otherwise, the masks will be used in order until it they are exhausted. If the masks are exhausted, the default mask will be used.
// (defaults to "__masked__").
func WithRequestCookieMask(cookies []string, masks ...string) MaskingOption {
return func(c *controller) {
for i, cookie := range cookies {
switch {
case len(masks) == 1:
c.requestCookieMasks[cookie] = masks[0]
case len(masks) > i:
c.requestCookieMasks[cookie] = masks[i]
default:
c.requestCookieMasks[cookie] = DefaultStringMask
}
}
}
}
// WithResponseCookieMask will mask the specified response cookies with an optional mask string.
// If no mask is provided, the value will be masked with the default mask.
// If a single mask is provided, it will be used for all cookies.
// If the number of masks provided is equal to the number of cookies, masks will be used in order.
// Otherwise, the masks will be used in order until it they are exhausted. If the masks are exhausted, the default mask will be used.
// (defaults to "__masked__").
func WithResponseCookieMask(cookies []string, masks ...string) MaskingOption {
return func(c *controller) {
for i, cookie := range cookies {
switch {
case len(masks) == 1:
c.responseCookieMasks[cookie] = masks[0]
case len(masks) > i:
c.responseCookieMasks[cookie] = masks[i]
default:
c.responseCookieMasks[cookie] = DefaultStringMask
}
}
}
}
// WithRequestFieldMaskString will mask the specified request body fields with an optional mask. Supports string fields only. Matches using regex.
// If no mask is provided, the value will be masked with the default mask.
// If a single mask is provided, it will be used for all fields.
// If the number of masks provided is equal to the number of fields, masks will be used in order.
// Otherwise, the masks will be used in order until it they are exhausted. If the masks are exhausted, the default mask will be used.
// (defaults to "__masked__").
func WithRequestFieldMaskString(fields []string, masks ...string) MaskingOption {
return func(c *controller) {
for i, field := range fields {
switch {
case len(masks) == 1:
c.requestFieldMasksString[field] = masks[0]
case len(masks) > i:
c.requestFieldMasksString[field] = masks[i]
default:
c.requestFieldMasksString[field] = DefaultStringMask
}
}
}
}
// WithRequestFieldMaskNumber will mask the specified request body fields with an optional mask. Supports number fields only. Matches using regex.
// If no mask is provided, the value will be masked with the default mask.
// If a single mask is provided, it will be used for all fields.
// If the number of masks provided is equal to the number of fields, masks will be used in order.
// Otherwise, the masks will be used in order until it they are exhausted. If the masks are exhausted, the default mask will be used.
// (defaults to "-12321").
func WithRequestFieldMaskNumber(fields []string, masks ...string) MaskingOption {
return func(c *controller) {
for i, field := range fields {
switch {
case len(masks) == 1:
c.requestFieldMasksNumber[field] = masks[0]
case len(masks) > i:
c.requestFieldMasksNumber[field] = masks[i]
default:
c.requestFieldMasksNumber[field] = DefaultNumberMask
}
}
}
}
// WithResponseFieldMaskString will mask the specified response body with an optional mask. Supports string only. Matches using regex.
// If no mask is provided, the value will be masked with the default mask.
// If a single mask is provided, it will be used for all fields.
// If the number of masks provided is equal to the number of fields, masks will be used in order.
// Otherwise, the masks will be used in order until it they are exhausted. If the masks are exhausted, the default mask will be used.
// (defaults to "__masked__").
func WithResponseFieldMaskString(fields []string, masks ...string) MaskingOption {
return func(c *controller) {
for i, field := range fields {
switch {
case len(masks) == 1:
c.responseFieldMasksString[field] = masks[0]
case len(masks) > i:
c.responseFieldMasksString[field] = masks[i]
default:
c.responseFieldMasksString[field] = DefaultStringMask
}
}
}
}
// WithResponseFieldMaskNumber will mask the specified response body with an optional mask. Supports number fields only. Matches using regex.
// If no mask is provided, the value will be masked with the default mask.
// If a single mask is provided, it will be used for all fields.
// If the number of masks provided is equal to the number of fields, masks will be used in order.
// Otherwise, the masks will be used in order until it they are exhausted. If the masks are exhausted, the default mask will be used.
// (defaults to "-12321").
func WithResponseFieldMaskNumber(fields []string, masks ...string) MaskingOption {
return func(c *controller) {
for i, field := range fields {
switch {
case len(masks) == 1:
c.responseFieldMasksNumber[field] = masks[0]
case len(masks) > i:
c.responseFieldMasksNumber[field] = masks[i]
default:
c.responseFieldMasksNumber[field] = DefaultNumberMask
}
}
}
}
type contextKey int
const (
controllerKey contextKey = iota
)
type controller struct {
pathHint string
customerID string
queryStringMasks map[string]string
requestHeaderMasks map[string]string
requestCookieMasks map[string]string
requestFieldMasksString map[string]string
requestFieldMasksNumber map[string]string
responseHeaderMasks map[string]string
responseCookieMasks map[string]string
responseFieldMasksString map[string]string
responseFieldMasksNumber map[string]string
sdkInstance *Speakeasy
}
// MiddlewareController will return the speakeasy middleware controller from the current request,
// if the current request is monitored by the speakeasy middleware.
func MiddlewareController(r *http.Request) (*controller, bool) {
c, _ := r.Context().Value(controllerKey).(*controller)
return c, c != nil
}
// PathHint will allow you to provide a path hint for the current request.
func (c *controller) PathHint(pathHint string) {
c.pathHint = pathHint
}
// CustomerID will allow you to associate a customer ID with the current request.
func (c *controller) CustomerID(customerID string) {
c.customerID = customerID
}
func (c *controller) Masking(opts ...MaskingOption) {
for _, opt := range opts {
opt(c)
}
}
func (c *controller) GetSDKInstance() *Speakeasy {
return c.sdkInstance
}
func contextWithController(ctx context.Context, sdk *Speakeasy) (context.Context, *controller) {
c := &controller{
queryStringMasks: make(map[string]string),
requestHeaderMasks: make(map[string]string),
requestCookieMasks: make(map[string]string),
requestFieldMasksString: make(map[string]string),
requestFieldMasksNumber: make(map[string]string),
responseHeaderMasks: make(map[string]string),
responseCookieMasks: make(map[string]string),
responseFieldMasksString: make(map[string]string),
responseFieldMasksNumber: make(map[string]string),
sdkInstance: sdk,
}
return context.WithValue(ctx, controllerKey, c), c
}