-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathapp.go
More file actions
156 lines (128 loc) · 3.52 KB
/
app.go
File metadata and controls
156 lines (128 loc) · 3.52 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"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
platform, err := GetOperatingSystem()
if err != nil {
glog.Errorf("Failed to get operating system platform: %v", err)
}
switch {
case linuxCisVersion != "":
version = linuxCisVersion
case platform == "bottlerocket":
version = "bottlerocket"
case strings.HasPrefix(platform, "amzn2023"):
version = "Amazon_Linux_2023"
case platform == "amzn2":
version = "amazon_linux_2"
case platform == "cos":
version = "Google_Container_Optimized_OS"
case platform == "rhel9":
version = "Red_hat_enterprise_linux_9"
case strings.HasPrefix(platform, "azurelinux3"):
version = "Azure_linux_3"
case platform == "ubuntu24":
version = "Ubuntu_24.04"
default:
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 := os.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
}