Skip to content

Commit

Permalink
BUG/MINOR: http_after_response: add missing action
Browse files Browse the repository at this point in the history
  • Loading branch information
mjuraga committed Apr 30, 2024
1 parent 5d017d2 commit fc8ae8f
Show file tree
Hide file tree
Showing 12 changed files with 287 additions and 62 deletions.
97 changes: 63 additions & 34 deletions configuration/http_after_response_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ func (c *client) GetHTTPAfterResponseRule(id int64, parentType, parentName strin
return v, nil, c.HandleError(strconv.FormatInt(id, 10), parentType, parentName, "", false, err)
}

httpRule := ParseHTTPAfterRule(data.(types.Action))
httpRule, err := ParseHTTPAfterRule(data.(types.Action))
if err != nil {
return v, nil, err
}
httpRule.Index = &id

return v, httpRule, nil
Expand Down Expand Up @@ -208,16 +211,16 @@ func ParseHTTPAfterRules(t, pName string, p parser.Parser) (models.HTTPAfterResp
}
for i, r := range rules {
id := int64(i)
httpResRule := ParseHTTPAfterRule(r)
if httpResRule != nil {
httpResRule, err := ParseHTTPAfterRule(r)
if err == nil && httpResRule != nil {
httpResRule.Index = &id
httpResRules = append(httpResRules, httpResRule)
}
}
return httpResRules, nil
}

