Skip to content

Commit 270dada

Browse files
authored
[CWS] Use secinfo track in security-agent (#44345)
### What does this PR do? This PR is a follow up to [this PR](#44107). In the same way, it adds the use of secinfo for remediation status in the security-agent sender ### Motivation ### Describe how you validated your changes ### Additional Notes Co-authored-by: momar.toure <momar.toure@datadoghq.com>
1 parent 651c11c commit 270dada

File tree

5 files changed

+44
-25
lines changed

5 files changed

+44
-25
lines changed

pkg/security/agent/agent.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type RuntimeSecurityAgent struct {
3939
statsdClient statsd.ClientInterface
4040
hostname string
4141
reporter common.RawReporter
42+
secInfoReporter common.RawReporter
4243
eventClient *RuntimeSecurityEventClient
4344
cmdClient *RuntimeSecurityCmdClient
4445
running *atomic.Bool
@@ -47,6 +48,7 @@ type RuntimeSecurityAgent struct {
4748
eventReceived *atomic.Uint64
4849
activityDumpReceived *atomic.Uint64
4950
endpoints *config.Endpoints
51+
secInfoEndpoints *config.Endpoints
5052
cancel context.CancelFunc
5153

5254
// activity dump
@@ -119,9 +121,11 @@ func (rsa *RuntimeSecurityAgent) SendActivityDumpStream(stream grpc.ClientStream
119121
}
120122

121123
// Start the runtime security agent
122-
func (rsa *RuntimeSecurityAgent) Start(reporter common.RawReporter, endpoints *config.Endpoints) {
124+
func (rsa *RuntimeSecurityAgent) Start(reporter common.RawReporter, endpoints *config.Endpoints, secInfoReporter common.RawReporter, secInfoEndpoints *config.Endpoints) {
123125
rsa.reporter = reporter
124126
rsa.endpoints = endpoints
127+
rsa.secInfoReporter = secInfoReporter
128+
rsa.secInfoEndpoints = secInfoEndpoints
125129

126130
ctx, cancel := context.WithCancel(context.Background())
127131
rsa.cancel = cancel
@@ -255,10 +259,17 @@ func (rsa *RuntimeSecurityAgent) startActivityDumpStreamListener() {
255259

256260
// DispatchEvent dispatches a security event message to the subsytems of the runtime security agent
257261
func (rsa *RuntimeSecurityAgent) DispatchEvent(evt *api.SecurityEventMessage) {
258-
if rsa.reporter == nil {
259-
return
262+
if evt.Track == string(common.SecInfo) {
263+
if rsa.secInfoReporter == nil {
264+
return
265+
}
266+
rsa.secInfoReporter.ReportRaw(evt.GetData(), evt.Service, evt.Timestamp.AsTime(), evt.GetTags()...)
267+
} else {
268+
if rsa.reporter == nil {
269+
return
270+
}
271+
rsa.reporter.ReportRaw(evt.GetData(), evt.Service, evt.Timestamp.AsTime(), evt.GetTags()...)
260272
}
261-
rsa.reporter.ReportRaw(evt.GetData(), evt.Service, evt.Timestamp.AsTime(), evt.GetTags()...)
262273
}
263274

264275
// DispatchActivityDump forwards an activity dump message to the backend

pkg/security/agent/start.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,23 @@ func StartRuntimeSecurity(log log.Component, config config.Component, hostname s
4848
}
4949
stopper.Add(ctx)
5050

51-
reporter, err := reporter.NewCWSReporter(hostname, stopper, endpoints, ctx, compression)
51+
runtimeReporter, err := reporter.NewCWSReporter(hostname, stopper, endpoints, ctx, compression)
5252
if err != nil {
5353
return nil, err
5454
}
5555

56-
agent.Start(reporter, endpoints)
56+
secInfoEndpoints, secInfoCtx, err := common.NewLogContextSecInfo()
57+
if err != nil {
58+
_ = log.Error(err)
59+
}
60+
stopper.Add(secInfoCtx)
61+
62+
secInfoReporter, err := reporter.NewCWSReporter(hostname, stopper, secInfoEndpoints, secInfoCtx, compression)
63+
if err != nil {
64+
return nil, err
65+
}
66+
67+
agent.Start(runtimeReporter, endpoints, secInfoReporter, secInfoEndpoints)
5768

5869
log.Info("Datadog runtime security agent is now running")
5970

pkg/security/common/logs_context.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,18 @@ import (
1515
"github.com/DataDog/datadog-agent/pkg/util/log"
1616
)
1717

18+
// TrackType represents the type of track for event routing
19+
type TrackType = logsconfig.IntakeTrackType
20+
1821
const (
1922
cwsIntakeOrigin logsconfig.IntakeOrigin = "cloud-workload-security"
23+
24+
// SecRuntime is the track type for secruntime events
25+
SecRuntime TrackType = "secruntime"
26+
// Logs is the track type for logs events
27+
Logs TrackType = "logs"
28+
// SecInfo is the track type for secinfo events
29+
SecInfo TrackType = "secinfo"
2030
)
2131

2232
// NewLogContextCompliance returns the context fields to send compliance events to the intake
@@ -28,14 +38,12 @@ func NewLogContextCompliance() (*logsconfig.Endpoints, *client.DestinationsConte
2838
// NewLogContextRuntime returns the context fields to send runtime (CWS) events to the intake
2939
// This function will only be used on Linux. The only platforms where the runtime agent runs
3040
func NewLogContextRuntime(useSecRuntimeTrack bool) (*logsconfig.Endpoints, *client.DestinationsContext, error) {
31-
var (
32-
trackType logsconfig.IntakeTrackType
33-
)
41+
var trackType TrackType
3442

3543
if useSecRuntimeTrack {
36-
trackType = "secruntime"
44+
trackType = SecRuntime
3745
} else {
38-
trackType = "logs"
46+
trackType = Logs
3947
}
4048

4149
logsRuntimeConfigKeys := logsconfig.NewLogsConfigKeys("runtime_security_config.endpoints.", pkgconfigsetup.Datadog())
@@ -45,7 +53,7 @@ func NewLogContextRuntime(useSecRuntimeTrack bool) (*logsconfig.Endpoints, *clie
4553
// NewLogContextSecInfo returns the context fields to send remediation events to the intake
4654
func NewLogContextSecInfo() (*logsconfig.Endpoints, *client.DestinationsContext, error) {
4755
logsRuntimeConfigKeys := logsconfig.NewLogsConfigKeys("runtime_security_config.endpoints.", pkgconfigsetup.Datadog())
48-
return NewLogContext(logsRuntimeConfigKeys, "runtime-security-http-intake.logs.", "secinfo", cwsIntakeOrigin, logsconfig.DefaultIntakeProtocol)
56+
return NewLogContext(logsRuntimeConfigKeys, "runtime-security-http-intake.logs.", SecInfo, cwsIntakeOrigin, logsconfig.DefaultIntakeProtocol)
4957
}
5058

5159
// NewLogContext returns the context fields to send events to the intake

pkg/security/module/msg_sender.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,6 @@ type ChanMsgSender[T any] struct {
4747
msgs chan *T
4848
}
4949

50-
type TrackType string
51-
52-
const (
53-
// Runtime is the track type for runtime events
54-
Runtime TrackType = "runtime"
55-
// Logs is the track type for logs events
56-
Logs TrackType = "logs"
57-
// SecInfo is the track type for secinfo events
58-
SecInfo TrackType = "secinfo"
59-
)
60-
6150
// Send the message
6251
func (cs *ChanMsgSender[T]) Send(msg *T, expireFnc func(*T)) {
6352
select {
@@ -108,7 +97,7 @@ var _ EndpointsStatusFetcher = &DirectEventMsgSender{}
10897

10998
// Send the message
11099
func (ds *DirectEventMsgSender) Send(msg *api.SecurityEventMessage, _ func(*api.SecurityEventMessage)) {
111-
if msg.Track == string(SecInfo) {
100+
if msg.Track == string(common.SecInfo) {
112101
ds.secInfoReporter.ReportRaw(msg.Data, msg.Service, msg.Timestamp.AsTime(), msg.Tags...)
113102
} else {
114103
ds.reporter.ReportRaw(msg.Data, msg.Service, msg.Timestamp.AsTime(), msg.Tags...)

pkg/security/module/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func (a *APIServer) updateMsgService(msg *api.SecurityEventMessage) {
284284

285285
func (a *APIServer) updateMsgTrack(msg *api.SecurityEventMessage) {
286286
if slices.Contains(events.AllSecInfoRuleIDs(), msg.RuleID) {
287-
msg.Track = string(SecInfo)
287+
msg.Track = string(common.SecInfo)
288288
}
289289
}
290290

0 commit comments

Comments
 (0)