Skip to content

Commit

Permalink
[SEC-11866] cws-instrumentation: add selftests command (#21445)
Browse files Browse the repository at this point in the history
[SEC-11866] cws-instrumentation: add selftests command
  • Loading branch information
YoannGh authored Dec 18, 2023
1 parent bba6924 commit 5e200ff
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 5e200ff

Please sign in to comment.