func ParseHTTPAfterRule(f types.Action) *models.HTTPAfterResponseRule { //nolint:maintidx
func ParseHTTPAfterRule(f types.Action) (*models.HTTPAfterResponseRule, error) { //nolint:maintidx,gocognit
switch v := f.(type) {
case *http_actions.AddHeader:
return &models.HTTPAfterResponseRule{
Expand All @@ -226,37 +229,54 @@ func ParseHTTPAfterRule(f types.Action) *models.HTTPAfterResponseRule { //nolint
HdrFormat: v.Fmt,
Cond: v.Cond,
CondTest: v.CondTest,
}
}, nil
case *http_actions.Allow:
return &models.HTTPAfterResponseRule{
Type: "allow",
Cond: v.Cond,
CondTest: v.CondTest,
}, nil
case *http_actions.Capture:
if (v.SlotID == nil && v.Len == nil) || (v.SlotID != nil && v.Len != nil) {
return nil, NewConfError(ErrValidationError, "capture len can't be zero")
}
rule := &models.HTTPAfterResponseRule{
Type: "capture",
CaptureSample: v.Sample,
Cond: v.Cond,
CondTest: v.CondTest,
}
if v.SlotID != nil {
rule.CaptureID = v.SlotID
}
if v.Len != nil {
rule.CaptureLen = *v.Len
}
return rule, nil
case *http_actions.DelACL:
return &models.HTTPAfterResponseRule{
Type: "del-acl",
ACLFile: v.FileName,
ACLKeyfmt: v.KeyFmt,
Cond: v.Cond,
CondTest: v.CondTest,
}
}, nil
case *http_actions.DelHeader:
return &models.HTTPAfterResponseRule{
Type: "del-header",
HdrName: v.Name,
Cond: v.Cond,
CondTest: v.CondTest,
HdrMethod: v.Method,
}
}, nil
case *http_actions.DelMap:
return &models.HTTPAfterResponseRule{
Type: "del-map",
MapFile: v.FileName,
MapKeyfmt: v.KeyFmt,
Cond: v.Cond,
CondTest: v.CondTest,
}
}, nil
case *http_actions.ReplaceHeader:
return &models.HTTPAfterResponseRule{
Type: "replace-header",
Expand All @@ -265,7 +285,7 @@ func ParseHTTPAfterRule(f types.Action) *models.HTTPAfterResponseRule { //nolint
HdrMatch: v.MatchRegex,
Cond: v.Cond,
CondTest: v.CondTest,
}
}, nil
case *http_actions.ReplaceValue:
return &models.HTTPAfterResponseRule{
Type: "replace-value",
Expand All @@ -274,20 +294,23 @@ func ParseHTTPAfterRule(f types.Action) *models.HTTPAfterResponseRule { //nolint
HdrMatch: v.MatchRegex,
Cond: v.Cond,
CondTest: v.CondTest,
}
}, nil
case *actions.ScAddGpc:
if v.Int == nil && len(v.Expr.Expr) == 0 {
return nil, NewConfError(ErrValidationError, "sc-add-gpc int or expr has to be set")
}
if v.Int != nil && len(v.Expr.Expr) > 0 {
return nil, NewConfError(ErrValidationError, "sc-add-gpc int and expr are exclusive")
}
ID, _ := strconv.ParseInt(v.ID, 10, 64)
Idx, _ := strconv.ParseInt(v.Idx, 10, 64)
if (v.Int == nil && len(v.Expr.Expr) == 0) || (v.Int != nil && len(v.Expr.Expr) > 0) {
return nil
}
return &models.HTTPAfterResponseRule{
Type: "sc-add-gpc",
ScID: ID,
ScIdx: Idx,
Cond: v.Cond,
CondTest: v.CondTest,
}
}, nil
case *actions.ScIncGpc:
ID, _ := strconv.ParseInt(v.ID, 10, 64)
Idx, _ := strconv.ParseInt(v.Idx, 10, 64)
Expand All @@ -297,30 +320,33 @@ func ParseHTTPAfterRule(f types.Action) *models.HTTPAfterResponseRule { //nolint
ScIdx: Idx,
Cond: v.Cond,
CondTest: v.CondTest,
}
}, nil
case *actions.ScIncGpc0:
ID, _ := strconv.ParseInt(v.ID, 10, 64)
return &models.HTTPAfterResponseRule{
Type: "sc-inc-gpc0",
ScID: ID,
Cond: v.Cond,
CondTest: v.CondTest,
}
}, nil
case *actions.ScIncGpc1:
ID, _ := strconv.ParseInt(v.ID, 10, 64)
return &models.HTTPAfterResponseRule{
Type: "sc-inc-gpc1",
ScID: ID,
Cond: v.Cond,
CondTest: v.CondTest,
}
}, nil
case *actions.ScSetGpt:
if (v.Int == nil && len(v.Expr.Expr) == 0) || (v.Int != nil && len(v.Expr.Expr) > 0) {
return nil
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, err := strconv.ParseInt(v.ScID, 10, 64)
if err != nil {
return nil
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")
}
return &models.HTTPAfterResponseRule{
Type: "sc-set-gpt",
Expand All @@ -330,10 +356,13 @@ func ParseHTTPAfterRule(f types.Action) *models.HTTPAfterResponseRule { //nolint
ScInt: v.Int,
Cond: v.Cond,
CondTest: v.CondTest,
}
}, nil
case *actions.ScSetGpt0:
if (v.Int == nil && len(v.Expr.Expr) == 0) || (v.Int != nil && len(v.Expr.Expr) > 0) {
return nil
if v.Int == nil && len(v.Expr.Expr) == 0 {
return nil, NewConfError(ErrValidationError, "sc-set-gpt0 int or expr has to be set")
}
if v.Int != nil && len(v.Expr.Expr) > 0 {
return nil, NewConfError(ErrValidationError, "sc-set-gpt0 int and expr are exclusive")
}
ID, _ := strconv.ParseInt(v.ID, 10, 64)
return &models.HTTPAfterResponseRule{
Expand All @@ -343,22 +372,22 @@ func ParseHTTPAfterRule(f types.Action) *models.HTTPAfterResponseRule { //nolint
ScInt: v.Int,
Cond: v.Cond,
CondTest: v.CondTest,
}
}, nil
case *http_actions.SetHeader:
return &models.HTTPAfterResponseRule{
Type: "set-header",
HdrName: v.Name,
HdrFormat: v.Fmt,
Cond: v.Cond,
CondTest: v.CondTest,
}
}, nil
case *actions.SetLogLevel:
return &models.HTTPAfterResponseRule{
Type: "set-log-level",
LogLevel: v.Level,
Cond: v.Cond,
CondTest: v.CondTest,
}
}, nil
case *http_actions.SetMap:
return &models.HTTPAfterResponseRule{
Type: "set-map",
Expand All @@ -367,7 +396,7 @@ func ParseHTTPAfterRule(f types.Action) *models.HTTPAfterResponseRule { //nolint
MapValuefmt: v.ValueFmt,
Cond: v.Cond,
CondTest: v.CondTest,
}
}, nil
case *http_actions.SetStatus:
status, _ := strconv.ParseInt(v.Status, 10, 64)
r := &models.HTTPAfterResponseRule{
Expand All @@ -379,7 +408,7 @@ func ParseHTTPAfterRule(f types.Action) *models.HTTPAfterResponseRule { //nolint
if status != 0 {
r.Status = status
}
return r
return r, nil
case *actions.SetVar:
return &models.HTTPAfterResponseRule{
Type: "set-var",
Expand All @@ -388,24 +417,24 @@ func ParseHTTPAfterRule(f types.Action) *models.HTTPAfterResponseRule { //nolint
VarScope: v.VarScope,
Cond: v.Cond,
CondTest: v.CondTest,
}
}, nil
case *http_actions.StrictMode:
return &models.HTTPAfterResponseRule{
Type: "strict-mode",
StrictMode: v.Mode,
Cond: v.Cond,
CondTest: v.CondTest,
}
}, nil
case *actions.UnsetVar:
return &models.HTTPAfterResponseRule{
Type: "unset-var",
VarName: v.Name,
VarScope: v.Scope,
Cond: v.Cond,
CondTest: v.CondTest,
}
}, nil
}
return nil
return nil, nil //nolint:nilnil
}

