-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathcustom_check.go
69 lines (56 loc) · 1.71 KB
/
custom_check.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
// 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 checks
import (
"errors"
"fmt"
"github.com/DataDog/datadog-agent/pkg/compliance"
"github.com/DataDog/datadog-agent/pkg/compliance/checks/custom"
"github.com/DataDog/datadog-agent/pkg/compliance/checks/env"
"github.com/DataDog/datadog-agent/pkg/compliance/eval"
)
type customCheck struct {
ruleID string
custom *compliance.Custom
expr *eval.IterableExpression
checkFunc custom.CheckFunc
}
type checkFactoryFunc func(name string) custom.CheckFunc
var customCheckFactory = custom.GetCustomCheck
func newCustomCheck(ruleID string, res compliance.Resource) (checkable, error) {
if res.Custom == nil {
return nil, errors.New("expecting custom resource in custom check")
}
if res.Custom.Name == "" {
return nil, errors.New("missing check name in custom check")
}
var (
expr *eval.IterableExpression
err error
)
if res.Condition != "" {
expr, err = eval.Cache.ParseIterable(res.Condition)
if err != nil {
return nil, err
}
}
checkFunc := customCheckFactory(res.Custom.Name)
if checkFunc == nil {
return nil, fmt.Errorf("custom check with name: %s does not exist", res.Custom.Name)
}
return &customCheck{
ruleID: ruleID,
custom: res.Custom,
expr: expr,
checkFunc: checkFunc,
}, nil
}
func (c *customCheck) check(e env.Env) []*compliance.Report {
report, err := c.checkFunc(e, c.ruleID, c.custom.Variables, c.expr)
if err != nil {
report = compliance.BuildReportForError(err)
}
return []*compliance.Report{report}
}