| 1 | package dns |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net" |
| 6 | "sync" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/containers/gvisor-tap-vsock/pkg/types" |
| 11 | "github.com/foxcpp/go-mockdns" |
| 12 | "github.com/miekg/dns" |
| 13 | "github.com/onsi/ginkgo" |
| 14 | "github.com/onsi/gomega" |
| 15 | log "github.com/sirupsen/logrus" |
| 16 | "github.com/stretchr/testify/require" |
| 17 | ) |
| 18 | |
| 19 | func TestSuite(t *testing.T) { |
| 20 | gomega.RegisterFailHandler(ginkgo.Fail) |
| 21 | ginkgo.RunSpecs(t, "gvisor-tap-vsock dns suit") |
| 22 | } |
| 23 | |
| 24 | var _ = ginkgo.Describe("dns add test", func() { |
| 25 | var server *Server |
| 26 | |
| 27 | ginkgo.BeforeEach(func() { |
| 28 | server, _ = New(nil, nil, []types.Zone{}) |
| 29 | }) |
| 30 | |
| 31 | ginkgo.It("should add dns zone with ip", func() { |
| 32 | req := types.Zone{ |
| 33 | Name: "internal.", |
| 34 | DefaultIP: net.ParseIP("192.168.0.1"), |
| 35 | } |
| 36 | server.addZone(req) |
| 37 | |
| 38 | gomega.Expect(server.handler.zones).To(gomega.Equal([]types.Zone{req})) |
| 39 | }) |
| 40 | |
| 41 | ginkgo.It("should add dns zone with record", func() { |
| 42 | req := types.Zone{ |
| 43 | Name: "internal.", |
| 44 | Records: []types.Record{{ |
| 45 | Name: "crc.testiing", |
| 46 | IP: net.ParseIP("192.168.0.2"), |
| 47 | }}, |
| 48 | } |
| 49 | server.addZone(req) |
| 50 | |
| 51 | gomega.Expect(server.handler.zones).To(gomega.Equal([]types.Zone{req})) |
| 52 | }) |
| 53 | |
| 54 | ginkgo.It("should add dns zone with record and ip", func() { |
| 55 | ipReq := types.Zone{ |
| 56 | Name: "dynamic.internal.", |
| 57 | DefaultIP: net.ParseIP("192.168.0.1"), |
| 58 | } |
| 59 | recordReq := types.Zone{ |
| 60 | Name: "internal.", |
| 61 | Records: []types.Record{{ |
| 62 | Name: "crc.testiing", |
| 63 | IP: net.ParseIP("192.168.0.2"), |
| 64 | }}, |
| 65 | } |
| 66 | server.addZone(ipReq) |
| 67 | server.addZone(recordReq) |
| 68 | |
| 69 | gomega.Expect(server.handler.zones).To(gomega.Equal([]types.Zone{ipReq, recordReq})) |
| 70 | }) |
| 71 | |
| 72 | ginkgo.It("should add new zone to existing zone with default ip", func() { |
| 73 | ipReq := types.Zone{ |
| 74 | Name: "internal.", |
| 75 | DefaultIP: net.ParseIP("192.168.0.1"), |
| 76 | } |
| 77 | server.addZone(ipReq) |
| 78 | recordReq := types.Zone{ |
| 79 | Name: "internal.", |
| 80 | Records: []types.Record{{ |
| 81 | Name: "crc.testing", |
| 82 | IP: net.ParseIP("192.168.0.2"), |
| 83 | }}, |
| 84 | } |
| 85 | server.addZone(recordReq) |
| 86 | |
| 87 | gomega.Expect(server.handler.zones).To(gomega.Equal([]types.Zone{{ |
| 88 | Name: "internal.", |
| 89 | Records: []types.Record{{ |
| 90 | Name: "crc.testing", |
| 91 | IP: net.ParseIP("192.168.0.2"), |
| 92 | }}, |
| 93 | }})) |
| 94 | }) |
| 95 | |
| 96 | ginkgo.It("should add new zone to existing zone with records", func() { |
| 97 | ipReq := types.Zone{ |
| 98 | Name: "internal.", |
| 99 | Records: []types.Record{{ |
| 100 | Name: "crc.testing", |
| 101 | IP: net.ParseIP("192.168.0.2"), |
| 102 | }}, |
| 103 | } |
| 104 | server.addZone(ipReq) |
| 105 | recordReq := types.Zone{ |
| 106 | Name: "internal.", |
| 107 | Records: []types.Record{{ |
| 108 | Name: "crc.testing", |
| 109 | IP: net.ParseIP("192.168.0.3"), |
| 110 | }}, |
| 111 | } |
| 112 | server.addZone(recordReq) |
| 113 | |
| 114 | gomega.Expect(server.handler.zones).To(gomega.Equal([]types.Zone{{ |
| 115 | Name: "internal.", |
| 116 | Records: []types.Record{{ |
| 117 | Name: "crc.testing", |
| 118 | IP: net.ParseIP("192.168.0.3"), |
| 119 | }, { |
| 120 | Name: "crc.testing", |
| 121 | IP: net.ParseIP("192.168.0.2"), |
| 122 | }}, |
| 123 | }})) |
| 124 | }) |
| 125 | |
| 126 | ginkgo.It("should add new zone to existing zone with records", func() { |
| 127 | ipReq := types.Zone{ |
| 128 | Name: "internal.", |
| 129 | Records: []types.Record{{ |
| 130 | Name: "crc.testing", |
| 131 | IP: net.ParseIP("192.168.0.2"), |
| 132 | }}, |
| 133 | } |
| 134 | server.addZone(ipReq) |
| 135 | recordReq := types.Zone{ |
| 136 | Name: "internal.", |
| 137 | Records: []types.Record{{ |
| 138 | Name: "crc.testing", |
| 139 | IP: net.ParseIP("192.168.0.3"), |
| 140 | }}, |
| 141 | } |
| 142 | server.addZone(recordReq) |
| 143 | |
| 144 | gomega.Expect(server.handler.zones).To(gomega.Equal([]types.Zone{{ |
| 145 | Name: "internal.", |
| 146 | Records: []types.Record{{ |
| 147 | Name: "crc.testing", |
| 148 | IP: net.ParseIP("192.168.0.3"), |
| 149 | }, { |
| 150 | Name: "crc.testing", |
| 151 | IP: net.ParseIP("192.168.0.2"), |
| 152 | }}, |
| 153 | }})) |
| 154 | }) |
| 155 | |
| 156 | ginkgo.It("should retain the order of zones", func() { |
| 157 | server, _ = New(nil, nil, []types.Zone{ |
| 158 | { |
| 159 | Name: "crc.testing.", |
| 160 | DefaultIP: net.ParseIP("192.168.127.2"), |
| 161 | }, |
| 162 | { |
| 163 | Name: "testing.", |
| 164 | Records: []types.Record{ |
| 165 | { |
| 166 | Name: "host", |
| 167 | IP: net.ParseIP("192.168.127.3"), |
| 168 | }, |
| 169 | }, |
| 170 | }, |
| 171 | }) |
| 172 | server.addZone(types.Zone{ |
| 173 | Name: "testing.", |
| 174 | Records: []types.Record{ |
| 175 | { |
| 176 | Name: "gateway", |
| 177 | IP: net.ParseIP("192.168.127.1"), |
| 178 | }, |
| 179 | }, |
| 180 | }) |
| 181 | gomega.Expect(server.handler.zones).To(gomega.Equal([]types.Zone{ |
| 182 | { |
| 183 | Name: "crc.testing.", |
| 184 | DefaultIP: net.ParseIP("192.168.127.2"), |
| 185 | }, |
| 186 | { |
| 187 | Name: "testing.", |
| 188 | Records: []types.Record{ |
| 189 | { |
| 190 | Name: "gateway", |
| 191 | IP: net.ParseIP("192.168.127.1"), |
| 192 | }, |
| 193 | { |
| 194 | Name: "host", |
| 195 | IP: net.ParseIP("192.168.127.3"), |
| 196 | }, |
| 197 | }, |
| 198 | }, |
| 199 | })) |
| 200 | }) |
| 201 | |
| 202 | ginkgo.It("Should pass DNS requests to default system DNS server", func() { |
| 203 | m := &dns.Msg{ |
| 204 | MsgHdr: dns.MsgHdr{ |
| 205 | Authoritative: false, |
| 206 | AuthenticatedData: false, |
| 207 | CheckingDisabled: false, |
| 208 | RecursionDesired: true, |
| 209 | Opcode: 0, |
| 210 | }, |
| 211 | Question: make([]dns.Question, 1), |
| 212 | } |
| 213 | |
| 214 | m.Question[0] = dns.Question{ |
| 215 | Name: "redhat.com.", |
| 216 | Qtype: 1, |
| 217 | Qclass: 1, |
| 218 | } |
| 219 | |
| 220 | server.handler.addAnswers(m) |
| 221 | |
| 222 | gomega.Expect(m.Answer[0].Header().Name).To(gomega.Equal("redhat.com.")) |
| 223 | gomega.Expect(m.Answer[0].String()).To(gomega.SatisfyAny(gomega.ContainSubstring("34.235.198.240"), gomega.ContainSubstring("52.200.142.250"))) |
| 224 | }) |
| 225 | |
| 226 | }) |
| 227 | |
| 228 | var _ = ginkgo.Describe("TXT records", func() { |
| 229 | |
| 230 | var cleanup func() |
| 231 | var vsockResolver *net.Resolver |
| 232 | var upstream upstreamResolver |
| 233 | |
| 234 | const longTxtDomain = "long.txt.test." |
| 235 | const multipleTxtDomain = "multiple.txt.test." |
| 236 | |
| 237 | ginkgo.BeforeEach(func() { |
| 238 | var nameserver string |
| 239 | var err error |
| 240 | |
| 241 | const txt = "abcdefghijklmnopqrstuvwxyz012456789" |
| 242 | const longTxt = txt + txt + txt + txt + txt + txt + txt + txt |
| 243 | upstream = &mockdns.Resolver{ |
| 244 | Zones: map[string]mockdns.Zone{ |
| 245 | longTxtDomain: { |
| 246 | TXT: []string{longTxt}, |
| 247 | }, |
| 248 | multipleTxtDomain: { |
| 249 | TXT: []string{"AAAAAAA", "BBBBBBB"}, |
| 250 | }, |
| 251 | }, |
| 252 | } |
| 253 | |
| 254 | nameserver, cleanup, err = startDNSServer(upstream) |
| 255 | gomega.Expect(err).To(gomega.BeNil()) |
| 256 | time.Sleep(100 * time.Millisecond) |
| 257 | |
| 258 | vsockResolver = &net.Resolver{ |
| 259 | PreferGo: true, |
| 260 | Dial: func(ctx context.Context, network, _ string) (net.Conn, error) { |
| 261 | d := net.Dialer{} |
| 262 | return d.DialContext(ctx, network, nameserver) |
| 263 | }, |
| 264 | } |
| 265 | }) |
| 266 | |
| 267 | ginkgo.AfterEach(func() { |
| 268 | if cleanup != nil { |
| 269 | cleanup() |
| 270 | |
| 271 | time.Sleep(100 * time.Millisecond) |
| 272 | } |
| 273 | }) |
| 274 | |
| 275 | ginkgo.When("There are long TXT Records", func() { |
| 276 | |
| 277 | hasLongString := func(records []string) bool { |
| 278 | for _, txt := range records { |
| 279 | if len(txt) > 255 { |
| 280 | return true |
| 281 | } |
| 282 | } |
| 283 | return false |
| 284 | } |
| 285 | ginkgo.It("Should produce the same result as upstream Resolver", func() { |
| 286 | |
| 287 | upstreamRecords, err := upstream.LookupTXT(context.Background(), longTxtDomain) |
| 288 | gomega.Expect(err).To(gomega.BeNil()) |
| 289 | gomega.Expect(hasLongString(upstreamRecords)).To(gomega.BeTrue(), "Expected at least one TXT string longer than 255 bytes") |
| 290 | |
| 291 | vsockRecords, err := vsockResolver.LookupTXT(context.Background(), longTxtDomain) |
| 292 | gomega.Expect(err).To(gomega.BeNil()) |
| 293 | |
| 294 | gomega.Expect(vsockRecords).To(gomega.Equal(upstreamRecords)) |
| 295 | |
| 296 | }) |
| 297 | |
| 298 | }) |
| 299 | |
| 300 | ginkgo.When("there are multiple TXT Records", func() { |
| 301 | ginkgo.It("Should produce the same result as upstream Resolver", func() { |
| 302 | upstreamRecords, err := upstream.LookupTXT(context.Background(), multipleTxtDomain) |
| 303 | gomega.Expect(err).To(gomega.BeNil()) |
| 304 | gomega.Expect(len(upstreamRecords)).To(gomega.BeNumerically(">", 1), "Expected more than one TXT record") |
| 305 | |
| 306 | vsockRecords, err := vsockResolver.LookupTXT(context.Background(), multipleTxtDomain) |
| 307 | gomega.Expect(err).To(gomega.BeNil()) |
| 308 | |
| 309 | gomega.Expect(vsockRecords).To(gomega.ConsistOf(upstreamRecords)) |
| 310 | |
| 311 | }) |
| 312 | }) |
| 313 | |
| 314 | }) |
| 315 | |
| 316 | type ARecord struct { |
| 317 | name string |
| 318 | expectedIPs []string |
| 319 | } |
| 320 | |
| 321 | func TestDNS(t *testing.T) { |
| 322 | log.Infof("starting test DNS servers") |
| 323 | |
| 324 | upstream := &mockdns.Resolver{ |
| 325 | Zones: map[string]mockdns.Zone{ |
| 326 | "redhat.com.": { |
| 327 | A: []string{"52.200.142.250", "34.235.198.240"}, |
| 328 | }, |
| 329 | }, |
| 330 | } |
| 331 | |
| 332 | nameserver, cleanup, err := startDNSServer(upstream) |
| 333 | require.NoError(t, err) |
| 334 | defer cleanup() |
| 335 | time.Sleep(100 * time.Millisecond) |
| 336 | log.Infof("test DNS servers started") |
| 337 | |
| 338 | r := &net.Resolver{ |
| 339 | PreferGo: true, |
| 340 | Dial: func(ctx context.Context, network, _ string) (net.Conn, error) { |
| 341 | d := net.Dialer{ |
| 342 | Timeout: time.Millisecond * time.Duration(10000), |
| 343 | } |
| 344 | log.Infof("dialing %s %s", network, nameserver) |
| 345 | |
| 346 | return d.DialContext(ctx, network, nameserver) |
| 347 | }, |
| 348 | } |
| 349 | redhatdotcom := ARecord{ |
| 350 | name: "redhat.com", |
| 351 | expectedIPs: []string{"52.200.142.250"}, |
| 352 | } |
| 353 | record := redhatdotcom |
| 354 | { |
| 355 | log.Infof("looking up %s", record.name) |
| 356 | ipGvisor, err := r.LookupHost(context.Background(), record.name) |
| 357 | require.NoError(t, err) |
| 358 | require.Subset(t, ipGvisor, record.expectedIPs) |
| 359 | log.Infof("ip gvisor: %+v", ipGvisor) |
| 360 | |
| 361 | ipGo, err := net.LookupHost(record.name) |
| 362 | require.NoError(t, err) |
| 363 | log.Infof("ip go: %+v", ipGo) |
| 364 | require.Subset(t, ipGvisor, ipGo) |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | func startDNSServer(upstream upstreamResolver) (string, func(), error) { |
| 369 | udpConn, err := net.ListenPacket("udp", "127.0.0.1:5354") |
| 370 | if err != nil { |
| 371 | return "", nil, err |
| 372 | } |
| 373 | |
| 374 | tcpLn, err := net.Listen("tcp", "127.0.0.1:5354") |
| 375 | if err != nil { |
| 376 | return "", nil, err |
| 377 | } |
| 378 | |
| 379 | server, err := NewWithUpstreamResolver(udpConn, tcpLn, nil, upstream) |
| 380 | if err != nil { |
| 381 | return "", nil, err |
| 382 | } |
| 383 | |
| 384 | go func() { |
| 385 | if err := server.Serve(); err != nil { |
| 386 | log.Errorf("serve UDP error: %T %s", err, err) |
| 387 | } |
| 388 | }() |
| 389 | go func() { |
| 390 | if err := server.ServeTCP(); err != nil { |
| 391 | log.Errorf("serve TCP error: %T %s", err, err) |
| 392 | } |
| 393 | }() |
| 394 | return "127.0.0.1:5354", func() { |
| 395 | udpConn.Close() |
| 396 | tcpLn.Close() |
| 397 | }, nil |
| 398 | } |
| 399 | |
| 400 | type countingResolver struct { |
| 401 | upstreamResolver |
| 402 | mu sync.Mutex |
| 403 | asked []string |
| 404 | } |
| 405 | |
| 406 | func (c *countingResolver) LookupIPAddr(ctx context.Context, host string) ([]net.IPAddr, error) { |
| 407 | c.mu.Lock() |
| 408 | c.asked = append(c.asked, host) |
| 409 | c.mu.Unlock() |
| 410 | return c.upstreamResolver.LookupIPAddr(ctx, host) |
| 411 | } |
| 412 | |
| 413 | func TestPolicy(t *testing.T) { |
| 414 | upstream := &countingResolver{upstreamResolver: &mockdns.Resolver{ |
| 415 | Zones: map[string]mockdns.Zone{ |
| 416 | "ok.test.": {A: []string{"192.0.2.1"}}, |
| 417 | "no.test.": {A: []string{"192.0.2.2"}}, |
| 418 | }, |
| 419 | }} |
| 420 | udpConn, err := net.ListenPacket("udp", "127.0.0.1:0") |
| 421 | require.NoError(t, err) |
| 422 | defer udpConn.Close() |
| 423 | tcpLn, err := net.Listen("tcp", "127.0.0.1:0") |
| 424 | require.NoError(t, err) |
| 425 | defer tcpLn.Close() |
| 426 | server, err := NewWithUpstreamResolver(udpConn, tcpLn, nil, upstream) |
| 427 | require.NoError(t, err) |
| 428 | |
| 429 | var mu sync.Mutex |
| 430 | told := map[string][]net.IP{} |
| 431 | server.SetPolicy( |
| 432 | func(name string) bool { return name == "ok.test" }, |
| 433 | func(name string, ips []net.IP) { |
| 434 | mu.Lock() |
| 435 | defer mu.Unlock() |
| 436 | told[name] = ips |
| 437 | }) |
| 438 | go func() { _ = server.Serve() }() |
| 439 | go func() { _ = server.ServeTCP() }() |
| 440 | time.Sleep(100 * time.Millisecond) |
| 441 | |
| 442 | for _, proto := range []string{"udp", "tcp"} { |
| 443 | c := &dns.Client{Net: proto} |
| 444 | addr := udpConn.LocalAddr().String() |
| 445 | if proto == "tcp" { |
| 446 | addr = tcpLn.Addr().String() |
| 447 | } |
| 448 | |
| 449 | q := new(dns.Msg) |
| 450 | q.SetQuestion("NO.test.", dns.TypeA) |
| 451 | r, _, err := c.Exchange(q, addr) |
| 452 | require.NoError(t, err, proto) |
| 453 | require.Equal(t, dns.RcodeNameError, r.Rcode, proto) |
| 454 | require.Empty(t, r.Answer, proto) |
| 455 | |
| 456 | q = new(dns.Msg) |
| 457 | q.SetQuestion("ok.test.", dns.TypeA) |
| 458 | r, _, err = c.Exchange(q, addr) |
| 459 | require.NoError(t, err, proto) |
| 460 | require.Equal(t, dns.RcodeSuccess, r.Rcode, proto) |
| 461 | require.Len(t, r.Answer, 1, proto) |
| 462 | |
| 463 | } |
| 464 | |
| 465 | |
| 466 | m := new(dns.Msg) |
| 467 | m.Question = []dns.Question{ |
| 468 | {Name: "ok.test.", Qtype: dns.TypeA, Qclass: dns.ClassINET}, |
| 469 | {Name: "no.test.", Qtype: dns.TypeA, Qclass: dns.ClassINET}, |
| 470 | } |
| 471 | server.handler.addAnswers(m) |
| 472 | require.Equal(t, dns.RcodeNameError, m.Rcode) |
| 473 | require.Empty(t, m.Answer) |
| 474 | |
| 475 | mu.Lock() |
| 476 | defer mu.Unlock() |
| 477 | require.Equal(t, []net.IP{net.ParseIP("192.0.2.1").To4()}, told["ok.test"]) |
| 478 | require.NotContains(t, told, "no.test") |
| 479 | upstream.mu.Lock() |
| 480 | defer upstream.mu.Unlock() |
| 481 | for _, host := range upstream.asked { |
| 482 | require.Equal(t, "ok.test.", host, "a refused name was looked up") |
| 483 | } |
| 484 | } |