Skip to content

Commit

Permalink
feat: add environment variables support for command execution
Browse files Browse the repository at this point in the history
Signed-off-by: Alessio Greggi <[email protected]>
  • Loading branch information
alegrey91 committed Jan 10, 2025
1 parent 6cd8bb5 commit e8291db
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
4 changes: 3 additions & 1 deletion cmd/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
)

var functionSymbols string
var envVars []string
var commandOutput bool
var commandError bool
var libbpfOutput bool
Expand Down Expand Up @@ -66,7 +67,7 @@ by passing the function name symbol and the binary args.
errorCh := make(chan error)
ctx := context.Background()

ebpf, err := captor.InitProbes(functionSymbol, args, opts)
ebpf, err := captor.InitProbes(functionSymbol, args, envVars, opts)
if err != nil {
return fmt.Errorf("error setting up ebpf module: %w", err)
}
Expand Down Expand Up @@ -100,6 +101,7 @@ func init() {

captureCmd.Flags().StringVarP(&functionSymbols, "functions", "f", "", "Name of the function symbols to be traced")
captureCmd.MarkFlagRequired("functions")
captureCmd.Flags().StringSliceVarP(&envVars, "env-vars", "E", []string{}, "Additional environment variables to pass to the executed command")

captureCmd.Flags().BoolVarP(&commandOutput, "include-cmd-stdout", "c", false, "Include the executed command output")
captureCmd.Flags().BoolVarP(&commandError, "include-cmd-stderr", "e", false, "Include the executed command error")
Expand Down
2 changes: 1 addition & 1 deletion cmd/hunt.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ var huntCmd = &cobra.Command{
errorCh := make(chan error)
ctx := context.Background()

ebpf, err := captor.InitProbes(functionSymbol, captureArgs, opts)
ebpf, err := captor.InitProbes(functionSymbol, captureArgs, envVars, opts)
if err != nil {
return fmt.Errorf("error setting up ebpf module: %w", err)
}
Expand Down
5 changes: 4 additions & 1 deletion internal/ebpf/probesfacade/captor/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ type ebpfSetup struct {
lostCh chan uint64
opts CaptureOptions
cmd []string
env []string
}

// InitProbes setup the ebpf module attaching probes and tracepoints
// to the ebpf program.
// Returns the ebpfSetup struct in case of seccess, an error in case of failure.
func InitProbes(functionSymbol string, cmdArgs []string, opts CaptureOptions) (*ebpfSetup, error) {
func InitProbes(functionSymbol string, cmdArgs []string, env []string, opts CaptureOptions) (*ebpfSetup, error) {
if len(cmdArgs) == 0 {
return nil, errors.New("error no arguments provided, at least 1 argument is required")
}
Expand Down Expand Up @@ -138,6 +139,7 @@ func InitProbes(functionSymbol string, cmdArgs []string, opts CaptureOptions) (*
lostCh: lostChannel,
opts: opts,
cmd: cmdArgs,
env: env,
}, nil
}

Expand Down Expand Up @@ -173,6 +175,7 @@ func (ebpf *ebpfSetup) Capture(ctx context.Context, resultCh chan []uint32, erro
// running command to trace its syscalls
go executor.Run(
ebpf.cmd,
ebpf.env,
ebpf.opts.CommandOutput,
ebpf.opts.CommandError,
&wg,
Expand Down
8 changes: 7 additions & 1 deletion internal/executor/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ import (

// Run execute the command and wait for its end.
// The cmdOutput argument is used to print the command output.
func Run(cmd []string, cmdOutput, cmdError bool, wg *sync.WaitGroup, outputCh, errorCh chan<- string) {
func Run(cmd []string, envVars []string, cmdOutput, cmdError bool, wg *sync.WaitGroup, outputCh, errorCh chan<- string) {
defer func() {
wg.Done()
}()

command := exec.Command(cmd[0], cmd[1:]...)
// assign custom env variables to the command
env := os.Environ()
if len(envVars) > 0 {
env = append(env, envVars...)
}
command.Env = env
stdout, _ := command.StdoutPipe()
stderr, _ := command.StderrPipe()

Expand Down

0 comments on commit e8291db

Please sign in to comment.