Skip to content

Commit 8ecc141

Browse files
authored
[CWS] Allow dumping process cache in raw JSON (#30522)
1 parent 5172b01 commit 8ecc141

File tree

10 files changed

+608
-506
lines changed

10 files changed

+608
-506
lines changed

cmd/security-agent/subcommands/runtime/command.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ type processCacheDumpCliParams struct {
223223
*command.GlobalParams
224224

225225
withArgs bool
226+
format string
226227
}
227228

228229
//nolint:unused // TODO(SEC) Fix unused linter
@@ -246,6 +247,7 @@ func processCacheCommands(globalParams *command.GlobalParams) []*cobra.Command {
246247
},
247248
}
248249
processCacheDumpCmd.Flags().BoolVar(&cliParams.withArgs, "with-args", false, "add process arguments to the dump")
250+
processCacheDumpCmd.Flags().StringVar(&cliParams.format, "format", "dot", "process cache dump format")
249251

250252
processCacheCmd := &cobra.Command{
251253
Use: "process-cache",
@@ -328,7 +330,7 @@ func dumpProcessCache(_ log.Component, _ config.Component, _ secrets.Component,
328330
}
329331
defer client.Close()
330332

331-
filename, err := client.DumpProcessCache(processCacheDumpArgs.withArgs)
333+
filename, err := client.DumpProcessCache(processCacheDumpArgs.withArgs, processCacheDumpArgs.format)
332334
if err != nil {
333335
return fmt.Errorf("unable to get a process cache dump: %w", err)
334336
}

cmd/system-probe/subcommands/runtime/command.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ type processCacheDumpCliParams struct {
217217
*command.GlobalParams
218218

219219
withArgs bool
220+
format string
220221
}
221222

222223
//nolint:unused // TODO(SEC) Fix unused linter
@@ -240,6 +241,7 @@ func processCacheCommands(globalParams *command.GlobalParams) []*cobra.Command {
240241
},
241242
}
242243
processCacheDumpCmd.Flags().BoolVar(&cliParams.withArgs, "with-args", false, "add process arguments to the dump")
244+
processCacheDumpCmd.Flags().StringVar(&cliParams.format, "format", "dot", "process cache dump format")
243245

244246
processCacheCmd := &cobra.Command{
245247
Use: "process-cache",
@@ -322,7 +324,7 @@ func dumpProcessCache(_ log.Component, _ config.Component, _ secrets.Component,
322324
}
323325
defer client.Close()
324326

325-
filename, err := client.DumpProcessCache(processCacheDumpArgs.withArgs)
327+
filename, err := client.DumpProcessCache(processCacheDumpArgs.withArgs, processCacheDumpArgs.format)
326328
if err != nil {
327329
return fmt.Errorf("unable to get a process cache dump: %w", err)
328330
}

pkg/security/agent/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type RuntimeSecurityClient struct {
3232
// SecurityModuleClientWrapper represents a security module client
3333
type SecurityModuleClientWrapper interface {
3434
DumpDiscarders() (string, error)
35-
DumpProcessCache(withArgs bool) (string, error)
35+
DumpProcessCache(withArgs bool, format string) (string, error)
3636
GenerateActivityDump(request *api.ActivityDumpParams) (*api.ActivityDumpMessage, error)
3737
ListActivityDumps() (*api.ActivityDumpListMessage, error)
3838
StopActivityDump(name, containerid string) (*api.ActivityDumpStopMessage, error)
@@ -61,8 +61,8 @@ func (c *RuntimeSecurityClient) DumpDiscarders() (string, error) {
6161
}
6262

6363
// DumpProcessCache sends a process cache dump request
64-
func (c *RuntimeSecurityClient) DumpProcessCache(withArgs bool) (string, error) {
65-
response, err := c.apiClient.DumpProcessCache(context.Background(), &api.DumpProcessCacheParams{WithArgs: withArgs})
64+
func (c *RuntimeSecurityClient) DumpProcessCache(withArgs bool, format string) (string, error) {
65+
response, err := c.apiClient.DumpProcessCache(context.Background(), &api.DumpProcessCacheParams{WithArgs: withArgs, Format: format})
6666
if err != nil {
6767
return "", err
6868
}

pkg/security/agent/mocks/security_module_client_wrapper.go

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/security/module/server_linux.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"context"
1212
"errors"
1313
"fmt"
14+
"os"
1415

1516
"github.com/DataDog/datadog-agent/pkg/security/probe"
1617
"github.com/DataDog/datadog-agent/pkg/security/proto/api"
@@ -36,9 +37,39 @@ func (a *APIServer) DumpProcessCache(_ context.Context, params *api.DumpProcessC
3637
return nil, fmt.Errorf("not supported")
3738
}
3839

39-
filename, err := p.Resolvers.ProcessResolver.ToDot(params.WithArgs)
40-
if err != nil {
41-
return nil, err
40+
var (
41+
filename string
42+
err error
43+
)
44+
45+
switch params.Format {
46+
case "json":
47+
jsonContent, err := p.Resolvers.ProcessResolver.ToJSON(true)
48+
if err != nil {
49+
return nil, err
50+
}
51+
52+
dump, err := os.CreateTemp("/tmp", "process-cache-dump-*.json")
53+
if err != nil {
54+
return nil, err
55+
}
56+
57+
defer dump.Close()
58+
59+
filename = dump.Name()
60+
if err := os.Chmod(dump.Name(), 0400); err != nil {
61+
return nil, err
62+
}
63+
64+
if _, err := dump.Write(jsonContent); err != nil {
65+
return nil, err
66+
}
67+
68+
case "dot", "":
69+
filename, err = p.Resolvers.ProcessResolver.ToDot(params.WithArgs)
70+
if err != nil {
71+
return nil, err
72+
}
4273
}
4374

4475
return &api.SecurityDumpProcessCacheMessage{

pkg/security/probe/coredump.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (cd *CoreDump) ToJSON() ([]byte, error) {
5050
}
5151

5252
if cd.definition.Process {
53-
data, _ := cd.resolvers.ProcessResolver.ToJSON()
53+
data, _ := cd.resolvers.ProcessResolver.ToJSON(false)
5454
content.Process = data
5555
}
5656

0 commit comments

Comments
 (0)