From 136efeb3be90a4368ac74fc433e041ec2a40b22f Mon Sep 17 00:00:00 2001 From: Matt Keeler Date: Thu, 12 Apr 2018 10:10:37 -0400 Subject: [PATCH] GH-3798: A couple more PR updates Test HTTP/DNS source IP without header/extra EDNS data. Add WARN log for when prepared query with near=_ip is executed without specifying the source ip --- agent/consul/prepared_query_endpoint.go | 5 +++++ agent/dns_test.go | 29 +++++++++++++++++++++++-- agent/prepared_query_endpoint_test.go | 18 +++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/agent/consul/prepared_query_endpoint.go b/agent/consul/prepared_query_endpoint.go index e776b4f9de..fea3ec9d3c 100644 --- a/agent/consul/prepared_query_endpoint.go +++ b/agent/consul/prepared_query_endpoint.go @@ -406,6 +406,11 @@ func (p *PreparedQuery) Execute(args *structs.PreparedQueryExecuteRequest, break } } + } else { + p.srv.logger.Printf("[WARN] Prepared Query using near=_ip requires "+ + "the source IP to be set but none was provided. No distance "+ + "sorting will be done.") + } // Either a source IP was given but we couldnt find the associated node diff --git a/agent/dns_test.go b/agent/dns_test.go index 934a66517c..5639557a56 100644 --- a/agent/dns_test.go +++ b/agent/dns_test.go @@ -1890,7 +1890,7 @@ func TestDNS_PreparedQueryNearIP(t *testing.T) { args := &structs.RegisterRequest{ Datacenter: "dc1", Node: "bar", - Address: "198.18.0.9", + Address: "127.0.0.1", } var out struct{} @@ -1937,7 +1937,7 @@ func TestDNS_PreparedQueryNearIP(t *testing.T) { e.Family = 1 e.SourceNetmask = 32 e.SourceScope = 0 - e.Address = net.ParseIP("198.18.0.9").To4() + e.Address = net.ParseIP("127.0.0.1").To4() o.Option = append(o.Option, e) m.Extra = append(m.Extra, o) @@ -1961,6 +1961,31 @@ func TestDNS_PreparedQueryNearIP(t *testing.T) { } } }) + + retry.Run(t, func(r *retry.R) { + m := new(dns.Msg) + m.SetQuestion("some.query.we.like.query.consul.", dns.TypeA) + + c := new(dns.Client) + in, _, err := c.Exchange(m, a.DNSAddr()) + if err != nil { + r.Fatalf("Error with call to dns.Client.Exchange: %s", err) + } + + if len(serviceNodes) != len(in.Answer) { + r.Fatalf("Expecting %d A RRs in response, Actual found was %d", len(serviceNodes), len(in.Answer)) + } + + for i, rr := range in.Answer { + if aRec, ok := rr.(*dns.A); ok { + if actual := aRec.A.String(); serviceNodes[i].address != actual { + r.Fatalf("Expecting A RR #%d = %s, Actual RR was %s", i, serviceNodes[i].address, actual) + } + } else { + r.Fatalf("DNS Answer contained a non-A RR") + } + } + }) } func TestDNS_ServiceLookup_PreparedQueryNamePeriod(t *testing.T) { diff --git a/agent/prepared_query_endpoint_test.go b/agent/prepared_query_endpoint_test.go index 350f1a5590..9fa685a658 100644 --- a/agent/prepared_query_endpoint_test.go +++ b/agent/prepared_query_endpoint_test.go @@ -380,6 +380,24 @@ func TestPreparedQuery_Execute(t *testing.T) { if r.Failovers != 99 { t.Fatalf("bad: %v", r) } + + req, _ = http.NewRequest("GET", "/v1/query/my-id/execute?token=my-token&consistent=true&near=_ip&limit=5", body) + req.RemoteAddr = "127.0.0.1:12345" + resp = httptest.NewRecorder() + obj, err = a.srv.PreparedQuerySpecific(resp, req) + if err != nil { + t.Fatalf("err: %v", err) + } + if resp.Code != 200 { + t.Fatalf("bad code: %d", resp.Code) + } + r, ok = obj.(structs.PreparedQueryExecuteResponse) + if !ok { + t.Fatalf("unexpected: %T", obj) + } + if r.Failovers != 99 { + t.Fatalf("bad: %v", r) + } }) // Ensure the proper params are set when no special args are passed