forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 2
/
compliance.go
172 lines (144 loc) · 5.37 KB
/
compliance.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
// 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 app
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
ddgostatsd "github.com/DataDog/datadog-go/statsd"
secagentcommon "github.com/StackVista/stackstate-agent/cmd/security-agent/common"
"github.com/StackVista/stackstate-agent/pkg/collector/runner"
"github.com/StackVista/stackstate-agent/pkg/collector/scheduler"
"github.com/StackVista/stackstate-agent/pkg/compliance/agent"
"github.com/StackVista/stackstate-agent/pkg/compliance/checks"
"github.com/StackVista/stackstate-agent/pkg/compliance/event"
coreconfig "github.com/StackVista/stackstate-agent/pkg/config"
"github.com/StackVista/stackstate-agent/pkg/logs"
"github.com/StackVista/stackstate-agent/pkg/logs/client"
"github.com/StackVista/stackstate-agent/pkg/logs/config"
"github.com/StackVista/stackstate-agent/pkg/logs/restart"
"github.com/StackVista/stackstate-agent/pkg/util/log"
)
var (
complianceCmd = &cobra.Command{
Use: "compliance",
Short: "Compliance Agent utility commands",
}
eventCmd = &cobra.Command{
Use: "event",
Short: "Issue logs to test Security Agent compliance events",
RunE: eventRun,
Hidden: true,
}
eventArgs = struct {
sourceName string
sourceType string
event event.Event
data []string
}{}
)
func init() {
complianceCmd.AddCommand(eventCmd)
eventCmd.Flags().StringVarP(&eventArgs.sourceType, "source-type", "", "compliance", "Log source name")
eventCmd.Flags().StringVarP(&eventArgs.sourceName, "source-name", "", "compliance-agent", "Log source name")
eventCmd.Flags().StringVarP(&eventArgs.event.AgentRuleID, "rule-id", "", "", "Rule ID")
eventCmd.Flags().StringVarP(&eventArgs.event.ResourceID, "resource-id", "", "", "Resource ID")
eventCmd.Flags().StringVarP(&eventArgs.event.ResourceType, "resource-type", "", "", "Resource type")
eventCmd.Flags().StringSliceVarP(&eventArgs.event.Tags, "tags", "t", []string{"security:compliance"}, "Tags")
eventCmd.Flags().StringSliceVarP(&eventArgs.data, "data", "d", []string{}, "Data KV fields")
}
func newLogContextCompliance() (*config.Endpoints, *client.DestinationsContext, error) {
logsConfigComplianceKeys := config.NewLogsConfigKeys("compliance_config.endpoints.", coreconfig.Datadog)
return newLogContext(logsConfigComplianceKeys, "cspm-intake.", "compliance", config.DefaultIntakeOrigin, logs.AgentJSONIntakeProtocol)
}
func eventRun(cmd *cobra.Command, args []string) error {
// Read configuration files received from the command line arguments '-c'
if err := secagentcommon.MergeConfigurationFiles("datadog", confPathArray, cmd.Flags().Lookup("cfgpath").Changed); err != nil {
return err
}
stopper := restart.NewSerialStopper()
defer stopper.Stop()
endpoints, dstContext, err := newLogContextCompliance()
if err != nil {
return err
}
runPath := coreconfig.Datadog.GetString("compliance_config.run_path")
reporter, err := event.NewLogReporter(stopper, eventArgs.sourceName, eventArgs.sourceType, runPath, endpoints, dstContext)
if err != nil {
return fmt.Errorf("failed to set up compliance log reporter: %w", err)
}
eventData := event.Data{}
for _, d := range eventArgs.data {
kv := strings.SplitN(d, ":", 2)
if len(kv) != 2 {
continue
}
eventData[kv[0]] = kv[1]
}
eventArgs.event.Data = eventData
reporter.Report(&eventArgs.event)
return nil
}
func startCompliance(hostname string, stopper restart.Stopper, statsdClient *ddgostatsd.Client) error {
enabled := coreconfig.Datadog.GetBool("compliance_config.enabled")
if !enabled {
return nil
}
endpoints, context, err := newLogContextCompliance()
if err != nil {
log.Error(err)
}
stopper.Add(context)
runPath := coreconfig.Datadog.GetString("compliance_config.run_path")
reporter, err := event.NewLogReporter(stopper, "compliance-agent", "compliance", runPath, endpoints, context)
if err != nil {
return err
}
runner := runner.NewRunner()
stopper.Add(runner)
scheduler := scheduler.NewScheduler(runner.GetChan())
runner.SetScheduler(scheduler)
checkInterval := coreconfig.Datadog.GetDuration("compliance_config.check_interval")
checkMaxEvents := coreconfig.Datadog.GetInt("compliance_config.check_max_events_per_run")
configDir := coreconfig.Datadog.GetString("compliance_config.dir")
options := []checks.BuilderOption{
checks.WithInterval(checkInterval),
checks.WithMaxEvents(checkMaxEvents),
checks.WithHostname(hostname),
checks.WithHostRootMount(os.Getenv("HOST_ROOT")),
checks.MayFail(checks.WithDocker()),
checks.MayFail(checks.WithAudit()),
}
if coreconfig.IsKubernetes() {
nodeLabels, err := agent.WaitGetNodeLabels()
if err != nil {
log.Error(err)
} else {
options = append(options, checks.WithNodeLabels(nodeLabels))
}
}
agent, err := agent.New(
reporter,
scheduler,
configDir,
options...,
)
if err != nil {
log.Errorf("Compliance agent failed to initialize: %v", err)
return err
}
err = agent.Run()
if err != nil {
log.Errorf("Error starting compliance agent, exiting: %v", err)
return err
}
stopper.Add(agent)
log.Infof("Running compliance checks every %s", checkInterval.String())
// Send the compliance 'running' metrics periodically
ticker := sendRunningMetrics(statsdClient, "compliance")
stopper.Add(ticker)
return nil
}