2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-02-02 21:18:52 -05:00
|
|
|
|
|
|
|
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"
|
2021-05-28 13:31:42 -04:00
|
|
|
clientv3 "go.etcd.io/etcd/client/v3"
|
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")
|
|
|
|
|
2021-01-28 14:44:48 -05:00
|
|
|
// ErrDomainMissing - Indicates domain is missing
|
|
|
|
var ErrDomainMissing = errors.New("domain is missing")
|
|
|
|
|
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) {
|
2022-01-02 12:15:06 -05:00
|
|
|
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)
|
2021-01-28 14:44:48 -05:00
|
|
|
records, err := c.list(key+etcdPathSeparator, true)
|
2019-02-22 22:18:01 -05:00
|
|
|
if err != nil {
|
2021-01-28 14:44:48 -05:00
|
|
|
return srvRecords, err
|
2019-02-22 22:18:01 -05:00
|
|
|
}
|
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)
|
2021-02-01 15:03:28 -05:00
|
|
|
records, err := c.list(key, false)
|
2019-02-22 22:18:01 -05:00
|
|
|
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.
|
2021-01-28 14:44:48 -05:00
|
|
|
func (c *CoreDNS) list(key string, domain bool) ([]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
|
|
|
|
}
|
2021-01-28 14:44:48 -05:00
|
|
|
|
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
|
|
|
|
}
|
2021-01-28 14:44:48 -05:00
|
|
|
// only if we are looking at `domain` as true
|
|
|
|
// we should return error here.
|
|
|
|
if domain && r.Count == 0 {
|
|
|
|
return nil, ErrDomainMissing
|
|
|
|
}
|
2018-07-12 17:12:40 -04:00
|
|
|
}
|
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
|
2021-05-24 12:28:19 -04:00
|
|
|
if err = json.Unmarshal(n.Value, &srvRecord); err != nil {
|
2018-07-12 17:12:40 -04:00
|
|
|
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))
|
2021-01-28 14:44:48 -05:00
|
|
|
cancel()
|
2019-02-22 22:18:01 -05:00
|
|
|
if err != nil {
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), defaultContextTimeout)
|
|
|
|
c.etcdClient.Delete(ctx, key)
|
2021-01-28 14:44:48 -05:00
|
|
|
cancel()
|
2019-02-22 22:18:01 -05:00
|
|
|
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)
|
2021-01-28 14:44:48 -05:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
|
|
|
_, err := c.etcdClient.Delete(ctx, key+etcdPathSeparator, clientv3.WithPrefix())
|
|
|
|
cancel()
|
2019-02-22 22:18:01 -05:00
|
|
|
if err != nil {
|
2018-12-12 13:47:03 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2021-01-28 14:44:48 -05:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
|
|
|
_, err := c.etcdClient.Delete(ctx, key+etcdPathSeparator+record.Host)
|
|
|
|
cancel()
|
|
|
|
if err != nil {
|
2019-08-13 11:49:26 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|