2016-03-20 17:54:21 +00:00
|
|
|
package setup
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
"github.com/miekg/coredns/middleware"
|
2016-03-20 21:36:55 +00:00
|
|
|
"github.com/miekg/coredns/middleware/etcd"
|
|
|
|
|
|
|
|
|
|
etcdc "github.com/coreos/etcd/client"
|
2016-03-20 17:54:21 +00:00
|
|
|
)
|
|
|
|
|
|
2016-03-20 21:36:55 +00:00
|
|
|
const defaultEndpoint = "http://127.0.0.1:2379"
|
2016-03-20 18:17:07 +00:00
|
|
|
|
|
|
|
|
// Etcd sets up the etcd middleware.
|
|
|
|
|
func Etcd(c *Controller) (middleware.Middleware, error) {
|
2016-03-20 21:36:55 +00:00
|
|
|
client, err := etcdParse(c)
|
2016-03-20 17:54:21 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2016-03-20 18:17:07 +00:00
|
|
|
|
2016-03-20 17:54:21 +00:00
|
|
|
return func(next middleware.Handler) middleware.Handler {
|
2016-03-20 21:36:55 +00:00
|
|
|
return etcd.NewEtcd(client, next, c.ServerBlockHosts)
|
2016-03-20 17:54:21 +00:00
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-20 18:17:07 +00:00
|
|
|
func etcdParse(c *Controller) (etcdc.KeysAPI, error) {
|
2016-03-20 17:54:21 +00:00
|
|
|
for c.Next() {
|
2016-03-20 18:17:07 +00:00
|
|
|
if c.Val() == "etcd" {
|
|
|
|
|
// etcd [address...]
|
2016-03-20 17:54:21 +00:00
|
|
|
if !c.NextArg() {
|
2016-03-20 21:36:55 +00:00
|
|
|
// TODO(certs) and friends, this is client side
|
|
|
|
|
client, err := newEtcdClient([]string{defaultEndpoint}, "", "", "")
|
|
|
|
|
return client, err
|
2016-03-20 17:54:21 +00:00
|
|
|
}
|
2016-03-20 21:36:55 +00:00
|
|
|
client, err := newEtcdClient(c.RemainingArgs(), "", "", "")
|
|
|
|
|
return client, err
|
2016-03-20 17:54:21 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-03-20 21:36:55 +00:00
|
|
|
return nil, 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-03-20 18:17:07 +00:00
|
|
|
var cc *tls.Config = nil
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|