-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
watcher.go
347 lines (308 loc) · 9.41 KB
/
watcher.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright 2021 The Cockroach Authors.
//
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
// License (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
package acl
import (
"context"
"time"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/metric"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/google/btree"
)
// Watcher maintains a list of connections waiting for changes to the
// access control list. If a connection becomes blocked because of changes
// to the access control list, the watcher notifies the connection via the
// Listener.Denied channel.
//
// All of Watcher's methods are thread safe.
type Watcher struct {
// locking mu is required to access any fields within Watcher.
mu syncutil.Mutex
// Each listener is given a unique id. The id is required to disambiguate
// connections with identical tags.
nextID int64
options *aclOptions
// All of the listeners waiting for changes to the access control list.
listeners *btree.BTree
// These control whether or not a connection is allowd based on it's
// ConnectionTags.
controllers []AccessController
}
// Listener contains the channel notified when a connection is denied
// and contains state needed to remove the listener from the watcher.
type listener struct {
// Lock Ordering: Only lock one listener at a time. Lock the listener before
// locking the watcher.
mu struct {
syncutil.Mutex
// The denied callback is notified iff the connection was blocked.
// After the callback is notified once, denied is set
// to nil to prevent duplicate calls.
denied func(error)
}
// Unique id. The id is required to disambiguate connections with identical
// connection tags.
id int64
// Used to identify if the connection matches the access control list.
connection ConnectionTags
}
// Option allows configuration of an access control list service.
type Option func(*aclOptions)
type aclOptions struct {
pollingInterval time.Duration
timeSource timeutil.TimeSource
errorCount *metric.Gauge
allowlistFile string
denylistFile string
lookupTenantFn lookupTenantFunc
}
// WithPollingInterval specifies interval between polling for config file
// changes.
func WithPollingInterval(d time.Duration) Option {
return func(op *aclOptions) {
op.pollingInterval = d
}
}
// WithTimeSource overrides the time source used to check expiration times.
func WithTimeSource(t timeutil.TimeSource) Option {
return func(op *aclOptions) {
op.timeSource = t
}
}
func WithErrorCount(errorCount *metric.Gauge) Option {
return func(op *aclOptions) {
op.errorCount = errorCount
}
}
func WithAllowListFile(allowlistFile string) Option {
return func(op *aclOptions) {
op.allowlistFile = allowlistFile
}
}
func WithDenyListFile(denylistFile string) Option {
return func(op *aclOptions) {
op.denylistFile = denylistFile
}
}
// WithLookupTenantFn sets the function used to perform a tenant lookup based
// on the tenant ID.
func WithLookupTenantFn(fn lookupTenantFunc) Option {
return func(op *aclOptions) {
op.lookupTenantFn = fn
}
}
const (
defaultPollingInterval = time.Minute
)
func NewWatcher(ctx context.Context, opts ...Option) (*Watcher, error) {
options := &aclOptions{
pollingInterval: defaultPollingInterval,
timeSource: timeutil.DefaultTimeSource{},
}
for _, opt := range opts {
opt(options)
}
w := &Watcher{
listeners: btree.New(8),
options: options,
controllers: make([]AccessController, 0),
}
if options.allowlistFile != "" {
c, next, err := newAccessControllerFromFile[*Allowlist](
ctx,
w.options.allowlistFile,
w.options.timeSource,
w.options.pollingInterval,
w.options.errorCount,
nil,
)
if err != nil {
return nil, err
}
w.addAccessController(ctx, c, next)
}
if options.denylistFile != "" {
c, next, err := newAccessControllerFromFile[*Denylist](
ctx,
w.options.denylistFile,
w.options.timeSource,
w.options.pollingInterval,
w.options.errorCount,
func(c *Denylist) {
c.timeSource = w.options.timeSource
},
)
if err != nil {
return nil, err
}
w.addAccessController(ctx, c, next)
}
if w.options.lookupTenantFn != nil {
controller := &PrivateEndpoints{
LookupTenantFn: w.options.lookupTenantFn,
}
// We use a normal polling interval to determine when we should check
// the connections for private endpoints update. This is reasonable
// for now as:
// 1. Calls to LookupTenant are cached most of the time.
// 2. This is only applied to existing connections, and a polling
// interval of 1 minute isn't too bad.
// 3. We are already iterating all the connections for the other types
// of ACLs.
//
// TODO(jaylim-crl): The directory cache already knows which tenants
// are updated through WatchTenants. We can do better here. Refactor
// AccessController in a way that allows those tenant metadata updates
// to be batched, and check connections for those tenants only. At the
// same time, the current AccessController design is poor because we
// iterate through all the connections for each ACL update (i.e. 1 for
// allowlist, 1 for denylist, and another for private endpoints).
next := pollAndUpdateChan(
ctx,
w.options.timeSource,
w.options.pollingInterval,
controller,
)
w.addAccessController(ctx, controller, next)
}
return w, nil
}
// addAccessController adds a new access controller to the watcher, and spawns
// a goroutine that watches for updates and replaces the controller as needed,
// using it's index in the slice.
func (w *Watcher) addAccessController(
ctx context.Context, controller AccessController, next chan AccessController,
) {
w.mu.Lock()
index := len(w.controllers)
w.controllers = append(w.controllers, controller)
w.mu.Unlock()
if next != nil {
go func() {
for n := range next {
w.updateAccessController(ctx, index, n)
}
}()
}
}
// updateAccessController replaces an old instance of a controller at a
// particular index with a new one. Once the new controller is added, all
// connections are re-checked to see if they're still valid. This is primarily
// used by the goroutine spawned in addAccessController.
func (w *Watcher) updateAccessController(
ctx context.Context, index int, controller AccessController,
) {
w.mu.Lock()
copy := w.listeners.Clone()
w.controllers[index] = controller
controllers := append([]AccessController(nil), w.controllers...)
w.mu.Unlock()
checkListeners(ctx, copy, controllers)
}
// ListenForDenied Adds a listener to the watcher for the given connection. If the
// connection is already blocked a nil remove function is returned and an error
// is returned immediately.
//
// Example Usage:
//
// remove, err := w.ListenForDenied(ctx, connection, func(err error) {
// /* connection was blocked by change */
// })
//
// if err != nil { /*connection already blocked*/ }
// defer remove()
//
// Warning:
// Do not call remove() within the error callback. It would deadlock.
func (w *Watcher) ListenForDenied(
ctx context.Context, connection ConnectionTags, callback func(error),
) (func(), error) {
w.mu.Lock()
defer w.mu.Unlock()
if err := checkConnection(ctx, connection, w.controllers); err != nil {
return nil, err
}
id := w.nextID
w.nextID++
l := &listener{
id: id,
connection: connection,
}
l.mu.denied = callback
w.listeners.ReplaceOrInsert(l)
return func() { w.removeListener(l) }, nil
}
func (w *Watcher) removeListener(l *listener) {
l.mu.Lock()
defer l.mu.Unlock()
w.mu.Lock()
defer w.mu.Unlock()
// remove the callback to prevent it from firing after removeListener returns.
l.mu.denied = nil
// remove the connection from the listeners tree to reclaim memory.
w.listeners.Delete(l)
}
// Less implements the btree.Item interface for listener.
func (l *listener) Less(than btree.Item) bool {
return l.id < than.(*listener).id
}
func checkListeners(ctx context.Context, listeners *btree.BTree, controllers []AccessController) {
listeners.Ascend(func(i btree.Item) bool {
lst := i.(*listener)
if err := checkConnection(ctx, lst.connection, controllers); err != nil {
lst.mu.Lock()
defer lst.mu.Unlock()
if lst.mu.denied != nil {
lst.mu.denied(err)
lst.mu.denied = nil
}
}
return true
})
}
func checkConnection(
ctx context.Context, connection ConnectionTags, controllers []AccessController,
) error {
for _, c := range controllers {
if err := c.CheckConnection(ctx, connection); err != nil {
return err
}
}
return nil
}
// pollAndUpdateChan sends the same access controller object into the returned
// channel every pollingInterval.
func pollAndUpdateChan(
ctx context.Context,
timeSource timeutil.TimeSource,
pollingInterval time.Duration,
accessController AccessController,
) chan AccessController {
result := make(chan AccessController)
go func() {
// TODO(ye): use notification via SIGHUP instead.
// TODO(ye): use inotify or similar mechanism for watching file updates
// instead of polling.
t := timeSource.NewTimer()
defer t.Stop()
for {
t.Reset(pollingInterval)
select {
case <-ctx.Done():
close(result)
log.Errorf(ctx, "WatchList daemon stopped: %v", ctx.Err())
return
case <-t.Ch():
t.MarkRead()
result <- accessController
}
}
}()
return result
}