-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
command.go
394 lines (341 loc) · 15.2 KB
/
command.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
// 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 start implements start related subcommands
package start
import (
"context"
"errors"
_ "expvar" // Blank import used because this isn't directly used in this file
"fmt"
"net/http"
_ "net/http/pprof" // Blank import used because this isn't directly used in this file
"os"
"os/signal"
"syscall"
"time"
"github.com/spf13/cobra"
"go.uber.org/fx"
ddgostatsd "github.com/DataDog/datadog-go/v5/statsd"
"github.com/DataDog/datadog-agent/cmd/security-agent/api"
"github.com/DataDog/datadog-agent/cmd/security-agent/command"
"github.com/DataDog/datadog-agent/cmd/security-agent/subcommands/compliance"
"github.com/DataDog/datadog-agent/cmd/security-agent/subcommands/runtime"
"github.com/DataDog/datadog-agent/comp/agent/autoexit"
"github.com/DataDog/datadog-agent/comp/agent/autoexit/autoexitimpl"
"github.com/DataDog/datadog-agent/comp/api/authtoken/fetchonlyimpl"
"github.com/DataDog/datadog-agent/comp/core"
"github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/comp/core/configsync"
"github.com/DataDog/datadog-agent/comp/core/configsync/configsyncimpl"
"github.com/DataDog/datadog-agent/comp/core/log"
"github.com/DataDog/datadog-agent/comp/core/log/logimpl"
"github.com/DataDog/datadog-agent/comp/core/pid"
"github.com/DataDog/datadog-agent/comp/core/pid/pidimpl"
"github.com/DataDog/datadog-agent/comp/core/secrets"
"github.com/DataDog/datadog-agent/comp/core/settings"
"github.com/DataDog/datadog-agent/comp/core/settings/settingsimpl"
"github.com/DataDog/datadog-agent/comp/core/status"
"github.com/DataDog/datadog-agent/comp/core/status/statusimpl"
"github.com/DataDog/datadog-agent/comp/core/sysprobeconfig"
"github.com/DataDog/datadog-agent/comp/core/sysprobeconfig/sysprobeconfigimpl"
"github.com/DataDog/datadog-agent/comp/core/tagger"
"github.com/DataDog/datadog-agent/comp/core/tagger/taggerimpl"
"github.com/DataDog/datadog-agent/comp/core/telemetry"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta/collectors"
"github.com/DataDog/datadog-agent/comp/dogstatsd"
"github.com/DataDog/datadog-agent/comp/dogstatsd/statsd"
"github.com/DataDog/datadog-agent/comp/metadata/host/hostimpl"
"github.com/DataDog/datadog-agent/pkg/collector/python"
pkgCompliance "github.com/DataDog/datadog-agent/pkg/compliance"
pkgconfig "github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/config/model"
commonsettings "github.com/DataDog/datadog-agent/pkg/config/settings"
"github.com/DataDog/datadog-agent/pkg/config/setup"
"github.com/DataDog/datadog-agent/pkg/security/agent"
"github.com/DataDog/datadog-agent/pkg/security/utils"
"github.com/DataDog/datadog-agent/pkg/status/health"
"github.com/DataDog/datadog-agent/pkg/util"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
"github.com/DataDog/datadog-agent/pkg/util/optional"
"github.com/DataDog/datadog-agent/pkg/util/profiling"
"github.com/DataDog/datadog-agent/pkg/util/startstop"
"github.com/DataDog/datadog-agent/pkg/version"
)
type cliParams struct {
*command.GlobalParams
pidfilePath string
}
// Commands returns the start commands
func Commands(globalParams *command.GlobalParams) []*cobra.Command {
params := &cliParams{
GlobalParams: globalParams,
}
startCmd := &cobra.Command{
Use: "start",
Short: "Start the Security Agent",
Long: `Runs Datadog Security agent in the foreground`,
RunE: func(_ *cobra.Command, _ []string) error {
// TODO: Similar to the agent itself, once the security agent is represented as a component, and not a function (start),
// this will use `fxutil.Run` instead of `fxutil.OneShot`.
// note that any changes to the arguments to OneShot need to be reflected into
// the service initialization in ../../main_windows.go
return fxutil.OneShot(start,
fx.Supply(core.BundleParams{
ConfigParams: config.NewSecurityAgentParams(params.ConfigFilePaths),
SysprobeConfigParams: sysprobeconfigimpl.NewParams(sysprobeconfigimpl.WithSysProbeConfFilePath(globalParams.SysProbeConfFilePath)),
SecretParams: secrets.NewEnabledParams(),
LogParams: logimpl.ForDaemon(command.LoggerName, "security_agent.log_file", setup.DefaultSecurityAgentLogFile),
}),
core.Bundle(),
dogstatsd.ClientBundle,
// workloadmeta setup
collectors.GetCatalog(),
workloadmeta.Module(),
fx.Provide(func(config config.Component) workloadmeta.Params {
catalog := workloadmeta.NodeAgent
if config.GetBool("security_agent.remote_workloadmeta") {
catalog = workloadmeta.Remote
}
return workloadmeta.Params{
AgentType: catalog,
}
}),
taggerimpl.Module(),
fx.Provide(func(config config.Component) tagger.Params {
if config.GetBool("security_agent.remote_tagger") {
return tagger.NewNodeRemoteTaggerParams()
}
return tagger.NewTaggerParams()
}),
fx.Provide(func() startstop.Stopper {
return startstop.NewSerialStopper()
}),
fx.Provide(func(config config.Component, statsd statsd.Component) (ddgostatsd.ClientInterface, error) {
return statsd.CreateForHostPort(setup.GetBindHost(config), config.GetInt("dogstatsd_port"))
}),
fx.Provide(func(stopper startstop.Stopper, log log.Component, config config.Component, statsdClient ddgostatsd.ClientInterface, wmeta workloadmeta.Component) (status.InformationProvider, *agent.RuntimeSecurityAgent, error) {
hostnameDetected, err := utils.GetHostnameWithContextAndFallback(context.TODO())
if err != nil {
return status.NewInformationProvider(nil), nil, err
}
runtimeAgent, err := runtime.StartRuntimeSecurity(log, config, hostnameDetected, stopper, statsdClient, wmeta)
if err != nil {
return status.NewInformationProvider(nil), nil, err
}
if runtimeAgent == nil {
return status.NewInformationProvider(nil), nil, nil
}
// TODO - components: Do not remove runtimeAgent ref until "github.com/DataDog/datadog-agent/pkg/security/agent" is a component so they're not GCed
return status.NewInformationProvider(runtimeAgent.StatusProvider()), runtimeAgent, nil
}),
fx.Provide(func(stopper startstop.Stopper, log log.Component, config config.Component, statsdClient ddgostatsd.ClientInterface, sysprobeconfig sysprobeconfig.Component, wmeta workloadmeta.Component) (status.InformationProvider, *pkgCompliance.Agent, error) {
hostnameDetected, err := utils.GetHostnameWithContextAndFallback(context.TODO())
if err != nil {
return status.NewInformationProvider(nil), nil, err
}
// start compliance security agent
complianceAgent, err := compliance.StartCompliance(log, config, sysprobeconfig, hostnameDetected, stopper, statsdClient, wmeta)
if err != nil {
return status.NewInformationProvider(nil), nil, err
}
if complianceAgent == nil {
return status.NewInformationProvider(nil), nil, nil
}
// TODO - components: Do not remove complianceAgent ref until "github.com/DataDog/datadog-agent/pkg/compliance" is a component so they're not GCed
return status.NewInformationProvider(complianceAgent.StatusProvider()), complianceAgent, nil
}),
fx.Supply(
status.Params{
PythonVersionGetFunc: python.GetPythonVersion,
},
),
fx.Provide(func(config config.Component) status.HeaderInformationProvider {
return status.NewHeaderInformationProvider(hostimpl.StatusProvider{
Config: config,
})
}),
statusimpl.Module(),
fetchonlyimpl.Module(),
configsyncimpl.OptionalModule(),
// Force the instantiation of the component
fx.Invoke(func(_ optional.Option[configsync.Component]) {}),
autoexitimpl.Module(),
fx.Supply(pidimpl.NewParams(params.pidfilePath)),
fx.Provide(func(c config.Component) settings.Params {
return settings.Params{
Settings: map[string]settings.RuntimeSetting{
"log_level": commonsettings.NewLogLevelRuntimeSetting(),
},
Config: c,
}
}),
settingsimpl.Module(),
)
},
}
startCmd.Flags().StringVarP(¶ms.pidfilePath, "pidfile", "p", "", "path to the pidfile")
return []*cobra.Command{startCmd}
}
// start will start the security-agent.
//
// TODO(components): note how workloadmeta is passed anonymously, it is still required as it is used
// as a global. This should eventually be fixed and all workloadmeta interactions should be via the
// injected instance.
func start(log log.Component, config config.Component, _ secrets.Component, _ statsd.Component, _ sysprobeconfig.Component, telemetry telemetry.Component, statusComponent status.Component, _ pid.Component, _ autoexit.Component, settings settings.Component) error {
defer StopAgent(log)
err := RunAgent(log, config, telemetry, statusComponent, settings)
if errors.Is(err, errAllComponentsDisabled) || errors.Is(err, errNoAPIKeyConfigured) {
return nil
}
if err != nil {
return err
}
stopCh := make(chan struct{})
defer close(stopCh)
go handleSignals(log, stopCh)
// Block here until we receive a stop signal
<-stopCh
return nil
}
// handleSignals handles OS signals, and sends a message on stopCh when an interrupt
// signal is received.
func handleSignals(log log.Component, stopCh chan struct{}) {
// Setup a channel to catch OS signals
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGPIPE)
// Block here until we receive the interrupt signal
for signo := range signalCh {
switch signo {
case syscall.SIGPIPE:
// By default, systemd redirects the stdout to journald. When journald is stopped or crashes we receive a SIGPIPE signal.
// Go ignores SIGPIPE signals unless it is when stdout or stdout is closed, in this case the agent is stopped.
// We never want dogstatsd to stop upon receiving SIGPIPE, so we intercept the SIGPIPE signals and just discard them.
default:
log.Infof("Received signal '%s', shutting down...", signo)
stopCh <- struct{}{}
return
}
}
}
var (
stopper startstop.Stopper
srv *api.Server
expvarServer *http.Server
)
var errAllComponentsDisabled = errors.New("all security-agent component are disabled")
var errNoAPIKeyConfigured = errors.New("no API key configured")
// RunAgent initialized resources and starts API server
func RunAgent(log log.Component, config config.Component, telemetry telemetry.Component, statusComponent status.Component, settings settings.Component) (err error) {
if err := util.SetupCoreDump(config); err != nil {
log.Warnf("Can't setup core dumps: %v, core dumps might not be available after a crash", err)
}
// Check if we have at least one component to start based on config
if !config.GetBool("compliance_config.enabled") && !config.GetBool("runtime_security_config.enabled") {
log.Infof("All security-agent components are deactivated, exiting")
// A sleep is necessary so that sysV doesn't think the agent has failed
// to startup because of an error. Only applies on Debian 7.
time.Sleep(5 * time.Second)
return errAllComponentsDisabled
}
if !config.IsSet("api_key") {
log.Critical("No API key configured, exiting")
// A sleep is necessary so that sysV doesn't think the agent has failed
// to startup because of an error. Only applies on Debian 7.
time.Sleep(5 * time.Second)
return errNoAPIKeyConfigured
}
// Setup expvar server
port := config.GetString("security_agent.expvar_port")
pkgconfig.Datadog.Set("expvar_port", port, model.SourceAgentRuntime)
if config.GetBool("telemetry.enabled") {
http.Handle("/telemetry", telemetry.Handler())
}
expvarServer := &http.Server{
Addr: "127.0.0.1:" + port,
Handler: http.DefaultServeMux,
}
go func() {
err := expvarServer.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Errorf("Error creating expvar server on port %v: %v", port, err)
}
}()
srv, err = api.NewServer(statusComponent, settings)
if err != nil {
return log.Errorf("Error while creating api server, exiting: %v", err)
}
if err = srv.Start(); err != nil {
return log.Errorf("Error while starting api server, exiting: %v", err)
}
if err := setupInternalProfiling(config); err != nil {
return log.Errorf("Error while setuping internal profiling, exiting: %v", err)
}
log.Infof("Datadog Security Agent is now running.")
return
}
// StopAgent stops the API server and clean up resources
func StopAgent(log log.Component) {
// retrieve the agent health before stopping the components
// GetReadyNonBlocking has a 100ms timeout to avoid blocking
healthStatus, err := health.GetReadyNonBlocking()
if err != nil {
log.Warnf("Security Agent health unknown: %s", err)
} else if len(healthStatus.Unhealthy) > 0 {
log.Warnf("Some components were unhealthy: %v", healthStatus.Unhealthy)
}
// stop metaScheduler and statsd if they are instantiated
if stopper != nil {
stopper.Stop()
}
if srv != nil {
srv.Stop()
}
if expvarServer != nil {
if err := expvarServer.Shutdown(context.Background()); err != nil {
log.Errorf("Error shutting down expvar server: %v", err)
}
}
log.Info("See ya!")
}
func setupInternalProfiling(config config.Component) error {
if config.GetBool(secAgentKey("internal_profiling.enabled")) {
v, _ := version.Agent()
cfgSite := config.GetString(secAgentKey("internal_profiling.site"))
cfgURL := config.GetString(secAgentKey("internal_profiling.profile_dd_url"))
// check if TRACE_AGENT_URL is set, in which case, forward the profiles to the trace agent
var site string
if traceAgentURL := os.Getenv("TRACE_AGENT_URL"); len(traceAgentURL) > 0 {
site = fmt.Sprintf(profiling.ProfilingLocalURLTemplate, traceAgentURL)
} else {
site = fmt.Sprintf(profiling.ProfilingURLTemplate, cfgSite)
if cfgURL != "" {
site = cfgURL
}
}
tags := config.GetStringSlice(secAgentKey("internal_profiling.extra_tags"))
tags = append(tags, fmt.Sprintf("version:%v", v))
profSettings := profiling.Settings{
ProfilingURL: site,
Env: config.GetString(secAgentKey("internal_profiling.env")),
Service: "security-agent",
Period: config.GetDuration(secAgentKey("internal_profiling.period")),
CPUDuration: config.GetDuration(secAgentKey("internal_profiling.cpu_duration")),
MutexProfileFraction: config.GetInt(secAgentKey("internal_profiling.mutex_profile_fraction")),
BlockProfileRate: config.GetInt(secAgentKey("internal_profiling.block_profile_rate")),
WithGoroutineProfile: config.GetBool(secAgentKey("internal_profiling.enable_goroutine_stacktraces")),
WithBlockProfile: config.GetBool(secAgentKey("internal_profiling.enable_block_profiling")),
WithMutexProfile: config.GetBool(secAgentKey("internal_profiling.enable_mutex_profiling")),
WithDeltaProfiles: config.GetBool(secAgentKey("internal_profiling.delta_profiles")),
Socket: config.GetString(secAgentKey("internal_profiling.unix_socket")),
Tags: tags,
}
return profiling.Start(profSettings)
}
return nil
}
func secAgentKey(sub string) string {
return fmt.Sprintf("security_agent.%s", sub)
}