forked from signalsciences/sigsci-module-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
390 lines (344 loc) · 11.5 KB
/
config.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
package sigsci
import (
"errors"
"fmt"
"net"
"net/http"
"path/filepath"
"runtime"
"strings"
"time"
)
var (
// DefaultAllowUnknownContentLength is the default value
DefaultAllowUnknownContentLength = false
// DefaultAnomalyDuration is the default value
DefaultAnomalyDuration = 1 * time.Second
// DefaultAnomalySize is the default value
DefaultAnomalySize = int64(512 * 1024)
// DefaultDebug is the default value
DefaultDebug = false
// DefaultInspector is the default value
DefaultInspector = Inspector(nil)
// DefaultMaxContentLength is the default value
DefaultMaxContentLength = int64(100000)
// DefaultModuleIdentifier is the default value
DefaultModuleIdentifier = "sigsci-module-golang " + version
// DefaultRPCAddress is the default value
DefaultRPCAddress = "/var/run/sigsci.sock"
// DefaultRPCNetwork is the default value
DefaultRPCNetwork = "unix"
// DefaultTimeout is the default value
DefaultTimeout = 100 * time.Millisecond
// DefaultServerIdentifier is the default value
DefaultServerIdentifier = runtime.Version()
// DefaultServerFlavor is the default value
DefaultServerFlavor = ""
)
// RawHeaderExtractorFunc is a header extraction function
type RawHeaderExtractorFunc func(*http.Request) [][2]string
// ModuleConfig is a configuration object for a Module
type ModuleConfig struct {
allowUnknownContentLength bool
anomalyDuration time.Duration
anomalySize int64
expectedContentTypes []string
debug bool
rawHeaderExtractor RawHeaderExtractorFunc
inspector Inspector
inspInit InspectorInitFunc
inspFini InspectorFiniFunc
maxContentLength int64
moduleIdentifier string
rpcAddress string
rpcNetwork string
serverIdentifier string
serverFlavor string
timeout time.Duration
}
// NewModuleConfig returns an object with any options set
func NewModuleConfig(options ...ModuleConfigOption) (*ModuleConfig, error) {
c := &ModuleConfig{
allowUnknownContentLength: DefaultAllowUnknownContentLength,
anomalyDuration: DefaultAnomalyDuration,
anomalySize: DefaultAnomalySize,
expectedContentTypes: make([]string, 0),
debug: DefaultDebug,
inspector: DefaultInspector,
inspInit: nil,
inspFini: nil,
maxContentLength: DefaultMaxContentLength,
moduleIdentifier: DefaultModuleIdentifier,
rpcAddress: DefaultRPCAddress,
rpcNetwork: DefaultRPCNetwork,
serverIdentifier: DefaultServerIdentifier,
serverFlavor: DefaultServerFlavor,
timeout: DefaultTimeout,
}
if err := c.SetOptions(options...); err != nil {
return nil, err
}
return c, nil
}
// SetOptions sets options specified as functional arguments
func (c *ModuleConfig) SetOptions(options ...ModuleConfigOption) error {
for _, opt := range options {
if opt == nil {
continue
}
err := opt(c)
if err != nil {
return err
}
}
return nil
}
// IsBlockCode returns true if the code is a configured block code
func (c *ModuleConfig) IsBlockCode(code int) bool {
return code >= 300 && code <= 599
}
// IsAllowCode returns true if the code is an allow code
func (c *ModuleConfig) IsAllowCode(code int) bool {
return code == 200
}
// IsExpectedContentType returns true if the given content type string is
// in the list of configured custom Content-Types
func (c *ModuleConfig) IsExpectedContentType(s string) bool {
for _, ct := range c.expectedContentTypes {
if strings.HasPrefix(s, ct) {
return true
}
}
return false
}
// AllowUnknownContentLength returns the configuration value
func (c *ModuleConfig) AllowUnknownContentLength() bool {
return c.allowUnknownContentLength
}
// AltResponseCodes returns the configuration value
//
// Deprecated: The `AltResponseCodes` concept has
// been deprecated in favor of treating all response
// codes 300-599 as blocking codes. Due to
// this, this method will always return nil. It is left
// here to avoid breakage, but will eventually be removed.
func (c *ModuleConfig) AltResponseCodes() []int {
return nil
}
// AnomalyDuration returns the configuration value
func (c *ModuleConfig) AnomalyDuration() time.Duration {
return c.anomalyDuration
}
// AnomalySize returns the configuration value
func (c *ModuleConfig) AnomalySize() int64 {
return c.anomalySize
}
// ExpectedContentTypes returns the slice of additional custom content types
func (c *ModuleConfig) ExpectedContentTypes() []string {
return c.expectedContentTypes
}
// Debug returns the configuration value
func (c *ModuleConfig) Debug() bool {
return c.debug
}
// RawHeaderExtractor returns the configuration value
func (c *ModuleConfig) RawHeaderExtractor() RawHeaderExtractorFunc {
return c.rawHeaderExtractor
}
// Inspector returns the inspector
func (c *ModuleConfig) Inspector() Inspector {
return c.inspector
}
// InspectorInit returns the inspector init function
func (c *ModuleConfig) InspectorInit() InspectorInitFunc {
return c.inspInit
}
// InspectorFini returns the inspector fini function
func (c *ModuleConfig) InspectorFini() InspectorFiniFunc {
return c.inspFini
}
// MaxContentLength returns the configuration value
func (c *ModuleConfig) MaxContentLength() int64 {
return c.maxContentLength
}
// ModuleIdentifier returns the configuration value
func (c *ModuleConfig) ModuleIdentifier() string {
return c.moduleIdentifier
}
// RPCAddress returns the configuration value
func (c *ModuleConfig) RPCAddress() string {
return c.rpcAddress
}
// RPCNetwork returns the configuration value
func (c *ModuleConfig) RPCNetwork() string {
return c.rpcNetwork
}
// RPCAddressString returns the RPCNetwork/RPCAddress as a combined string
func (c *ModuleConfig) RPCAddressString() string {
return c.rpcNetwork + ":" + c.rpcAddress
}
// ServerIdentifier returns the configuration value
func (c *ModuleConfig) ServerIdentifier() string {
return c.serverIdentifier
}
// ServerFlavor returns the configuration value
func (c *ModuleConfig) ServerFlavor() string {
return c.serverFlavor
}
// Timeout returns the configuration value
func (c *ModuleConfig) Timeout() time.Duration {
return c.timeout
}
// Functional Config Options
// ModuleConfigOption is a functional config option for configuring the module
// See: https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis
type ModuleConfigOption func(*ModuleConfig) error
// AllowUnknownContentLength is a function argument to set the ability
// to read the body when the content length is not specified.
//
// NOTE: This can be dangerous (fill RAM) if set when the max content
//
// length is not limited by the server itself. This is intended
// for use with gRPC where the max message receive length is limited.
// Do NOT enable this if there is no limit set on the request
// content length!
func AllowUnknownContentLength(allow bool) ModuleConfigOption {
return func(c *ModuleConfig) error {
c.allowUnknownContentLength = allow
return nil
}
}
// AltResponseCodes is a function argument to set the alternative
// response codes from the agent that are considered "blocking"
//
// Deprecated: The `AltResponseCodes` concept has
// been deprecated in favor of treating all response
// codes 300-599 as blocking codes. Due to
// this, this method will always return nil. It is left
// here to avoid breakage, but will eventually be removed.
func AltResponseCodes(codes ...int) ModuleConfigOption {
return nil
}
// AnomalyDuration is a function argument to indicate when to send data
// to the inspector if the response was abnormally slow
func AnomalyDuration(dur time.Duration) ModuleConfigOption {
return func(c *ModuleConfig) error {
c.anomalyDuration = dur
return nil
}
}
// AnomalySize is a function argument to indicate when to send data to
// the inspector if the response was abnormally large
func AnomalySize(size int64) ModuleConfigOption {
return func(c *ModuleConfig) error {
c.anomalySize = size
return nil
}
}
// ExpectedContentType is a function argument that adds a custom Content-Type
// that should have request bodies sent to the agent for inspection
func ExpectedContentType(s string) ModuleConfigOption {
return func(c *ModuleConfig) error {
c.expectedContentTypes = append(c.expectedContentTypes, s)
return nil
}
}
// CustomInspector is a function argument that sets a custom inspector,
// an optional inspector initializer to decide if inspection should occur, and
// an optional inspector finalizer that can perform any post-inspection steps
func CustomInspector(insp Inspector, init InspectorInitFunc, fini InspectorFiniFunc) ModuleConfigOption {
return func(c *ModuleConfig) error {
c.inspector = insp
c.inspInit = init
c.inspFini = fini
return nil
}
}
// RawHeaderExtractor is a function argument that sets a function to extract
// an alternative header object from the request. It is primarily intended only
// for internal use.
func RawHeaderExtractor(fn RawHeaderExtractorFunc) ModuleConfigOption {
return func(c *ModuleConfig) error {
c.rawHeaderExtractor = fn
return nil
}
}
// Debug turns on debug logging
func Debug(enable bool) ModuleConfigOption {
return func(c *ModuleConfig) error {
c.debug = enable
return nil
}
}
// MaxContentLength is a function argument to set the maximum post
// body length that will be processed
func MaxContentLength(size int64) ModuleConfigOption {
return func(c *ModuleConfig) error {
c.maxContentLength = size
return nil
}
}
// Socket is a function argument to set where to send data to the
// Signal Sciences Agent. The network argument should be `unix`
// or `tcp` and the corresponding address should be either an absolute
// path or an `address:port`, respectively.
func Socket(network, address string) ModuleConfigOption {
return func(c *ModuleConfig) error {
switch network {
case "unix":
if !filepath.IsAbs(address) {
return errors.New(`address must be an absolute path for network="unix"`)
}
case "tcp":
if _, _, err := net.SplitHostPort(address); err != nil {
return fmt.Errorf(`address must be in "address:port" form for network="tcp": %s`, err)
}
default:
return errors.New(`network must be "tcp" or "unix"`)
}
c.rpcNetwork = network
c.rpcAddress = address
return nil
}
}
// Timeout is a function argument that sets the maximum time to wait until
// receiving a reply from the inspector. Once this timeout is reached, the
// module will fail open.
func Timeout(t time.Duration) ModuleConfigOption {
return func(c *ModuleConfig) error {
c.timeout = t
return nil
}
}
// ModuleIdentifier is a function argument that sets the module name
// and version for custom setups.
// The version should be a sem-version (e.g., "1.2.3")
func ModuleIdentifier(name, version string) ModuleConfigOption {
return func(c *ModuleConfig) error {
c.moduleIdentifier = name + " " + version
return nil
}
}
// ServerIdentifier is a function argument that sets the server
// identifier for custom setups
func ServerIdentifier(id string) ModuleConfigOption {
return func(c *ModuleConfig) error {
c.serverIdentifier = id
return nil
}
}
// ServerFlavor is a function argument that sets the server
// flavor for custom setups using revproxy.
func ServerFlavor(serverFlavor string) ModuleConfigOption {
return func(c *ModuleConfig) error {
c.serverFlavor = serverFlavor
return nil
}
}
// FromModuleConfig allow cloning the config
func FromModuleConfig(mcfg *ModuleConfig) ModuleConfigOption {
return func(c *ModuleConfig) error {
*c = *mcfg
return nil
}
}