forked from DataDog/dd-trace-go
/
remoteconfig.go
613 lines (552 loc) · 18.3 KB
/
remoteconfig.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2022 Datadog, Inc.
package remoteconfig
import (
"bytes"
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"math/big"
"net/http"
"reflect"
"strings"
"sync"
"time"
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
rc "github.com/DataDog/datadog-agent/pkg/remoteconfig/state"
)
// Callback represents a function that can process a remote config update.
// A Callback function can be registered to a remote config client to automatically
// react upon receiving updates. This function returns the configuration processing status
// for each config file received through the update.
type Callback func(updates map[string]ProductUpdate) map[string]rc.ApplyStatus
// ProductCallback is like Callback but for a specific product.
type ProductCallback func(update ProductUpdate) map[string]rc.ApplyStatus
// Capability represents a bit index to be set in clientData.Capabilites in order to register a client
// for a specific capability
type Capability uint
const (
_ Capability = iota
// ASMActivation represents the capability to activate ASM through remote configuration
ASMActivation
// ASMIPBlocking represents the capability for ASM to block requests based on user IP
ASMIPBlocking
// ASMDDRules represents the capability to update the rules used by the ASM WAF for threat detection
ASMDDRules
// ASMExclusions represents the capability for ASM to exclude traffic from its protections
ASMExclusions
// ASMRequestBlocking represents the capability for ASM to block requests based on the HTTP request related WAF addresses
ASMRequestBlocking
// ASMResponseBlocking represents the capability for ASM to block requests based on the HTTP response related WAF addresses
ASMResponseBlocking
// ASMUserBlocking represents the capability for ASM to block requests based on user ID
ASMUserBlocking
// ASMCustomRules represents the capability for ASM to receive and use user-defined security rules
ASMCustomRules
// ASMCustomBlockingResponse represents the capability for ASM to receive and use user-defined blocking responses
ASMCustomBlockingResponse
// ASMTrustedIPs represents Trusted IPs through the ASM product
ASMTrustedIPs
// ASMApiSecuritySampleRate represents API Security sampling rate
ASMApiSecuritySampleRate
// APMTracingSampleRate represents the rate at which to sample traces from APM client libraries
APMTracingSampleRate
// APMTracingLogsInjection enables APM client libraries to inject trace ids into log records
APMTracingLogsInjection
// APMTracingHTTPHeaderTags enables APM client libraries to tag http header values to http server or client spans
APMTracingHTTPHeaderTags
// APMTracingCustomTags enables APM client to set custom tags on all spans
APMTracingCustomTags
)
// ErrClientNotStarted is returned when the remote config client is not started.
var ErrClientNotStarted = errors.New("remote config client not started")
// ProductUpdate represents an update for a specific product.
// It is a map of file path to raw file content
type ProductUpdate map[string][]byte
// A Client interacts with an Agent to update and track the state of remote
// configuration
type Client struct {
sync.RWMutex
ClientConfig
clientID string
endpoint string
repository *rc.Repository
stop chan struct{}
// When acquiring several locks and using defer to release them, make sure to acquire the locks in the following order:
callbacks []Callback
_callbacksMu sync.RWMutex
products map[string]struct{}
productsMu sync.RWMutex
productsWithCallbacks map[string]ProductCallback
productsWithCallbacksMu sync.RWMutex
capabilities map[Capability]struct{}
capabilitiesMu sync.RWMutex
lastError error
}
// client is a RC client singleton that can be accessed by multiple products (tracing, ASM, profiling etc.).
// Using a single RC client instance in the tracer is a requirement for remote configuration.
var client *Client
var (
startOnce sync.Once
stopOnce sync.Once
)
// newClient creates a new remoteconfig Client
func newClient(config ClientConfig) (*Client, error) {
repo, err := rc.NewUnverifiedRepository()
if err != nil {
return nil, err
}
if config.HTTP == nil {
config.HTTP = DefaultClientConfig().HTTP
}
return &Client{
ClientConfig: config,
clientID: generateID(),
endpoint: fmt.Sprintf("%s/v0.7/config", config.AgentURL),
repository: repo,
stop: make(chan struct{}),
lastError: nil,
callbacks: []Callback{},
capabilities: map[Capability]struct{}{},
products: map[string]struct{}{},
productsWithCallbacks: make(map[string]ProductCallback),
}, nil
}
// Start starts the client's update poll loop in a fresh goroutine.
// Noop if the client has already started.
func Start(config ClientConfig) error {
var err error
startOnce.Do(func() {
client, err = newClient(config)
if err != nil {
return
}
go func() {
ticker := time.NewTicker(client.PollInterval)
defer ticker.Stop()
for {
select {
case <-client.stop:
close(client.stop)
return
case <-ticker.C:
client.Lock()
client.updateState()
client.Unlock()
}
}
}()
})
return err
}
// Stop stops the client's update poll loop.
// Noop if the client has already been stopped.
// The remote config client is supposed to have the same lifecycle as the tracer.
// It can't be restarted after a call to Stop() unless explicitly calling Reset().
func Stop() {
if client == nil {
// In case Stop() is called before Start()
return
}
stopOnce.Do(func() {
log.Debug("remoteconfig: gracefully stopping the client")
client.stop <- struct{}{}
select {
case <-client.stop:
log.Debug("remoteconfig: client stopped successfully")
case <-time.After(time.Second):
log.Debug("remoteconfig: client stopping timeout")
}
})
}
// Reset destroys the client instance.
// To be used only in tests to reset the state of the client.
func Reset() {
client = nil
startOnce = sync.Once{}
stopOnce = sync.Once{}
}
func (c *Client) updateState() {
data, err := c.newUpdateRequest()
if err != nil {
log.Error("remoteconfig: unexpected error while creating a new update request payload: %v", err)
return
}
req, err := http.NewRequest(http.MethodGet, c.endpoint, &data)
if err != nil {
log.Error("remoteconfig: unexpected error while creating a new http request: %v", err)
return
}
resp, err := c.HTTP.Do(req)
if err != nil {
log.Debug("remoteconfig: http request error: %v", err)
return
}
// Flush and close the response body when returning (cf. https://pkg.go.dev/net/http#Client.Do)
defer func() {
io.ReadAll(resp.Body)
resp.Body.Close()
}()
if sc := resp.StatusCode; sc != http.StatusOK {
log.Debug("remoteconfig: http request error: response status code is not 200 (OK) but %s", http.StatusText(sc))
return
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
log.Error("remoteconfig: http request error: could not read the response body: %v", err)
return
}
if body := string(respBody); body == `{}` || body == `null` {
return
}
var update clientGetConfigsResponse
if err := json.Unmarshal(respBody, &update); err != nil {
log.Error("remoteconfig: http request error: could not parse the json response body: %v", err)
return
}
c.lastError = c.applyUpdate(&update)
}
// Subscribe registers a product and its callback to be invoked when the client receives configuration updates.
// Subscribe should be preferred over RegisterProduct and RegisterCallback if your callback only handles a single product.
func Subscribe(product string, callback ProductCallback, capabilities ...Capability) error {
if client == nil {
return ErrClientNotStarted
}
client.productsMu.RLock()
defer client.productsMu.RUnlock()
if _, found := client.products[product]; found {
return fmt.Errorf("product %s already registered via RegisterProduct", product)
}
client.productsWithCallbacksMu.Lock()
defer client.productsWithCallbacksMu.Unlock()
client.productsWithCallbacks[product] = callback
client.capabilitiesMu.Lock()
defer client.capabilitiesMu.Unlock()
for _, cap := range capabilities {
client.capabilities[cap] = struct{}{}
}
return nil
}
// RegisterCallback allows registering a callback that will be invoked when the client
// receives configuration updates. It is up to that callback to then decide what to do
// depending on the product related to the configuration update.
func RegisterCallback(f Callback) error {
if client == nil {
return ErrClientNotStarted
}
client._callbacksMu.Lock()
defer client._callbacksMu.Unlock()
client.callbacks = append(client.callbacks, f)
return nil
}
// UnregisterCallback removes a previously registered callback from the active callbacks list
// This remove operation preserves ordering
func UnregisterCallback(f Callback) error {
if client == nil {
return ErrClientNotStarted
}
client._callbacksMu.Lock()
defer client._callbacksMu.Unlock()
fValue := reflect.ValueOf(f)
for i, callback := range client.callbacks {
if reflect.ValueOf(callback) == fValue {
client.callbacks = append(client.callbacks[:i], client.callbacks[i+1:]...)
break
}
}
return nil
}
// RegisterProduct adds a product to the list of products listened by the client
func RegisterProduct(p string) error {
if client == nil {
return ErrClientNotStarted
}
client.productsMu.Lock()
defer client.productsMu.Unlock()
client.productsWithCallbacksMu.RLock()
defer client.productsWithCallbacksMu.RUnlock()
if _, found := client.productsWithCallbacks[p]; found {
return fmt.Errorf("product %s already registered via Subscribe", p)
}
client.products[p] = struct{}{}
return nil
}
// UnregisterProduct removes a product from the list of products listened by the client
func UnregisterProduct(p string) error {
if client == nil {
return ErrClientNotStarted
}
client.productsMu.Lock()
defer client.productsMu.Unlock()
delete(client.products, p)
return nil
}
// HasProduct returns whether a given product was registered
func HasProduct(p string) (bool, error) {
if client == nil {
return false, ErrClientNotStarted
}
client.productsMu.RLock()
defer client.productsMu.RUnlock()
client.productsWithCallbacksMu.RLock()
defer client.productsWithCallbacksMu.RUnlock()
_, found := client.products[p]
_, foundWithCallback := client.productsWithCallbacks[p]
return found || foundWithCallback, nil
}
// RegisterCapability adds a capability to the list of capabilities exposed by the client when requesting
// configuration updates
func RegisterCapability(cap Capability) error {
if client == nil {
return ErrClientNotStarted
}
client.capabilitiesMu.Lock()
defer client.capabilitiesMu.Unlock()
client.capabilities[cap] = struct{}{}
return nil
}
// UnregisterCapability removes a capability from the list of capabilities exposed by the client when requesting
// configuration updates
func UnregisterCapability(cap Capability) error {
if client == nil {
return ErrClientNotStarted
}
client.capabilitiesMu.Lock()
defer client.capabilitiesMu.Unlock()
delete(client.capabilities, cap)
return nil
}
// HasCapability returns whether a given capability was registered
func HasCapability(cap Capability) (bool, error) {
if client == nil {
return false, ErrClientNotStarted
}
client.capabilitiesMu.RLock()
defer client.capabilitiesMu.RUnlock()
_, found := client.capabilities[cap]
return found, nil
}
func (c *Client) globalCallbacks() []Callback {
c._callbacksMu.RLock()
defer c._callbacksMu.RUnlock()
callbacks := make([]Callback, len(c.callbacks))
copy(callbacks, c.callbacks)
return callbacks
}
func (c *Client) productCallbacks() map[string]ProductCallback {
c.productsWithCallbacksMu.RLock()
defer c.productsWithCallbacksMu.RUnlock()
callbacks := make(map[string]ProductCallback, len(c.productsWithCallbacks))
for k, v := range c.productsWithCallbacks {
callbacks[k] = v
}
return callbacks
}
func (c *Client) allProducts() []string {
c.productsMu.RLock()
defer c.productsMu.RUnlock()
c.productsWithCallbacksMu.RLock()
defer c.productsWithCallbacksMu.RUnlock()
products := make([]string, 0, len(c.products)+len(c.productsWithCallbacks))
for p := range c.products {
products = append(products, p)
}
for p := range c.productsWithCallbacks {
products = append(products, p)
}
return products
}
func (c *Client) applyUpdate(pbUpdate *clientGetConfigsResponse) error {
fileMap := make(map[string][]byte, len(pbUpdate.TargetFiles))
allProducts := c.allProducts()
productUpdates := make(map[string]ProductUpdate, len(allProducts))
for _, p := range allProducts {
productUpdates[p] = make(ProductUpdate)
}
for _, f := range pbUpdate.TargetFiles {
fileMap[f.Path] = f.Raw
for _, p := range allProducts {
// Check the config file path to make sure it belongs to the right product
if strings.Contains(f.Path, "/"+p+"/") {
productUpdates[p][f.Path] = f.Raw
}
}
}
mapify := func(s *rc.RepositoryState) map[string]string {
m := make(map[string]string)
for i := range s.Configs {
path := s.CachedFiles[i].Path
product := s.Configs[i].Product
m[path] = product
}
return m
}
// Check the repository state before and after the update to detect which configs are not being sent anymore.
// This is needed because some products can stop sending configurations, and we want to make sure that the subscribers
// are provided with this information in this case
stateBefore, err := c.repository.CurrentState()
if err != nil {
return fmt.Errorf("repository current state error: %v", err)
}
products, err := c.repository.Update(rc.Update{
TUFRoots: pbUpdate.Roots,
TUFTargets: pbUpdate.Targets,
TargetFiles: fileMap,
ClientConfigs: pbUpdate.ClientConfigs,
})
if err != nil {
return fmt.Errorf("repository update error: %v", err)
}
stateAfter, err := c.repository.CurrentState()
if err != nil {
return fmt.Errorf("repository current state error after update: %v", err)
}
// Create a config files diff between before/after the update to see which config files are missing
mBefore := mapify(&stateBefore)
for k := range mapify(&stateAfter) {
delete(mBefore, k)
}
// Set the payload data to nil for missing config files. The callbacks then can handle the nil config case to detect
// that this config will not be updated anymore.
updatedProducts := make(map[string]struct{})
for path, product := range mBefore {
if productUpdates[product] == nil {
productUpdates[product] = make(ProductUpdate)
}
productUpdates[product][path] = nil
updatedProducts[product] = struct{}{}
}
// Aggregate updated products and missing products so that callbacks get called for both
for _, p := range products {
updatedProducts[p] = struct{}{}
}
if len(updatedProducts) == 0 {
return nil
}
// Performs the callbacks registered and update the application status in the repository (RCTE2)
// In case of several callbacks handling the same config, statuses take precedence in this order:
// 1 - ApplyStateError
// 2 - ApplyStateUnacknowledged
// 3 - ApplyStateAcknowledged
// This makes sure that any product that would need to re-receive the config in a subsequent update will be allowed to
statuses := make(map[string]rc.ApplyStatus)
for _, fn := range c.globalCallbacks() {
for path, status := range fn(productUpdates) {
if s, ok := statuses[path]; !ok || status.State == rc.ApplyStateError ||
s.State == rc.ApplyStateAcknowledged && status.State == rc.ApplyStateUnacknowledged {
statuses[path] = status
}
}
}
// Call the product-specific callbacks registered via Subscribe
productCallbacks := c.productCallbacks()
for product, update := range productUpdates {
if fn, ok := productCallbacks[product]; ok {
for path, status := range fn(update) {
statuses[path] = status
}
}
}
for p, s := range statuses {
c.repository.UpdateApplyStatus(p, s)
}
return nil
}
func (c *Client) newUpdateRequest() (bytes.Buffer, error) {
state, err := c.repository.CurrentState()
if err != nil {
return bytes.Buffer{}, err
}
// Temporary check while using untrusted repo, for which no initial root file is provided
if state.RootsVersion < 1 {
state.RootsVersion = 1
}
pbCachedFiles := make([]*targetFileMeta, 0, len(state.CachedFiles))
for _, f := range state.CachedFiles {
pbHashes := make([]*targetFileHash, 0, len(f.Hashes))
for alg, hash := range f.Hashes {
pbHashes = append(pbHashes, &targetFileHash{
Algorithm: alg,
Hash: hex.EncodeToString(hash),
})
}
pbCachedFiles = append(pbCachedFiles, &targetFileMeta{
Path: f.Path,
Length: int64(f.Length),
Hashes: pbHashes,
})
}
hasError := c.lastError != nil
errMsg := ""
if hasError {
errMsg = c.lastError.Error()
}
var pbConfigState []*configState
if !hasError {
pbConfigState = make([]*configState, 0, len(state.Configs))
for _, f := range state.Configs {
pbConfigState = append(pbConfigState, &configState{
ID: f.ID,
Version: f.Version,
Product: f.Product,
ApplyState: f.ApplyStatus.State,
ApplyError: f.ApplyStatus.Error,
})
}
}
capa := big.NewInt(0)
for i := range c.capabilities {
capa.SetBit(capa, int(i), 1)
}
req := clientGetConfigsRequest{
Client: &clientData{
State: &clientState{
RootVersion: uint64(state.RootsVersion),
TargetsVersion: uint64(state.TargetsVersion),
ConfigStates: pbConfigState,
HasError: hasError,
Error: errMsg,
},
ID: c.clientID,
Products: c.allProducts(),
IsTracer: true,
ClientTracer: &clientTracer{
RuntimeID: c.RuntimeID,
Language: "go",
TracerVersion: c.TracerVersion,
Service: c.ServiceName,
Env: c.Env,
AppVersion: c.AppVersion,
},
Capabilities: capa.Bytes(),
},
CachedTargetFiles: pbCachedFiles,
}
var b bytes.Buffer
err = json.NewEncoder(&b).Encode(&req)
if err != nil {
return bytes.Buffer{}, err
}
return b, nil
}
var (
idSize = 21
idAlphabet = []rune("_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
)
func generateID() string {
bytes := make([]byte, idSize)
_, err := rand.Read(bytes)
if err != nil {
panic(err)
}
id := make([]rune, idSize)
for i := 0; i < idSize; i++ {
id[i] = idAlphabet[bytes[i]&63]
}
return string(id[:idSize])
}