forked from unRob/coredns-consul
/
catalog.go
204 lines (176 loc) · 5.07 KB
/
catalog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright © 2022 Roberto Hidalgo <coredns-consul@un.rob.mx>
// Modified by Charles Powell, 2023
// SPDX-License-Identifier: Apache-2.0
package catalog
import (
"context"
"fmt"
"net"
"strings"
"sync"
"time"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/file"
"github.com/coredns/coredns/plugin/pkg/upstream"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
var defaultTag = "coredns.enabled"
var defaultEndpoint = "consul.service.consul:8500"
var defaultTTL = uint32((5 * time.Minute).Seconds())
var defaultMeta = "coredns-acl"
var defaultLookup = func(ctx context.Context, state request.Request, target string) (*dns.Msg, error) {
recursor := upstream.New()
req := state.NewWithQuestion(target, dns.TypeA)
return recursor.Lookup(ctx, req, target, dns.TypeA)
}
// Catalog holds published Consul Catalog services.
type Catalog struct {
sync.RWMutex
Endpoint string
Scheme string
Tag string
ACLIgnoreTag string
AliasTag string
services map[string]*Service
staticEntries map[string]*Service
FQDN []string
TTL uint32
Token string
ProxyService string
ProxyTag string
Networks map[string]*net.IPNet
MetadataTag string
ConfigKey string
Next plugin.Handler
Zone string
lastCatalogIndex uint64
lastConfigIndex uint64
lastUpdate time.Time
ready bool
client ClientCatalog
kv KVClient
}
// New returns a Catalog plugin.
func New() *Catalog {
return &Catalog{
Endpoint: defaultEndpoint,
Scheme: "http",
TTL: defaultTTL,
Tag: defaultTag,
MetadataTag: defaultMeta,
services: map[string]*Service{},
}
}
// SetClient sets a consul client for a catalog.
func (c *Catalog) SetClients(client ClientCatalog, kv KVClient) {
c.client = client
c.kv = kv
}
// Ready implements ready.Readiness.
func (c *Catalog) Ready() bool {
return c.ready
}
// LastUpdated returns the last time services changed.
func (c *Catalog) LastUpdated() time.Time {
return c.lastUpdate
}
// Services returns a map of services to their target.
func (c *Catalog) Services() map[string]*Service {
return c.services
}
// Name implements plugin.Handler.
func (c *Catalog) Name() string { return "consul_catalog" }
func (c *Catalog) ServiceFor(name string) (svc *Service) {
var exists bool
c.RLock()
if svc, exists = c.staticEntries[name]; !exists {
Log.Debugf("Zone missing from static entries %s", name)
svc = c.services[name]
}
c.RUnlock()
return
}
// ServeDNS implements plugin.Handler.
func (c *Catalog) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := request.Request{W: w, Req: r, Zone: c.Zone}
name := state.QName()
for _, fqdn := range c.FQDN {
name = strings.Replace(name, "."+fqdn, "", 1)
}
svc := c.ServiceFor(name)
if svc == nil {
Log.Debugf("Zone not found: %s", name)
return plugin.NextOrFailure("consul_catalog", c.Next, ctx, w, r)
}
if len(c.Networks) > 0 {
ip := net.ParseIP(state.IP())
if !svc.ApplyACL {
Log.Debugf("Ignoring ACLs for service %s, as specified", name)
} else {
if !svc.RespondsTo(ip) {
Log.Warningf("Blocked resolution for service %s from ip %s", name, ip)
return plugin.NextOrFailure("consul_catalog", c.Next, ctx, w, r)
}
}
}
m := new(dns.Msg)
m.SetReply(r)
m.Authoritative = true
m.Rcode = dns.RcodeSuccess
m.Compress = true
m.Answer = []dns.RR{}
header := dns.RR_Header{
Name: state.QName(),
Rrtype: state.QType(),
Class: dns.ClassINET,
Ttl: c.TTL,
}
if fp, ok := c.Next.(file.File); ok && len(fp.Zones.Z) > 0 {
// grab the SOA from the file plugin, if available and next in the chain
if zone, ok := fp.Zones.Z[c.FQDN[0]]; ok {
Log.Debugf("Adding SOA %s", zone.SOA.String())
m.Ns = []dns.RR{zone.SOA}
}
}
if state.QType() != dns.TypeA {
// return NODATA
Log.Debugf("Record for %s does not contain answers for type %s", name, state.Type())
err := w.WriteMsg(m)
return dns.RcodeSuccess, err
}
lookupName := svc.Target
if svc.Target == "@service_proxy" {
lookupName = c.ProxyService
}
Log.Debugf("looking up target: %s", lookupName)
if target := c.ServiceFor(lookupName); target != nil && len(target.Addresses) > 0 {
Log.Debugf("Found addresses in catalog for %s: %v", lookupName, target.Addresses)
for _, addr := range target.Addresses {
m.Answer = append(m.Answer, &dns.A{
Hdr: header,
A: addr,
})
}
} else {
Log.Debugf("Looking up address for %s upstream", lookupName)
reply, err := defaultLookup(ctx, state, fmt.Sprintf("%s.service.consul", lookupName))
if err != nil {
return 0, plugin.Error("Failed to lookup target upstream", err)
}
Log.Debugf("Found record for %s upstream", name)
for _, a := range reply.Answer {
record, ok := a.(*dns.A)
if !ok {
Log.Warningf("Found non-A record upstream: %s", a.String())
continue
}
m.Answer = append(m.Answer, &dns.A{
Hdr: header,
A: record.A,
})
}
}
err := w.WriteMsg(m)
return dns.RcodeSuccess, err
}