Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --status flag #1030

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions check/controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ type Summary struct {
Info int `json:"total_info"`
}

func (s Summary) Results(c State) int {
var r int
switch c {
case "PASS":
r = s.Pass
case "FAIL":
r = s.Fail
case "WARN":
r = s.Warn
case "INFO":
r = s.Info
}
return r
}

// Predicate a predicate on the given Group and Check arguments.
type Predicate func(group *Group, check *Check) bool

Expand Down
56 changes: 46 additions & 10 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -184,24 +205,29 @@ func prettyPrint(r *check.Controls, summary check.Summary) {

// Print remediations.
if !noRemediations {
var remediationOutput strings.Builder
if summary.Fail > 0 || summary.Warn > 0 {
colors[check.WARN].Printf("== Remediations %s ==\n", r.Type)
for _, g := range r.Groups {
for _, c := range g.Checks {
if c.State == check.FAIL {
fmt.Printf("%s %s\n", c.ID, c.Remediation)
if c.State == check.FAIL && printStatus(check.FAIL) {
remediationOutput.WriteString(fmt.Sprintf("%s %s\n", c.ID, c.Remediation))
}
if c.State == check.WARN {
if c.State == check.WARN && printStatus(check.WARN) {
// Print the error if test failed due to problem with the audit command
if c.Reason != "" && c.Type != "manual" {
fmt.Printf("%s audit test did not run: %s\n", c.ID, c.Reason)
remediationOutput.WriteString(fmt.Sprintf("%s audit test did not run: %s\n", c.ID, c.Reason))
} else {
fmt.Printf("%s %s\n", c.ID, c.Remediation)
remediationOutput.WriteString(fmt.Sprintf("%s %s\n", c.ID, c.Remediation))
}
}
}
}
fmt.Println()
output := remediationOutput.String()
if len(output) > 0 {
remediationOutput.WriteString("\n")
fmt.Printf(colors[check.WARN].Sprintf("== Remediations %s ==\n", r.Type))
fmt.Printf(remediationOutput.String())
}
}
}

Expand All @@ -222,9 +248,19 @@ func printSummary(summary check.Summary, sectionName string) {
}

colors[res].Printf("== Summary %s ==\n", sectionName)
fmt.Printf("%d checks PASS\n%d checks FAIL\n%d checks WARN\n%d checks INFO\n\n",
summary.Pass, summary.Fail, summary.Warn, summary.Info,
)
if statusList == "" {
fmt.Printf("%d checks PASS\n%d checks FAIL\n%d checks WARN\n%d checks INFO\n\n", summary.Pass, summary.Fail, summary.Warn, summary.Info)
return
}
statusMap := parseStatus(statusList)
var summaryBuilder strings.Builder
for s, b := range statusMap {
if b {
summaryBuilder.WriteString(fmt.Sprintf("%d checks %v\n", summary.Results(s), s))
}
}
summaryBuilder.WriteString("\n")
fmt.Printf(summaryBuilder.String())
}

// loadConfig finds the correct config dir based on the kubernetes version,
Expand Down
66 changes: 66 additions & 0 deletions cmd/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,72 @@ 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
contains []string
}
testCases := []testCase{
{
name: "statusList PASS",
statusList: "PASS",
notContains: []string{"INFO", "WARN", "FAIL", "== Remediations controlplane =="},
},
{
name: "statusList PASS,INFO",
statusList: "PASS,INFO",
notContains: []string{"WARN", "FAIL", "== Remediations controlplane =="},
},
{
name: "statusList WARN",
statusList: "WARN",
notContains: []string{"INFO", "FAIL", "PASS"},
contains: []string{"== Remediations controlplane =="},
},
{
name: "statusList FAIL",
statusList: "FAIL",
notContains: []string{"INFO", "WARN", "PASS", "== Remediations controlplane =="},
},
{
name: "statusList empty",
statusList: "",
notContains: nil,
contains: []string{"== Remediations controlplane =="},
},
}

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), n)
}

for _, c := range tt.contains {
assert.Contains(t, string(out), c)
}
}
}

func parseControlsJsonFile(filepath string) ([]*check.Controls, error) {
var result []*check.Controls

Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ var (
filterOpts FilterOpts
includeTestOutput bool
outputFile string
statusList string
configFileError error
controlsCollection []*check.Controls
)
Expand Down Expand Up @@ -174,6 +175,7 @@ func init() {
RootCmd.PersistentFlags().StringVar(&skipIds, "skip", "", "List of comma separated values of checks to be skipped")
RootCmd.PersistentFlags().BoolVar(&includeTestOutput, "include-test-output", false, "Prints the actual result when test fails")
RootCmd.PersistentFlags().StringVar(&outputFile, "outputfile", "", "Writes the JSON results to output file")
RootCmd.PersistentFlags().StringVar(&statusList, "status", "", "List of comma separated status to be printed")

RootCmd.PersistentFlags().StringVarP(
&filterOpts.CheckList,
Expand Down