Skip to content

Commit

Permalink
caddyfile: Populate regexp matcher names by default (#6145)
Browse files Browse the repository at this point in the history
* caddyfile: Populate regexp matcher names by default

* Some lint cleanup that my VSCode complained about

* Pass down matcher name through expression matcher

* Compat with #6113: fix adapt test, set both styles in replacer
  • Loading branch information
francislavoie authored Apr 17, 2024
1 parent e0daa39 commit 9cd472c
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 27 deletions.
34 changes: 34 additions & 0 deletions caddyconfig/caddyfile/dispenser.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ type Dispenser struct {
tokens []Token
cursor int
nesting int

// A map of arbitrary context data that can be used
// to pass through some information to unmarshalers.
context map[string]any
}

// NewDispenser returns a Dispenser filled with the given tokens.
Expand Down Expand Up @@ -454,6 +458,34 @@ func (d *Dispenser) DeleteN(amount int) []Token {
return d.tokens
}

// SetContext sets a key-value pair in the context map.
func (d *Dispenser) SetContext(key string, value any) {
if d.context == nil {
d.context = make(map[string]any)
}
d.context[key] = value
}

// GetContext gets the value of a key in the context map.
func (d *Dispenser) GetContext(key string) any {
if d.context == nil {
return nil
}
return d.context[key]
}

// GetContextString gets the value of a key in the context map
// as a string, or an empty string if the key does not exist.
func (d *Dispenser) GetContextString(key string) string {
if d.context == nil {
return ""
}
if val, ok := d.context[key].(string); ok {
return val
}
return ""
}

// isNewLine determines whether the current token is on a different
// line (higher line number) than the previous token. It handles imported
// tokens correctly. If there isn't a previous token, it returns true.
Expand Down Expand Up @@ -485,3 +517,5 @@ func (d *Dispenser) isNextOnNewLine() bool {
next := d.tokens[d.cursor+1]
return isNextOnNewLine(curr, next)
}

const MatcherNameCtxKey = "matcher_name"
10 changes: 9 additions & 1 deletion caddyconfig/httpcaddyfile/httptype.go
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,14 @@ func parseMatcherDefinitions(d *caddyfile.Dispenser, matchers map[string]caddy.M
// given a matcher name and the tokens following it, parse
// the tokens as a matcher module and record it
makeMatcher := func(matcherName string, tokens []caddyfile.Token) error {
// create a new dispenser from the tokens
dispenser := caddyfile.NewDispenser(tokens)

// set the matcher name (without @) in the dispenser context so
// that matcher modules can access it to use it as their name
// (e.g. regexp matchers which use the name for capture groups)
dispenser.SetContext(caddyfile.MatcherNameCtxKey, definitionName[1:])

mod, err := caddy.GetModule("http.matchers." + matcherName)
if err != nil {
return fmt.Errorf("getting matcher module '%s': %v", matcherName, err)
Expand All @@ -1405,7 +1413,7 @@ func parseMatcherDefinitions(d *caddyfile.Dispenser, matchers map[string]caddy.M
if !ok {
return fmt.Errorf("matcher module '%s' is not a Caddyfile unmarshaler", matcherName)
}
err = unm.UnmarshalCaddyfile(caddyfile.NewDispenser(tokens))
err = unm.UnmarshalCaddyfile(dispenser)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion caddyconfig/httpcaddyfile/serveroptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func unmarshalCaddyfileServerOptions(d *caddyfile.Dispenser) (any, error) {
func applyServerOptions(
servers map[string]*caddyhttp.Server,
options map[string]any,
warnings *[]caddyconfig.Warning,
_ *[]caddyconfig.Warning,
) error {
serverOpts, ok := options["servers"].([]serverOptions)
if !ok {
Expand Down
6 changes: 5 additions & 1 deletion caddyconfig/httpcaddyfile/tlsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,11 @@ func fillInGlobalACMEDefaults(issuer certmagic.Issuer, options map[string]any) e
// for any other automation policies. A nil policy (and no error) will be
// returned if there are no default/global options. However, if always is
// true, a non-nil value will always be returned (unless there is an error).
func newBaseAutomationPolicy(options map[string]any, warnings []caddyconfig.Warning, always bool) (*caddytls.AutomationPolicy, error) {
func newBaseAutomationPolicy(
options map[string]any,
_ []caddyconfig.Warning,
always bool,
) (*caddytls.AutomationPolicy, error) {
issuers, hasIssuers := options["cert_issuer"]
_, hasLocalCerts := options["local_certs"]
keyType, hasKeyType := options["key_type"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ abort @e
],
"match": [
{
"expression": "{http.error.status_code} == 403"
"expression": {
"expr": "{http.error.status_code} == 403",
"name": "d"
}
}
]
},
Expand All @@ -97,7 +100,10 @@ abort @e
],
"match": [
{
"expression": "{http.error.status_code} == 404"
"expression": {
"expr": "{http.error.status_code} == 404",
"name": "e"
}
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
{
"vars_regexp": {
"{http.request.uri}": {
"name": "matcher6",
"pattern": "\\.([a-f0-9]{6})\\.(css|js)$"
}
}
Expand All @@ -161,7 +162,10 @@
{
"match": [
{
"expression": "path('/foo*') \u0026\u0026 method('GET')"
"expression": {
"expr": "path('/foo*') \u0026\u0026 method('GET')",
"name": "matcher7"
}
}
],
"handle": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ respond @match "{re.1}"
"match": [
{
"path_regexp": {
"name": "match",
"pattern": "^/foo(.*)$"
}
}
Expand Down
12 changes: 12 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,3 +556,15 @@ func (ctx Context) Module() Module {
}
return ctx.ancestry[len(ctx.ancestry)-1]
}

// WithValue returns a new context with the given key-value pair.
func (ctx *Context) WithValue(key, value any) Context {
return Context{
Context: context.WithValue(ctx.Context, key, value),
moduleInstances: ctx.moduleInstances,
cfg: ctx.cfg,
ancestry: ctx.ancestry,
cleanupFuncs: ctx.cleanupFuncs,
exitFuncs: ctx.exitFuncs,
}
}
47 changes: 44 additions & 3 deletions modules/caddyhttp/celmatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ type MatchExpression struct {
// The CEL expression to evaluate. Any Caddy placeholders
// will be expanded and situated into proper CEL function
// calls before evaluating.
Expr string
Expr string `json:"expr,omitempty"`

// Name is an optional name for this matcher.
// This is used to populate the name for regexp
// matchers that appear in the expression.
Name string `json:"name,omitempty"`

expandedExpr string
prg cel.Program
Expand All @@ -81,12 +86,36 @@ func (MatchExpression) CaddyModule() caddy.ModuleInfo {

// MarshalJSON marshals m's expression.
func (m MatchExpression) MarshalJSON() ([]byte, error) {
return json.Marshal(m.Expr)
// if the name is empty, then we can marshal just the expression string
if m.Name == "" {
return json.Marshal(m.Expr)
}
// otherwise, we need to marshal the full object, using an
// anonymous struct to avoid infinite recursion
return json.Marshal(struct {
Expr string `json:"expr"`
Name string `json:"name"`
}{
Expr: m.Expr,
Name: m.Name,
})
}

// UnmarshalJSON unmarshals m's expression.
func (m *MatchExpression) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &m.Expr)
// if the data is a string, then it's just the expression
if data[0] == '"' {
return json.Unmarshal(data, &m.Expr)
}
// otherwise, it's a full object, so unmarshal it,
// using an temp map to avoid infinite recursion
var tmpJson map[string]any
err := json.Unmarshal(data, &tmpJson)
*m = MatchExpression{
Expr: tmpJson["expr"].(string),
Name: tmpJson["name"].(string),
}
return err
}

// Provision sets ups m.
Expand All @@ -109,6 +138,11 @@ func (m *MatchExpression) Provision(ctx caddy.Context) error {
matcherLibProducers = append(matcherLibProducers, p)
}
}

// add the matcher name to the context so that the matcher name
// can be used by regexp matchers being provisioned
ctx = ctx.WithValue(MatcherNameCtxKey, m.Name)

// Assemble the compilation and program options from the different library
// producers into a single cel.Library implementation.
matcherEnvOpts := []cel.EnvOption{}
Expand Down Expand Up @@ -197,6 +231,11 @@ func (m *MatchExpression) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
// quoted string; commonly quotes are used in Caddyfile to
// define the expression
m.Expr = d.Val()

// use the named matcher's name, to fill regexp
// matchers names by default
m.Name = d.GetContextString(caddyfile.MatcherNameCtxKey)

return nil
}

Expand Down Expand Up @@ -673,6 +712,8 @@ var httpRequestObjectType = cel.ObjectType("http.Request")
// The name of the CEL function which accesses Replacer values.
const placeholderFuncName = "caddyPlaceholder"

const MatcherNameCtxKey = "matcher_name"

// Interface guards
var (
_ caddy.Provisioner = (*MatchExpression)(nil)
Expand Down
8 changes: 6 additions & 2 deletions modules/caddyhttp/celmatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,9 @@ func TestMatchExpressionMatch(t *testing.T) {
for _, tst := range matcherTests {
tc := tst
t.Run(tc.name, func(t *testing.T) {
err := tc.expression.Provision(caddy.Context{})
caddyCtx, cancel := caddy.NewContext(caddy.Context{Context: context.Background()})
defer cancel()
err := tc.expression.Provision(caddyCtx)
if err != nil {
if !tc.wantErr {
t.Errorf("MatchExpression.Provision() error = %v, wantErr %v", err, tc.wantErr)
Expand Down Expand Up @@ -482,7 +484,9 @@ func TestMatchExpressionProvision(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.expression.Provision(caddy.Context{}); (err != nil) != tt.wantErr {
ctx, cancel := caddy.NewContext(caddy.Context{Context: context.Background()})
defer cancel()
if err := tt.expression.Provision(ctx); (err != nil) != tt.wantErr {
t.Errorf("MatchExpression.Provision() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
4 changes: 3 additions & 1 deletion modules/caddyhttp/fileserver/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,9 @@ func TestMatchExpressionMatch(t *testing.T) {
for _, tst := range expressionTests {
tc := tst
t.Run(tc.name, func(t *testing.T) {
err := tc.expression.Provision(caddy.Context{})
caddyCtx, cancel := caddy.NewContext(caddy.Context{Context: context.Background()})
defer cancel()
err := tc.expression.Provision(caddyCtx)
if err != nil {
if !tc.wantErr {
t.Errorf("MatchExpression.Provision() error = %v, wantErr %v", err, tc.wantErr)
Expand Down
Loading

0 comments on commit 9cd472c

Please sign in to comment.