/
policies.go
177 lines (142 loc) · 4.71 KB
/
policies.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
// 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 2016-present Datadog, Inc.
// Package rconfig holds rconfig related files
package rconfig
import (
"bytes"
"fmt"
"strings"
"sync"
"time"
"github.com/hashicorp/go-multierror"
"github.com/skydive-project/go-debouncer"
"github.com/DataDog/datadog-agent/pkg/api/security"
"github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/config/remote/client"
"github.com/DataDog/datadog-agent/pkg/remoteconfig/state"
"github.com/DataDog/datadog-agent/pkg/security/secl/rules"
"github.com/DataDog/datadog-agent/pkg/security/utils"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
const (
securityAgentRCPollInterval = time.Second * 1
debounceDelay = 5 * time.Second
)
// RCPolicyProvider defines a remote config policy provider
type RCPolicyProvider struct {
sync.RWMutex
client *client.Client
onNewPoliciesReadyCb func()
lastDefaults map[string]state.RawConfig
lastCustoms map[string]state.RawConfig
debouncer *debouncer.Debouncer
}
var _ rules.PolicyProvider = (*RCPolicyProvider)(nil)
// NewRCPolicyProvider returns a new Remote Config based policy provider
func NewRCPolicyProvider() (*RCPolicyProvider, error) {
agentVersion, err := utils.GetAgentSemverVersion()
if err != nil {
return nil, fmt.Errorf("failed to parse agent version: %w", err)
}
ipcAddress, err := config.GetIPCAddress()
if err != nil {
return nil, fmt.Errorf("failed to get ipc address: %w", err)
}
c, err := client.NewGRPCClient(ipcAddress, config.GetIPCPort(), func() (string, error) { return security.FetchAuthToken(config.Datadog) },
client.WithAgent(agentName, agentVersion.String()),
client.WithProducts(state.ProductCWSDD, state.ProductCWSCustom),
client.WithPollInterval(securityAgentRCPollInterval),
client.WithDirectorRootOverride(config.Datadog.GetString("site"), config.Datadog.GetString("remote_configuration.director_root")),
)
if err != nil {
return nil, err
}
r := &RCPolicyProvider{
client: c,
}
r.debouncer = debouncer.New(debounceDelay, r.onNewPoliciesReady)
return r, nil
}
// Start starts the Remote Config policy provider and subscribes to updates
func (r *RCPolicyProvider) Start() {
log.Info("remote-config policies provider started")
r.debouncer.Start()
r.client.Subscribe(state.ProductCWSDD, r.rcDefaultsUpdateCallback)
r.client.Subscribe(state.ProductCWSCustom, r.rcCustomsUpdateCallback)
r.client.Start()
}
func (r *RCPolicyProvider) rcDefaultsUpdateCallback(configs map[string]state.RawConfig, _ func(string, state.ApplyStatus)) {
r.Lock()
if len(r.lastDefaults) == 0 && len(configs) == 0 {
r.Unlock()
return
}
r.lastDefaults = configs
r.Unlock()
log.Info("new policies from remote-config policy provider")
r.debouncer.Call()
}
func (r *RCPolicyProvider) rcCustomsUpdateCallback(configs map[string]state.RawConfig, _ func(string, state.ApplyStatus)) {
r.Lock()
if len(r.lastCustoms) == 0 && len(configs) == 0 {
r.Unlock()
return
}
r.lastCustoms = configs
r.Unlock()
log.Info("new policies from remote-config policy provider")
r.debouncer.Call()
}
func normalize(policy *rules.Policy) {
// remove the version
_, normalized, found := strings.Cut(policy.Name, ".")
if found {
policy.Name = normalized
}
}
// LoadPolicies implements the PolicyProvider interface
func (r *RCPolicyProvider) LoadPolicies(macroFilters []rules.MacroFilter, ruleFilters []rules.RuleFilter) ([]*rules.Policy, *multierror.Error) {
var policies []*rules.Policy
var errs *multierror.Error
r.RLock()
defer r.RUnlock()
load := func(id string, cfg []byte) {
reader := bytes.NewReader(cfg)
policy, err := rules.LoadPolicy(id, rules.PolicyProviderTypeRC, reader, macroFilters, ruleFilters)
if err != nil {
errs = multierror.Append(errs, err)
}
normalize(policy)
policies = append(policies, policy)
}
for _, c := range r.lastDefaults {
load(c.Metadata.ID, c.Config)
}
for _, c := range r.lastCustoms {
load(c.Metadata.ID, c.Config)
}
return policies, errs
}
// SetOnNewPoliciesReadyCb implements the PolicyProvider interface
func (r *RCPolicyProvider) SetOnNewPoliciesReadyCb(cb func()) {
r.onNewPoliciesReadyCb = cb
}
func (r *RCPolicyProvider) onNewPoliciesReady() {
r.RLock()
defer r.RUnlock()
if r.onNewPoliciesReadyCb != nil {
r.onNewPoliciesReadyCb()
}
}
// Close stops the client
func (r *RCPolicyProvider) Close() error {
r.debouncer.Stop()
r.client.Close()
return nil
}
// Type returns the type of this policy provider
func (r *RCPolicyProvider) Type() string {
return rules.PolicyProviderTypeRC
}