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

Add --outputfile flag for writing json results to output file (#295)

This commit is contained in:
nshauli 2019-05-29 18:05:55 +03:00 committed by jerbia
parent 5e80f41066
commit e64f61fa7f
2 changed files with 28 additions and 2 deletions

View File

@ -15,6 +15,7 @@
package cmd
import (
"bufio"
"fmt"
"io/ioutil"
"os"
@ -112,7 +113,7 @@ func runChecks(nodetype check.NodeType) {
exitWithError(fmt.Errorf("failed to output in JSON format: %v", err))
}
fmt.Println(string(out))
PrintOutput(string(out), outputFile)
} else {
// if we want to store in PostgreSQL, convert to JSON and save it
if (summary.Fail > 0 || summary.Warn > 0 || summary.Pass > 0 || summary.Info > 0) && pgSQL {
@ -251,3 +252,26 @@ func printRawOutput(output string) {
fmt.Println(fmt.Sprintf("\t %s", row))
}
}
func writeOutputToFile(output string, outputFile string) error {
file, err := os.Create(outputFile)
if err != nil {
return err
}
defer file.Close()
w := bufio.NewWriter(file)
fmt.Fprintln(w, output)
return w.Flush()
}
func PrintOutput(output string, outputFile string) {
if len(outputFile) == 0 {
fmt.Println(output)
} else {
err := writeOutputToFile(output, outputFile)
if err != nil {
exitWithError(fmt.Errorf("Failed to write to output file %s: %v", outputFile, err))
}
}
}

View File

@ -47,7 +47,8 @@ var (
noSummary bool
noRemediations bool
filterOpts FilterOpts
includeTestOutput bool
includeTestOutput bool
outputFile string
)
// RootCmd represents the base command when called without any subcommands
@ -89,6 +90,7 @@ func init() {
RootCmd.PersistentFlags().BoolVar(&filterOpts.Scored, "scored", true, "Run the scored CIS checks")
RootCmd.PersistentFlags().BoolVar(&filterOpts.Unscored, "unscored", true, "Run the unscored CIS checks")
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().StringVarP(
&filterOpts.CheckList,