1
0
mirror of https://github.com/aquasecurity/kube-bench.git synced 2024-12-03 05:18:12 +00:00

Do not log lines whose check.State is in --status

This commit is contained in:
Manuel Tiago Pereira 2021-10-23 17:36:48 +01:00
parent 0feaa5d75f
commit fe50154f17
No known key found for this signature in database
GPG Key ID: 0F7AEBEEEEB5DC0C
2 changed files with 70 additions and 0 deletions

View File

@ -157,8 +157,29 @@ func parseSkipIds(skipIds string) map[string]bool {
return skipIdMap
}
func parseStatus(statusList string) map[check.State]bool {
var statusMap = make(map[check.State]bool, 0)
if statusList != "" {
for _, status := range strings.Split(statusList, ",") {
statusMap[check.State(strings.ToUpper(strings.Trim(status, " ")))] = true
}
}
return statusMap
}
func printStatus(state check.State) bool {
if statusList == "" {
return true
}
statusMap := parseStatus(statusList)
return statusMap[state]
}
// colorPrint outputs the state in a specific colour, along with a message string
func colorPrint(state check.State, s string) {
if !printStatus(state) {
return
}
colors[state].Printf("[%s] ", state)
fmt.Printf("%s", s)
}

View File

@ -750,6 +750,55 @@ func TestWriteStdoutOutputTotal(t *testing.T) {
assert.Contains(t, string(out), "49 checks PASS")
}
func TestWriteStdoutOutputStatusList(t *testing.T) {
type testCase struct {
name string
statusList string
notContains []string
}
testCases := []testCase{
{
name: "statusList PASS",
statusList: "PASS",
notContains: []string{"INFO", "WARN", "ERRO"},
},
{
name: "statusList PASS,INFO",
statusList: "PASS,INFO",
notContains: []string{"WARN", "ERRO"},
},
{
name: "statusList empty",
statusList: "",
notContains: nil,
},
}
controlsCollection, err := parseControlsJsonFile("./testdata/controlsCollection.json")
if err != nil {
t.Error(err)
}
for _, tt := range testCases {
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
statusList = tt.statusList
writeStdoutOutput(controlsCollection)
w.Close()
out, _ := ioutil.ReadAll(r)
os.Stdout = rescueStdout
for _, n := range tt.notContains {
assert.NotContains(t, string(out), fmt.Sprintf("[%s]", n))
}
}
}
func parseControlsJsonFile(filepath string) ([]*check.Controls, error) {
var result []*check.Controls