Skip to content

Commit

Permalink
Fix coraza args and message names to match current expiremental branch
Browse files Browse the repository at this point in the history
  • Loading branch information
mac-chaffee committed Nov 21, 2022
1 parent 29fb6a2 commit a3e109f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/converters/ingress/types/defaults.go
Expand Up @@ -25,7 +25,7 @@ const (
// Ref: https://github.com/haproxy/spoa-modsecurity/blob/3c895f3e7dd291dba19d57ba054b277e6fb80ca4/README#L70
defaultModsecurityArgs = "unique-id method path query req.ver req.hdrs_bin req.body_size req.body"
// Ref: https://github.com/corazawaf/coraza-spoa/blob/179cf897147e165ccc1b8974888d8029737b7af5/doc/config/coraza.cfg#L14
DefaultCorazaArgs = "id=unique-id src-ip=src src-port=src_port dst-ip=dst dst-port=dst_port method=method path=path query=query version=req.ver headers=req.hdrs body=req.body"
DefaultCorazaArgs = "app=hdr(host) id=unique-id src-ip=src src-port=src_port dst-ip=dst dst-port=dst_port method=method path=path query=query version=req.ver headers=req.hdrs body=req.body"
)

func CreateDefaults() map[string]string {
Expand Down
23 changes: 20 additions & 3 deletions pkg/haproxy/instance_test.go
Expand Up @@ -4900,15 +4900,18 @@ func TestModSecurity(t *testing.T) {
endpoints: []string{"10.0.0.101:9000"},
backendExp: `
filter spoe engine modsecurity config /etc/haproxy/spoe-modsecurity.conf
http-request deny if { var(txn.coraza.fail) -m int eq 1 }`,
http-request deny deny_status 504 if { var(txn.coraza.error) -m int gt 0 }
http-request deny if !{ var(txn.coraza.fail) -m int eq 0 }`,
modsecExp: `
timeout connect 1s
timeout server 2s
server modsec-spoa0 10.0.0.101:9000`,
modsecOtherExp: `
messages coraza-req
option var-prefix coraza`,
modsecAgentExp: `
args id=unique-id src-ip=src src-port=src_port dst-ip=dst dst-port=dst_port method=method path=path query=query version=req.ver headers=req.hdrs body=req.body
spoe-message coraza-req
args app=hdr(host) id=unique-id src-ip=src src-port=src_port dst-ip=dst dst-port=dst_port method=method path=path query=query version=req.ver headers=req.hdrs body=req.body
event on-backend-http-request`,
useCoraza: true,
},
Expand Down Expand Up @@ -4952,7 +4955,19 @@ func TestModSecurity(t *testing.T) {
backend spoe-modsecurity
mode tcp` + test.modsecExp
}
c.checkConfig(`
if test.useCoraza {
c.checkConfig(`
<<global>>
<<defaults>>
unique-id-format %[uuid()]
backend d1_app_8080
mode http` + test.backendExp + `
server s1 172.17.0.11:8080 weight 100
<<backends-default>>
<<frontends-default>>
<<support>>` + modsec)
} else {
c.checkConfig(`
<<global>>
<<defaults>>
backend d1_app_8080
Expand All @@ -4961,6 +4976,8 @@ backend d1_app_8080
<<backends-default>>
<<frontends-default>>
<<support>>` + modsec)
}

if test.modsecAgentExp != "" {
c.containsText("spoe-modsecurity.conf", c.readConfig(c.tempdir+"/spoe-modsecurity.conf"), test.modsecAgentExp)
}
Expand Down
7 changes: 6 additions & 1 deletion rootfs/etc/templates/haproxy/haproxy.tmpl
Expand Up @@ -184,6 +184,10 @@ defaults
{{- if $global.Timeout.Tunnel }}
timeout tunnel {{ $global.Timeout.Tunnel }}
{{- end }}
{{- /* Coraza will crash if the unique-id isn't set, which requires us to set the format here */}}
{{- if $global.UseCoraza }}
unique-id-format %[uuid()]
{{- end }}
{{- range $snippet := $global.CustomDefaults }}
{{ $snippet }}
{{- end }}
Expand Down Expand Up @@ -583,7 +587,8 @@ backend {{ $backend.ID }}
{{- if eq $waf.Mode "deny" }}
{{- range $pathIDs := $wafCfg.PathIDs $i }}
{{- if $global.UseCoraza }}
http-request deny if { var(txn.coraza.fail) -m int eq 1 }
http-request deny deny_status 504 if { var(txn.coraza.error) -m int gt 0 }
http-request deny if !{ var(txn.coraza.fail) -m int eq 0 }
{{- else }}
http-request deny if { var(txn.modsec.code) -m int gt 0 }
{{- end }}
Expand Down
10 changes: 9 additions & 1 deletion rootfs/etc/templates/modsecurity/modsecurity.tmpl
Expand Up @@ -9,16 +9,24 @@
{{- $modsec := .Global.ModSecurity }}
[modsecurity]
spoe-agent modsecurity-agent
messages check-request
{{- if .Global.UseCoraza }}
messages coraza-req
option var-prefix coraza
{{- else }}
messages check-request
option var-prefix modsec
{{- end }}
timeout hello {{ $modsec.Timeout.Hello }}
timeout idle {{ $modsec.Timeout.Idle }}
timeout processing {{ $modsec.Timeout.Processing }}
use-backend spoe-modsecurity
log global
option dontlog-normal

{{- if .Global.UseCoraza }}
spoe-message coraza-req
{{- else }}
spoe-message check-request
{{- end }}
args {{ $modsec.Args | join " " }}
event on-backend-http-request

0 comments on commit a3e109f

Please sign in to comment.