-
Notifications
You must be signed in to change notification settings - Fork 52
/
tcp_response_rule.go
579 lines (549 loc) · 18.5 KB
/
tcp_response_rule.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
// Copyright 2019 HAProxy Technologies
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package configuration
import (
"errors"
"strconv"
"strings"
"github.com/go-openapi/strfmt"
parser "github.com/haproxytech/config-parser/v5"
"github.com/haproxytech/config-parser/v5/common"
parser_errors "github.com/haproxytech/config-parser/v5/errors"
"github.com/haproxytech/config-parser/v5/parsers/actions"
tcp_actions "github.com/haproxytech/config-parser/v5/parsers/tcp/actions"
tcp_types "github.com/haproxytech/config-parser/v5/parsers/tcp/types"
"github.com/haproxytech/config-parser/v5/types"
"github.com/haproxytech/client-native/v5/misc"
"github.com/haproxytech/client-native/v5/models"
)
type TCPResponseRule interface {
GetTCPResponseRules(backend string, transactionID string) (int64, models.TCPResponseRules, error)
GetTCPResponseRule(id int64, backend string, transactionID string) (int64, *models.TCPResponseRule, error)
DeleteTCPResponseRule(id int64, backend string, transactionID string, version int64) error
CreateTCPResponseRule(backend string, data *models.TCPResponseRule, transactionID string, version int64) error
EditTCPResponseRule(id int64, backend string, data *models.TCPResponseRule, transactionID string, version int64) error
}
// GetTCPResponseRules returns configuration version and an array of
// configured tcp response rules in the specified backend. Returns error on fail.
func (c *client) GetTCPResponseRules(backend string, transactionID string) (int64, models.TCPResponseRules, error) {
p, err := c.GetParser(transactionID)
if err != nil {
return 0, nil, err
}
v, err := c.GetVersion(transactionID)
if err != nil {
return 0, nil, err
}
tcpRules, err := ParseTCPResponseRules(backend, p)
if err != nil {
return v, nil, c.HandleError("", "backend", backend, "", false, err)
}
return v, tcpRules, nil
}
// GetTCPResponseRule returns configuration version and a requested tcp response rule
// in the specified backend. Returns error on fail or if tcp response rule does not exist.
func (c *client) GetTCPResponseRule(id int64, backend string, transactionID string) (int64, *models.TCPResponseRule, error) {
p, err := c.GetParser(transactionID)
if err != nil {
return 0, nil, err
}
v, err := c.GetVersion(transactionID)
if err != nil {
return 0, nil, err
}
data, err := p.GetOne(parser.Backends, backend, "tcp-response", int(id))
if err != nil {
return v, nil, c.HandleError(strconv.FormatInt(id, 10), "backend", backend, "", false, err)
}
tcpRule, parseErr := ParseTCPResponseRule(data.(types.TCPType))
if parseErr != nil {
return 0, nil, parseErr
}
tcpRule.Index = &id
return v, tcpRule, nil
}
// DeleteTCPResponseRule deletes a tcp response rule in configuration. One of version or transactionID is
// mandatory. Returns error on fail, nil on success.
func (c *client) DeleteTCPResponseRule(id int64, backend string, transactionID string, version int64) error {
p, t, err := c.loadDataForChange(transactionID, version)
if err != nil {
return err
}
if err := p.Delete(parser.Backends, backend, "tcp-response", int(id)); err != nil {
return c.HandleError(strconv.FormatInt(id, 10), "backend", backend, t, transactionID == "", err)
}
return c.SaveData(p, t, transactionID == "")
}
// CreateTCPResponseRule creates a tcp response rule in configuration. One of version or transactionID is
// mandatory. Returns error on fail, nil on success.
func (c *client) CreateTCPResponseRule(backend string, data *models.TCPResponseRule, transactionID string, version int64) error {
if c.UseModelsValidation {
validationErr := data.Validate(strfmt.Default)
if validationErr != nil {
return NewConfError(ErrValidationError, validationErr.Error())
}
}
p, t, err := c.loadDataForChange(transactionID, version)
if err != nil {
return err
}
tcpRule, serializeErr := SerializeTCPResponseRule(*data)
if serializeErr != nil {
return serializeErr
}
if err := p.Insert(parser.Backends, backend, "tcp-response", tcpRule, int(*data.Index)); err != nil {
return c.HandleError(strconv.FormatInt(*data.Index, 10), "backend", backend, t, transactionID == "", err)
}
return c.SaveData(p, t, transactionID == "")
}
// EditTCPResponseRule edits a tcp response rule in configuration. One of version or transactionID is
// mandatory. Returns error on fail, nil on success.
func (c *client) EditTCPResponseRule(id int64, backend string, data *models.TCPResponseRule, transactionID string, version int64) error {
if c.UseModelsValidation {
validationErr := data.Validate(strfmt.Default)
if validationErr != nil {
return NewConfError(ErrValidationError, validationErr.Error())
}
}
p, t, err := c.loadDataForChange(transactionID, version)
if err != nil {
return err
}
if _, err := p.GetOne(parser.Backends, backend, "tcp-response", int(id)); err != nil {
return c.HandleError(strconv.FormatInt(*data.Index, 10), "backend", backend, t, transactionID == "", err)
}
tcpRule, serializeErr := SerializeTCPResponseRule(*data)
if serializeErr != nil {
return serializeErr
}
if err := p.Set(parser.Backends, backend, "tcp-response", tcpRule, int(id)); err != nil {
return c.HandleError(strconv.FormatInt(*data.Index, 10), "backend", backend, t, transactionID == "", err)
}
return c.SaveData(p, t, transactionID == "")
}
func ParseTCPResponseRules(backend string, p parser.Parser) (models.TCPResponseRules, error) {
tcpResRules := models.TCPResponseRules{}
data, err := p.Get(parser.Backends, backend, "tcp-response", false)
if err != nil {
if errors.Is(err, parser_errors.ErrFetch) {
return tcpResRules, nil
}
return nil, err
}
tRules, ok := data.([]types.TCPType)
if !ok {
return nil, misc.CreateTypeAssertError("tcp response")
}
for i, tRule := range tRules {
id := int64(i)
tcpResRule, parseErr := ParseTCPResponseRule(tRule)
if parseErr != nil {
return nil, parseErr
}
if tcpResRule != nil {
tcpResRule.Index = &id
tcpResRules = append(tcpResRules, tcpResRule)
}
}
return tcpResRules, nil
}
//nolint:maintidx,gocognit
func ParseTCPResponseRule(t types.TCPType) (*models.TCPResponseRule, error) {
switch v := t.(type) {
case *tcp_types.InspectDelay:
return &models.TCPResponseRule{
Type: models.TCPResponseRuleTypeInspectDashDelay,
Timeout: misc.ParseTimeout(v.Timeout),
}, nil
case *tcp_types.Content:
switch a := v.Action.(type) {
case *tcp_actions.Accept:
return &models.TCPResponseRule{
Type: models.TCPResponseRuleTypeContent,
Action: models.TCPRequestRuleActionAccept,
Cond: a.Cond,
CondTest: a.CondTest,
}, nil
case *actions.Reject:
return &models.TCPResponseRule{
Type: models.TCPResponseRuleTypeContent,
Action: models.TCPRequestRuleActionReject,
Cond: a.Cond,
CondTest: a.CondTest,
}, nil
case *actions.Lua:
return &models.TCPResponseRule{
Type: models.TCPResponseRuleTypeContent,
Action: models.TCPResponseRuleActionLua,
LuaAction: a.Action,
LuaParams: a.Params,
Cond: a.Cond,
CondTest: a.CondTest,
}, nil
case *actions.SetBandwidthLimit:
return &models.TCPResponseRule{
Type: models.TCPResponseRuleTypeContent,
Action: models.TCPRequestRuleActionSetDashBandwidthDashLimit,
BandwidthLimitName: a.Name,
BandwidthLimitLimit: a.Limit.String(),
BandwidthLimitPeriod: a.Period.String(),
Cond: a.Cond,
CondTest: a.CondTest,
}, nil
case *tcp_actions.Close:
return &models.TCPResponseRule{
Type: models.TCPResponseRuleTypeContent,
Action: models.TCPResponseRuleActionClose,
Cond: a.Cond,
CondTest: a.CondTest,
}, nil
case *actions.ScAddGpc:
if a.Int == nil && len(a.Expr.Expr) == 0 {
return nil, NewConfError(ErrValidationError, "sc-add-gpc int or expr has to be set")
}
if a.Int != nil && len(a.Expr.Expr) > 0 {
return nil, NewConfError(ErrValidationError, "sc-add-gpc int and expr are exclusive")
}
ID, _ := strconv.ParseInt(a.ID, 10, 64)
Idx, _ := strconv.ParseInt(a.Idx, 10, 64)
return &models.TCPResponseRule{
Type: models.TCPResponseRuleTypeContent,
Action: models.TCPResponseRuleActionScDashAddDashGpc,
ScID: ID,
ScIdx: Idx,
Expr: strings.Join(a.Expr.Expr, " "),
ScInt: a.Int,
Cond: a.Cond,
CondTest: a.CondTest,
}, nil
case *actions.ScIncGpc:
ID, _ := strconv.ParseInt(a.ID, 10, 64)
Idx, _ := strconv.ParseInt(a.Idx, 10, 64)
return &models.TCPResponseRule{
Type: models.TCPResponseRuleTypeContent,
Action: models.TCPResponseRuleActionScDashIncDashGpc,
ScID: ID,
ScIdx: Idx,
Cond: a.Cond,
CondTest: a.CondTest,
}, nil
case *actions.ScIncGpc0:
ID, _ := strconv.ParseInt(a.ID, 10, 64)
return &models.TCPResponseRule{
Type: models.TCPResponseRuleTypeContent,
Action: models.TCPResponseRuleActionScDashIncDashGpc0,
ScID: ID,
Cond: a.Cond,
CondTest: a.CondTest,
}, nil
case *actions.ScIncGpc1:
ID, _ := strconv.ParseInt(a.ID, 10, 64)
return &models.TCPResponseRule{
Type: models.TCPResponseRuleTypeContent,
Action: models.TCPResponseRuleActionScDashIncDashGpc1,
ScID: ID,
Cond: a.Cond,
CondTest: a.CondTest,
}, nil
case *actions.ScSetGpt:
if a.Int == nil && len(a.Expr.Expr) == 0 {
return nil, NewConfError(ErrValidationError, "sc-set-gpt: int or expr has to be set")
}
if a.Int != nil && len(a.Expr.Expr) > 0 {
return nil, NewConfError(ErrValidationError, "sc-set-gpt: int and expr are exclusive")
}
scID, err := strconv.ParseInt(a.ScID, 10, 64)
if err != nil {
return nil, NewConfError(ErrValidationError, "sc-set-gpt: failed to parse sc-id as an int")
}
return &models.TCPResponseRule{
Type: models.TCPResponseRuleTypeContent,
Action: models.TCPResponseRuleActionScDashSetDashGpt,
ScID: scID,
ScIdx: a.Idx,
Expr: strings.Join(a.Expr.Expr, " "),
ScInt: a.Int,
Cond: a.Cond,
CondTest: a.CondTest,
}, nil
case *actions.ScSetGpt0:
if a.Int == nil && len(a.Expr.Expr) == 0 {
return nil, NewConfError(ErrValidationError, "sc-set-gpt0 int or expr has to be set")
}
if a.Int != nil && len(a.Expr.Expr) > 0 {
return nil, NewConfError(ErrValidationError, "sc-set-gpt0 int and expr are exclusive")
}
ID, _ := strconv.ParseInt(a.ID, 10, 64)
return &models.TCPResponseRule{
Type: models.TCPResponseRuleTypeContent,
Action: models.TCPResponseRuleActionScDashSetDashGpt0,
ScID: ID,
Expr: strings.Join(a.Expr.Expr, " "),
ScInt: a.Int,
Cond: a.Cond,
CondTest: a.CondTest,
}, nil
case *actions.SendSpoeGroup:
return &models.TCPResponseRule{
Type: models.TCPResponseRuleTypeContent,
Action: models.TCPResponseRuleActionSendDashSpoeDashGroup,
SpoeEngine: a.Engine,
SpoeGroup: a.Group,
Cond: a.Cond,
CondTest: a.CondTest,
}, nil
case *actions.SetLogLevel:
return &models.TCPResponseRule{
Type: models.TCPResponseRuleTypeContent,
Action: models.TCPResponseRuleActionSetDashLogDashLevel,
LogLevel: a.Level,
Cond: a.Cond,
CondTest: a.CondTest,
}, nil
case *actions.SetMark:
return &models.TCPResponseRule{
Type: models.TCPResponseRuleTypeContent,
Action: models.TCPResponseRuleActionSetDashMark,
MarkValue: a.Value,
Cond: a.Cond,
CondTest: a.CondTest,
}, nil
case *actions.SetNice:
nice, err := strconv.ParseInt(a.Value, 10, 64)
if err != nil {
return nil, err
}
return &models.TCPResponseRule{
Type: models.TCPResponseRuleTypeContent,
Action: models.TCPResponseRuleActionSetDashNice,
NiceValue: nice,
Cond: a.Cond,
CondTest: a.CondTest,
}, nil
case *actions.SetTos:
return &models.TCPResponseRule{
Type: models.TCPResponseRuleTypeContent,
Action: models.TCPResponseRuleActionSetDashTos,
TosValue: a.Value,
Cond: a.Cond,
CondTest: a.CondTest,
}, nil
case *actions.SilentDrop:
return &models.TCPResponseRule{
Type: models.TCPResponseRuleTypeContent,
Action: models.TCPResponseRuleActionSilentDashDrop,
Cond: a.Cond,
CondTest: a.CondTest,
}, nil
case *actions.UnsetVar:
return &models.TCPResponseRule{
Type: models.TCPResponseRuleTypeContent,
Action: models.TCPResponseRuleActionUnsetDashVar,
VarName: a.Name,
VarScope: a.Scope,
Cond: a.Cond,
CondTest: a.CondTest,
}, nil
}
}
return nil, NewConfError(ErrValidationError, "invalid action")
}
//nolint:maintidx
func SerializeTCPResponseRule(t models.TCPResponseRule) (types.TCPType, error) {
switch t.Type {
case models.TCPResponseRuleTypeContent:
switch t.Action {
case models.TCPResponseRuleActionAccept:
return &tcp_types.Content{
Action: &tcp_actions.Accept{
Cond: t.Cond,
CondTest: t.CondTest,
},
}, nil
case models.TCPResponseRuleActionReject:
return &tcp_types.Content{
Action: &actions.Reject{
Cond: t.Cond,
CondTest: t.CondTest,
},
}, nil
case models.TCPResponseRuleActionLua:
return &tcp_types.Content{
Action: &actions.Lua{
Action: t.LuaAction,
Params: t.LuaParams,
Cond: t.Cond,
CondTest: t.CondTest,
},
}, nil
case models.TCPRequestRuleActionSetDashBandwidthDashLimit:
return &tcp_types.Content{
Action: &actions.SetBandwidthLimit{
Name: t.BandwidthLimitName,
Limit: common.Expression{Expr: strings.Split(t.BandwidthLimitLimit, " ")},
Period: common.Expression{Expr: strings.Split(t.BandwidthLimitPeriod, " ")},
Cond: t.Cond,
CondTest: t.CondTest,
},
}, nil
case models.TCPResponseRuleActionClose:
return &tcp_types.Content{
Action: &tcp_actions.Close{
Cond: t.Cond,
CondTest: t.CondTest,
},
}, nil
case models.TCPResponseRuleActionScDashAddDashGpc:
if len(t.Expr) > 0 && t.ScInt != nil {
return nil, NewConfError(ErrValidationError, "sc-add-gpc int and expr are exclusive")
}
if len(t.Expr) == 0 && t.ScInt == nil {
return nil, NewConfError(ErrValidationError, "sc-add-gpc int or expr has to be set")
}
return &tcp_types.Content{
Action: &actions.ScAddGpc{
ID: strconv.FormatInt(t.ScID, 10),
Idx: strconv.FormatInt(t.ScIdx, 10),
Int: t.ScInt,
Expr: common.Expression{Expr: strings.Split(t.Expr, " ")},
Cond: t.Cond,
CondTest: t.CondTest,
},
}, nil
case models.TCPResponseRuleActionScDashIncDashGpc:
return &tcp_types.Content{
Action: &actions.ScIncGpc{
ID: strconv.FormatInt(t.ScID, 10),
Idx: strconv.FormatInt(t.ScIdx, 10),
Cond: t.Cond,
CondTest: t.CondTest,
},
}, nil
case models.TCPResponseRuleActionScDashIncDashGpc0:
return &tcp_types.Content{
Action: &actions.ScIncGpc0{
ID: strconv.FormatInt(t.ScID, 10),
Cond: t.Cond,
CondTest: t.CondTest,
},
}, nil
case models.TCPResponseRuleActionScDashIncDashGpc1:
return &tcp_types.Content{
Action: &actions.ScIncGpc1{
ID: strconv.FormatInt(t.ScID, 10),
Cond: t.Cond,
CondTest: t.CondTest,
},
}, nil
case models.TCPResponseRuleActionScDashSetDashGpt:
if len(t.Expr) > 0 && t.ScInt != nil {
return nil, NewConfError(ErrValidationError, "sc-set-gpt: int and expr are exclusive")
}
if len(t.Expr) == 0 && t.ScInt == nil {
return nil, NewConfError(ErrValidationError, "sc-set-gpt: int or expr has to be set")
}
return &tcp_types.Content{
Action: &actions.ScSetGpt{
ScID: strconv.FormatInt(t.ScID, 10),
Idx: t.ScIdx,
Int: t.ScInt,
Expr: common.Expression{Expr: strings.Split(t.Expr, " ")},
Cond: t.Cond,
CondTest: t.CondTest,
},
}, nil
case models.TCPResponseRuleActionScDashSetDashGpt0:
if len(t.Expr) > 0 && t.ScInt != nil {
return nil, NewConfError(ErrValidationError, "sc-set-gpt0 int and expr are exclusive")
}
if len(t.Expr) == 0 && t.ScInt == nil {
return nil, NewConfError(ErrValidationError, "sc-set-gpt0 int or expr has to be set")
}
return &tcp_types.Content{
Action: &actions.ScSetGpt0{
ID: strconv.FormatInt(t.ScID, 10),
Int: t.ScInt,
Expr: common.Expression{Expr: strings.Split(t.Expr, " ")},
Cond: t.Cond,
CondTest: t.CondTest,
},
}, nil
case models.TCPResponseRuleActionSendDashSpoeDashGroup:
return &tcp_types.Content{
Action: &actions.SendSpoeGroup{
Engine: t.SpoeEngine,
Group: t.SpoeGroup,
Cond: t.Cond,
CondTest: t.CondTest,
},
}, nil
case models.TCPResponseRuleActionSetDashLogDashLevel:
return &tcp_types.Content{
Action: &actions.SetLogLevel{
Level: t.LogLevel,
Cond: t.Cond,
CondTest: t.CondTest,
},
}, nil
case models.TCPResponseRuleActionSetDashMark:
return &tcp_types.Content{
Action: &actions.SetMark{
Value: t.MarkValue,
Cond: t.Cond,
CondTest: t.CondTest,
},
}, nil
case models.TCPResponseRuleActionSetDashNice:
return &tcp_types.Content{
Action: &actions.SetNice{
Value: strconv.FormatInt(t.NiceValue, 10),
Cond: t.Cond,
CondTest: t.CondTest,
},
}, nil
case models.TCPResponseRuleActionSetDashTos:
return &tcp_types.Content{
Action: &actions.SetTos{
Value: t.TosValue,
Cond: t.Cond,
CondTest: t.CondTest,
},
}, nil
case models.TCPResponseRuleActionSilentDashDrop:
return &tcp_types.Content{
Action: &actions.SilentDrop{
Cond: t.Cond,
CondTest: t.CondTest,
},
}, nil
case models.TCPResponseRuleActionUnsetDashVar:
return &tcp_types.Content{
Action: &actions.UnsetVar{
Name: t.VarName,
Scope: t.VarScope,
Cond: t.Cond,
CondTest: t.CondTest,
},
}, nil
}
case models.TCPResponseRuleTypeInspectDashDelay:
if t.Timeout != nil {
return &tcp_types.InspectDelay{
Timeout: strconv.FormatInt(*t.Timeout, 10),
}, nil
}
}
return nil, NewConfError(ErrValidationError, "invalid action")
}