| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | package etcd
 | 
					
						
							| 
									
										
										
										
											2016-03-20 17:54:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2016-03-20 18:17:07 +00:00
										 |  |  | 	"crypto/tls"
 | 
					
						
							|  |  |  | 	"crypto/x509"
 | 
					
						
							|  |  |  | 	"io/ioutil"
 | 
					
						
							|  |  |  | 	"net"
 | 
					
						
							|  |  |  | 	"net/http"
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							| 
									
										
										
										
											2016-03-20 17:54:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	"github.com/miekg/coredns/core/dnsserver"
 | 
					
						
							| 
									
										
										
										
											2016-03-20 17:54:21 +00:00
										 |  |  | 	"github.com/miekg/coredns/middleware"
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	"github.com/miekg/coredns/middleware/pkg/singleflight"
 | 
					
						
							| 
									
										
										
										
											2016-03-22 22:44:50 +00:00
										 |  |  | 	"github.com/miekg/coredns/middleware/proxy"
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	etcdc "github.com/coreos/etcd/client"
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	"github.com/mholt/caddy"
 | 
					
						
							| 
									
										
										
										
											2016-03-22 22:44:50 +00:00
										 |  |  | 	"golang.org/x/net/context"
 | 
					
						
							| 
									
										
										
										
											2016-03-20 17:54:21 +00:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | func init() {
 | 
					
						
							|  |  |  | 	caddy.RegisterPlugin("etcd", caddy.Plugin{
 | 
					
						
							|  |  |  | 		ServerType: "dns",
 | 
					
						
							|  |  |  | 		Action:     setup,
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-03-20 18:17:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | func setup(c *caddy.Controller) error {
 | 
					
						
							|  |  |  | 	e, stubzones, err := etcdParse(c)
 | 
					
						
							| 
									
										
										
										
											2016-03-20 17:54:21 +00:00
										 |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-09-10 09:16:25 +01:00
										 |  |  | 		return middleware.Error("etcd", err)
 | 
					
						
							| 
									
										
										
										
											2016-03-20 17:54:21 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-10-30 15:54:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 	if stubzones {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		c.OnStartup(func() error {
 | 
					
						
							|  |  |  | 			e.UpdateStubZones()
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 			return nil
 | 
					
						
							|  |  |  | 		})
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-20 18:17:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 11:26:00 +01:00
										 |  |  | 	dnsserver.GetConfig(c).AddMiddleware(func(next middleware.Handler) middleware.Handler {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		e.Next = next
 | 
					
						
							|  |  |  | 		return e
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							| 
									
										
										
										
											2016-03-20 17:54:21 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | func etcdParse(c *caddy.Controller) (*Etcd, bool, error) {
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 	stub := make(map[string]proxy.Proxy)
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	etc := Etcd{
 | 
					
						
							| 
									
										
										
										
											2016-04-11 15:56:22 +01:00
										 |  |  | 		Proxy:      proxy.New([]string{"8.8.8.8:53", "8.8.4.4:53"}),
 | 
					
						
							| 
									
										
										
										
											2016-03-22 22:44:50 +00:00
										 |  |  | 		PathPrefix: "skydns",
 | 
					
						
							|  |  |  | 		Ctx:        context.Background(),
 | 
					
						
							|  |  |  | 		Inflight:   &singleflight.Group{},
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 		Stubmap:    &stub,
 | 
					
						
							| 
									
										
										
										
											2016-03-22 22:44:50 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 	var (
 | 
					
						
							|  |  |  | 		tlsCertFile   = ""
 | 
					
						
							|  |  |  | 		tlsKeyFile    = ""
 | 
					
						
							|  |  |  | 		tlsCAcertFile = ""
 | 
					
						
							|  |  |  | 		endpoints     = []string{defaultEndpoint}
 | 
					
						
							|  |  |  | 		stubzones     = false
 | 
					
						
							|  |  |  | 	)
 | 
					
						
							| 
									
										
										
										
											2016-03-20 17:54:21 +00:00
										 |  |  | 	for c.Next() {
 | 
					
						
							| 
									
										
										
										
											2016-03-20 18:17:07 +00:00
										 |  |  | 		if c.Val() == "etcd" {
 | 
					
						
							| 
									
										
										
										
											2016-03-22 22:44:50 +00:00
										 |  |  | 			etc.Zones = c.RemainingArgs()
 | 
					
						
							|  |  |  | 			if len(etc.Zones) == 0 {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 				etc.Zones = make([]string, len(c.ServerBlockKeys))
 | 
					
						
							|  |  |  | 				copy(etc.Zones, c.ServerBlockKeys)
 | 
					
						
							| 
									
										
										
										
											2016-03-20 17:54:21 +00:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 			middleware.Zones(etc.Zones).Normalize()
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 			if c.NextBlock() {
 | 
					
						
							|  |  |  | 				// TODO(miek): 2 switches?
 | 
					
						
							|  |  |  | 				switch c.Val() {
 | 
					
						
							|  |  |  | 				case "stubzones":
 | 
					
						
							|  |  |  | 					stubzones = true
 | 
					
						
							| 
									
										
											  
											
												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
										 |  |  | 				case "debug":
 | 
					
						
							| 
									
										
										
										
											2016-10-30 15:54:16 +00:00
										 |  |  | 					etc.Debugging = true
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 				case "path":
 | 
					
						
							|  |  |  | 					if !c.NextArg() {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 						return &Etcd{}, false, c.ArgErr()
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 					}
 | 
					
						
							|  |  |  | 					etc.PathPrefix = c.Val()
 | 
					
						
							|  |  |  | 				case "endpoint":
 | 
					
						
							|  |  |  | 					args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 					if len(args) == 0 {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 						return &Etcd{}, false, c.ArgErr()
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 					}
 | 
					
						
							|  |  |  | 					endpoints = args
 | 
					
						
							|  |  |  | 				case "upstream":
 | 
					
						
							|  |  |  | 					args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 					if len(args) == 0 {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 						return &Etcd{}, false, c.ArgErr()
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 					}
 | 
					
						
							|  |  |  | 					for i := 0; i < len(args); i++ {
 | 
					
						
							|  |  |  | 						h, p, e := net.SplitHostPort(args[i])
 | 
					
						
							|  |  |  | 						if e != nil && p == "" {
 | 
					
						
							|  |  |  | 							args[i] = h + ":53"
 | 
					
						
							|  |  |  | 						}
 | 
					
						
							|  |  |  | 					}
 | 
					
						
							|  |  |  | 					etc.Proxy = proxy.New(args)
 | 
					
						
							|  |  |  | 				case "tls": // cert key cacertfile
 | 
					
						
							|  |  |  | 					args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 					if len(args) != 3 {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 						return &Etcd{}, false, c.ArgErr()
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 					}
 | 
					
						
							|  |  |  | 					tlsCertFile, tlsKeyFile, tlsCAcertFile = args[0], args[1], args[2]
 | 
					
						
							| 
									
										
										
										
											2016-09-26 14:43:38 +01:00
										 |  |  | 				default:
 | 
					
						
							|  |  |  | 					if c.Val() != "}" {
 | 
					
						
							|  |  |  | 						return &Etcd{}, false, c.Errf("unknown property '%s'", c.Val())
 | 
					
						
							|  |  |  | 					}
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 				}
 | 
					
						
							|  |  |  | 				for c.Next() {
 | 
					
						
							|  |  |  | 					switch c.Val() {
 | 
					
						
							|  |  |  | 					case "stubzones":
 | 
					
						
							|  |  |  | 						stubzones = true
 | 
					
						
							| 
									
										
											  
											
												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
										 |  |  | 					case "debug":
 | 
					
						
							| 
									
										
										
										
											2016-10-30 15:54:16 +00:00
										 |  |  | 						etc.Debugging = true
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 					case "path":
 | 
					
						
							|  |  |  | 						if !c.NextArg() {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 							return &Etcd{}, false, c.ArgErr()
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 						}
 | 
					
						
							|  |  |  | 						etc.PathPrefix = c.Val()
 | 
					
						
							|  |  |  | 					case "endpoint":
 | 
					
						
							|  |  |  | 						args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 						if len(args) == 0 {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 							return &Etcd{}, false, c.ArgErr()
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 						}
 | 
					
						
							|  |  |  | 						endpoints = args
 | 
					
						
							|  |  |  | 					case "upstream":
 | 
					
						
							|  |  |  | 						args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 						if len(args) == 0 {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 							return &Etcd{}, false, c.ArgErr()
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 						}
 | 
					
						
							|  |  |  | 						for i := 0; i < len(args); i++ {
 | 
					
						
							|  |  |  | 							h, p, e := net.SplitHostPort(args[i])
 | 
					
						
							|  |  |  | 							if e != nil && p == "" {
 | 
					
						
							|  |  |  | 								args[i] = h + ":53"
 | 
					
						
							|  |  |  | 							}
 | 
					
						
							|  |  |  | 						}
 | 
					
						
							|  |  |  | 						etc.Proxy = proxy.New(args)
 | 
					
						
							|  |  |  | 					case "tls": // cert key cacertfile
 | 
					
						
							|  |  |  | 						args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 						if len(args) != 3 {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 							return &Etcd{}, false, c.ArgErr()
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 						}
 | 
					
						
							|  |  |  | 						tlsCertFile, tlsKeyFile, tlsCAcertFile = args[0], args[1], args[2]
 | 
					
						
							| 
									
										
										
										
											2016-09-26 14:43:38 +01:00
										 |  |  | 					default:
 | 
					
						
							|  |  |  | 						if c.Val() != "}" { // TODO(miek): this feels like I'm doing it completely wrong.
 | 
					
						
							|  |  |  | 							return &Etcd{}, false, c.Errf("unknown property '%s'", c.Val())
 | 
					
						
							|  |  |  | 						}
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 					}
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2016-09-26 14:43:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 			client, err := newEtcdClient(endpoints, tlsCertFile, tlsKeyFile, tlsCAcertFile)
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 				return &Etcd{}, false, err
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 			etc.Client = client
 | 
					
						
							| 
									
										
										
										
											2016-09-26 14:43:38 +01:00
										 |  |  | 			etc.endpoints = endpoints
 | 
					
						
							| 
									
										
										
										
											2016-10-30 15:54:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-08 19:18:55 -07:00
										 |  |  | 			return &etc, stubzones, nil
 | 
					
						
							| 
									
										
										
										
											2016-03-20 17:54:21 +00:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	return &Etcd{}, false, nil
 | 
					
						
							| 
									
										
										
										
											2016-03-20 17:54:21 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | func newEtcdClient(endpoints []string, tlsCert, tlsKey, tlsCACert string) (etcdc.KeysAPI, error) {
 | 
					
						
							|  |  |  | 	etcdCfg := etcdc.Config{
 | 
					
						
							|  |  |  | 		Endpoints: endpoints,
 | 
					
						
							| 
									
										
										
										
											2016-03-20 18:17:07 +00:00
										 |  |  | 		Transport: newHTTPSTransport(tlsCert, tlsKey, tlsCACert),
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | 	cli, err := etcdc.New(etcdCfg)
 | 
					
						
							| 
									
										
										
										
											2016-03-20 17:54:21 +00:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | 	return etcdc.NewKeysAPI(cli), nil
 | 
					
						
							| 
									
										
										
										
											2016-03-20 18:17:07 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | func newHTTPSTransport(tlsCertFile, tlsKeyFile, tlsCACertFile string) etcdc.CancelableTransport {
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | 	var cc *tls.Config
 | 
					
						
							| 
									
										
										
										
											2016-03-20 18:17:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if tlsCertFile != "" && tlsKeyFile != "" {
 | 
					
						
							|  |  |  | 		var rpool *x509.CertPool
 | 
					
						
							|  |  |  | 		if tlsCACertFile != "" {
 | 
					
						
							|  |  |  | 			if pemBytes, err := ioutil.ReadFile(tlsCACertFile); err == nil {
 | 
					
						
							|  |  |  | 				rpool = x509.NewCertPool()
 | 
					
						
							|  |  |  | 				rpool.AppendCertsFromPEM(pemBytes)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if tlsCert, err := tls.LoadX509KeyPair(tlsCertFile, tlsKeyFile); err == nil {
 | 
					
						
							|  |  |  | 			cc = &tls.Config{
 | 
					
						
							|  |  |  | 				RootCAs:            rpool,
 | 
					
						
							|  |  |  | 				Certificates:       []tls.Certificate{tlsCert},
 | 
					
						
							|  |  |  | 				InsecureSkipVerify: true,
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-03-20 17:54:21 +00:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 18:17:07 +00:00
										 |  |  | 	tr := &http.Transport{
 | 
					
						
							|  |  |  | 		Proxy: http.ProxyFromEnvironment,
 | 
					
						
							|  |  |  | 		Dial: (&net.Dialer{
 | 
					
						
							|  |  |  | 			Timeout:   30 * time.Second,
 | 
					
						
							|  |  |  | 			KeepAlive: 30 * time.Second,
 | 
					
						
							|  |  |  | 		}).Dial,
 | 
					
						
							|  |  |  | 		TLSHandshakeTimeout: 10 * time.Second,
 | 
					
						
							|  |  |  | 		TLSClientConfig:     cc,
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return tr
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | const defaultEndpoint = "http://localhost:2379"
 |