mirror of
https://github.com/coredns/coredns.git
synced 2026-07-17 13:10:11 -04:00
plugin/rewrite: normalize exact cname rewrite targets and preserve all records (#8285)
Signed-off-by: ncesam <rybushkin09@bk.ru>
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/coredns/coredns/plugin"
|
||||
"github.com/coredns/coredns/plugin/pkg/log"
|
||||
"github.com/coredns/coredns/plugin/pkg/upstream"
|
||||
"github.com/coredns/coredns/request"
|
||||
@@ -139,6 +140,10 @@ func newCNAMERule(nextAction string, args ...string) (Rule, error) {
|
||||
} else {
|
||||
return nil, fmt.Errorf("too few (%d) arguments for a cname rule", len(args))
|
||||
}
|
||||
if rewriteType == ExactMatch {
|
||||
paramFromTarget = plugin.Name(paramFromTarget).Normalize()
|
||||
paramToTarget = plugin.Name(paramToTarget).Normalize()
|
||||
}
|
||||
rule := cnameTargetRule{
|
||||
rewriteType: rewriteType,
|
||||
paramFromTarget: paramFromTarget,
|
||||
|
||||
@@ -76,7 +76,7 @@ func TestCNameTargetRewrite(t *testing.T) {
|
||||
args []string
|
||||
expectedType reflect.Type
|
||||
}{
|
||||
{[]string{"continue", "cname", "exact", "def.example.com.", "xyz.example.com."}, reflect.TypeFor[*cnameTargetRule]()},
|
||||
{[]string{"continue", "cname", "exact", "def.example.com", "xyz.example.com"}, reflect.TypeFor[*cnameTargetRule]()},
|
||||
{[]string{"continue", "cname", "prefix", "chat.openai.com", "bard.google.com"}, reflect.TypeFor[*cnameTargetRule]()},
|
||||
{[]string{"continue", "cname", "suffix", "uvw.", "xyz."}, reflect.TypeFor[*cnameTargetRule]()},
|
||||
{[]string{"continue", "cname", "substring", "efgh", "zzzz.www"}, reflect.TypeFor[*cnameTargetRule]()},
|
||||
@@ -297,3 +297,20 @@ func TestNewCNAMERuleLargeRegex(t *testing.T) {
|
||||
t.Errorf("Expected 'too long' error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewCNAMERuleNormalization(t *testing.T) {
|
||||
rule, err := newCNAMERule("stop", "exact", "newyork.foo.com", "vpce-123.amazonaws.com")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
cnameRule, ok := rule.(*cnameTargetRule)
|
||||
if !ok {
|
||||
t.Fatalf("expected *cnameTargetRule, got %T", rule)
|
||||
}
|
||||
if cnameRule.paramFromTarget != "newyork.foo.com." {
|
||||
t.Errorf("expected fromTarget to be normalized to 'newyork.foo.com.', got %q", cnameRule.paramFromTarget)
|
||||
}
|
||||
if cnameRule.paramToTarget != "vpce-123.amazonaws.com." {
|
||||
t.Errorf("expected toTarget to be normalized to 'vpce-123.amazonaws.com.', got %q", cnameRule.paramToTarget)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user