mirror of
https://github.com/coredns/coredns.git
synced 2025-11-01 10:43:17 -04:00
plugin/rewrite - extend edns0 local variable support with metadata (#1928)
* - add support of metadata values for edns0 local variables * - comments from review. * - simplify label check. Add UT * - enhance check for Labels, add UT - remove IsMetadataSet * - edns0 variable - if variable is not found just ignore the rewrite.
This commit is contained in:
committed by
Miek Gieben
parent
6ec1978340
commit
7745462430
@@ -2,12 +2,14 @@
|
||||
package rewrite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/coredns/coredns/plugin/metadata"
|
||||
"github.com/coredns/coredns/request"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
@@ -46,7 +48,7 @@ func setupEdns0Opt(r *dns.Msg) *dns.OPT {
|
||||
}
|
||||
|
||||
// Rewrite will alter the request EDNS0 NSID option
|
||||
func (rule *edns0NsidRule) Rewrite(state request.Request) Result {
|
||||
func (rule *edns0NsidRule) Rewrite(ctx context.Context, state request.Request) Result {
|
||||
o := setupEdns0Opt(state.Req)
|
||||
|
||||
for _, s := range o.Option {
|
||||
@@ -74,7 +76,7 @@ func (rule *edns0NsidRule) Mode() string { return rule.mode }
|
||||
func (rule *edns0NsidRule) GetResponseRule() ResponseRule { return ResponseRule{} }
|
||||
|
||||
// Rewrite will alter the request EDNS0 local options.
|
||||
func (rule *edns0LocalRule) Rewrite(state request.Request) Result {
|
||||
func (rule *edns0LocalRule) Rewrite(ctx context.Context, state request.Request) Result {
|
||||
o := setupEdns0Opt(state.Req)
|
||||
|
||||
for _, s := range o.Option {
|
||||
@@ -174,7 +176,7 @@ func newEdns0VariableRule(mode, action, code, variable string) (*edns0VariableRu
|
||||
}
|
||||
|
||||
// ruleData returns the data specified by the variable.
|
||||
func (rule *edns0VariableRule) ruleData(state request.Request) ([]byte, error) {
|
||||
func (rule *edns0VariableRule) ruleData(ctx context.Context, state request.Request) ([]byte, error) {
|
||||
|
||||
switch rule.variable {
|
||||
case queryName:
|
||||
@@ -199,12 +201,17 @@ func (rule *edns0VariableRule) ruleData(state request.Request) ([]byte, error) {
|
||||
return []byte(state.Proto()), nil
|
||||
}
|
||||
|
||||
fetcher := metadata.ValueFunc(ctx, rule.variable[1:len(rule.variable)-1])
|
||||
if fetcher != nil {
|
||||
return []byte(fetcher()), nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("unable to extract data for variable %s", rule.variable)
|
||||
}
|
||||
|
||||
// Rewrite will alter the request EDNS0 local options with specified variables.
|
||||
func (rule *edns0VariableRule) Rewrite(state request.Request) Result {
|
||||
data, err := rule.ruleData(state)
|
||||
func (rule *edns0VariableRule) Rewrite(ctx context.Context, state request.Request) Result {
|
||||
data, err := rule.ruleData(ctx, state)
|
||||
if err != nil || data == nil {
|
||||
return RewriteIgnored
|
||||
}
|
||||
@@ -249,6 +256,10 @@ func isValidVariable(variable string) bool {
|
||||
serverPort:
|
||||
return true
|
||||
}
|
||||
// we cannot validate the labels of metadata - but we can verify it has the syntax of a label
|
||||
if strings.HasPrefix(variable, "{") && strings.HasSuffix(variable, "}") && metadata.IsLabel(variable[1:len(variable)-1]) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -310,7 +321,7 @@ func (rule *edns0SubnetRule) fillEcsData(state request.Request, ecs *dns.EDNS0_S
|
||||
}
|
||||
|
||||
// Rewrite will alter the request EDNS0 subnet option.
|
||||
func (rule *edns0SubnetRule) Rewrite(state request.Request) Result {
|
||||
func (rule *edns0SubnetRule) Rewrite(ctx context.Context, state request.Request) Result {
|
||||
o := setupEdns0Opt(state.Req)
|
||||
|
||||
for _, s := range o.Option {
|
||||
|
||||
Reference in New Issue
Block a user