1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
package rewrite
import (
"context"
"reflect"
"testing"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
type MockedUpstream struct{}
func (u *MockedUpstream) Lookup(ctx context.Context, state request.Request, name string, typ uint16) (*dns.Msg, error) {
m := new(dns.Msg)
m.SetReply(state.Req)
m.Authoritative = true
switch state.Req.Question[0].Name {
case "xyz.example.com.":
switch state.Req.Question[0].Qtype {
case dns.TypeA:
m.Answer = []dns.RR{
test.A("xyz.example.com. 3600 IN A 3.4.5.6"),
}
case dns.TypeAAAA:
m.Answer = []dns.RR{
test.AAAA("xyz.example.com. 3600 IN AAAA 3a01:7e00::f03c:91ff:fe79:234c"),
}
}
return m, nil
case "bard.google.com.cdn.cloudflare.net.":
m.Answer = []dns.RR{
test.A("bard.google.com.cdn.cloudflare.net. 1800 IN A 9.7.2.1"),
}
return m, nil
case "www.hosting.xyz.":
m.Answer = []dns.RR{
test.A("www.hosting.xyz. 500 IN A 20.30.40.50"),
}
return m, nil
case "abcd.zzzz.www.pqrst.":
m.Answer = []dns.RR{
test.A("abcd.zzzz.www.pqrst. 120 IN A 101.20.5.1"),
test.A("abcd.zzzz.www.pqrst. 120 IN A 101.20.5.2"),
}
return m, nil
case "orders.webapp.eu.org.":
m.Answer = []dns.RR{
test.A("orders.webapp.eu.org. 120 IN A 20.0.0.9"),
}
return m, nil
}
return &dns.Msg{}, nil
}
func TestCNameTargetRewrite(t *testing.T) {
rules := []Rule{}
ruleset := []struct {
args []string
expectedType reflect.Type
}{
{[]string{"continue", "cname", "exact", "def.example.com.", "xyz.example.com."}, reflect.TypeOf(&cnameTargetRule{})},
{[]string{"continue", "cname", "prefix", "chat.openai.com", "bard.google.com"}, reflect.TypeOf(&cnameTargetRule{})},
{[]string{"continue", "cname", "suffix", "uvw.", "xyz."}, reflect.TypeOf(&cnameTargetRule{})},
{[]string{"continue", "cname", "substring", "efgh", "zzzz.www"}, reflect.TypeOf(&cnameTargetRule{})},
{[]string{"continue", "cname", "regex", `(.*)\.web\.(.*)\.site\.`, `{1}.webapp.{2}.org.`}, reflect.TypeOf(&cnameTargetRule{})},
}
for i, r := range ruleset {
rule, err := newRule(r.args...)
if err != nil {
t.Fatalf("Rule %d: FAIL, %s: %s", i, r.args, err)
}
if reflect.TypeOf(rule) != r.expectedType {
t.Fatalf("Rule %d: FAIL, %s: rule type mismatch, expected %q, but got %q", i, r.args, r.expectedType, rule)
}
cnameTargetRule := rule.(*cnameTargetRule)
cnameTargetRule.Upstream = &MockedUpstream{}
rules = append(rules, rule)
}
doTestCNameTargetTests(rules, t)
}
func doTestCNameTargetTests(rules []Rule, t *testing.T) {
tests := []struct {
from string
fromType uint16
answer []dns.RR
expectedAnswer []dns.RR
}{
{"abc.example.com", dns.TypeA,
[]dns.RR{
test.CNAME("abc.example.com. 5 IN CNAME def.example.com."),
test.A("def.example.com. 5 IN A 1.2.3.4"),
},
[]dns.RR{
test.CNAME("abc.example.com. 5 IN CNAME xyz.example.com."),
test.A("xyz.example.com. 3600 IN A 3.4.5.6"),
},
},
{"abc.example.com", dns.TypeAAAA,
[]dns.RR{
test.CNAME("abc.example.com. 5 IN CNAME def.example.com."),
test.AAAA("def.example.com. 5 IN AAAA 2a01:7e00::f03c:91ff:fe79:234c"),
},
[]dns.RR{
test.CNAME("abc.example.com. 5 IN CNAME xyz.example.com."),
test.AAAA("xyz.example.com. 3600 IN AAAA 3a01:7e00::f03c:91ff:fe79:234c"),
},
},
{"chat.openai.com", dns.TypeA,
[]dns.RR{
test.CNAME("chat.openai.com. 20 IN CNAME chat.openai.com.cdn.cloudflare.net."),
test.A("chat.openai.com.cdn.cloudflare.net. 30 IN A 23.2.1.2"),
test.A("chat.openai.com.cdn.cloudflare.net. 30 IN A 24.6.0.8"),
},
[]dns.RR{
test.CNAME("chat.openai.com. 20 IN CNAME bard.google.com.cdn.cloudflare.net."),
test.A("bard.google.com.cdn.cloudflare.net. 1800 IN A 9.7.2.1"),
},
},
{"coredns.io", dns.TypeA,
[]dns.RR{
test.CNAME("coredns.io. 100 IN CNAME www.hosting.uvw."),
test.A("www.hosting.uvw. 200 IN A 7.2.3.4"),
},
[]dns.RR{
test.CNAME("coredns.io. 100 IN CNAME www.hosting.xyz."),
test.A("www.hosting.xyz. 500 IN A 20.30.40.50"),
},
},
{"core.dns.rocks", dns.TypeA,
[]dns.RR{
test.CNAME("core.dns.rocks. 200 IN CNAME abcd.efgh.pqrst."),
test.A("abcd.efgh.pqrst. 100 IN A 200.30.45.67"),
},
[]dns.RR{
test.CNAME("core.dns.rocks. 200 IN CNAME abcd.zzzz.www.pqrst."),
test.A("abcd.zzzz.www.pqrst. 120 IN A 101.20.5.1"),
test.A("abcd.zzzz.www.pqrst. 120 IN A 101.20.5.2"),
},
},
{"order.service.eu", dns.TypeA,
[]dns.RR{
test.CNAME("order.service.eu. 200 IN CNAME orders.web.eu.site."),
test.A("orders.web.eu.site. 50 IN A 10.10.15.1"),
},
[]dns.RR{
test.CNAME("order.service.eu. 200 IN CNAME orders.webapp.eu.org."),
test.A("orders.webapp.eu.org. 120 IN A 20.0.0.9"),
},
},
}
ctx := context.TODO()
for i, tc := range tests {
m := new(dns.Msg)
m.SetQuestion(tc.from, tc.fromType)
m.Question[0].Qclass = dns.ClassINET
m.Answer = tc.answer
rw := Rewrite{
Next: plugin.HandlerFunc(msgPrinter),
Rules: rules,
}
rec := dnstest.NewRecorder(&test.ResponseWriter{})
rw.ServeDNS(ctx, rec, m)
resp := rec.Msg
if len(resp.Answer) == 0 {
t.Errorf("Test %d: FAIL %s (%d) Expected valid response but received %q", i, tc.from, tc.fromType, resp)
continue
}
if !reflect.DeepEqual(resp.Answer, tc.expectedAnswer) {
t.Errorf("Test %d: FAIL %s (%d) Actual are expected answer does not match, actual: %v, expected: %v",
i, tc.from, tc.fromType, resp.Answer, tc.expectedAnswer)
continue
}
}
}
|