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"
|
2025-09-05 03:14:27 +03:00
|
|
|
"errors"
|
2023-12-08 16:50:30 +01:00
|
|
|
"path/filepath"
|
2025-09-05 03:14:27 +03:00
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
2016-03-20 17:54:21 +00:00
|
|
|
|
2020-09-24 18:14:41 +02:00
|
|
|
"github.com/coredns/caddy"
|
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"
|
|
|
|
|
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
|
|
|
|
2021-05-14 23:55:01 -07:00
|
|
|
etcdcv3 "go.etcd.io/etcd/client/v3"
|
2016-03-20 17:54:21 +00:00
|
|
|
)
|
|
|
|
|
|
2019-09-20 08:02:30 +01:00
|
|
|
func init() { plugin.Register("etcd", 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
|
|
|
|
2024-08-27 21:35:19 +08:00
|
|
|
c.OnShutdown(e.OnShutdown)
|
|
|
|
|
|
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) {
|
2023-12-08 16:50:30 +01:00
|
|
|
config := dnsserver.GetConfig(c)
|
2025-09-05 03:14:27 +03:00
|
|
|
etc := Etcd{
|
|
|
|
|
PathPrefix: "skydns",
|
|
|
|
|
MinLeaseTTL: defaultLeaseMinTTL,
|
|
|
|
|
MaxLeaseTTL: defaultLeaseMaxTTL,
|
|
|
|
|
}
|
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()
|
|
|
|
|
|
2022-07-10 20:06:33 +02:00
|
|
|
if c.Next() {
|
2021-05-17 22:19:54 +02:00
|
|
|
etc.Zones = plugin.OriginsFromArgsOrServerBlock(c.RemainingArgs(), c.ServerBlockKeys)
|
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()
|
2023-12-08 16:50:30 +01:00
|
|
|
for i := range args {
|
|
|
|
|
if !filepath.IsAbs(args[i]) && config.Root != "" {
|
|
|
|
|
args[i] = filepath.Join(config.Root, args[i])
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-18 15:17:46 +08:00
|
|
|
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]
|
2025-09-05 03:14:27 +03:00
|
|
|
case "min-lease-ttl":
|
|
|
|
|
if !c.NextArg() {
|
|
|
|
|
return &Etcd{}, c.ArgErr()
|
|
|
|
|
}
|
|
|
|
|
minLeaseTTL, err := parseTTL(c.Val())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return &Etcd{}, c.Errf("invalid min-lease-ttl value: %v", err)
|
|
|
|
|
}
|
|
|
|
|
etc.MinLeaseTTL = minLeaseTTL
|
|
|
|
|
case "max-lease-ttl":
|
|
|
|
|
if !c.NextArg() {
|
|
|
|
|
return &Etcd{}, c.ArgErr()
|
|
|
|
|
}
|
|
|
|
|
maxLeaseTTL, err := parseTTL(c.Val())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return &Etcd{}, c.Errf("invalid max-lease-ttl value: %v", err)
|
|
|
|
|
}
|
|
|
|
|
etc.MaxLeaseTTL = maxLeaseTTL
|
2019-03-18 15:17:46 +08:00
|
|
|
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{
|
2023-11-11 06:17:55 +08:00
|
|
|
Endpoints: endpoints,
|
|
|
|
|
TLS: cc,
|
|
|
|
|
DialKeepAliveTime: etcdTimeout,
|
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"
|
2025-09-05 03:14:27 +03:00
|
|
|
|
|
|
|
|
// parseTTL parses a TTL value with flexible time units using Go's standard duration parsing.
|
|
|
|
|
// Supports formats like: "30", "30s", "5m", "1h", "90s", "2h30m", etc.
|
|
|
|
|
func parseTTL(s string) (uint32, error) {
|
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
|
if s == "" {
|
|
|
|
|
return 0, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle plain numbers (assume seconds)
|
|
|
|
|
if _, err := strconv.ParseUint(s, 10, 64); err == nil {
|
|
|
|
|
// If it's just a number, append "s" for seconds
|
|
|
|
|
s += "s"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use Go's standard time.ParseDuration for robust parsing
|
|
|
|
|
duration, err := time.ParseDuration(s)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, errors.New("invalid TTL format, use format like '30', '30s', '5m', '1h', or '2h30m'")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convert to seconds and check bounds
|
|
|
|
|
seconds := duration.Seconds()
|
|
|
|
|
if seconds < 0 {
|
|
|
|
|
return 0, errors.New("TTL must be non-negative")
|
|
|
|
|
}
|
|
|
|
|
if seconds > 4294967295 { // uint32 max value
|
|
|
|
|
return 0, errors.New("TTL too large, maximum is 4294967295 seconds")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return uint32(seconds), nil
|
|
|
|
|
}
|