forked from coredns/policy
/
config.go
347 lines (281 loc) · 7.01 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
package themis
import (
"errors"
"math"
"strconv"
"strings"
"time"
"github.com/coredns/caddy"
)
var errInvalidOption = errors.New("invalid themis plugin option")
var allowedAttrTypes = map[string]string{
"string": "string",
"domain": "domain",
"address": "address"}
type config struct {
policyFile string
contentFiles []string
endpoints []string
options []*attrSetting
custAttrs map[string]custAttr
debugID string
debugSuffix string
streams int
hotSpot bool
connTimeout time.Duration
autoReqSize bool
maxReqSize int
autoResAttrs bool
maxResAttrs int
log bool
cacheTTL time.Duration
cacheLimit int
}
func themisParse(c *caddy.Controller) (*ThemisPlugin, error) {
tp := newThemisPlugin()
for c.Next() {
args := c.RemainingArgs()
if len(args) != 1 {
return nil, c.Errf("themis plugin should have the format : themis <EngineName> {...} ")
}
name := args[0]
if _, ok := tp.engines[name]; ok {
return nil, c.Errf("themis plugin with engine name %s is already declared", name)
}
p := newThemisEngine()
for c.NextBlock() {
if err := p.conf.parseOption(c); err != nil {
return nil, err
}
}
tp.engines[name] = p
}
return tp, nil
}
func (conf *config) parseOption(c *caddy.Controller) error {
switch c.Val() {
case "pdp":
return conf.parsePDP(c)
case "endpoint":
return conf.parseEndpoint(c)
case "attr":
return conf.parseAttr(c)
case "debug_query_suffix":
return conf.parseDebugQuerySuffix(c)
case "streams":
return conf.parseStreams(c)
case "transfer":
return conf.parseAttributes(c, custAttrTransfer)
case "metrics":
return conf.parseAttributes(c, custAttrMetrics)
case "debug_id":
return conf.parseDebugID(c)
case "connection_timeout":
return conf.parseConnectionTimeout(c)
case "log":
return conf.parseLog(c)
case "max_request_size":
return conf.parseMaxRequestSize(c)
case "max_response_attributes":
return conf.parseMaxResponseAttributes(c)
case "cache":
return conf.parseCache(c)
}
return errInvalidOption // TODO: anonymous error is lazy. Add invalid option name to error.
}
// Usage: pdp policy.[yaml|json] content1 content2...
func (conf *config) parsePDP(c *caddy.Controller) error {
args := c.RemainingArgs()
argsLen := len(args)
if argsLen < 1 {
return c.ArgErr()
}
conf.policyFile = args[0]
if argsLen > 1 {
conf.contentFiles = args[1:]
}
return nil
}
func (conf *config) parseEndpoint(c *caddy.Controller) error {
args := c.RemainingArgs()
if len(args) <= 0 {
return c.ArgErr()
}
conf.endpoints = args
return nil
}
func (conf *config) parseAttr(c *caddy.Controller) error {
args := c.RemainingArgs()
// Valid destTypes depend on PDP (default string).
argsLen := len(args)
if argsLen != 2 && argsLen != 3 {
return c.Errf("Invalid attr directive. Expected 2 or 3 arguments but got %d", argsLen)
}
name := args[0]
label := args[1]
dataType := "string"
if argsLen > 2 {
dataType = args[2]
}
if _, ok := allowedAttrTypes[strings.ToLower(dataType)]; !ok {
tp := make([]string, 0)
for k := range allowedAttrTypes {
tp = append(tp, k)
}
return c.Errf("invalid type %s for an attribute - allowed types are : %s", dataType, strings.Join(tp, ","))
}
conf.options = append(conf.options, &attrSetting{name, label, dataType, false})
conf.custAttrs[name] = conf.custAttrs[name] | custAttrEdns
return nil
}
func (conf *config) parseAttributes(c *caddy.Controller, a custAttr) error {
args := c.RemainingArgs()
if len(args) <= 0 {
return c.ArgErr()
}
for _, item := range args {
conf.custAttrs[item] = conf.custAttrs[item] | a
}
return nil
}
func (conf *config) parseStreams(c *caddy.Controller) error {
args := c.RemainingArgs()
if len(args) < 1 || len(args) > 2 {
return c.ArgErr()
}
streams, err := strconv.ParseInt(args[0], 10, 32)
if err != nil {
return c.Errf("Could not parse number of streams: %s", err)
}
if streams < 1 {
return c.Errf("Expected at least one stream got %d", streams)
}
conf.streams = int(streams)
if len(args) > 1 {
switch strings.ToLower(args[1]) {
default:
return c.Errf("Expected round-robin or hot-spot balancing but got %s", args[1])
case "round-robin":
conf.hotSpot = false
case "hot-spot":
conf.hotSpot = true
}
} else {
conf.hotSpot = false
}
return nil
}
func (conf *config) parseDebugQuerySuffix(c *caddy.Controller) error {
args := c.RemainingArgs()
if len(args) != 1 {
return c.ArgErr()
}
conf.debugSuffix = args[0]
return nil
}
func (conf *config) parseDebugID(c *caddy.Controller) error {
args := c.RemainingArgs()
if len(args) != 1 {
return c.ArgErr()
}
conf.debugID = args[0]
return nil
}
func (conf *config) parseConnectionTimeout(c *caddy.Controller) error {
args := c.RemainingArgs()
if len(args) != 1 {
return c.ArgErr()
}
if strings.ToLower(args[0]) == "no" {
conf.connTimeout = -1
} else {
timeout, err := time.ParseDuration(args[0])
if err != nil {
return c.Errf("Could not parse timeout: %s", err)
}
conf.connTimeout = timeout
}
return nil
}
func (conf *config) parseLog(c *caddy.Controller) error {
args := c.RemainingArgs()
if len(args) != 0 {
return c.ArgErr()
}
conf.log = true
return nil
}
func (conf *config) parseMaxRequestSize(c *caddy.Controller) error {
args := c.RemainingArgs()
if len(args) < 1 || len(args) > 2 {
return c.ArgErr()
}
s := ""
if strings.ToLower(args[0]) == "auto" {
conf.autoReqSize = true
if len(args) > 1 {
s = args[1]
}
} else {
s = args[0]
}
if len(s) > 0 {
size, err := strconv.ParseUint(s, 10, 0)
if err != nil {
return c.Errf("Could not parse PDP request size limit: %s", err)
}
if size > math.MaxInt32 {
return c.Errf("Size limit %d (> %d) for PDP request is too high", size, math.MaxInt32)
}
conf.maxReqSize = int(size)
}
return nil
}
func (conf *config) parseMaxResponseAttributes(c *caddy.Controller) error {
args := c.RemainingArgs()
if len(args) != 1 {
return c.ArgErr()
}
if strings.ToLower(args[0]) == "auto" {
conf.autoResAttrs = true
return nil
}
n, err := strconv.ParseUint(args[0], 10, 0)
if err != nil {
return c.Errf("Could not parse PDP response attributes limit: %s", err)
}
if n > math.MaxInt32 {
return c.Errf("Attributes limit %d (> %d) for PDP response is too high", n, math.MaxInt32)
}
conf.maxResAttrs = int(n)
return nil
}
func (conf *config) parseCache(c *caddy.Controller) error {
args := c.RemainingArgs()
if len(args) > 2 {
return c.ArgErr()
}
if len(args) > 0 {
ttl, err := time.ParseDuration(args[0])
if err != nil {
return c.Errf("Could not parse decision cache TTL: %s", err)
}
if ttl <= 0 {
return c.Errf("Can't set decision cache TTL to %s", ttl)
}
conf.cacheTTL = ttl
} else {
conf.cacheTTL = 10 * time.Minute
}
if len(args) > 1 {
n, err := strconv.ParseUint(args[1], 10, 0)
if err != nil {
return c.Errf("Could not parse decision cache limit: %s", err)
}
if n > math.MaxInt32 {
return c.Errf("Cache limit %d (> %d) is too high", n, math.MaxInt32)
}
conf.cacheLimit = int(n)
}
return nil
}