| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | package etcd
 | 
					
						
							| 
									
										
										
										
											2016-03-20 17:54:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-04-22 08:34:35 +01:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2016-03-20 18:17:07 +00:00
										 |  |  | 	"crypto/tls"
 | 
					
						
							| 
									
										
										
										
											2016-03-20 17:54:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/core/dnsserver"
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							| 
									
										
										
										
											2018-04-22 21:40:33 +01:00
										 |  |  | 	clog "github.com/coredns/coredns/plugin/pkg/log"
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	mwtls "github.com/coredns/coredns/plugin/pkg/tls"
 | 
					
						
							| 
									
										
										
										
											2018-02-14 15:11:26 -05:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/upstream"
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/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-20 17:54:21 +00:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-22 21:40:33 +01:00
										 |  |  | var log = clog.NewWithPlugin("etcd")
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		return plugin.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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.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{
 | 
					
						
							| 
									
										
										
										
											2017-03-16 14:52:30 +00:00
										 |  |  | 		// Don't default to a proxy for lookups.
 | 
					
						
							|  |  |  | 		//		Proxy:      proxy.NewLookup([]string{"8.8.8.8:53", "8.8.4.4:53"}),
 | 
					
						
							| 
									
										
										
										
											2016-03-22 22:44:50 +00:00
										 |  |  | 		PathPrefix: "skydns",
 | 
					
						
							|  |  |  | 		Ctx:        context.Background(),
 | 
					
						
							| 
									
										
										
										
											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 (
 | 
					
						
							| 
									
										
										
										
											2017-01-12 03:14:24 -05:00
										 |  |  | 		tlsConfig *tls.Config
 | 
					
						
							| 
									
										
										
										
											2017-01-15 08:12:58 +00:00
										 |  |  | 		err       error
 | 
					
						
							| 
									
										
										
										
											2017-01-12 03:14:24 -05:00
										 |  |  | 		endpoints = []string{defaultEndpoint}
 | 
					
						
							|  |  |  | 		stubzones = false
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 	)
 | 
					
						
							| 
									
										
										
										
											2016-03-20 17:54:21 +00:00
										 |  |  | 	for c.Next() {
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 		etc.Zones = c.RemainingArgs()
 | 
					
						
							|  |  |  | 		if len(etc.Zones) == 0 {
 | 
					
						
							|  |  |  | 			etc.Zones = make([]string, len(c.ServerBlockKeys))
 | 
					
						
							|  |  |  | 			copy(etc.Zones, c.ServerBlockKeys)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		for i, str := range etc.Zones {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 			etc.Zones[i] = plugin.Host(str).Normalize()
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-03-16 14:52:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 		if c.NextBlock() {
 | 
					
						
							|  |  |  | 			for {
 | 
					
						
							|  |  |  | 				switch c.Val() {
 | 
					
						
							|  |  |  | 				case "stubzones":
 | 
					
						
							|  |  |  | 					stubzones = true
 | 
					
						
							| 
									
										
										
										
											2017-08-14 08:49:26 +01:00
										 |  |  | 				case "fallthrough":
 | 
					
						
							| 
									
										
										
										
											2018-01-07 14:51:32 -05:00
										 |  |  | 					etc.Fall.SetZonesFromArgs(c.RemainingArgs())
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 				case "debug":
 | 
					
						
							| 
									
										
										
										
											2017-09-12 10:52:43 +01:00
										 |  |  | 					/* it is a noop now */
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 				case "path":
 | 
					
						
							|  |  |  | 					if !c.NextArg() {
 | 
					
						
							|  |  |  | 						return &Etcd{}, false, c.ArgErr()
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 					}
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 					etc.PathPrefix = c.Val()
 | 
					
						
							|  |  |  | 				case "endpoint":
 | 
					
						
							|  |  |  | 					args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 					if len(args) == 0 {
 | 
					
						
							|  |  |  | 						return &Etcd{}, false, c.ArgErr()
 | 
					
						
							|  |  |  | 					}
 | 
					
						
							|  |  |  | 					endpoints = args
 | 
					
						
							|  |  |  | 				case "upstream":
 | 
					
						
							|  |  |  | 					args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 					if len(args) == 0 {
 | 
					
						
							| 
									
										
										
										
											2018-02-14 15:11:26 -05:00
										 |  |  | 						return nil, false, c.ArgErr()
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 					}
 | 
					
						
							| 
									
										
										
										
											2018-02-14 15:11:26 -05:00
										 |  |  | 					u, err := upstream.NewUpstream(args)
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 					if err != nil {
 | 
					
						
							| 
									
										
										
										
											2018-02-14 15:11:26 -05:00
										 |  |  | 						return nil, false, err
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 					}
 | 
					
						
							| 
									
										
										
										
											2018-02-14 15:11:26 -05:00
										 |  |  | 					etc.Upstream = u
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 				case "tls": // cert key cacertfile
 | 
					
						
							|  |  |  | 					args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 					tlsConfig, err = mwtls.NewTLSConfigFromArgs(args...)
 | 
					
						
							|  |  |  | 					if err != nil {
 | 
					
						
							|  |  |  | 						return &Etcd{}, false, err
 | 
					
						
							|  |  |  | 					}
 | 
					
						
							|  |  |  | 				default:
 | 
					
						
							|  |  |  | 					if c.Val() != "}" {
 | 
					
						
							|  |  |  | 						return &Etcd{}, false, c.Errf("unknown property '%s'", c.Val())
 | 
					
						
							| 
									
										
										
										
											2017-07-01 16:17:09 -04:00
										 |  |  | 					}
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2016-09-26 14:43:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 				if !c.Next() {
 | 
					
						
							|  |  |  | 					break
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-10-30 15:54:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 17:54:21 +00:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 		client, err := newEtcdClient(endpoints, tlsConfig)
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			return &Etcd{}, false, err
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		etc.Client = client
 | 
					
						
							|  |  |  | 		etc.endpoints = endpoints
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		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
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 03:14:24 -05:00
										 |  |  | func newEtcdClient(endpoints []string, cc *tls.Config) (etcdc.KeysAPI, error) {
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | 	etcdCfg := etcdc.Config{
 | 
					
						
							|  |  |  | 		Endpoints: endpoints,
 | 
					
						
							| 
									
										
										
										
											2017-07-01 16:17:53 -04:00
										 |  |  | 		Transport: mwtls.NewHTTPSTransport(cc),
 | 
					
						
							| 
									
										
										
										
											2016-03-20 18:17:07 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											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-08-19 17:14:17 -07:00
										 |  |  | const defaultEndpoint = "http://localhost:2379"
 |