forked from aquasecurity/linux-bench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
138 lines (109 loc) · 2.94 KB
/
app.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/aquasecurity/bench-common/check"
"github.com/aquasecurity/bench-common/util"
"github.com/golang/glog"
"github.com/spf13/cobra"
)
func app(cmd *cobra.Command, args []string) {
var version string
var err error
if linuxCisVersion != "" {
version = linuxCisVersion
} else {
version = "2.0.0"
}
path, err := getDefinitionFilePath(version)
if err != nil {
util.ExitWithError(err)
}
constraints, err := getConstraints()
if err != nil {
util.ExitWithError(err)
}
controls, err := getControls(path, constraints)
if err != nil {
util.ExitWithError(err)
}
summary := runControls(controls, checkList)
err = outputResults(controls, summary)
if err != nil {
util.ExitWithError(err)
}
}
func outputResults(controls *check.Controls, summary check.Summary) error {
// if we successfully ran some tests and it's json format, ignore the warnings
if (summary.Fail > 0 || summary.Warn > 0 || summary.Pass > 0) && jsonFmt {
out, err := controls.JSON()
if err != nil {
return err
}
util.PrintOutput(string(out), outputFile)
} else {
util.PrettyPrint(controls, summary, noRemediations, includeTestOutput)
}
return nil
}
func runControls(controls *check.Controls, checkList string) check.Summary {
var summary check.Summary
if checkList != "" {
ids := util.CleanIDs(checkList)
summary = controls.RunChecks(ids...)
} else {
summary = controls.RunGroup()
}
return summary
}
func getControls(path string, constraints []string) (*check.Controls, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
controls, err := check.NewControls([]byte(data), constraints)
if err != nil {
return nil, err
}
return controls, err
}
func getDefinitionFilePath(version string) (string, error) {
filename := "definitions.yaml"
glog.V(2).Info(fmt.Sprintf("Looking for config for version %s", version))
path := filepath.Join(cfgDir, version)
file := filepath.Join(path, filename)
glog.V(2).Info(fmt.Sprintf("Looking for config file: %s\n", file))
_, err := os.Stat(file)
if err != nil {
return "", err
}
return file, nil
}
func getConstraints() (constraints []string, err error) {
platform, err := GetOperatingSystem()
if err != nil {
glog.V(1).Info(fmt.Sprintf("Failed to get operating system platform, %s", err))
}
boot, err := GetBootLoader()
if err != nil {
glog.V(1).Info(fmt.Sprintf("Failed to get boot loader, %s", err))
}
syslog, err := GetSystemLogManager()
if err != nil {
glog.V(1).Info(fmt.Sprintf("Failed to get syslog tool, %s", err))
}
lsm, err := GetLSM()
if err != nil {
glog.V(1).Info(fmt.Sprintf("Failed to get lsm, %s", err))
}
constraints = append(constraints,
fmt.Sprintf("platform=%s", platform),
fmt.Sprintf("boot=%s", boot),
fmt.Sprintf("syslog=%s", syslog),
fmt.Sprintf("lsm=%s", lsm),
)
glog.V(1).Info(fmt.Sprintf("The constraints are:, %s", constraints))
return constraints, nil
}