Skip to content

Commit

Permalink
MINOR: add support for sc-set-gpt(sc-id,idx)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliwer committed Mar 27, 2024
1 parent 6c71a36 commit e914b0d
Show file tree
Hide file tree
Showing 20 changed files with 344 additions and 42 deletions.
34 changes: 33 additions & 1 deletion configuration/http_after_response_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func ParseHTTPAfterRules(t, pName string, p parser.Parser) (models.HTTPAfterResp
return httpResRules, nil
}

func ParseHTTPAfterRule(f types.Action) *models.HTTPAfterResponseRule {
func ParseHTTPAfterRule(f types.Action) *models.HTTPAfterResponseRule { //nolint:maintidx
switch v := f.(type) {
case *http_actions.AddHeader:
return &models.HTTPAfterResponseRule{
Expand Down Expand Up @@ -314,6 +314,23 @@ func ParseHTTPAfterRule(f types.Action) *models.HTTPAfterResponseRule {
Cond: v.Cond,
CondTest: v.CondTest,
}
case *actions.ScSetGpt:
if (v.Int == nil && len(v.Expr.Expr) == 0) || (v.Int != nil && len(v.Expr.Expr) > 0) {
return nil
}
scID, err := strconv.ParseInt(v.ScID, 10, 64)
if err != nil {
return nil
}
return &models.HTTPAfterResponseRule{
Type: "sc-set-gpt",
ScID: scID,
ScIdx: v.Idx,
ScExpr: strings.Join(v.Expr.Expr, " "),
ScInt: v.Int,
Cond: v.Cond,
CondTest: v.CondTest,
}
case *actions.ScSetGpt0:
if (v.Int == nil && len(v.Expr.Expr) == 0) || (v.Int != nil && len(v.Expr.Expr) > 0) {
return nil
Expand Down Expand Up @@ -468,6 +485,21 @@ func SerializeHTTPAfterRule(f models.HTTPAfterResponseRule) (rule types.Action,
Cond: f.Cond,
CondTest: f.CondTest,
}
case "sc-set-gpt":
if len(f.ScExpr) > 0 && f.ScInt != nil {
return nil, NewConfError(ErrValidationError, "sc-set-gpt: int and expr are exclusive")
}
if len(f.ScExpr) == 0 && f.ScInt == nil {
return nil, NewConfError(ErrValidationError, "sc-set-gpt: int or expr has to be set")
}
rule = &actions.ScSetGpt{
ScID: strconv.FormatInt(f.ScID, 10),
Idx: f.ScIdx,
Int: f.ScInt,
Expr: common.Expression{Expr: strings.Split(f.ScExpr, " ")},
Cond: f.Cond,
CondTest: f.CondTest,
}
case "sc-set-gpt0":
if len(f.ScExpr) > 0 && f.ScInt != nil {
return nil, NewConfError(ErrValidationError, "sc-set-gpt0 int and expr are exclusive")
Expand Down
37 changes: 36 additions & 1 deletion configuration/http_request_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func ParseHTTPRequestRules(t, pName string, p parser.Parser) (models.HTTPRequest
return httpReqRules, nil
}

func ParseHTTPRequestRule(f types.Action) (rule *models.HTTPRequestRule, err error) { //nolint:gocyclo,cyclop,maintidx
func ParseHTTPRequestRule(f types.Action) (rule *models.HTTPRequestRule, err error) { //nolint:gocyclo,cyclop,maintidx,gocognit
switch v := f.(type) {
case *http_actions.AddACL:
rule = &models.HTTPRequestRule{
Expand Down Expand Up @@ -473,6 +473,26 @@ func ParseHTTPRequestRule(f types.Action) (rule *models.HTTPRequestRule, err err
Cond: v.Cond,
CondTest: v.CondTest,
}
case *actions.ScSetGpt:
if v.Int == nil && len(v.Expr.Expr) == 0 {
return nil, NewConfError(ErrValidationError, "sc-set-gpt: int or expr has to be set")
}
if v.Int != nil && len(v.Expr.Expr) > 0 {
return nil, NewConfError(ErrValidationError, "sc-set-gpt: int and expr are exclusive")
}
scID, errp := strconv.ParseInt(v.ScID, 10, 64)
if errp != nil {
return nil, NewConfError(ErrValidationError, "sc-set-gpt: failed to parse sc-id an an int")
}
rule = &models.HTTPRequestRule{
Type: "sc-set-gpt",
ScID: scID,
ScIdx: v.Idx,
ScExpr: strings.Join(v.Expr.Expr, " "),
ScInt: v.Int,
Cond: v.Cond,
CondTest: v.CondTest,
}
case *actions.ScSetGpt0:
if v.Int == nil && len(v.Expr.Expr) == 0 {
return nil, NewConfError(ErrValidationError, "sc-set-gpt0 int or expr has to be set")
Expand Down Expand Up @@ -981,6 +1001,21 @@ func SerializeHTTPRequestRule(f models.HTTPRequestRule) (rule types.Action, err
Cond: f.Cond,
CondTest: f.CondTest,
}
case "sc-set-gpt":
if len(f.ScExpr) > 0 && f.ScInt != nil {
return nil, NewConfError(ErrValidationError, "sc-set-gpt: int and expr are exclusive")
}
if len(f.ScExpr) == 0 && f.ScInt == nil {
return nil, NewConfError(ErrValidationError, "sc-set-gpt: int or expr has to be set")
}
rule = &actions.ScSetGpt{
ScID: strconv.FormatInt(f.ScID, 10),
Idx: f.ScIdx,
Int: f.ScInt,
Expr: common.Expression{Expr: strings.Split(f.ScExpr, " ")},
Cond: f.Cond,
CondTest: f.CondTest,
}
case "sc-set-gpt0":
if len(f.ScExpr) > 0 && f.ScInt != nil {
return nil, NewConfError(ErrValidationError, "sc-set-gpt0 int and expr are exclusive")
Expand Down
34 changes: 33 additions & 1 deletion configuration/http_response_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,23 @@ func ParseHTTPResponseRule(f types.Action) *models.HTTPResponseRule { //nolint:m
Cond: v.Cond,
CondTest: v.CondTest,
}
case *actions.ScSetGpt:
if (v.Int == nil && len(v.Expr.Expr) == 0) || (v.Int != nil && len(v.Expr.Expr) > 0) {
return nil
}
scID, err := strconv.ParseInt(v.ScID, 10, 64)
if err != nil {
return nil
}
return &models.HTTPResponseRule{
Type: "sc-set-gpt",
ScID: scID,
ScIdx: v.Idx,
ScExpr: strings.Join(v.Expr.Expr, " "),
ScInt: v.Int,
Cond: v.Cond,
CondTest: v.CondTest,
}
case *actions.ScSetGpt0:
if (v.Int == nil && len(v.Expr.Expr) == 0) || (v.Int != nil && len(v.Expr.Expr) > 0) {
return nil
Expand Down Expand Up @@ -552,7 +569,7 @@ func ParseHTTPResponseRule(f types.Action) *models.HTTPResponseRule { //nolint:m
return nil
}

func SerializeHTTPResponseRule(f models.HTTPResponseRule) (rule types.Action, err error) { //nolint:gocyclo,ireturn,cyclop,maintidx
func SerializeHTTPResponseRule(f models.HTTPResponseRule) (rule types.Action, err error) { //nolint:gocyclo,ireturn,cyclop,maintidx,gocognit
switch f.Type {
case "add-acl":
rule = &http_actions.AddACL{
Expand Down Expand Up @@ -707,6 +724,21 @@ func SerializeHTTPResponseRule(f models.HTTPResponseRule) (rule types.Action, er
Cond: f.Cond,
CondTest: f.CondTest,
}
case "sc-set-gpt":
if len(f.ScExpr) > 0 && f.ScInt != nil {
return nil, NewConfError(ErrValidationError, "sc-set-gpt: int and expr are exclusive")
}
if len(f.ScExpr) == 0 && f.ScInt == nil {
return nil, NewConfError(ErrValidationError, "sc-set-gpt: int or expr has to be set")
}
rule = &actions.ScSetGpt{
ScID: strconv.FormatInt(f.ScID, 10),
Idx: f.ScIdx,
Int: f.ScInt,
Expr: common.Expression{Expr: strings.Split(f.ScExpr, " ")},
Cond: f.Cond,
CondTest: f.CondTest,
}
case "sc-set-gpt0":
if len(f.ScExpr) > 0 && f.ScInt != nil {
return nil, NewConfError(ErrValidationError, "sc-set-gpt0 int and expr are exclusive")
Expand Down
60 changes: 60 additions & 0 deletions configuration/tcp_request_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,14 @@ func ParseTCPRequestRule(f types.TCPType) (rule *models.TCPRequestRule, err erro
rule.ScIncID = a.ID
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *actions.ScSetGpt:
rule.Action = models.TCPRequestRuleActionScDashSetDashGpt
rule.ScIncID = a.ScID
rule.ScIdx = strconv.FormatInt(a.Idx, 10)
rule.ScInt = a.Int
rule.Expr = a.Expr.String()
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *actions.ScSetGpt0:
rule.Action = models.TCPRequestRuleActionScDashSetDashGpt0
rule.ScIncID = a.ID
Expand Down Expand Up @@ -446,6 +454,14 @@ func ParseTCPRequestRule(f types.TCPType) (rule *models.TCPRequestRule, err erro
rule.ScIncID = a.ID
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *actions.ScSetGpt:
rule.Action = models.TCPRequestRuleActionScDashSetDashGpt
rule.ScIncID = a.ScID
rule.ScIdx = strconv.FormatInt(a.Idx, 10)
rule.ScInt = a.Int
rule.Expr = a.Expr.String()
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *actions.ScSetGpt0:
rule.Action = models.TCPRequestRuleActionScDashSetDashGpt0
rule.ScIncID = a.ID
Expand Down Expand Up @@ -625,6 +641,14 @@ func ParseTCPRequestRule(f types.TCPType) (rule *models.TCPRequestRule, err erro
rule.ScIncID = a.ID
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *actions.ScSetGpt:
rule.Action = models.TCPRequestRuleActionScDashSetDashGpt
rule.ScIncID = a.ScID
rule.ScIdx = strconv.FormatInt(a.Idx, 10)
rule.ScInt = a.Int
rule.Expr = a.Expr.String()
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *actions.ScSetGpt0:
rule.Action = models.TCPRequestRuleActionScDashSetDashGpt0
rule.ScIncID = a.ID
Expand Down Expand Up @@ -828,6 +852,18 @@ func SerializeTCPRequestRule(f models.TCPRequestRule) (rule types.TCPType, err e
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionScDashSetDashGpt:
idx, _ := strconv.ParseInt(f.ScIdx, 10, 64)
return &tcp_types.Connection{
Action: &actions.ScSetGpt{
ScID: f.ScIncID,
Idx: idx,
Int: f.ScInt,
Expr: common.Expression{Expr: strings.Split(f.Expr, " ")},
Cond: f.Cond,
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionScDashSetDashGpt0:
return &tcp_types.Connection{
Action: &actions.ScSetGpt0{
Expand Down Expand Up @@ -1083,6 +1119,18 @@ func SerializeTCPRequestRule(f models.TCPRequestRule) (rule types.TCPType, err e
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionScDashSetDashGpt:
idx, _ := strconv.ParseInt(f.ScIdx, 10, 64)
return &tcp_types.Content{
Action: &actions.ScSetGpt{
ScID: f.ScIncID,
Idx: idx,
Int: f.ScInt,
Expr: common.Expression{Expr: strings.Split(f.Expr, " ")},
Cond: f.Cond,
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionScDashSetDashGpt0:
return &tcp_types.Content{
Action: &actions.ScSetGpt0{
Expand Down Expand Up @@ -1378,6 +1426,18 @@ func SerializeTCPRequestRule(f models.TCPRequestRule) (rule types.TCPType, err e
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionScDashSetDashGpt:
idx, _ := strconv.ParseInt(f.ScIdx, 10, 64)
return &tcp_types.Session{
Action: &actions.ScSetGpt{
ScID: f.ScIncID,
Idx: idx,
Int: f.ScInt,
Expr: common.Expression{Expr: strings.Split(f.Expr, " ")},
Cond: f.Cond,
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionScDashSetDashGpt0:
return &tcp_types.Session{
Action: &actions.ScSetGpt0{
Expand Down
48 changes: 43 additions & 5 deletions configuration/tcp_response_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func ParseTCPResponseRules(backend string, p parser.Parser) (models.TCPResponseR
return tcpResRules, nil
}

//nolint:maintidx
//nolint:maintidx,gocognit
func ParseTCPResponseRule(t types.TCPType) (*models.TCPResponseRule, error) {
switch v := t.(type) {
case *tcp_types.InspectDelay:
Expand Down Expand Up @@ -239,10 +239,10 @@ func ParseTCPResponseRule(t types.TCPType) (*models.TCPResponseRule, error) {
}, nil
case *actions.ScAddGpc:
if a.Int == nil && len(a.Expr.Expr) == 0 {
return nil, NewConfError(ErrValidationError, "sc-set-gpt0 int or expr has to be set")
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-set-gpt0 int and expr are exclusive")
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)
Expand Down Expand Up @@ -285,6 +285,27 @@ func ParseTCPResponseRule(t types.TCPType) (*models.TCPResponseRule, error) {
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")
Expand Down Expand Up @@ -448,10 +469,10 @@ func SerializeTCPResponseRule(t models.TCPResponseRule) (types.TCPType, error) {
}, nil
case models.TCPResponseRuleActionScDashAddDashGpc:
if len(t.Expr) > 0 && t.ScInt != nil {
return nil, NewConfError(ErrValidationError, "sc-set-gpt0 int and expr are exclusive")
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-set-gpt0 int or expr has to be set")
return nil, NewConfError(ErrValidationError, "sc-add-gpc int or expr has to be set")
}
return &tcp_types.Content{
Action: &actions.ScAddGpc{
Expand Down Expand Up @@ -488,6 +509,23 @@ func SerializeTCPResponseRule(t models.TCPResponseRule) (types.TCPType, error) {
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")
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/google/go-cmp v0.6.0
github.com/google/renameio v1.0.1
github.com/google/uuid v1.6.0
github.com/haproxytech/config-parser/v5 v5.1.1-0.20240228090858-f72c25c8ad69
github.com/haproxytech/config-parser/v5 v5.1.1-0.20240326140233-2ebfd8254626
github.com/json-iterator/go v1.1.12
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/mitchellh/mapstructure v1.5.0
Expand Down Expand Up @@ -40,6 +40,6 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
go.mongodb.org/mongo-driver v1.13.1 // indirect
golang.org/x/mod v0.15.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/sys v0.18.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit e914b0d

Please sign in to comment.