forked from unRob/coredns-consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsul.go
295 lines (250 loc) · 6.95 KB
/
consul.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright © 2022 Roberto Hidalgo <[email protected]>
// Modified by Charles Powell, 2023
// SPDX-License-Identifier: Apache-2.0
package catalog
import (
"encoding/json"
"fmt"
"net"
"regexp"
"strings"
"time"
"github.com/hashicorp/consul/api"
)
var watchTimeout = 10 * time.Minute
// ClientCatalog is implemented by github.com/hashicorp/consul/api.Catalog.
type ClientCatalog interface {
Service(string, string, *api.QueryOptions) ([]*api.CatalogService, *api.QueryMeta, error)
Services(*api.QueryOptions) (map[string][]string, *api.QueryMeta, error)
}
// KVClient is implemented by github.com/hashicorp/consul/api.Catalog.
type KVClient interface {
Get(string, *api.QueryOptions) (*api.KVPair, *api.QueryMeta, error)
}
type KVEntries struct {
Target string
ACL []string
}
// CreateClient initializes the consul catalog client.
func CreateClient(scheme, endpoint, token string) (catalog ClientCatalog, kv KVClient, err error) {
cfg := api.DefaultConfig()
cfg.Address = endpoint
if token != "" {
cfg.Token = token
}
if scheme == "https" {
cfg.Scheme = "https"
}
client, err := api.NewClient(cfg)
if err != nil {
return
}
catalog = client.Catalog()
kv = client.KV()
return
}
func (c *Catalog) FetchConfig() error {
c.RLock()
lastIndex := c.lastConfigIndex
c.RUnlock()
configPair, meta, err := c.kv.Get(c.ConfigKey, &api.QueryOptions{
WaitTime: watchTimeout,
WaitIndex: lastIndex,
})
if err != nil {
return err
}
nextIndex := meta.LastIndex
// reset the index if it goes backwards
// https://www.consul.io/api/features/blocking.html#implementation-details
if nextIndex < lastIndex {
Log.Debugf("Resetting consul kv watch index")
nextIndex = 0
}
if nextIndex == lastIndex {
// watch timed out, safe to retry
Log.Debugf("No changes found, %d", nextIndex)
return nil
}
Log.Debugf("Found config %s", configPair.Value)
services := map[string]*Service{}
entries := map[string]*KVEntries{}
err = json.Unmarshal(configPair.Value, &entries)
if err != nil {
return err
}
found := []string{}
for name, entry := range entries {
target := entry.Target
if target == "@service_proxy" {
if c.ProxyService == "" {
Log.Warningf("Ignoring service %s. Requested service proxy but none is configured", name)
continue
}
}
service := &Service{
Name: name,
Target: target,
ACL: []*ServiceACL{},
}
if c.MetadataTag != "" {
err := c.parseACL(service, entry.ACL)
if err != nil {
Log.Warningf("Ignoring service %s. Could not parse ACL: %s", name, err)
continue
}
}
services[name] = service
found = append(found, name)
}
c.Lock()
c.ready = true
c.staticEntries = services
c.lastConfigIndex = nextIndex
c.lastUpdate = time.Now()
c.Unlock()
Log.Debugf("Serving records for %d kv entries: %s", len(found), strings.Join(found, ","))
return nil
}
func (c *Catalog) parseACLString(svc *Service, acl string) error {
aclRules := regexp.MustCompile(`;\s*`).Split(acl, -1)
return c.parseACL(svc, aclRules)
}
func (c *Catalog) parseACL(svc *Service, rules []string) error {
Log.Debugf("Parsing ACL for %s: %s", svc.Name, rules)
for _, rule := range rules {
ruleParts := strings.SplitN(rule, " ", 2)
if len(ruleParts) != 2 {
return fmt.Errorf("could not parse acl rule <%s>", rule)
}
action := ruleParts[0]
for _, networkName := range regexp.MustCompile(`,\s*`).Split(ruleParts[1], -1) {
if cidr, ok := c.Networks[networkName]; ok {
svc.ACL = append(svc.ACL, &ServiceACL{
Action: action,
Network: cidr,
})
} else {
return fmt.Errorf("unknown network %s", networkName)
}
}
}
return nil
}
// FetchServices populates zones.
func (c *Catalog) FetchServices() error {
c.RLock()
lastIndex := c.lastCatalogIndex
c.RUnlock()
svcs, meta, err := c.client.Services(&api.QueryOptions{
WaitTime: watchTimeout,
WaitIndex: lastIndex,
})
if err != nil {
return err
}
nextIndex := meta.LastIndex
// reset the index if it goes backwards
// https://www.consul.io/api/features/blocking.html#implementation-details
if nextIndex < lastIndex {
Log.Debugf("Resetting consul catalog watch index")
nextIndex = 0
}
if nextIndex == lastIndex {
// watch timed out, safe to retry
Log.Debugf("No changes found, %d", nextIndex)
return nil
}
Log.Debugf("Found %d catalog services", len(svcs))
found := []string{}
currentServices := map[string]*Service{}
for svc, serviceTags := range svcs {
target := svc
exposed := false
acl_ignore := false
aliases := []string{}
for _, tag := range serviceTags {
switch tag {
case c.ACLIgnoreTag:
Log.Debugf("ACL rules will be ignored for %s", svc)
acl_ignore = true
case c.ProxyTag:
if c.ProxyTag != "" {
Log.Debugf("%s has a proxy tag, would provide proxy as target", svc)
target = c.ProxyService
}
case c.Tag:
Log.Debugf("CoreDNS exposure is enabled for %s", svc)
exposed = true
default:
// Look for alias tag definitions
possibleAlias := strings.Split(tag, "=")
if strings.TrimSpace(possibleAlias[0]) == c.AliasTag {
for _, atag := range strings.Split(possibleAlias[1], ",") {
cAtag := strings.TrimSpace(atag)
Log.Debugf("Found alias %s for service %s", cAtag, svc)
aliases = append(aliases, cAtag)
}
}
}
}
// do not publish services without the tag
if !exposed {
Log.Debugf("%s has neither proxy tag or direct exposure tag, not exposing", svc)
continue
}
hydratedServices, _, err := c.client.Service(svc, "", nil)
if err != nil {
// couldn't find service, ignore
Log.Debugf("Failed to fetch service info for %s: %e", svc, err)
continue
}
service := &Service{
Name: svc,
Target: target,
ACL: []*ServiceACL{},
Addresses: []net.IP{},
ApplyACL: true,
}
if len(hydratedServices) > 0 {
for _, svc := range hydratedServices {
service.Addresses = append(service.Addresses, net.ParseIP(svc.Address))
}
metadata := hydratedServices[0].ServiceMeta
if c.MetadataTag != "" {
acl, exists := metadata[c.MetadataTag]
if !exists {
// No ACL for service
if acl_ignore {
Log.Infof("Configured to ignore ACL for service %s", svc)
service.ApplyACL = false
} else {
Log.Warningf("No ACL found for service %s, will not expose", svc)
// Continue to next service
continue
}
}
if err := c.parseACLString(service, acl); err != nil {
Log.Warningf("Ignoring service %s: %s", service.Name, err)
}
}
} else {
Log.Warningf("No services found for %s, check the permissions for your token", svc)
}
// Add main service
currentServices[svc] = service
// Add aliases to service
for _, alias := range aliases {
currentServices[alias] = service
}
found = append(found, svc)
}
c.Lock()
c.ready = true
c.services = currentServices
c.lastCatalogIndex = nextIndex
c.lastUpdate = time.Now()
c.Unlock()
Log.Debugf("Serving records for %d catalog services: %s", len(found), strings.Join(found, ","))
return nil
}