1
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-16 21:56:40 -05:00

caddyhttp: Log error from CEL evaluation (fix #4832)

This commit is contained in:
Matthew Holt 2022-06-08 16:42:24 -06:00
parent 7f9b1f43c9
commit 1498132ea3
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5
2 changed files with 12 additions and 3 deletions

View file

@ -33,6 +33,7 @@ import (
"github.com/google/cel-go/common/types/traits" "github.com/google/cel-go/common/types/traits"
"github.com/google/cel-go/ext" "github.com/google/cel-go/ext"
"github.com/google/cel-go/interpreter/functions" "github.com/google/cel-go/interpreter/functions"
"go.uber.org/zap"
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1" exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
) )
@ -62,6 +63,8 @@ type MatchExpression struct {
expandedExpr string expandedExpr string
prg cel.Program prg cel.Program
ta ref.TypeAdapter ta ref.TypeAdapter
log *zap.Logger
} }
// CaddyModule returns the Caddy module information. // CaddyModule returns the Caddy module information.
@ -83,7 +86,9 @@ func (m *MatchExpression) UnmarshalJSON(data []byte) error {
} }
// Provision sets ups m. // Provision sets ups m.
func (m *MatchExpression) Provision(_ caddy.Context) error { func (m *MatchExpression) Provision(ctx caddy.Context) error {
m.log = ctx.Logger(m)
// replace placeholders with a function call - this is just some // replace placeholders with a function call - this is just some
// light (and possibly naïve) syntactic sugar // light (and possibly naïve) syntactic sugar
m.expandedExpr = placeholderRegexp.ReplaceAllString(m.Expr, placeholderExpansion) m.expandedExpr = placeholderRegexp.ReplaceAllString(m.Expr, placeholderExpansion)
@ -137,9 +142,13 @@ func (m *MatchExpression) Provision(_ caddy.Context) error {
// Match returns true if r matches m. // Match returns true if r matches m.
func (m MatchExpression) Match(r *http.Request) bool { func (m MatchExpression) Match(r *http.Request) bool {
out, _, _ := m.prg.Eval(map[string]interface{}{ out, _, err := m.prg.Eval(map[string]interface{}{
"request": celHTTPRequest{r}, "request": celHTTPRequest{r},
}) })
if err != nil {
m.log.Error("evaluating expression", zap.Error(err))
return false
}
if outBool, ok := out.Value().(bool); ok { if outBool, ok := out.Value().(bool); ok {
return outBool return outBool
} }

View file

@ -116,7 +116,7 @@ eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV
} }
if tt.expression.Match(req) != tt.wantResult { if tt.expression.Match(req) != tt.wantResult {
t.Errorf("MatchExpression.Match() expected to return '%t', for expression : '%s'", tt.wantResult, tt.expression) t.Errorf("MatchExpression.Match() expected to return '%t', for expression : '%s'", tt.wantResult, tt.expression.Expr)
} }
}) })