1
0
mirror of https://github.com/aquasecurity/kube-bench.git synced 2024-11-22 16:18:07 +00:00

colorPrint for the output

Use the same format output for warnings even if they aren’t related to
a specific test ID
This commit is contained in:
Liz Rice 2017-06-20 09:54:17 +01:00
parent dcd416a521
commit 800c18ccf3
2 changed files with 29 additions and 19 deletions

View File

@ -38,6 +38,8 @@ const (
FAIL = "FAIL" FAIL = "FAIL"
// WARN could not carry out check. // WARN could not carry out check.
WARN = "WARN" WARN = "WARN"
// INFO informational message
INFO = "INFO"
// MASTER a master node // MASTER a master node
MASTER NodeType = "master" MASTER NodeType = "master"

View File

@ -42,6 +42,14 @@ var (
// Used for variable substitution // Used for variable substitution
symbols = map[string]string{} symbols = map[string]string{}
// Print colors
colors = map[check.State]*color.Color{
check.PASS: color.New(color.FgGreen),
check.FAIL: color.New(color.FgRed),
check.WARN: color.New(color.FgYellow),
check.INFO: color.New(color.FgWhite),
}
) )
func runChecks(t check.NodeType) { func runChecks(t check.NodeType) {
@ -145,7 +153,7 @@ func verifyNodeType(t check.NodeType) {
for _, b := range binPath { for _, b := range binPath {
_, err := exec.LookPath(b) _, err := exec.LookPath(b)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "WARNING: %s: command not found on path - version check skipped\n", b) colorPrint(check.WARN, fmt.Sprintf("%s: command not found on path - version check skipped\n", b))
continue continue
} }
@ -153,12 +161,13 @@ func verifyNodeType(t check.NodeType) {
cmd := exec.Command(b, "--version") cmd := exec.Command(b, "--version")
out, _ = cmd.Output() out, _ = cmd.Output()
if matched, _ := regexp.MatchString(kubeVersion, string(out)); !matched { if matched, _ := regexp.MatchString(kubeVersion, string(out)); !matched {
fmt.Fprintf(os.Stderr, colorPrint(check.FAIL,
fmt.Sprintf(
"%s unsupported version, expected %s, got %s\n", "%s unsupported version, expected %s, got %s\n",
b, b,
kubeVersion, kubeVersion,
string(out), string(out),
) ))
os.Exit(1) os.Exit(1)
} }
} }
@ -168,35 +177,34 @@ func verifyNodeType(t check.NodeType) {
cmd := exec.Command("ps", "-ef") cmd := exec.Command("ps", "-ef")
out, _ = cmd.Output() out, _ = cmd.Output()
if matched, _ := regexp.MatchString(".*"+b, string(out)); !matched { if matched, _ := regexp.MatchString(".*"+b, string(out)); !matched {
fmt.Fprintf(os.Stderr, "%s is not running\n", b) colorPrint(check.FAIL, fmt.Sprintf("%s is not running\n", b))
os.Exit(1) os.Exit(1)
} }
} }
for _, c := range confPath { for _, c := range confPath {
if _, err := os.Stat(c); os.IsNotExist(err) { if _, err := os.Stat(c); os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "WARNING: config file %s does not exist\n", c) colorPrint(check.WARN, fmt.Sprintf("config file %s does not exist\n", c))
// os.Exit(1)
} }
} }
} }
func prettyPrint(r *check.Controls, summary check.Summary) { // colorPrint outputs the state in a specific colour, along with a message string
colors := map[check.State]*color.Color{ func colorPrint(state check.State, s string) {
check.PASS: color.New(color.FgGreen), colors[state].Printf("[%s] ", state)
check.FAIL: color.New(color.FgRed), fmt.Printf("%s", s)
check.WARN: color.New(color.FgYellow), }
}
func prettyPrint(r *check.Controls, summary check.Summary) {
// Print checks and results. // Print checks and results.
fmt.Printf("[INFO] %s %s\n", r.ID, r.Text) colorPrint(check.INFO, fmt.Sprintf("%s %s\n", r.ID, r.Text))
for _, g := range r.Groups { for _, g := range r.Groups {
fmt.Printf("[INFO] %s %s\n", g.ID, g.Text) fmt.Printf("[INFO] %s %s\n", g.ID, g.Text)
for _, c := range g.Checks { for _, c := range g.Checks {
colors[c.State].Printf("[%s] ", c.State) colorPrint(c.State, fmt.Sprintf("%s %s\n", c.ID, c.Text))
fmt.Printf("%s %s\n", c.ID, c.Text)
} }
} }
fmt.Println() fmt.Println()
// Print remediations. // Print remediations.