2018-02-02 21:18:52 -05:00
|
|
|
/*
|
2020-07-07 22:04:29 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2018,2020 MinIO, Inc.
|
2018-02-02 21:18:52 -05:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-08-02 15:40:51 -04:00
|
|
|
"net"
|
2018-02-02 21:18:52 -05:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/coredns/coredns/plugin/etcd/msg"
|
2020-09-10 17:19:32 -04:00
|
|
|
"github.com/minio/minio-go/v7/pkg/set"
|
2020-12-03 14:35:18 -05:00
|
|
|
"go.etcd.io/etcd/clientv3"
|
2018-02-02 21:18:52 -05:00
|
|
|
)
|
|
|
|
|
2018-05-11 15:02:30 -04:00
|
|
|
// ErrNoEntriesFound - Indicates no entries were found for the given key (directory)
|
|
|
|
var ErrNoEntriesFound = errors.New("No entries found for this key")
|
|
|
|
|
2019-03-12 20:57:08 -04:00
|
|
|
const etcdPathSeparator = "/"
|
|
|
|
|
2018-02-02 21:18:52 -05:00
|
|
|
// create a new coredns service record for the bucket.
|
2020-02-03 03:24:20 -05:00
|
|
|
func newCoreDNSMsg(ip string, port string, ttl uint32, t time.Time) ([]byte, error) {
|
2018-02-02 21:18:52 -05:00
|
|
|
return json.Marshal(&SrvRecord{
|
|
|
|
Host: ip,
|
2019-11-12 17:42:35 -05:00
|
|
|
Port: json.Number(port),
|
2018-02-02 21:18:52 -05:00
|
|
|
TTL: ttl,
|
2020-02-03 03:24:20 -05:00
|
|
|
CreationDate: t,
|
2018-02-02 21:18:52 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-18 20:31:56 -04:00
|
|
|
// Close closes the internal etcd client and cannot be used further
|
2020-07-20 15:28:48 -04:00
|
|
|
func (c *CoreDNS) Close() error {
|
2020-06-18 20:31:56 -04:00
|
|
|
c.etcdClient.Close()
|
2020-07-20 15:28:48 -04:00
|
|
|
return nil
|
2020-06-18 20:31:56 -04:00
|
|
|
}
|
|
|
|
|
2019-10-30 03:04:39 -04:00
|
|
|
// List - Retrieves list of DNS entries for the domain.
|
2020-02-03 03:24:20 -05:00
|
|
|
func (c *CoreDNS) List() (map[string][]SrvRecord, error) {
|
|
|
|
var srvRecords = map[string][]SrvRecord{}
|
2019-02-22 22:18:01 -05:00
|
|
|
for _, domainName := range c.domainNames {
|
2019-10-30 03:04:39 -04:00
|
|
|
key := msg.Path(fmt.Sprintf("%s.", domainName), c.prefixPath)
|
2019-02-22 22:18:01 -05:00
|
|
|
records, err := c.list(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-03 19:16:05 -04:00
|
|
|
for _, record := range records {
|
|
|
|
if record.Key == "" {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-18 19:19:29 -04:00
|
|
|
srvRecords[record.Key] = append(srvRecords[record.Key], record)
|
2019-06-03 19:16:05 -04:00
|
|
|
}
|
2019-02-22 22:18:01 -05:00
|
|
|
}
|
|
|
|
return srvRecords, nil
|
2018-05-11 15:02:30 -04:00
|
|
|
}
|
|
|
|
|
2019-10-30 03:04:39 -04:00
|
|
|
// Get - Retrieves DNS records for a bucket.
|
|
|
|
func (c *CoreDNS) Get(bucket string) ([]SrvRecord, error) {
|
2019-02-22 22:18:01 -05:00
|
|
|
var srvRecords []SrvRecord
|
|
|
|
for _, domainName := range c.domainNames {
|
2019-10-30 03:04:39 -04:00
|
|
|
key := msg.Path(fmt.Sprintf("%s.%s.", bucket, domainName), c.prefixPath)
|
2019-02-22 22:18:01 -05:00
|
|
|
records, err := c.list(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-17 20:29:49 -04:00
|
|
|
// Make sure we have record.Key is empty
|
|
|
|
// this can only happen when record.Key
|
|
|
|
// has bucket entry with exact prefix
|
|
|
|
// match any record.Key which do not
|
|
|
|
// match the prefixes we skip them.
|
|
|
|
for _, record := range records {
|
|
|
|
if record.Key != "" {
|
|
|
|
continue
|
|
|
|
}
|
2019-07-20 06:29:05 -04:00
|
|
|
srvRecords = append(srvRecords, record)
|
2019-04-17 20:29:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(srvRecords) == 0 {
|
|
|
|
return nil, ErrNoEntriesFound
|
2019-02-22 22:18:01 -05:00
|
|
|
}
|
|
|
|
return srvRecords, nil
|
2018-05-11 15:02:30 -04:00
|
|
|
}
|
|
|
|
|
2019-03-12 20:57:08 -04:00
|
|
|
// msgUnPath converts a etcd path to domainname.
|
|
|
|
func msgUnPath(s string) string {
|
|
|
|
ks := strings.Split(strings.Trim(s, etcdPathSeparator), etcdPathSeparator)
|
|
|
|
for i, j := 0, len(ks)-1; i < j; i, j = i+1, j-1 {
|
|
|
|
ks[i], ks[j] = ks[j], ks[i]
|
|
|
|
}
|
|
|
|
return strings.Join(ks, ".")
|
|
|
|
}
|
|
|
|
|
2018-05-11 15:02:30 -04:00
|
|
|
// Retrieves list of entries under the key passed.
|
|
|
|
// Note that this method fetches entries upto only two levels deep.
|
2019-10-30 03:04:39 -04:00
|
|
|
func (c *CoreDNS) list(key string) ([]SrvRecord, error) {
|
2018-02-02 21:18:52 -05:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
2019-11-25 19:33:34 -05:00
|
|
|
r, err := c.etcdClient.Get(ctx, key, clientv3.WithPrefix())
|
2018-07-12 17:12:40 -04:00
|
|
|
defer cancel()
|
2018-02-02 21:18:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-12 17:12:40 -04:00
|
|
|
if r.Count == 0 {
|
2019-03-12 20:57:08 -04:00
|
|
|
key = strings.TrimSuffix(key, etcdPathSeparator)
|
2018-07-12 17:12:40 -04:00
|
|
|
r, err = c.etcdClient.Get(ctx, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2018-02-02 21:18:52 -05:00
|
|
|
|
|
|
|
var srvRecords []SrvRecord
|
2018-07-12 17:12:40 -04:00
|
|
|
for _, n := range r.Kvs {
|
|
|
|
var srvRecord SrvRecord
|
|
|
|
if err = json.Unmarshal([]byte(n.Value), &srvRecord); err != nil {
|
|
|
|
return nil, err
|
2018-02-02 21:18:52 -05:00
|
|
|
}
|
2018-07-12 17:12:40 -04:00
|
|
|
srvRecord.Key = strings.TrimPrefix(string(n.Key), key)
|
|
|
|
srvRecord.Key = strings.TrimSuffix(srvRecord.Key, srvRecord.Host)
|
2019-03-08 17:22:58 -05:00
|
|
|
|
|
|
|
// Skip non-bucket entry like for a key
|
|
|
|
// /skydns/net/miniocloud/10.0.0.1 that may exist as
|
|
|
|
// dns entry for the server (rather than the bucket
|
|
|
|
// itself).
|
2019-06-03 19:16:05 -04:00
|
|
|
if srvRecord.Key == "" {
|
2019-03-08 17:22:58 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-03-12 20:57:08 -04:00
|
|
|
srvRecord.Key = msgUnPath(srvRecord.Key)
|
|
|
|
srvRecords = append(srvRecords, srvRecord)
|
2018-07-12 17:12:40 -04:00
|
|
|
|
2018-02-02 21:18:52 -05:00
|
|
|
}
|
2018-12-12 13:47:03 -05:00
|
|
|
sort.Slice(srvRecords, func(i int, j int) bool {
|
|
|
|
return srvRecords[i].Key < srvRecords[j].Key
|
|
|
|
})
|
2018-05-11 15:02:30 -04:00
|
|
|
return srvRecords, nil
|
2018-02-02 21:18:52 -05:00
|
|
|
}
|
|
|
|
|
2019-10-30 03:04:39 -04:00
|
|
|
// Put - Adds DNS entries into etcd endpoint in CoreDNS etcd message format.
|
|
|
|
func (c *CoreDNS) Put(bucket string) error {
|
2019-12-18 00:32:45 -05:00
|
|
|
c.Delete(bucket) // delete any existing entries.
|
|
|
|
|
2020-02-03 03:24:20 -05:00
|
|
|
t := time.Now().UTC()
|
2018-05-11 15:02:30 -04:00
|
|
|
for ip := range c.domainIPs {
|
2020-02-03 03:24:20 -05:00
|
|
|
bucketMsg, err := newCoreDNSMsg(ip, c.domainPort, defaultTTL, t)
|
2018-05-11 15:02:30 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-22 22:18:01 -05:00
|
|
|
for _, domainName := range c.domainNames {
|
2019-10-30 03:04:39 -04:00
|
|
|
key := msg.Path(fmt.Sprintf("%s.%s", bucket, domainName), c.prefixPath)
|
2019-03-12 20:57:08 -04:00
|
|
|
key = key + etcdPathSeparator + ip
|
2019-02-22 22:18:01 -05:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
|
|
|
_, err = c.etcdClient.Put(ctx, key, string(bucketMsg))
|
2018-07-12 17:12:40 -04:00
|
|
|
defer cancel()
|
2019-02-22 22:18:01 -05:00
|
|
|
if err != nil {
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), defaultContextTimeout)
|
|
|
|
c.etcdClient.Delete(ctx, key)
|
|
|
|
defer cancel()
|
|
|
|
return err
|
|
|
|
}
|
2018-06-18 15:05:17 -04:00
|
|
|
}
|
2018-02-02 21:18:52 -05:00
|
|
|
}
|
2018-06-18 15:05:17 -04:00
|
|
|
return nil
|
2018-02-02 21:18:52 -05:00
|
|
|
}
|
|
|
|
|
2019-10-30 03:04:39 -04:00
|
|
|
// Delete - Removes DNS entries added in Put().
|
|
|
|
func (c *CoreDNS) Delete(bucket string) error {
|
2019-02-22 22:18:01 -05:00
|
|
|
for _, domainName := range c.domainNames {
|
2019-10-30 03:04:39 -04:00
|
|
|
key := msg.Path(fmt.Sprintf("%s.%s.", bucket, domainName), c.prefixPath)
|
2019-02-22 22:18:01 -05:00
|
|
|
srvRecords, err := c.list(key)
|
|
|
|
if err != nil {
|
2018-12-12 13:47:03 -05:00
|
|
|
return err
|
|
|
|
}
|
2019-02-22 22:18:01 -05:00
|
|
|
for _, record := range srvRecords {
|
|
|
|
dctx, dcancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
2019-03-12 20:57:08 -04:00
|
|
|
if _, err = c.etcdClient.Delete(dctx, key+etcdPathSeparator+record.Host); err != nil {
|
2019-02-22 22:18:01 -05:00
|
|
|
dcancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dcancel()
|
|
|
|
}
|
2018-12-12 13:47:03 -05:00
|
|
|
}
|
2019-02-22 22:18:01 -05:00
|
|
|
return nil
|
2018-02-02 21:18:52 -05:00
|
|
|
}
|
|
|
|
|
2019-10-30 03:04:39 -04:00
|
|
|
// DeleteRecord - Removes a specific DNS entry
|
|
|
|
func (c *CoreDNS) DeleteRecord(record SrvRecord) error {
|
2019-08-13 11:49:26 -04:00
|
|
|
for _, domainName := range c.domainNames {
|
2019-10-30 03:04:39 -04:00
|
|
|
key := msg.Path(fmt.Sprintf("%s.%s.", record.Key, domainName), c.prefixPath)
|
2019-08-13 11:49:26 -04:00
|
|
|
|
|
|
|
dctx, dcancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
|
|
|
if _, err := c.etcdClient.Delete(dctx, key+etcdPathSeparator+record.Host); err != nil {
|
|
|
|
dcancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dcancel()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-10 17:19:32 -04:00
|
|
|
// String stringer name for this implementation of dns.Store
|
|
|
|
func (c *CoreDNS) String() string {
|
|
|
|
return "etcdDNS"
|
|
|
|
}
|
|
|
|
|
2018-02-02 21:18:52 -05:00
|
|
|
// CoreDNS - represents dns config for coredns server.
|
2019-10-30 03:04:39 -04:00
|
|
|
type CoreDNS struct {
|
2019-02-22 22:18:01 -05:00
|
|
|
domainNames []string
|
|
|
|
domainIPs set.StringSet
|
2019-10-30 03:04:39 -04:00
|
|
|
domainPort string
|
|
|
|
prefixPath string
|
2019-11-25 19:33:34 -05:00
|
|
|
etcdClient *clientv3.Client
|
2018-02-02 21:18:52 -05:00
|
|
|
}
|
|
|
|
|
2020-09-10 17:19:32 -04:00
|
|
|
// EtcdOption - functional options pattern style
|
|
|
|
type EtcdOption func(*CoreDNS)
|
2019-10-30 03:04:39 -04:00
|
|
|
|
|
|
|
// DomainNames set a list of domain names used by this CoreDNS
|
|
|
|
// client setting, note this will fail if set to empty when
|
|
|
|
// constructor initializes.
|
2020-09-10 17:19:32 -04:00
|
|
|
func DomainNames(domainNames []string) EtcdOption {
|
2019-10-30 03:04:39 -04:00
|
|
|
return func(args *CoreDNS) {
|
|
|
|
args.domainNames = domainNames
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DomainIPs set a list of custom domain IPs, note this will
|
|
|
|
// fail if set to empty when constructor initializes.
|
2020-09-10 17:19:32 -04:00
|
|
|
func DomainIPs(domainIPs set.StringSet) EtcdOption {
|
2019-10-30 03:04:39 -04:00
|
|
|
return func(args *CoreDNS) {
|
|
|
|
args.domainIPs = domainIPs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DomainPort - is a string version of server port
|
2020-09-10 17:19:32 -04:00
|
|
|
func DomainPort(domainPort string) EtcdOption {
|
2019-10-30 03:04:39 -04:00
|
|
|
return func(args *CoreDNS) {
|
|
|
|
args.domainPort = domainPort
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CoreDNSPath - custom prefix on etcd to populate DNS
|
|
|
|
// service records, optional and can be empty.
|
|
|
|
// if empty then c.prefixPath is used i.e "/skydns"
|
2020-09-10 17:19:32 -04:00
|
|
|
func CoreDNSPath(prefix string) EtcdOption {
|
2019-10-30 03:04:39 -04:00
|
|
|
return func(args *CoreDNS) {
|
|
|
|
args.prefixPath = prefix
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-02 21:18:52 -05:00
|
|
|
// NewCoreDNS - initialize a new coreDNS set/unset values.
|
2020-09-10 17:19:32 -04:00
|
|
|
func NewCoreDNS(cfg clientv3.Config, setters ...EtcdOption) (Store, error) {
|
2019-11-25 19:33:34 -05:00
|
|
|
etcdClient, err := clientv3.New(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-02-02 21:18:52 -05:00
|
|
|
}
|
|
|
|
|
2019-10-30 03:04:39 -04:00
|
|
|
args := &CoreDNS{
|
|
|
|
etcdClient: etcdClient,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, setter := range setters {
|
|
|
|
setter(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args.domainNames) == 0 || args.domainIPs.IsEmpty() {
|
|
|
|
return nil, errors.New("invalid argument")
|
2018-02-02 21:18:52 -05:00
|
|
|
}
|
|
|
|
|
2019-08-02 15:40:51 -04:00
|
|
|
// strip ports off of domainIPs
|
2019-10-30 03:04:39 -04:00
|
|
|
domainIPsWithoutPorts := args.domainIPs.ApplyFunc(func(ip string) string {
|
2019-08-02 15:40:51 -04:00
|
|
|
host, _, err := net.SplitHostPort(ip)
|
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "missing port in address") {
|
|
|
|
host = ip
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return host
|
|
|
|
})
|
2019-10-30 03:04:39 -04:00
|
|
|
args.domainIPs = domainIPsWithoutPorts
|
2019-08-02 15:40:51 -04:00
|
|
|
|
2019-10-30 03:04:39 -04:00
|
|
|
return args, nil
|
2018-02-02 21:18:52 -05:00
|
|
|
}
|