| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-03 09:04:47 +08:00
										 |  |  | 	"github.com/caddyserver/caddy"
 | 
					
						
							| 
									
										
										
										
											2019-08-27 16:58:35 +01:00
										 |  |  | 	etcdcv3 "go.etcd.io/etcd/clientv3"
 | 
					
						
							| 
									
										
										
										
											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 {
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 	e, 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | func etcdParse(c *caddy.Controller) (*Etcd, error) {
 | 
					
						
							| 
									
										
										
										
											2019-03-26 14:37:30 +00:00
										 |  |  | 	etc := Etcd{PathPrefix: "skydns"}
 | 
					
						
							| 
									
										
										
										
											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}
 | 
					
						
							| 
									
										
										
										
											2019-02-01 16:30:53 +01:00
										 |  |  | 		username  string
 | 
					
						
							|  |  |  | 		password  string
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 	)
 | 
					
						
							| 
									
										
										
										
											2019-07-02 16:23:47 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	etc.Upstream = upstream.New()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-18 15:17:46 +08:00
										 |  |  | 		for c.NextBlock() {
 | 
					
						
							|  |  |  | 			switch c.Val() {
 | 
					
						
							|  |  |  | 			case "stubzones":
 | 
					
						
							|  |  |  | 				// ignored, remove later.
 | 
					
						
							|  |  |  | 			case "fallthrough":
 | 
					
						
							|  |  |  | 				etc.Fall.SetZonesFromArgs(c.RemainingArgs())
 | 
					
						
							|  |  |  | 			case "debug":
 | 
					
						
							|  |  |  | 				/* it is a noop now */
 | 
					
						
							|  |  |  | 			case "path":
 | 
					
						
							|  |  |  | 				if !c.NextArg() {
 | 
					
						
							|  |  |  | 					return &Etcd{}, c.ArgErr()
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2019-03-18 15:17:46 +08:00
										 |  |  | 				etc.PathPrefix = c.Val()
 | 
					
						
							|  |  |  | 			case "endpoint":
 | 
					
						
							|  |  |  | 				args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 				if len(args) == 0 {
 | 
					
						
							|  |  |  | 					return &Etcd{}, c.ArgErr()
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 				endpoints = args
 | 
					
						
							|  |  |  | 			case "upstream":
 | 
					
						
							| 
									
										
										
										
											2019-07-02 16:23:47 +01:00
										 |  |  | 				// remove soon
 | 
					
						
							|  |  |  | 				c.RemainingArgs()
 | 
					
						
							| 
									
										
										
										
											2019-03-18 15:17:46 +08:00
										 |  |  | 			case "tls": // cert key cacertfile
 | 
					
						
							|  |  |  | 				args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 				tlsConfig, err = mwtls.NewTLSConfigFromArgs(args...)
 | 
					
						
							|  |  |  | 				if err != nil {
 | 
					
						
							|  |  |  | 					return &Etcd{}, err
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 			case "credentials":
 | 
					
						
							|  |  |  | 				args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 				if len(args) == 0 {
 | 
					
						
							|  |  |  | 					return &Etcd{}, c.ArgErr()
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 				if len(args) != 2 {
 | 
					
						
							|  |  |  | 					return &Etcd{}, c.Errf("credentials requires 2 arguments, username and password")
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 				username, password = args[0], args[1]
 | 
					
						
							|  |  |  | 			default:
 | 
					
						
							|  |  |  | 				if c.Val() != "}" {
 | 
					
						
							|  |  |  | 					return &Etcd{}, c.Errf("unknown property '%s'", c.Val())
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-03-20 17:54:21 +00:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2019-02-01 16:30:53 +01:00
										 |  |  | 		client, err := newEtcdClient(endpoints, tlsConfig, username, password)
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 		if err != nil {
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 			return &Etcd{}, err
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 		etc.Client = client
 | 
					
						
							|  |  |  | 		etc.endpoints = endpoints
 | 
					
						
							| 
									
										
										
										
											2018-07-09 13:08:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 		return &etc, nil
 | 
					
						
							| 
									
										
										
										
											2016-03-20 17:54:21 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 	return &Etcd{}, nil
 | 
					
						
							| 
									
										
										
										
											2016-03-20 17:54:21 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-01 16:30:53 +01:00
										 |  |  | func newEtcdClient(endpoints []string, cc *tls.Config, username, password string) (*etcdcv3.Client, error) {
 | 
					
						
							| 
									
										
										
										
											2018-06-30 20:49:13 +05:30
										 |  |  | 	etcdCfg := etcdcv3.Config{
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | 		Endpoints: endpoints,
 | 
					
						
							| 
									
										
										
										
											2018-06-30 20:49:13 +05:30
										 |  |  | 		TLS:       cc,
 | 
					
						
							| 
									
										
										
										
											2016-03-20 18:17:07 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2019-02-01 16:30:53 +01:00
										 |  |  | 	if username != "" && password != "" {
 | 
					
						
							|  |  |  | 		etcdCfg.Username = username
 | 
					
						
							|  |  |  | 		etcdCfg.Password = password
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-06-30 20:49:13 +05:30
										 |  |  | 	cli, err := etcdcv3.New(etcdCfg)
 | 
					
						
							| 
									
										
										
										
											2016-03-20 17:54:21 +00:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-06-30 20:49:13 +05:30
										 |  |  | 	return cli, nil
 | 
					
						
							| 
									
										
										
										
											2016-03-20 18:17:07 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | const defaultEndpoint = "http://localhost:2379"
 |