2016-03-20 21:36:55 +00:00
|
|
|
package etcd
|
|
|
|
|
|
|
|
|
|
import (
|
2017-02-21 22:51:47 -08:00
|
|
|
"github.com/coredns/coredns/middleware"
|
|
|
|
|
"github.com/coredns/coredns/middleware/pkg/dnsutil"
|
|
|
|
|
"github.com/coredns/coredns/request"
|
2016-03-22 22:44:50 +00:00
|
|
|
|
2016-03-20 21:36:55 +00:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
)
|
|
|
|
|
|
2016-09-23 09:14:12 +01:00
|
|
|
// ServeDNS implements the middleware.Handler interface.
|
2016-08-08 19:18:55 -07:00
|
|
|
func (e *Etcd) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
2016-10-30 15:54:16 +00:00
|
|
|
opt := middleware.Options{}
|
2016-09-07 11:10:16 +01:00
|
|
|
state := request.Request{W: w, Req: r}
|
2017-08-25 08:55:53 +01:00
|
|
|
|
Allow debug queries to etcd middleware (#150)
With this you can retreive the raw data that the etcd middleware
used to create the reply. The debug data is put in TXT records
that are stuffed in the CH classs. This is only enabled if you
specify `debug` in the etcd stanza.
You can retrieve it by prefixing your query with 'o-o.debug.'
For instance:
; <<>> DiG 9.10.3-P4-Ubuntu <<>> @localhost -p 1053 SRV o-o.debug.production.*.skydns.local
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 47798
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 3
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;o-o.debug.production.*.skydns.local. IN SRV
;; ANSWER SECTION:
production.*.skydns.local. 154 IN SRV 10 50 8080 service1.example.com.
production.*.skydns.local. 154 IN SRV 10 50 8080 service2.example.com.
;; ADDITIONAL SECTION:
skydns.local.skydns.east.production.rails.1. 154 CH TXT "service1.example.com:8080(10,0,,false)[0,]"
skydns.local.skydns.west.production.rails.2. 154 CH TXT "service2.example.com:8080(10,0,,false)[0,]"
2016-05-22 21:16:26 +01:00
|
|
|
name := state.Name()
|
2016-03-25 20:26:42 +00:00
|
|
|
|
|
|
|
|
// We need to check stubzones first, because we may get a request for a zone we
|
|
|
|
|
// are not auth. for *but* do have a stubzone forward for. If we do the stubzone
|
|
|
|
|
// handler will handle the request.
|
2016-03-28 10:49:28 +01:00
|
|
|
if e.Stubmap != nil && len(*e.Stubmap) > 0 {
|
2016-06-11 08:00:47 +10:00
|
|
|
for zone := range *e.Stubmap {
|
2016-04-12 23:26:46 +01:00
|
|
|
if middleware.Name(zone).Matches(name) {
|
2016-03-25 20:26:42 +00:00
|
|
|
stub := Stub{Etcd: e, Zone: zone}
|
|
|
|
|
return stub.ServeDNS(ctx, w, r)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-22 11:04:56 +00:00
|
|
|
zone := middleware.Zones(e.Zones).Matches(state.Name())
|
|
|
|
|
if zone == "" {
|
2017-09-07 18:21:37 +01:00
|
|
|
return middleware.NextOrFailure(e.Name(), e.Next, ctx, w, r)
|
2016-03-22 11:04:56 +00:00
|
|
|
}
|
2016-03-20 21:36:55 +00:00
|
|
|
|
2016-03-22 11:04:56 +00:00
|
|
|
var (
|
|
|
|
|
records, extra []dns.RR
|
|
|
|
|
err error
|
|
|
|
|
)
|
2016-03-22 10:29:48 +00:00
|
|
|
switch state.Type() {
|
|
|
|
|
case "A":
|
2017-09-12 10:52:43 +01:00
|
|
|
records, err = middleware.A(e, zone, state, nil, opt)
|
2016-03-22 10:29:48 +00:00
|
|
|
case "AAAA":
|
2017-09-12 10:52:43 +01:00
|
|
|
records, err = middleware.AAAA(e, zone, state, nil, opt)
|
2016-03-22 10:29:48 +00:00
|
|
|
case "TXT":
|
2017-09-12 10:52:43 +01:00
|
|
|
records, err = middleware.TXT(e, zone, state, opt)
|
2016-03-22 10:29:48 +00:00
|
|
|
case "CNAME":
|
2017-09-12 10:52:43 +01:00
|
|
|
records, err = middleware.CNAME(e, zone, state, opt)
|
2016-06-08 10:29:46 +01:00
|
|
|
case "PTR":
|
2017-09-12 10:52:43 +01:00
|
|
|
records, err = middleware.PTR(e, zone, state, opt)
|
2016-03-22 10:29:48 +00:00
|
|
|
case "MX":
|
2017-09-12 10:52:43 +01:00
|
|
|
records, extra, err = middleware.MX(e, zone, state, opt)
|
2016-03-22 10:29:48 +00:00
|
|
|
case "SRV":
|
2017-09-12 10:52:43 +01:00
|
|
|
records, extra, err = middleware.SRV(e, zone, state, opt)
|
2016-04-12 23:26:46 +01:00
|
|
|
case "SOA":
|
2017-09-12 10:52:43 +01:00
|
|
|
records, err = middleware.SOA(e, zone, state, opt)
|
2016-04-12 23:26:46 +01:00
|
|
|
case "NS":
|
2016-04-13 08:03:56 +01:00
|
|
|
if state.Name() == zone {
|
2017-09-12 10:52:43 +01:00
|
|
|
records, extra, err = middleware.NS(e, zone, state, opt)
|
2016-04-13 08:03:56 +01:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
fallthrough
|
2016-03-22 10:29:48 +00:00
|
|
|
default:
|
Allow debug queries to etcd middleware (#150)
With this you can retreive the raw data that the etcd middleware
used to create the reply. The debug data is put in TXT records
that are stuffed in the CH classs. This is only enabled if you
specify `debug` in the etcd stanza.
You can retrieve it by prefixing your query with 'o-o.debug.'
For instance:
; <<>> DiG 9.10.3-P4-Ubuntu <<>> @localhost -p 1053 SRV o-o.debug.production.*.skydns.local
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 47798
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 3
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;o-o.debug.production.*.skydns.local. IN SRV
;; ANSWER SECTION:
production.*.skydns.local. 154 IN SRV 10 50 8080 service1.example.com.
production.*.skydns.local. 154 IN SRV 10 50 8080 service2.example.com.
;; ADDITIONAL SECTION:
skydns.local.skydns.east.production.rails.1. 154 CH TXT "service1.example.com:8080(10,0,,false)[0,]"
skydns.local.skydns.west.production.rails.2. 154 CH TXT "service2.example.com:8080(10,0,,false)[0,]"
2016-05-22 21:16:26 +01:00
|
|
|
// Do a fake A lookup, so we can distinguish between NODATA and NXDOMAIN
|
2017-09-12 10:52:43 +01:00
|
|
|
_, err = middleware.A(e, zone, state, nil, opt)
|
Allow debug queries to etcd middleware (#150)
With this you can retreive the raw data that the etcd middleware
used to create the reply. The debug data is put in TXT records
that are stuffed in the CH classs. This is only enabled if you
specify `debug` in the etcd stanza.
You can retrieve it by prefixing your query with 'o-o.debug.'
For instance:
; <<>> DiG 9.10.3-P4-Ubuntu <<>> @localhost -p 1053 SRV o-o.debug.production.*.skydns.local
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 47798
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 3
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;o-o.debug.production.*.skydns.local. IN SRV
;; ANSWER SECTION:
production.*.skydns.local. 154 IN SRV 10 50 8080 service1.example.com.
production.*.skydns.local. 154 IN SRV 10 50 8080 service2.example.com.
;; ADDITIONAL SECTION:
skydns.local.skydns.east.production.rails.1. 154 CH TXT "service1.example.com:8080(10,0,,false)[0,]"
skydns.local.skydns.west.production.rails.2. 154 CH TXT "service2.example.com:8080(10,0,,false)[0,]"
2016-05-22 21:16:26 +01:00
|
|
|
}
|
|
|
|
|
|
2016-10-30 15:54:16 +00:00
|
|
|
if e.IsNameError(err) {
|
2017-09-07 18:21:37 +01:00
|
|
|
if e.Fallthrough {
|
|
|
|
|
return middleware.NextOrFailure(e.Name(), e.Next, ctx, w, r)
|
|
|
|
|
}
|
2017-03-06 11:43:23 +00:00
|
|
|
// Make err nil when returning here, so we don't log spam for NXDOMAIN.
|
2017-09-12 10:52:43 +01:00
|
|
|
return middleware.BackendError(e, zone, dns.RcodeNameError, state, nil /* err */, opt)
|
2016-03-22 11:04:56 +00:00
|
|
|
}
|
|
|
|
|
if err != nil {
|
2017-09-12 10:52:43 +01:00
|
|
|
return middleware.BackendError(e, zone, dns.RcodeServerFailure, state, err, opt)
|
2016-03-20 21:36:55 +00:00
|
|
|
}
|
2016-03-23 22:59:05 +00:00
|
|
|
|
|
|
|
|
if len(records) == 0 {
|
2017-09-12 10:52:43 +01:00
|
|
|
return middleware.BackendError(e, zone, dns.RcodeSuccess, state, err, opt)
|
2016-03-22 11:04:56 +00:00
|
|
|
}
|
2016-03-25 10:32:12 +00:00
|
|
|
|
Allow debug queries to etcd middleware (#150)
With this you can retreive the raw data that the etcd middleware
used to create the reply. The debug data is put in TXT records
that are stuffed in the CH classs. This is only enabled if you
specify `debug` in the etcd stanza.
You can retrieve it by prefixing your query with 'o-o.debug.'
For instance:
; <<>> DiG 9.10.3-P4-Ubuntu <<>> @localhost -p 1053 SRV o-o.debug.production.*.skydns.local
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 47798
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 3
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;o-o.debug.production.*.skydns.local. IN SRV
;; ANSWER SECTION:
production.*.skydns.local. 154 IN SRV 10 50 8080 service1.example.com.
production.*.skydns.local. 154 IN SRV 10 50 8080 service2.example.com.
;; ADDITIONAL SECTION:
skydns.local.skydns.east.production.rails.1. 154 CH TXT "service1.example.com:8080(10,0,,false)[0,]"
skydns.local.skydns.west.production.rails.2. 154 CH TXT "service2.example.com:8080(10,0,,false)[0,]"
2016-05-22 21:16:26 +01:00
|
|
|
m := new(dns.Msg)
|
|
|
|
|
m.SetReply(r)
|
|
|
|
|
m.Authoritative, m.RecursionAvailable, m.Compress = true, true, true
|
2016-04-09 16:17:53 +01:00
|
|
|
m.Answer = append(m.Answer, records...)
|
|
|
|
|
m.Extra = append(m.Extra, extra...)
|
|
|
|
|
|
2016-09-23 09:14:12 +01:00
|
|
|
m = dnsutil.Dedup(m)
|
2016-04-09 16:17:53 +01:00
|
|
|
state.SizeAndDo(m)
|
2016-03-26 09:26:54 +00:00
|
|
|
m, _ = state.Scrub(m)
|
2016-04-09 16:17:53 +01:00
|
|
|
w.WriteMsg(m)
|
|
|
|
|
return dns.RcodeSuccess, nil
|
2016-03-22 10:29:48 +00:00
|
|
|
}
|
2016-03-20 21:36:55 +00:00
|
|
|
|
2016-10-27 11:48:37 +00:00
|
|
|
// Name implements the Handler interface.
|
2016-10-26 10:01:52 +01:00
|
|
|
func (e *Etcd) Name() string { return "etcd" }
|