| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | package route53
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-04-22 08:34:35 +01:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 	"errors"
 | 
					
						
							|  |  |  | 	"reflect"
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/dnstest"
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/upstream"
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 	"github.com/coredns/coredns/plugin/test"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/aws/aws-sdk-go/aws"
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 	"github.com/aws/aws-sdk-go/aws/request"
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 	"github.com/aws/aws-sdk-go/service/route53"
 | 
					
						
							|  |  |  | 	"github.com/aws/aws-sdk-go/service/route53/route53iface"
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | type fakeRoute53 struct {
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 	route53iface.Route53API
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | func (fakeRoute53) ListHostedZonesByNameWithContext(_ aws.Context, input *route53.ListHostedZonesByNameInput, _ ...request.Option) (*route53.ListHostedZonesByNameOutput, error) {
 | 
					
						
							|  |  |  | 	return nil, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (fakeRoute53) ListResourceRecordSetsPagesWithContext(_ aws.Context, in *route53.ListResourceRecordSetsInput, fn func(*route53.ListResourceRecordSetsOutput, bool) bool, _ ...request.Option) error {
 | 
					
						
							|  |  |  | 	if aws.StringValue(in.HostedZoneId) == "0987654321" {
 | 
					
						
							|  |  |  | 		return errors.New("bad. zone is bad")
 | 
					
						
							| 
									
										
										
										
											2018-03-13 00:06:50 -07:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 	var rrs []*route53.ResourceRecordSet
 | 
					
						
							|  |  |  | 	for _, r := range []struct {
 | 
					
						
							|  |  |  | 		rType, name, value string
 | 
					
						
							|  |  |  | 	}{
 | 
					
						
							|  |  |  | 		{"A", "example.org.", "1.2.3.4"},
 | 
					
						
							|  |  |  | 		{"AAAA", "example.org.", "2001:db8:85a3::8a2e:370:7334"},
 | 
					
						
							|  |  |  | 		{"CNAME", "sample.example.org.", "example.org"},
 | 
					
						
							|  |  |  | 		{"PTR", "example.org.", "ptr.example.org."},
 | 
					
						
							|  |  |  | 		{"SOA", "org.", "ns-1536.awsdns-00.co.uk. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400"},
 | 
					
						
							|  |  |  | 		{"NS", "com.", "ns-1536.awsdns-00.co.uk."},
 | 
					
						
							|  |  |  | 		// Unsupported type should be ignored.
 | 
					
						
							|  |  |  | 		{"YOLO", "swag.", "foobar"},
 | 
					
						
							|  |  |  | 	} {
 | 
					
						
							|  |  |  | 		rrs = append(rrs, &route53.ResourceRecordSet{Type: aws.String(r.rType),
 | 
					
						
							|  |  |  | 			Name: aws.String(r.name),
 | 
					
						
							|  |  |  | 			ResourceRecords: []*route53.ResourceRecord{
 | 
					
						
							|  |  |  | 				{
 | 
					
						
							|  |  |  | 					Value: aws.String(r.value),
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 				},
 | 
					
						
							|  |  |  | 			},
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 			TTL: aws.Int64(300),
 | 
					
						
							|  |  |  | 		})
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if ok := fn(&route53.ListResourceRecordSetsOutput{
 | 
					
						
							|  |  |  | 		ResourceRecordSets: rrs,
 | 
					
						
							|  |  |  | 	}, true); !ok {
 | 
					
						
							|  |  |  | 		return errors.New("paging function return false")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestRoute53(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 	ctx := context.Background()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	r, err := New(ctx, fakeRoute53{}, map[string]string{"bad.": "0987654321"}, &upstream.Upstream{})
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatalf("Failed to create Route53: %v", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if err = r.Run(ctx); err == nil {
 | 
					
						
							|  |  |  | 		t.Fatalf("Expected errors for zone bad.")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	r, err = New(ctx, fakeRoute53{}, map[string]string{"org.": "1234567890"}, &upstream.Upstream{})
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatalf("Failed to create Route53: %v", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	r.Next = test.ErrorHandler()
 | 
					
						
							|  |  |  | 	err = r.Run(ctx)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatalf("Failed to initialize Route53: %v", err)
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tests := []struct {
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 		qname        string
 | 
					
						
							|  |  |  | 		qtype        uint16
 | 
					
						
							|  |  |  | 		expectedCode int
 | 
					
						
							|  |  |  | 		wantAnswer   []string // ownernames for the records in the additional section.
 | 
					
						
							|  |  |  | 		wantNS       []string
 | 
					
						
							|  |  |  | 		expectedErr  error
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 	}{
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 		// 0. example.org A found - success.
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			qname:        "example.org",
 | 
					
						
							|  |  |  | 			qtype:        dns.TypeA,
 | 
					
						
							|  |  |  | 			expectedCode: dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 			wantAnswer: []string{"example.org.	300	IN	A	1.2.3.4"},
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		// 1. example.org AAAA found - success.
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			qname:        "example.org",
 | 
					
						
							|  |  |  | 			qtype:        dns.TypeAAAA,
 | 
					
						
							|  |  |  | 			expectedCode: dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 			wantAnswer: []string{"example.org.	300	IN	AAAA	2001:db8:85a3::8a2e:370:7334"},
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		// 2. exampled.org PTR found - success.
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 		{
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 			qname:        "example.org",
 | 
					
						
							|  |  |  | 			qtype:        dns.TypePTR,
 | 
					
						
							|  |  |  | 			expectedCode: dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 			wantAnswer: []string{"example.org.	300	IN	PTR	ptr.example.org."},
 | 
					
						
							| 
									
										
										
										
											2018-03-13 00:06:50 -07:00
										 |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 		// 3. sample.example.org points to example.org CNAME.
 | 
					
						
							|  |  |  | 		// Query must return both CNAME and A recs.
 | 
					
						
							| 
									
										
										
										
											2018-03-13 00:06:50 -07:00
										 |  |  | 		{
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 			qname:        "sample.example.org",
 | 
					
						
							|  |  |  | 			qtype:        dns.TypeA,
 | 
					
						
							|  |  |  | 			expectedCode: dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 			wantAnswer: []string{
 | 
					
						
							|  |  |  | 				"sample.example.org.	300	IN	CNAME	example.org.",
 | 
					
						
							|  |  |  | 				"example.org.	300	IN	A	1.2.3.4",
 | 
					
						
							|  |  |  | 			},
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		// 4. Explicit CNAME query for sample.example.org.
 | 
					
						
							|  |  |  | 		// Query must return just CNAME.
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			qname:        "sample.example.org",
 | 
					
						
							|  |  |  | 			qtype:        dns.TypeCNAME,
 | 
					
						
							|  |  |  | 			expectedCode: dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 			wantAnswer: []string{"sample.example.org.	300	IN	CNAME	example.org."},
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		// 5. Explicit SOA query for example.org.
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			qname:        "example.org",
 | 
					
						
							|  |  |  | 			qtype:        dns.TypeSOA,
 | 
					
						
							|  |  |  | 			expectedCode: dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 			wantAnswer: []string{"org.	300	IN	SOA	ns-1536.awsdns-00.co.uk. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400"},
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		// 6. Explicit SOA query for example.org.
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			qname:        "example.org",
 | 
					
						
							|  |  |  | 			qtype:        dns.TypeNS,
 | 
					
						
							|  |  |  | 			expectedCode: dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 			wantNS: []string{"org.	300	IN	SOA	ns-1536.awsdns-00.co.uk. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400"},
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 		// 7. Zone not configured.
 | 
					
						
							| 
									
										
										
										
											2018-03-13 15:10:07 -07:00
										 |  |  | 		{
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 			qname:        "badexample.com",
 | 
					
						
							|  |  |  | 			qtype:        dns.TypeA,
 | 
					
						
							|  |  |  | 			expectedCode: dns.RcodeServerFailure,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		// 8. No record found. Return SOA record.
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			qname:        "bad.org",
 | 
					
						
							|  |  |  | 			qtype:        dns.TypeA,
 | 
					
						
							|  |  |  | 			expectedCode: dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 			wantNS: []string{"org.	300	IN	SOA	ns-1536.awsdns-00.co.uk. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400"},
 | 
					
						
							| 
									
										
										
										
											2018-03-13 15:10:07 -07:00
										 |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 	for ti, tc := range tests {
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 		req := new(dns.Msg)
 | 
					
						
							|  |  |  | 		req.SetQuestion(dns.Fqdn(tc.qname), tc.qtype)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		rec := dnstest.NewRecorder(&test.ResponseWriter{})
 | 
					
						
							|  |  |  | 		code, err := r.ServeDNS(ctx, rec, req)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if err != tc.expectedErr {
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 			t.Fatalf("Test %d: Expected error %v, but got %v", ti, tc.expectedErr, err)
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 		if code != int(tc.expectedCode) {
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 			t.Fatalf("Test %d: Expected status code %s, but got %s", ti, dns.RcodeToString[tc.expectedCode], dns.RcodeToString[code])
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if len(tc.wantAnswer) != len(rec.Msg.Answer) {
 | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Unexpected number of Answers. Want: %d, got: %d", ti, len(tc.wantAnswer), len(rec.Msg.Answer))
 | 
					
						
							|  |  |  | 		} else {
 | 
					
						
							|  |  |  | 			for i, gotAnswer := range rec.Msg.Answer {
 | 
					
						
							|  |  |  | 				if gotAnswer.String() != tc.wantAnswer[i] {
 | 
					
						
							|  |  |  | 					t.Errorf("Test %d: Unexpected answer.\nWant:\n\t%s\nGot:\n\t%s", ti, tc.wantAnswer[i], gotAnswer)
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if len(tc.wantNS) != len(rec.Msg.Ns) {
 | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Unexpected NS number. Want: %d, got: %d", ti, len(tc.wantNS), len(rec.Msg.Ns))
 | 
					
						
							|  |  |  | 		} else {
 | 
					
						
							|  |  |  | 			for i, ns := range rec.Msg.Ns {
 | 
					
						
							|  |  |  | 				got, ok := ns.(*dns.SOA)
 | 
					
						
							|  |  |  | 				if !ok {
 | 
					
						
							|  |  |  | 					t.Errorf("Test %d: Unexpected NS type. Want: SOA, got: %v", ti, reflect.TypeOf(got))
 | 
					
						
							| 
									
										
										
										
											2018-03-13 00:06:50 -07:00
										 |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 				if got.String() != tc.wantNS[i] {
 | 
					
						
							|  |  |  | 					t.Errorf("Test %d: Unexpected NS. Want: %v, got: %v", ti, tc.wantNS[i], got)
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 				}
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 |