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 b1058c2 commit d781019
Show file tree
Hide file tree
Showing 24 changed files with 362 additions and 65 deletions.
7 changes: 7 additions & 0 deletions configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ frontend test
http-request track-sc1 src table tr1 if TRUE
http-request track-sc2 src table tr2 if TRUE
http-request track-sc5 src table test if TRUE
http-request sc-set-gpt(1,2) hdr(Host),lower if FALSE
http-response allow if src 192.168.0.0/16
http-response set-header X-SSL %[ssl_fc]
http-response set-var(req.my_var) req.fhdr(user-agent),lower
Expand Down Expand Up @@ -451,6 +452,7 @@ frontend test
http-response set-timeout server 20
http-response set-timeout tunnel 20
http-response set-timeout client 20
http-response sc-set-gpt(1,2) 1234 if FALSE
http-after-response set-map(map.lst) %[src] %[res.hdr(X-Value)]
http-after-response del-map(map.lst) %[src] if FALSE
http-after-response del-acl(map.lst) %[src] if FALSE
Expand All @@ -467,6 +469,7 @@ frontend test
http-after-response set-status 503 reason "SlowDown"
http-after-response set-var(sess.last_redir) res.hdr(location)
http-after-response unset-var(sess.last_redir)
http-after-response sc-set-gpt(1,2) hdr(port) if FALSE
http-error status 400 content-type application/json lf-file /var/errors.file
tcp-request connection accept if TRUE
tcp-request connection reject if FALSE
Expand Down Expand Up @@ -502,6 +505,9 @@ frontend test
tcp-request session attach-srv srv2 name example.com
tcp-request session attach-srv srv3 if is_cached
tcp-request connection set-var-fmt(txn.ip_port) %%[dst]:%%[dst_port]
tcp-request connection sc-set-gpt(1,2) 1234 if FALSE
tcp-request content sc-set-gpt(1,2) hdr(port) if FALSE
tcp-request session sc-set-gpt(1,2) 1234
log global
no log
log 127.0.0.1:514 local0 notice notice
Expand Down Expand Up @@ -644,6 +650,7 @@ backend test
tcp-response content set-tos 2 if FALSE
tcp-response content silent-drop if FALSE
tcp-response content unset-var(req.my_var) if FALSE
tcp-response content sc-set-gpt(1,2) 1234
option contstats
timeout check 2s
timeout tunnel 5s
Expand Down
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
18 changes: 17 additions & 1 deletion configuration/http_after_response_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestGetHTTPAfterResponseRules(t *testing.T) {
t.Error(err.Error())
}

if expected := 16; len(hRules) != expected {
if expected := 17; len(hRules) != expected {
t.Errorf("%v http after response rules returned, expected %d", len(hRules), expected)
}

Expand Down Expand Up @@ -266,6 +266,22 @@ func TestGetHTTPAfterResponseRules(t *testing.T) {
if scope := "sess"; r.VarScope != scope {
t.Errorf("%v: VarName not %s: %v", *r.Index, scope, r.VarScope)
}
case 16:
if actionType := "sc-set-gpt"; r.Type != actionType {
t.Errorf("%v: Type not %s: %v", *r.Index, actionType, r.Type)
}
if scID := 1; r.ScID != int64(scID) {
t.Errorf("%v: sc-id not %d: %v", *r.Index, scID, r.ScID)
}
if scIdx := 2; r.ScIdx != int64(scIdx) {
t.Errorf("%v: sc-idx not %d: %v", *r.Index, scIdx, r.ScIdx)
}
if cond := "if"; r.Cond != cond {
t.Errorf("%v: cond not %s: %v", *r.Index, cond, r.Cond)
}
if condTest := "FALSE"; r.CondTest != condTest {
t.Errorf("%v: condtest not %s: %v", *r.Index, condTest, r.CondTest)
}
}
}

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 @@ -953,6 +973,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
22 changes: 16 additions & 6 deletions configuration/http_request_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func TestGetHTTPRequestRules(t *testing.T) { //nolint:gocognit,gocyclo
t.Error(err.Error())
}

