-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
server_linux.go
233 lines (194 loc) · 6.7 KB
/
server_linux.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
// 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.
//go:build linux
package module
import (
"context"
"errors"
"fmt"
"github.com/DataDog/datadog-agent/pkg/security/probe"
"github.com/DataDog/datadog-agent/pkg/security/proto/api"
"github.com/DataDog/datadog-agent/pkg/security/seclog"
"github.com/DataDog/datadog-agent/pkg/util/kernel"
)
// DumpDiscarders handles discarder dump requests
func (a *APIServer) DumpDiscarders(_ context.Context, _ *api.DumpDiscardersParams) (*api.DumpDiscardersMessage, error) {
filePath, err := a.probe.DumpDiscarders()
if err != nil {
return nil, err
}
seclog.Infof("Discarder dump file path: %s", filePath)
return &api.DumpDiscardersMessage{DumpFilename: filePath}, nil
}
// DumpProcessCache handles process cache dump requests
func (a *APIServer) DumpProcessCache(_ context.Context, params *api.DumpProcessCacheParams) (*api.SecurityDumpProcessCacheMessage, error) {
filename, err := a.probe.DumpProcessCache(params.WithArgs)
if err != nil {
return nil, err
}
return &api.SecurityDumpProcessCacheMessage{
Filename: filename,
}, nil
}
// DumpActivity handle an activity dump request
func (a *APIServer) DumpActivity(_ context.Context, params *api.ActivityDumpParams) (*api.ActivityDumpMessage, error) {
p, ok := a.probe.PlatformProbe.(*probe.EBPFProbe)
if !ok {
return nil, fmt.Errorf("not supported")
}
if managers := p.GetProfileManagers(); managers != nil {
msg, err := managers.DumpActivity(params)
if err != nil {
seclog.Errorf(err.Error())
}
return msg, nil
}
return nil, fmt.Errorf("monitor not configured")
}
// ListActivityDumps returns the list of active dumps
func (a *APIServer) ListActivityDumps(_ context.Context, params *api.ActivityDumpListParams) (*api.ActivityDumpListMessage, error) {
p, ok := a.probe.PlatformProbe.(*probe.EBPFProbe)
if !ok {
return nil, fmt.Errorf("not supported")
}
if managers := p.GetProfileManagers(); managers != nil {
msg, err := managers.ListActivityDumps(params)
if err != nil {
seclog.Errorf(err.Error())
}
return msg, nil
}
return nil, fmt.Errorf("monitor not configured")
}
// StopActivityDump stops an active activity dump if it exists
func (a *APIServer) StopActivityDump(_ context.Context, params *api.ActivityDumpStopParams) (*api.ActivityDumpStopMessage, error) {
p, ok := a.probe.PlatformProbe.(*probe.EBPFProbe)
if !ok {
return nil, fmt.Errorf("not supported")
}
if managers := p.GetProfileManagers(); managers != nil {
msg, err := managers.StopActivityDump(params)
if err != nil {
seclog.Errorf(err.Error())
}
return msg, nil
}
return nil, fmt.Errorf("monitor not configured")
}
// TranscodingRequest encodes an activity dump following the requested parameters
func (a *APIServer) TranscodingRequest(_ context.Context, params *api.TranscodingRequestParams) (*api.TranscodingRequestMessage, error) {
p, ok := a.probe.PlatformProbe.(*probe.EBPFProbe)
if !ok {
return nil, fmt.Errorf("not supported")
}
if managers := p.GetProfileManagers(); managers != nil {
msg, err := managers.GenerateTranscoding(params)
if err != nil {
seclog.Errorf(err.Error())
}
return msg, nil
}
return nil, fmt.Errorf("monitor not configured")
}
// ListSecurityProfiles returns the list of security profiles
func (a *APIServer) ListSecurityProfiles(_ context.Context, params *api.SecurityProfileListParams) (*api.SecurityProfileListMessage, error) {
p, ok := a.probe.PlatformProbe.(*probe.EBPFProbe)
if !ok {
return nil, fmt.Errorf("not supported")
}
if managers := p.GetProfileManagers(); managers != nil {
msg, err := managers.ListSecurityProfiles(params)
if err != nil {
seclog.Errorf(err.Error())
}
return msg, nil
}
return nil, fmt.Errorf("monitor not configured")
}
// SaveSecurityProfile saves the requested security profile to disk
func (a *APIServer) SaveSecurityProfile(_ context.Context, params *api.SecurityProfileSaveParams) (*api.SecurityProfileSaveMessage, error) {
p, ok := a.probe.PlatformProbe.(*probe.EBPFProbe)
if !ok {
return nil, fmt.Errorf("not supported")
}
if managers := p.GetProfileManagers(); managers != nil {
msg, err := managers.SaveSecurityProfile(params)
if err != nil {
seclog.Errorf(err.Error())
}
return msg, nil
}
return nil, fmt.Errorf("monitor not configured")
}
// GetStatus returns the status of the module
func (a *APIServer) GetStatus(_ context.Context, _ *api.GetStatusParams) (*api.Status, error) {
var apiStatus api.Status
if a.selfTester != nil {
apiStatus.SelfTests = a.selfTester.GetStatus()
}
apiStatus.PoliciesStatus = a.policiesStatus
p, ok := a.probe.PlatformProbe.(*probe.EBPFProbe)
if ok {
status, err := p.GetConstantFetcherStatus()
if err != nil {
return nil, err
}
constants := make([]*api.ConstantValueAndSource, 0, len(status.Values))
for _, v := range status.Values {
constants = append(constants, &api.ConstantValueAndSource{
ID: v.ID,
Value: v.Value,
Source: v.FetcherName,
})
}
apiStatus.Environment = &api.EnvironmentStatus{
Constants: &api.ConstantFetcherStatus{
Fetchers: status.Fetchers,
Values: constants,
},
KernelLockdown: string(kernel.GetLockdownMode()),
UseMmapableMaps: p.GetKernelVersion().HaveMmapableMaps(),
UseRingBuffer: p.UseRingBuffers(),
}
envErrors := p.VerifyEnvironment()
if envErrors != nil {
apiStatus.Environment.Warnings = make([]string, len(envErrors.Errors))
for i, err := range envErrors.Errors {
apiStatus.Environment.Warnings[i] = err.Error()
}
}
}
return &apiStatus, nil
}
// DumpNetworkNamespace handles network namespace cache dump requests
func (a *APIServer) DumpNetworkNamespace(_ context.Context, params *api.DumpNetworkNamespaceParams) (*api.DumpNetworkNamespaceMessage, error) {
p, ok := a.probe.PlatformProbe.(*probe.EBPFProbe)
if !ok {
return nil, fmt.Errorf("not supported")
}
return p.Resolvers.NamespaceResolver.DumpNetworkNamespaces(params), nil
}
// RunSelfTest runs self test and then reload the current policies
func (a *APIServer) RunSelfTest(_ context.Context, _ *api.RunSelfTestParams) (*api.SecuritySelfTestResultMessage, error) {
if a.cwsConsumer == nil {
return nil, errors.New("failed to found module in APIServer")
}
if a.selfTester == nil {
return &api.SecuritySelfTestResultMessage{
Ok: false,
Error: "self-tests are disabled",
}, nil
}
if _, err := a.cwsConsumer.RunSelfTest(true); err != nil {
return &api.SecuritySelfTestResultMessage{
Ok: false,
Error: err.Error(),
}, nil
}
return &api.SecuritySelfTestResultMessage{
Ok: true,
Error: "",
}, nil
}