-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathcommand_check.go
75 lines (59 loc) · 2.05 KB
/
command_check.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
// 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.
package checks
import (
"context"
"fmt"
"time"
"github.com/DataDog/datadog-agent/pkg/compliance"
"github.com/DataDog/datadog-agent/pkg/compliance/checks/env"
"github.com/DataDog/datadog-agent/pkg/compliance/eval"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
const (
defaultTimeout = 30 * time.Second
)
var commandReportedFields = []string{
compliance.CommandFieldExitCode,
}
func resolveCommand(ctx context.Context, _ env.Env, ruleID string, res compliance.ResourceCommon, rego bool) (resolved, error) {
if res.Command == nil {
return nil, fmt.Errorf("%s: expecting command resource in command check", ruleID)
}
command := res.Command
log.Debugf("%s: running command check: %v", ruleID, command)
if command.BinaryCmd == nil && command.ShellCmd == nil {
return nil, fmt.Errorf("unable to execute commandCheck - need a binary or a shell command")
}
var execCommand = command.BinaryCmd
// Create `execCommand` from `command` model
// Binary takes precedence over Shell
if execCommand == nil {
execCommand = shellCmdToBinaryCmd(command.ShellCmd)
}
commandTimeout := defaultTimeout
if command.TimeoutSeconds != 0 {
commandTimeout = time.Duration(command.TimeoutSeconds) * time.Second
}
context, cancel := context.WithTimeout(ctx, commandTimeout)
defer cancel()
exitCode, stdout, err := commandRunner(context, execCommand.Name, execCommand.Args, true)
if exitCode == -1 && err != nil {
return nil, fmt.Errorf("command '%v' execution failed, error: %v", command, err)
}
stdoutStr := string(stdout)
instance := eval.NewInstance(
eval.VarMap{
compliance.CommandFieldExitCode: exitCode,
compliance.CommandFieldStdout: stdoutStr,
},
nil,
eval.RegoInputMap{
"exitCode": exitCode,
"stdout": stdoutStr,
},
)
return newResolvedInstance(instance, execCommand.Name, "command"), nil
}