From 0cbf467b3fbde471e5ee67680fb98a9af2a9b16e Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Thu, 21 May 2020 18:19:01 -0600 Subject: [PATCH] caddyhttp: Add time.now placeholder and update cel-go (closes #2594) --- go.mod | 3 ++- go.sum | 4 ++-- modules/caddyhttp/celmatcher.go | 5 +++++ replacer.go | 2 ++ 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index c1e47a3ec..621426b63 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,8 @@ require ( github.com/go-acme/lego/v3 v3.7.0 github.com/go-chi/chi v4.1.1+incompatible github.com/gogo/protobuf v1.3.1 - github.com/google/cel-go v0.5.0 + github.com/golang/protobuf v1.3.4 + github.com/google/cel-go v0.5.1 github.com/jsternberg/zap-logfmt v1.2.0 github.com/klauspost/compress v1.10.5 github.com/klauspost/cpuid v1.2.4 diff --git a/go.sum b/go.sum index a6d8624e0..50236292e 100644 --- a/go.sum +++ b/go.sum @@ -286,8 +286,8 @@ github.com/golangci/revgrep v0.0.0-20180812185044-276a5c0a1039/go.mod h1:qOQCunE github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4/go.mod h1:Izgrg8RkN3rCIMLGE9CyYmU9pY2Jer6DgANEnZ/L/cQ= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= -github.com/google/cel-go v0.5.0 h1:mKsiw/KmT+VlDZnYFd2SE5ULrbe8RPlb0rHBem2Bv7I= -github.com/google/cel-go v0.5.0/go.mod h1:9SvtVVTtZV4DTB1/RuAD1D2HhuqEIdmZEE/r/lrFyKE= +github.com/google/cel-go v0.5.1 h1:oDsbtAwlwFPEcC8dMoRWNuVzWJUDeDZeHjoet9rXjTs= +github.com/google/cel-go v0.5.1/go.mod h1:9SvtVVTtZV4DTB1/RuAD1D2HhuqEIdmZEE/r/lrFyKE= github.com/google/cel-spec v0.4.0/go.mod h1:2pBM5cU4UKjbPDXBgwWkiwBsVgnxknuEJ7C5TDWwORQ= github.com/google/certificate-transparency-go v1.1.0/go.mod h1:i+Q7XY+ArBveOUT36jiHGfuSK1fHICIg6sUkRxPAbCs= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= diff --git a/modules/caddyhttp/celmatcher.go b/modules/caddyhttp/celmatcher.go index bede78f67..a22635292 100644 --- a/modules/caddyhttp/celmatcher.go +++ b/modules/caddyhttp/celmatcher.go @@ -21,10 +21,12 @@ import ( "reflect" "regexp" "strings" + "time" "github.com/caddyserver/caddy/v2" "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" "github.com/gogo/protobuf/proto" + "github.com/golang/protobuf/ptypes/timestamp" "github.com/google/cel-go/cel" "github.com/google/cel-go/checker/decls" "github.com/google/cel-go/common/types" @@ -204,6 +206,9 @@ func (celTypeAdapter) NativeToValue(value interface{}) ref.Val { switch v := value.(type) { case celHTTPRequest: return v + case time.Time: + // TODO: eliminate direct protobuf dependency, sigh -- just wrap stdlib time.Time instead... + return types.Timestamp{Timestamp: ×tamp.Timestamp{Seconds: v.Unix(), Nanos: int32(v.Nanosecond())}} case error: types.NewErr(v.Error()) } diff --git a/replacer.go b/replacer.go index eac208086..86cd72980 100644 --- a/replacer.go +++ b/replacer.go @@ -276,6 +276,8 @@ func globalDefaultReplacements(key string) (interface{}, bool) { return runtime.GOOS, true case "system.arch": return runtime.GOARCH, true + case "time.now": + return nowFunc(), true case "time.now.common_log": return nowFunc().Format("02/Jan/2006:15:04:05 -0700"), true case "time.now.year":