Skip to content

Commit

Permalink
[mq] working branch - merge 6b7fd10 on top of main at 57c758b
Browse files Browse the repository at this point in the history
{"baseBranch":"main","baseCommit":"57c758bf428462d2d74149ed0f9311f812de9d36","createdAt":"2023-12-18T16:08:49.869157Z","headSha":"6b7fd100809a245d1bcdcf4c4a98f619a1ae7aeb","id":"ae9c3566-5670-4b46-881a-00980a9c4dae","priority":"200","pullRequestNumber":"21445","queuedAt":"2023-12-18T16:08:49.867012Z","status":"STATUS_QUEUED"}
  • Loading branch information
dd-mergequeue[bot] authored Dec 18, 2023
2 parents 85a30f7 + 6b7fd10 commit 92c6872
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
81 changes: 81 additions & 0 deletions cmd/cws-instrumentation/subcommands/selftestscmd/selftests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// 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 selftestscmd holds the selftests command of CWS injector
package selftestscmd

import (
"errors"
"os"
"os/exec"
"strings"

"github.com/spf13/cobra"
)

type execParams struct {
enabled bool
path string
args string
}

type openParams struct {
enabled bool
path string
}

type selftestsCliParams struct {
exec execParams
open openParams
}

// Command returns the commands for the selftests subcommand
func Command() []*cobra.Command {
var params selftestsCliParams

selftestsCmd := &cobra.Command{
Use: "selftests",
Short: "run selftests against the tracer",
RunE: func(cmd *cobra.Command, args []string) error {
var err error
if params.exec.enabled {
err = errors.Join(err, selftestExec(&params.exec))
}
if params.open.enabled {
err = errors.Join(err, selftestOpen(&params.open))
}
return err
},
}

selftestsCmd.Flags().BoolVar(&params.exec.enabled, "exec", false, "run the exec selftest")
selftestsCmd.Flags().StringVar(&params.exec.path, "exec.path", "/usr/bin/date", "path to the file to execute")
selftestsCmd.Flags().StringVar(&params.exec.args, "exec.args", "", "arguments to pass to the executable")
selftestsCmd.Flags().BoolVar(&params.open.enabled, "open", false, "run the open selftest")
selftestsCmd.Flags().StringVar(&params.open.path, "open.path", "/tmp/open.test", "path to the file to open")

return []*cobra.Command{selftestsCmd}
}

func selftestExec(params *execParams) error {
if params.args != "" {
return exec.Command(params.path, strings.Split(params.args, " ")...).Run()
}
return exec.Command(params.path).Run()
}

func selftestOpen(params *openParams) error {
f, createErr := os.OpenFile(params.path, os.O_CREATE|os.O_EXCL, 0400)
if createErr != nil {
f, openErr := os.Open(params.path)
if openErr != nil {
return errors.Join(createErr, openErr)
}
return f.Close()
}
return errors.Join(f.Close(), os.Remove(params.path))
}
3 changes: 3 additions & 0 deletions cmd/cws-instrumentation/subcommands/tracecmd/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package tracecmd
import (
"github.com/spf13/cobra"

"github.com/DataDog/datadog-agent/cmd/cws-instrumentation/subcommands/selftestscmd"
"github.com/DataDog/datadog-agent/pkg/security/ptracer"
)

Expand Down Expand Up @@ -58,5 +59,7 @@ func Command() []*cobra.Command {
traceCmd.Flags().Int32Var(&params.UID, uid, -1, "uid used to start the tracee")
traceCmd.Flags().Int32Var(&params.GID, gid, -1, "gid used to start the tracee")

traceCmd.AddCommand(selftestscmd.Command()...)

return []*cobra.Command{traceCmd}
}

0 comments on commit 92c6872

Please sign in to comment.