mirror of
https://github.com/coredns/coredns.git
synced 2025-11-19 18:32:16 -05:00
plugin/rewrite: streamline the ResponseRule handling. (#4473)
* plugin/rewrite: streamline the ResponseRule handling. The functionality of a response rule is now completely encapsulated behind a `ResponseRule` interface. This significantly simplifies the complete processing flow, it enables more flexible response handling and it is possible to eliminate lots of state flags, ifs and switches. Based on the new flexibility the pull request also enables to support a response name rewrite for all name rewrite types. To be compatible, an explicit `answer auto` option is added to support a best effort response rewrite (name and value). Additionally now all name rewrite rules support additional name and value reponse rewrite options. Using this feature it is also possible now to rewrite a complete sub domain hierarchy to a single domain name combined with a correct rewrite (#2389). Signed-off-by: Uwe Krueger <uwe.krueger@sap.com> * revert policy Signed-off-by: Uwe Krueger <uwe.krueger@sap.com> Co-authored-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
@@ -49,14 +49,14 @@ func setupEdns0Opt(r *dns.Msg) *dns.OPT {
|
||||
}
|
||||
|
||||
// Rewrite will alter the request EDNS0 NSID option
|
||||
func (rule *edns0NsidRule) Rewrite(ctx context.Context, state request.Request) Result {
|
||||
func (rule *edns0NsidRule) Rewrite(ctx context.Context, state request.Request) (ResponseRules, Result) {
|
||||
o := setupEdns0Opt(state.Req)
|
||||
|
||||
for _, s := range o.Option {
|
||||
if e, ok := s.(*dns.EDNS0_NSID); ok {
|
||||
if rule.action == Replace || rule.action == Set {
|
||||
e.Nsid = "" // make sure it is empty for request
|
||||
return RewriteDone
|
||||
return nil, RewriteDone
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -64,20 +64,17 @@ func (rule *edns0NsidRule) Rewrite(ctx context.Context, state request.Request) R
|
||||
// add option if not found
|
||||
if rule.action == Append || rule.action == Set {
|
||||
o.Option = append(o.Option, &dns.EDNS0_NSID{Code: dns.EDNS0NSID, Nsid: ""})
|
||||
return RewriteDone
|
||||
return nil, RewriteDone
|
||||
}
|
||||
|
||||
return RewriteIgnored
|
||||
return nil, RewriteIgnored
|
||||
}
|
||||
|
||||
// Mode returns the processing mode.
|
||||
func (rule *edns0NsidRule) Mode() string { return rule.mode }
|
||||
|
||||
// GetResponseRules returns rules to rewrite the response with. Currently not implemented.
|
||||
func (rule *edns0NsidRule) GetResponseRules() []ResponseRule { return []ResponseRule{} }
|
||||
|
||||
// Rewrite will alter the request EDNS0 local options.
|
||||
func (rule *edns0LocalRule) Rewrite(ctx context.Context, state request.Request) Result {
|
||||
func (rule *edns0LocalRule) Rewrite(ctx context.Context, state request.Request) (ResponseRules, Result) {
|
||||
o := setupEdns0Opt(state.Req)
|
||||
|
||||
for _, s := range o.Option {
|
||||
@@ -85,7 +82,7 @@ func (rule *edns0LocalRule) Rewrite(ctx context.Context, state request.Request)
|
||||
if rule.code == e.Code {
|
||||
if rule.action == Replace || rule.action == Set {
|
||||
e.Data = rule.data
|
||||
return RewriteDone
|
||||
return nil, RewriteDone
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -94,18 +91,15 @@ func (rule *edns0LocalRule) Rewrite(ctx context.Context, state request.Request)
|
||||
// add option if not found
|
||||
if rule.action == Append || rule.action == Set {
|
||||
o.Option = append(o.Option, &dns.EDNS0_LOCAL{Code: rule.code, Data: rule.data})
|
||||
return RewriteDone
|
||||
return nil, RewriteDone
|
||||
}
|
||||
|
||||
return RewriteIgnored
|
||||
return nil, RewriteIgnored
|
||||
}
|
||||
|
||||
// Mode returns the processing mode.
|
||||
func (rule *edns0LocalRule) Mode() string { return rule.mode }
|
||||
|
||||
// GetResponseRules returns a rule to rewrite the response with. Currently not implemented.
|
||||
func (rule *edns0LocalRule) GetResponseRules() []ResponseRule { return []ResponseRule{} }
|
||||
|
||||
// newEdns0Rule creates an EDNS0 rule of the appropriate type based on the args
|
||||
func newEdns0Rule(mode string, args ...string) (Rule, error) {
|
||||
if len(args) < 2 {
|
||||
@@ -222,10 +216,10 @@ func (rule *edns0VariableRule) ruleData(ctx context.Context, state request.Reque
|
||||
}
|
||||
|
||||
// Rewrite will alter the request EDNS0 local options with specified variables.
|
||||
func (rule *edns0VariableRule) Rewrite(ctx context.Context, state request.Request) Result {
|
||||
func (rule *edns0VariableRule) Rewrite(ctx context.Context, state request.Request) (ResponseRules, Result) {
|
||||
data, err := rule.ruleData(ctx, state)
|
||||
if err != nil || data == nil {
|
||||
return RewriteIgnored
|
||||
return nil, RewriteIgnored
|
||||
}
|
||||
|
||||
o := setupEdns0Opt(state.Req)
|
||||
@@ -234,9 +228,9 @@ func (rule *edns0VariableRule) Rewrite(ctx context.Context, state request.Reques
|
||||
if rule.code == e.Code {
|
||||
if rule.action == Replace || rule.action == Set {
|
||||
e.Data = data
|
||||
return RewriteDone
|
||||
return nil, RewriteDone
|
||||
}
|
||||
return RewriteIgnored
|
||||
return nil, RewriteIgnored
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -244,18 +238,15 @@ func (rule *edns0VariableRule) Rewrite(ctx context.Context, state request.Reques
|
||||
// add option if not found
|
||||
if rule.action == Append || rule.action == Set {
|
||||
o.Option = append(o.Option, &dns.EDNS0_LOCAL{Code: rule.code, Data: data})
|
||||
return RewriteDone
|
||||
return nil, RewriteDone
|
||||
}
|
||||
|
||||
return RewriteIgnored
|
||||
return nil, RewriteIgnored
|
||||
}
|
||||
|
||||
// Mode returns the processing mode.
|
||||
func (rule *edns0VariableRule) Mode() string { return rule.mode }
|
||||
|
||||
// GetResponseRules returns rules to rewrite the response with. Currently not implemented.
|
||||
func (rule *edns0VariableRule) GetResponseRules() []ResponseRule { return []ResponseRule{} }
|
||||
|
||||
func isValidVariable(variable string) bool {
|
||||
switch variable {
|
||||
case
|
||||
@@ -333,17 +324,17 @@ func (rule *edns0SubnetRule) fillEcsData(state request.Request, ecs *dns.EDNS0_S
|
||||
}
|
||||
|
||||
// Rewrite will alter the request EDNS0 subnet option.
|
||||
func (rule *edns0SubnetRule) Rewrite(ctx context.Context, state request.Request) Result {
|
||||
func (rule *edns0SubnetRule) Rewrite(ctx context.Context, state request.Request) (ResponseRules, Result) {
|
||||
o := setupEdns0Opt(state.Req)
|
||||
|
||||
for _, s := range o.Option {
|
||||
if e, ok := s.(*dns.EDNS0_SUBNET); ok {
|
||||
if rule.action == Replace || rule.action == Set {
|
||||
if rule.fillEcsData(state, e) == nil {
|
||||
return RewriteDone
|
||||
return nil, RewriteDone
|
||||
}
|
||||
}
|
||||
return RewriteIgnored
|
||||
return nil, RewriteIgnored
|
||||
}
|
||||
}
|
||||
|
||||
@@ -352,19 +343,16 @@ func (rule *edns0SubnetRule) Rewrite(ctx context.Context, state request.Request)
|
||||
opt := &dns.EDNS0_SUBNET{Code: dns.EDNS0SUBNET}
|
||||
if rule.fillEcsData(state, opt) == nil {
|
||||
o.Option = append(o.Option, opt)
|
||||
return RewriteDone
|
||||
return nil, RewriteDone
|
||||
}
|
||||
}
|
||||
|
||||
return RewriteIgnored
|
||||
return nil, RewriteIgnored
|
||||
}
|
||||
|
||||
// Mode returns the processing mode
|
||||
func (rule *edns0SubnetRule) Mode() string { return rule.mode }
|
||||
|
||||
// GetResponseRules return rules to rewrite the response with. Currently not implemented.
|
||||
func (rule *edns0SubnetRule) GetResponseRules() []ResponseRule { return []ResponseRule{} }
|
||||
|
||||
// These are all defined actions.
|
||||
const (
|
||||
Replace = "replace"
|
||||
|
||||
Reference in New Issue
Block a user