/
command-helper.go
268 lines (246 loc) · 7.82 KB
/
command-helper.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package commands
import (
"fmt"
"github.com/briandowns/spinner"
"github.com/chen-keinan/mesh-kridik/internal/common"
"github.com/chen-keinan/mesh-kridik/internal/logger"
"github.com/chen-keinan/mesh-kridik/internal/models"
"github.com/chen-keinan/mesh-kridik/pkg/filters"
"github.com/chen-keinan/mesh-kridik/pkg/utils"
"github.com/chen-keinan/mesh-kridik/ui"
"github.com/mitchellh/colorstring"
"github.com/olekukonko/tablewriter"
"gopkg.in/yaml.v2"
"strings"
"time"
)
func printTestResults(at []*models.SecurityCheck, table *tablewriter.Table, category string) models.CheckTotals {
var (
warnCounter int
passCounter int
failCounter int
)
for _, a := range at {
var testType string
if a.NonApplicable {
testType = "Manual"
} else {
testType = "Automated"
}
if a.NonApplicable {
warnTest := colorstring.Color("[yellow][Warn]")
warnCounter++
table.Append([]string{category, warnTest, testType, a.Name})
continue
}
if a.TestSucceed {
passTest := colorstring.Color("[green][Pass]")
table.Append([]string{category, passTest, testType, a.Name})
passCounter++
} else {
failTest := colorstring.Color("[red][Fail]")
table.Append([]string{category, failTest, testType, a.Name})
failCounter++
}
}
return models.CheckTotals{Fail: failCounter, Pass: passCounter, Warn: warnCounter}
}
func printClassicTestResults(at []*models.SecurityCheck, log *logger.MeshKridikLogger) models.CheckTotals {
var (
warnCounter int
passCounter int
failCounter int
)
// just empty line
fmt.Println()
for _, a := range at {
if a.NonApplicable {
warnTest := colorstring.Color("[yellow][Warn]")
log.Console(fmt.Sprintf("%s %s\n", warnTest, a.Name))
log.Console(fmt.Sprintf(" %s %d. %s\n", warnTest, 1, a.EvalMessage))
warnCounter++
continue
}
if a.TestSucceed {
passTest := colorstring.Color("[green][Pass]")
log.Console(fmt.Sprintf("%s %s\n", passTest, a.Name))
passCounter++
} else {
failTest := colorstring.Color("[red][Fail]")
log.Console(fmt.Sprintf("%s %s\n", failTest, a.Name))
failCounter++
}
for index, pr := range a.PolicyResult.PolicyResult {
sentence := a.EvalMessage
var testStatus string
if pr.ReturnValues[strings.TrimSpace(a.PolicyResult.ReturnKeys[0])] == "true" {
testStatus = colorstring.Color("[green][Pass]")
} else {
testStatus = colorstring.Color("[red][Fail]")
}
for key, val := range pr.ReturnValues {
sentence = strings.Replace(sentence, fmt.Sprintf("$%s", key), val, -1)
}
log.Console(fmt.Sprintf(" %s %d. %s\n", testStatus, index+1, sentence))
}
}
return models.CheckTotals{Fail: failCounter, Pass: passCounter, Warn: warnCounter}
}
func calculateTotals(at []*models.SecurityCheck) models.CheckTotals {
var (
warnCounter int
passCounter int
failCounter int
)
for _, a := range at {
if a.NonApplicable {
warnCounter++
continue
}
if a.TestSucceed {
passCounter++
} else {
failCounter++
}
}
return models.CheckTotals{Fail: failCounter, Pass: passCounter, Warn: warnCounter}
}
//AddFailedMessages add failed audit test to report data
func AddFailedMessages(at *models.SecurityCheck, isSucceeded bool) []*models.SecurityCheck {
av := make([]*models.SecurityCheck, 0)
at.TestSucceed = isSucceeded
if !isSucceeded || at.NonApplicable {
av = append(av, at)
}
return av
}
//AddAllMessages add all audit test to report data
func AddAllMessages(at *models.SecurityCheck, isSucceeded bool) []*models.SecurityCheck {
av := make([]*models.SecurityCheck, 0)
at.TestSucceed = isSucceeded
av = append(av, at)
return av
}
//TestLoader load tests from filesystem
//command-helper.go
//go:generate mockgen -destination=../../mocks/mock_TestLoader.go -package=mocks . TestLoader
type TestLoader interface {
LoadSecurityChecks(fi []utils.FilesInfo) []*models.SubCategory
}
//AuditTestLoader object
type AuditTestLoader struct {
}
//NewFileLoader create new file loader
func NewFileLoader() TestLoader {
return &AuditTestLoader{}
}
//LoadSecurityChecks load audit test from benchmark folder
func (tl AuditTestLoader) LoadSecurityChecks(auditFiles []utils.FilesInfo) []*models.SubCategory {
auditTests := make([]*models.SubCategory, 0)
audit := models.Check{}
for _, auditFile := range auditFiles {
if !strings.HasSuffix(auditFile.Name, common.PolicySuffix) {
err := yaml.Unmarshal([]byte(auditFile.Data), &audit)
if err != nil {
panic("Failed to unmarshal audit test yaml file")
}
auditTests = append(auditTests, audit.Categories[0].SubCategory)
}
}
return auditTests
}
//FilterAuditTests filter audit tests by predicate chain
func FilterAuditTests(predicates []filters.Predicate, predicateParams []string, at *models.SubCategory) *models.SubCategory {
return RunPredicateChain(predicates, predicateParams, len(predicates), at)
}
//RunPredicateChain call every predicate in chain and filter tests
func RunPredicateChain(predicates []filters.Predicate, predicateParams []string, size int, at *models.SubCategory) *models.SubCategory {
if size == 0 {
return at
}
return RunPredicateChain(predicates[1:size], predicateParams[1:size], size-1, predicates[size-1](at, predicateParams[size-1]))
}
// check weather are exist in array of specificTests
func isArgsExist(args []string, name string) bool {
for _, n := range args {
if n == name {
return true
}
}
return false
}
//GetResultProcessingFunction return processing function by specificTests
func GetResultProcessingFunction(args []string) ResultProcessor {
if isArgsExist(args, common.Report) || isArgsExist(args, common.ReportFull) {
return reportResultProcessor
}
return simpleResultProcessor
}
//getOutPutGeneratorFunction return output generator function
func getOutputGeneratorFunction(args []string) ui.OutputGenerator {
switch {
case isArgsExist(args, common.Report) || isArgsExist(args, common.ReportFull):
return ReportOutputGenerator
case isArgsExist(args, common.Classic) || isArgsExist(args, common.ClassicFull):
return ConsoleOutputGenerator
default:
return ClassicOutputGenerator
}
}
//buildPredicateChain build chain of filters based on command criteria
func buildPredicateChain(args []string) []filters.Predicate {
pc := make([]filters.Predicate, 0)
for _, n := range args {
switch {
case strings.HasPrefix(n, common.IncludeParam):
pc = append(pc, filters.IncludeAudit)
case strings.HasPrefix(n, common.ExcludeParam):
pc = append(pc, filters.ExcludeAudit)
case strings.HasPrefix(n, common.NodeParam):
pc = append(pc, filters.NodeAudit)
case n == "a":
pc = append(pc, filters.Basic)
}
}
return pc
}
//buildPredicateParams build chain of filters params based on command criteria
func buildPredicateChainParams(args []string) []string {
pp := make([]string, 0)
pp = append(pp, args...)
return pp
}
func filteredAuditBenchTests(auditTests []*models.SubCategory, pc []filters.Predicate, pp []string) []*models.SubCategory {
ft := make([]*models.SubCategory, 0)
for _, adt := range auditTests {
filteredAudit := FilterAuditTests(pc, pp, adt)
if len(filteredAudit.Checks) == 0 {
continue
}
ft = append(ft, filteredAudit)
}
return ft
}
func loadPolicies(fi []utils.FilesInfo) map[string]string {
policyMap := make(map[string]string)
for _, policy := range fi {
if strings.HasSuffix(policy.Name, ".policy") {
policyMap[policy.Name] = policy.Data
}
}
return policyMap
}
func executeTests(ft []*models.SubCategory, mc *MeshCheck, policies map[string]string) []*models.SubCategory {
completedTest := make([]*models.SubCategory, 0)
mc.log.Console(ui.MeshCheck)
s := spinner.New(spinner.CharSets[9], 50*time.Millisecond) // Build our new spinner
for _, f := range ft {
s.Prefix = fmt.Sprintf("[Category] %s ", f.Name)
s.Start()
tr := ui.ExecuteSpecs(f, mc.runAuditTest, policies)
printClassicTestResults(tr.Checks, mc.log)
completedTest = append(completedTest, tr)
s.Stop()
}
return completedTest
}