Skip to content

Commit

Permalink
Wire up error logs (envoyproxy#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga authored Aug 30, 2022
1 parent 81b43cd commit 9b73e0b
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
23 changes: 23 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (ctx *corazaPlugin) OnPluginStart(pluginConfigurationSize int) types.OnPlug

// First we initialize our waf and our seclang parser
waf := coraza.NewWaf()
waf.SetErrorLogCb(logError)

// TinyGo compilation will prevent buffering request body to files anyways, so this is
// effectively no-op but make clear our expectations.
Expand Down Expand Up @@ -270,3 +271,25 @@ func (ctx *httpContext) handleInterruption(interruption *ctypes.Interruption) {
panic(err)
}
}

func logError(error ctypes.MatchedRule) {
msg := error.ErrorLog(0)
switch error.Rule.Severity {
case ctypes.RuleSeverityEmergency:
proxywasm.LogCritical(msg)
case ctypes.RuleSeverityAlert:
proxywasm.LogCritical(msg)
case ctypes.RuleSeverityCritical:
proxywasm.LogCritical(msg)
case ctypes.RuleSeverityError:
proxywasm.LogError(msg)
case ctypes.RuleSeverityWarning:
proxywasm.LogWarn(msg)
case ctypes.RuleSeverityNotice:
proxywasm.LogInfo(msg)
case ctypes.RuleSeverityInfo:
proxywasm.LogInfo(msg)
case ctypes.RuleSeverityDebug:
proxywasm.LogDebug(msg)
}
}
92 changes: 92 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,98 @@ func TestEmptyBody(t *testing.T) {
})
}

func TestLogError(t *testing.T) {
reqHdrs := [][2]string{
{":path", "/hello"},
{":method", "GET"},
{":authority", "localhost"},
{"X-CRS-Test", "for the win!"},
}

tests := []struct {
severity int
logs func(host proxytest.HostEmulator) []string
}{
{
severity: 0,
logs: func(host proxytest.HostEmulator) []string {
return host.GetCriticalLogs()
},
},
{
severity: 1,
logs: func(host proxytest.HostEmulator) []string {
return host.GetCriticalLogs()
},
},
{
severity: 2,
logs: func(host proxytest.HostEmulator) []string {
return host.GetCriticalLogs()
},
},
{
severity: 3,
logs: func(host proxytest.HostEmulator) []string {
return host.GetErrorLogs()
},
},
{
severity: 4,
logs: func(host proxytest.HostEmulator) []string {
return host.GetWarnLogs()
},
},
{
severity: 5,
logs: func(host proxytest.HostEmulator) []string {
return host.GetInfoLogs()
},
},
{
severity: 6,
logs: func(host proxytest.HostEmulator) []string {
return host.GetInfoLogs()
},
},
{
severity: 7,
logs: func(host proxytest.HostEmulator) []string {
return host.GetDebugLogs()
},
},
}
vmTest(t, func(t *testing.T, vm types.VMContext) {
for _, tc := range tests {
tt := tc
t.Run(fmt.Sprintf("%d", tt.severity), func(t *testing.T) {
conf := fmt.Sprintf(`
{
"rules" : "SecRule REQUEST_HEADERS:X-CRS-Test \"@rx ^.*$\" \"id:999999,phase:1,log,severity:%d,msg:'%%{MATCHED_VAR}',pass,t:none\""
}
`, tt.severity)

opt := proxytest.
NewEmulatorOption().
WithVMContext(vm).
WithPluginConfiguration([]byte(strings.TrimSpace(conf)))

host, reset := proxytest.NewHostEmulator(opt)
defer reset()

require.Equal(t, types.OnPluginStartStatusOK, host.StartPlugin())

id := host.InitializeHttpContext()
action := host.CallOnRequestHeaders(id, reqHdrs, false)
require.Equal(t, types.ActionContinue, action)

logs := strings.Join(tt.logs(host), "\n")
require.Contains(t, logs, "for the win!")
})
}
})
}

func vmTest(t *testing.T, f func(*testing.T, types.VMContext)) {
t.Helper()

Expand Down

0 comments on commit 9b73e0b

Please sign in to comment.