/
winregistryimpl.go
374 lines (339 loc) · 12.6 KB
/
winregistryimpl.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
// 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 2023-present Datadog, Inc.
//go:build windows
// Package winregistryimpl contains the implementation of the Windows Registry check
package winregistryimpl
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/fs"
"strconv"
"strings"
"github.com/DataDog/datadog-agent/comp/checks/winregistry"
"github.com/DataDog/datadog-agent/comp/core/autodiscovery/integration"
"github.com/DataDog/datadog-agent/comp/core/log"
"github.com/DataDog/datadog-agent/comp/logs/agent"
logsConfig "github.com/DataDog/datadog-agent/comp/logs/agent/config"
"github.com/DataDog/datadog-agent/pkg/aggregator/sender"
"github.com/DataDog/datadog-agent/pkg/collector/check"
core "github.com/DataDog/datadog-agent/pkg/collector/corechecks"
"github.com/DataDog/datadog-agent/pkg/logs/message"
"github.com/DataDog/datadog-agent/pkg/logs/sources"
"github.com/DataDog/datadog-agent/pkg/metrics"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
agentLog "github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/util/optional"
yy "github.com/ghodss/yaml"
"github.com/swaggest/jsonschema-go"
"github.com/xeipuuv/gojsonschema"
"go.uber.org/fx"
"golang.org/x/sys/windows/registry"
"gopkg.in/yaml.v2"
)
const (
checkName = "windows_registry" // this appears in the Agent Manager and Agent status
checkPrefix = "winregistry" // This is the prefix used for all metrics emitted by this check
)
// Module defines the fx options for this component.
func Module() fxutil.Module {
return fxutil.Component(
fx.Provide(newWindowsRegistryComponent))
}
type dependencies struct {
fx.In
// Logs Agent component, used to send integration logs
// It is optional because the Logs Agent can be disabled
LogsComponent optional.Option[agent.Component]
// Datadog Agent logs component, used to log to the Agent logs
Log log.Component
Lifecycle fx.Lifecycle
}
type registryValueCfg struct {
Name string `json:"name" yaml:"name" required:"true"` // The metric name of the registry value
DefaultValue optional.Option[float64] `json:"default_value" yaml:"default_value"`
Mappings []map[string]float64 `json:"mapping" yaml:"mapping"`
}
type registryKeyCfg struct {
Name string `json:"name" yaml:"name" required:"true"` // The metric name of the registry key
RegistryValues map[string]registryValueCfg `json:"registry_values" yaml:"registry_values" minItems:"1" nullable:"false" required:"true"` // The map key is the registry value name
}
// checkCfg is the config that is specific to each check instance
type checkCfg struct {
RegistryKeys map[string]registryKeyCfg `json:"registry_keys" yaml:"registry_keys" nullable:"false" required:"true"`
SendOnStart optional.Option[bool] `json:"send_on_start" yaml:"send_on_start"`
}
// checkInitCfg is the config that is common to all check instances
type checkInitCfg struct {
SendOnStart optional.Option[bool] `yaml:"send_on_start"`
}
// registryKey is the in-memory representation of the key to monitor
// it's different from the registryKeyCfg because we need to split the hive
// from the keypath. It's easier to do this once during the Configure phase,
// than every time the check runs.
type registryKey struct {
name string
hive registry.Key
keyPath string
originalKeyPath string // keep the original keypath around, for logging errors
registryValues map[string]registryValueCfg
}
// WindowsRegistryCheck contains the field for the WindowsRegistryCheck
type WindowsRegistryCheck struct {
core.CheckBase
metrics.Gauge
senderManager sender.SenderManager
sender sender.Sender
logsComponent agent.Component
log log.Component
registryKeys []registryKey
registryDelegate registryDelegate
integrationLogsDelegate *integrationLogsRegistryDelegate
}
func createOptionMapping[T any](reflector *jsonschema.Reflector, sourceType jsonschema.SimpleType) {
option := jsonschema.Schema{}
option.AddType(sourceType)
reflector.AddTypeMapping(optional.Option[T]{}, option)
}
func createSchema() ([]byte, error) {
reflector := jsonschema.Reflector{}
createOptionMapping[bool](&reflector, jsonschema.Boolean)
createOptionMapping[float64](&reflector, jsonschema.Number)
schema, err := reflector.Reflect(checkCfg{})
if err != nil {
return nil, err
}
return json.MarshalIndent(schema, "", " ")
}
// Configure configures the check
func (c *WindowsRegistryCheck) Configure(senderManager sender.SenderManager, integrationConfigDigest uint64, data integration.Data, initConfig integration.Data, source string) error {
c.senderManager = senderManager
c.BuildID(integrationConfigDigest, data, initConfig)
err := c.CommonConfigure(senderManager, integrationConfigDigest, initConfig, data, source)
if err != nil {
return err
}
schemaString, err := createSchema()
if err != nil {
agentLog.Errorf("failed to create validation schema: %s", err)
return err
}
schemaLoader := gojsonschema.NewBytesLoader(schemaString)
rawDocument, err := yy.YAMLToJSON(data)
if err != nil {
agentLog.Errorf("failed to load the config to JSON: %s", err)
return err
}
documentLoader := gojsonschema.NewBytesLoader(rawDocument)
result, _ := gojsonschema.Validate(schemaLoader, documentLoader)
if !result.Valid() {
for _, err := range result.Errors() {
if err.Value() != nil {
agentLog.Errorf("configuration error: %s", err)
} else {
agentLog.Errorf("configuration error: %s (%v)", err, err.Value())
}
}
return fmt.Errorf("configuration validation failed")
}
var initCfg checkInitCfg
if err := yaml.Unmarshal(initConfig, &initCfg); err != nil {
agentLog.Errorf("cannot unmarshal shared configuration: %s", err)
return err
}
var conf checkCfg
if err := yaml.Unmarshal(data, &conf); err != nil {
agentLog.Errorf("cannot unmarshal configuration: %s", err)
return err
}
var sendOnStart, sendOnStartSet bool
if sendOnStart, sendOnStartSet = conf.SendOnStart.Get(); !sendOnStartSet {
if initSendOnStart, initSendOnStartSet := initCfg.SendOnStart.Get(); initSendOnStartSet {
sendOnStart = initSendOnStart
} else {
sendOnStart = true
}
}
hiveMap := map[string]registry.Key{
"HKLM": registry.LOCAL_MACHINE,
"HKEY_LOCAL_MACHINE": registry.LOCAL_MACHINE,
"HKU": registry.USERS,
"HKEY_USERS": registry.USERS,
"HKEY_CLASSES_ROOT": registry.CLASSES_ROOT,
"HKCR": registry.CLASSES_ROOT,
}
for regKey, regKeyConfig := range conf.RegistryKeys {
splitKeypath := strings.SplitN(regKey, "\\", 2)
if len(splitKeypath) != 2 {
err = fmt.Errorf("the key %s is too short to be a valid key", regKey)
agentLog.Errorf("configuration error: %s", err)
return err
}
if len(regKeyConfig.Name) == 0 {
c.log.Warnf("the key %s does not have a metric name, skipping", regKey)
continue
}
if hive, found := hiveMap[splitKeypath[0]]; found {
regValues := make(map[string]registryValueCfg)
for valueName, regValueCfg := range regKeyConfig.RegistryValues {
if len(regValueCfg.Name) == 0 {
c.log.Warnf("the subkey %s of %s does not have a metric name, skipping", valueName, regKey)
} else {
regValues[valueName] = regValueCfg
}
}
c.registryKeys = append(c.registryKeys, registryKey{
name: regKeyConfig.Name,
hive: hive,
originalKeyPath: regKey,
keyPath: splitKeypath[1],
registryValues: regValues,
})
} else {
err = fmt.Errorf("unknown hive %s", splitKeypath[0])
agentLog.Errorf("configuration error: %s", err)
return err
}
}
c.sender, err = c.GetSender()
if err != nil {
agentLog.Errorf("failed to retrieve a sender for check %s: %s", string(c.ID()), err)
return err
}
c.sender.FinalizeCheckServiceTag()
c.integrationLogsDelegate = &integrationLogsRegistryDelegate{
logsComponent: c.logsComponent,
valueMap: make(map[string]interface{}),
origin: message.NewOrigin(sources.NewLogSource("Windows registry check", &logsConfig.LogsConfig{
Source: checkName,
Service: checkName,
})),
// When sendOnStart is enabled, we unmute the integrations logs sender
// which will produce a "key_created" event for the existing keys in the registry
// during the first check run.
// Otherwise, the integrations logs sender will get unmuted on the subsequent check runs.
muted: !sendOnStart,
}
c.registryDelegate = compositeRegistryDelegate{
registryDelegates: []registryDelegate{
loggingRegistryDelegate{
log: c.log,
},
metricsRegistryDelegate{
sender: c.sender,
},
c.integrationLogsDelegate,
},
}
return nil
}
func (c *WindowsRegistryCheck) processRegistryValues(regDelegate registryDelegate, regKey registry.Key, regKeyCfg registryKey) {
for valueName, regValueCfg := range regKeyCfg.registryValues {
var err error
var valueType uint32
// regKey == 0 means parent key didn't exist, but we want to emit missing metric for each of its values
if regKey == 0 {
err = registry.ErrNotExist
} else {
_, valueType, err = regKey.GetValue(valueName, nil)
}
if errors.Is(err, registry.ErrNotExist) {
regDelegate.onMissing(valueName, regKeyCfg, regValueCfg, err)
} else if errors.Is(err, fs.ErrPermission) {
regDelegate.onAccessDenied(valueName, regKeyCfg, regValueCfg, err)
} else if errors.Is(err, registry.ErrShortBuffer) || err == nil {
switch valueType {
case registry.DWORD:
fallthrough
case registry.QWORD:
val, _, err := regKey.GetIntegerValue(valueName)
if err != nil {
regDelegate.onRetrievalError(valueName, regKeyCfg, regValueCfg, err)
continue
}
regDelegate.onSendNumber(valueName, float64(val), regKeyCfg, regValueCfg)
case registry.SZ:
fallthrough
case registry.EXPAND_SZ:
val, _, err := regKey.GetStringValue(valueName)
if err != nil {
regDelegate.onRetrievalError(valueName, regKeyCfg, regValueCfg, err)
continue
}
if valueType == registry.EXPAND_SZ {
val, err = registry.ExpandString(val)
}
if err != nil {
regDelegate.onRetrievalError(valueName, regKeyCfg, regValueCfg, err)
continue
}
// First try to parse the value into a float64
if parsedVal, err := strconv.ParseFloat(val, 64); err == nil {
regDelegate.onSendNumber(valueName, parsedVal, regKeyCfg, regValueCfg)
} else {
// Value can't be parsed, let's check the mappings
var mappingFound = false
for _, mapping := range regValueCfg.Mappings {
if mappedValue, found := mapping[val]; found {
regDelegate.onSendMappedNumber(valueName, val, mappedValue, regKeyCfg, regValueCfg)
mappingFound = true
break
}
}
if !mappingFound {
regDelegate.onNoMappingFound(valueName, val, regKeyCfg, regValueCfg)
}
}
default:
regDelegate.onUnsupportedDataType(valueName, valueType, regKeyCfg, regValueCfg)
}
}
}
}
func (c *WindowsRegistryCheck) processRegistryKeys(regDelegate registryDelegate) {
for _, regKeyCfg := range c.registryKeys {
regKey, err := registry.OpenKey(regKeyCfg.hive, regKeyCfg.keyPath, registry.QUERY_VALUE)
if err != nil {
if errors.Is(err, fs.ErrPermission) {
// Treat access denied as errors
c.log.Errorf("access denied while accessing key %s: %s", regKeyCfg.originalKeyPath, err)
} else if errors.Is(err, registry.ErrNotExist) {
c.log.Warnf("key %s was not found: %s", regKeyCfg.originalKeyPath, err)
// Process registryValues too so that we can emit missing values for each registryValues
c.processRegistryValues(regDelegate, regKey, regKeyCfg)
}
} else {
// if err == nil the key was opened, so we need to close it after we are done.
c.processRegistryValues(regDelegate, regKey, regKeyCfg)
regKey.Close()
}
}
}
// Run runs the check
func (c *WindowsRegistryCheck) Run() error {
c.processRegistryKeys(c.registryDelegate)
c.sender.Commit()
if c.integrationLogsDelegate.muted {
c.integrationLogsDelegate.muted = false
}
return nil
}
func newWindowsRegistryComponent(deps dependencies) winregistry.Component {
deps.Lifecycle.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
core.RegisterCheck(checkName, optional.NewOption(func() check.Check {
integrationLogs, _ := deps.LogsComponent.Get()
return &WindowsRegistryCheck{
CheckBase: core.NewCheckBase(checkName),
logsComponent: integrationLogs,
log: deps.Log,
}
}))
return nil
},
})
return struct{}{}
}