func SerializeHTTPAfterRule(f models.HTTPAfterResponseRule) (rule types.Action, err error) { //nolint:ireturn
Expand Down
2 changes: 1 addition & 1 deletion 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.20240419150429-e4dc867964ef
github.com/haproxytech/config-parser/v5 v5.1.1-0.20240430110427-8eea621717fd
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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ github.com/google/renameio v1.0.1 h1:Lh/jXZmvZxb0BBeSY5VKEfidcbcbenKjZFzM/q0fSeU
github.com/google/renameio v1.0.1/go.mod h1:t/HQoYBZSsWSNK35C6CO/TpPLDVWvxOHboWUAweKUpk=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/haproxytech/config-parser/v5 v5.1.1-0.20240419150429-e4dc867964ef h1:3GGRU4hQ+3B0l9ug386A0Zc+5KKGvjQoSyqQWaRViLc=
github.com/haproxytech/config-parser/v5 v5.1.1-0.20240419150429-e4dc867964ef/go.mod h1:T2iWRfXW84pxyk3PlYresm1ntHodIWgBN6ZxzB5Yqow=
github.com/haproxytech/config-parser/v5 v5.1.1-0.20240430110427-8eea621717fd h1:CnSc8gxb8FTAFCYB74lHjl0p1ohzQAOMWrSO5cRO7RE=
github.com/haproxytech/config-parser/v5 v5.1.1-0.20240430110427-8eea621717fd/go.mod h1:T2iWRfXW84pxyk3PlYresm1ntHodIWgBN6ZxzB5Yqow=
github.com/haproxytech/go-logger v1.1.0 h1:HgGtYaI1ApkvbQdsm7f9AzQQoxTB7w37criTflh7IQE=
github.com/haproxytech/go-logger v1.1.0/go.mod h1:OekUd8HCb7ubxMplzHUPBTHNxZmddOWfOjWclZsqIeM=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
Expand Down
42 changes: 39 additions & 3 deletions models/http_after_response_rule.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fc8ae8f

Please sign in to comment.