if len(hRules) != 47 {
t.Errorf("%v http request rules returned, expected 47", len(hRules))
if len(hRules) != 48 {
t.Errorf("%v http request rules returned, expected 48", len(hRules))
}

if v != version {
Expand Down Expand Up @@ -735,8 +735,18 @@ func TestGetHTTPRequestRules(t *testing.T) { //nolint:gocognit,gocyclo
if r.CondTest != "TRUE" {
t.Errorf("%v: CondTest not TRUE: %v", *r.Index, r.CondTest)
}
case 47:
if r.Type != "sc-set-gpt" {
t.Errorf("%v: Type not sc-set-gpt: %v", *r.Index, r.Type)
}
if r.ScID != 1 {
t.Errorf("%v: sc-id not 1: %v", *r.Index, r.ScID)
}
if r.ScIdx != 2 {
t.Errorf("%v: sc-idx not 2: %v", *r.Index, r.ScIdx)
}
default:
t.Errorf("Expect only http-request 0 to 46, %v found", *r.Index)
t.Errorf("Expect only http-request 0 to 47, %v found", *r.Index)
}
}

Expand Down Expand Up @@ -898,7 +908,7 @@ func TestCreateEditDeleteHTTPRequestRule(t *testing.T) {
}

// TestDeleteHTTPRequest
err = clientTest.DeleteHTTPRequestRule(47, "frontend", "test", "", version)
err = clientTest.DeleteHTTPRequestRule(48, "frontend", "test", "", version)
if err != nil {
t.Error(err.Error())
} else {
Expand All @@ -909,9 +919,9 @@ func TestCreateEditDeleteHTTPRequestRule(t *testing.T) {
t.Error("Version not incremented")
}

_, _, err = clientTest.GetHTTPRequestRule(47, "frontend", "test", "")
_, _, err = clientTest.GetHTTPRequestRule(48, "frontend", "test", "")
if err == nil {
t.Error("DeleteHTTPRequestRule failed, HTTP Request Rule 47 still exists")
t.Error("DeleteHTTPRequestRule failed, HTTP Request Rule 48 still exists")
}

err = clientTest.DeleteHTTPRequestRule(2, "backend", "test_2", "", version)
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 @@ -538,7 +555,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 @@ -693,6 +710,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
22 changes: 16 additions & 6 deletions configuration/http_response_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func TestGetHTTPResponseRules(t *testing.T) { //nolint:gocognit,gocyclo
t.Error(err.Error())
}

if len(hRules) != 35 {
t.Errorf("%v http response rules returned, expected 35", len(hRules))
if len(hRules) != 36 {
t.Errorf("%v http response rules returned, expected 36", len(hRules))
}

if v != version {
Expand Down Expand Up @@ -559,8 +559,18 @@ func TestGetHTTPResponseRules(t *testing.T) { //nolint:gocognit,gocyclo
if r.Timeout != "20" {
t.Errorf("%v: Timeout not 20: %v", *r.Index, r.Timeout)
}
case 35:
if r.Type != "sc-set-gpt" {
t.Errorf("%v: Type not sc-set-gpt: %v", *r.Index, r.Type)
}
if r.ScID != 1 {
t.Errorf("%v: sc-id not 1: %v", *r.Index, r.ScID)
}
if r.ScIdx != 2 {
t.Errorf("%v: sc-idx not 2: %v", *r.Index, r.ScIdx)
}
default:
t.Errorf("Expect only http-response 0 to 34, %v found", *r.Index)
t.Errorf("Expect only http-response 0 to 35, %v found", *r.Index)
}
}

Expand Down Expand Up @@ -679,7 +689,7 @@ func TestCreateEditDeleteHTTPResponseRule(t *testing.T) {
}

// TestDeleteHTTPResponse
err = clientTest.DeleteHTTPResponseRule(35, "frontend", "test", "", version)
err = clientTest.DeleteHTTPResponseRule(36, "frontend", "test", "", version)
if err != nil {
t.Error(err.Error())
} else {
Expand All @@ -690,9 +700,9 @@ func TestCreateEditDeleteHTTPResponseRule(t *testing.T) {
t.Error("Version not incremented")
}

_, _, err = clientTest.GetHTTPResponseRule(35, "frontend", "test", "")
_, _, err = clientTest.GetHTTPResponseRule(36, "frontend", "test", "")
if err == nil {
t.Error("DeleteHTTPResponseRule failed, HTTPResponse Rule 35 still exists")
t.Error("DeleteHTTPResponseRule failed, HTTPResponse Rule 36 still exists")
}

err = clientTest.DeleteHTTPResponseRule(2, "backend", "test_2", "", version)
Expand Down
Loading

0 comments on commit d781019

Please sign in to comment.