Skip to content

Commit

Permalink
Add --json-indent option
Browse files Browse the repository at this point in the history
  • Loading branch information
yuvalavra committed Oct 5, 2022
1 parent f33987f commit a1c6eb6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
8 changes: 4 additions & 4 deletions cmd/collect.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"encoding/json"

"github.com/PaloAltoNetworks/rbac-police/pkg/collect"
"github.com/spf13/cobra"

Expand All @@ -23,9 +21,11 @@ func runCollect(cmd *cobra.Command, args []string) {
if collectResult == nil {
return // error printed by Collect()
}
output, err := json.Marshal(collectResult)

// Output collect results
output, err := marshalResults(collectResult)
if err != nil {
log.Errorln("runCollect: failed to marshal results with", err)
log.Errorln("runCollect: failed to marshal collectResult with", err)
return
}
outputResults(output)
Expand Down
4 changes: 2 additions & 2 deletions cmd/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ func runEval(cmd *cobra.Command, args []string) {
}

if !shortMode {
output, err = json.MarshalIndent(policyResults, "", " ")
output, err = marshalResults(policyResults)
if err != nil {
log.Errorln("runEval: failed to marshal results with", err)
return
}
} else {
abbreviatedResults := eval.AbbreviateResults(policyResults)
output, err = json.MarshalIndent(abbreviatedResults, "", " ")
output, err = marshalResults(abbreviatedResults)
if err != nil {
log.Errorln("runEval: failed to marshal abbreviated results with", err)
return
Expand Down
5 changes: 4 additions & 1 deletion cmd/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ func runExpand(cmd *cobra.Command, args []string) {
collectResult = *collectResultPtr
}

// Expand collection results
expandResult := expand.Expand(collectResult)
if expandResult == nil {
return // error printed by Expand()
}
output, err := json.MarshalIndent(expandResult, "", " ")

// Output expand results
output, err := marshalResults(expandResult)
if err != nil {
log.Errorln("runExpand: failed to marshal results with", err)
return
Expand Down
31 changes: 28 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package cmd

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/PaloAltoNetworks/rbac-police/pkg/collect"
log "github.com/sirupsen/logrus"
Expand All @@ -14,6 +15,7 @@ import (
var (
outFile string
loudMode bool
jsonIndentLen uint
collectConfig collect.CollectConfig

rootCmd = &cobra.Command{
Expand All @@ -35,6 +37,7 @@ func Execute() {
func init() {
rootCmd.PersistentFlags().StringVarP(&outFile, "out-file", "o", "", "save results to file")
rootCmd.PersistentFlags().BoolVarP(&loudMode, "loud", "l", false, "loud mode, print results regardless of -o")
rootCmd.PersistentFlags().UintVarP(&jsonIndentLen, "json-indent", "j", 4, "json indent, 0 means compact mode")
// Collect config
rootCmd.PersistentFlags().BoolVarP(&collectConfig.AllServiceAccounts, "all-serviceaccounts", "a", false, "collect data on all serviceAccounts, not only those assigned to a pod")
rootCmd.PersistentFlags().BoolVarP(&collectConfig.DiscoverProtections, "discover-protections", "w", false, "discover features gates and admission controllers that protect against certain attacks, partly by emulating the attacks via impersonation & dry-run write operations")
Expand All @@ -48,7 +51,7 @@ func init() {
// Prints and / or saves output to file
func outputResults(output []byte) {
if outFile != "" {
err := ioutil.WriteFile(outFile, output, 0644)
err := os.WriteFile(outFile, output, 0644)
if err != nil {
log.Errorf("runCollect: failed to write results to %v with %v\n", outFile, err)
return
Expand All @@ -60,10 +63,32 @@ func outputResults(output []byte) {
fmt.Println(string(output))
}

// Is an option related to collection set
// Is an option related to collection is set
func collectionOptionsSet() bool {
return collectConfig.IgnoreControlPlane || collectConfig.AllServiceAccounts ||
collectConfig.Namespace != "" || collectConfig.NodeUser != "" ||
(len(collectConfig.NodeGroups) != 1 && collectConfig.NodeGroups[0] != "system:nodes") ||
collectConfig.DiscoverProtections
}

// Marshal results into a json byte slice, indented based on the global jsonIndentLen variable
func marshalResults(results interface{}) ([]byte, error) {
if jsonIndentLen > 0 {
return json.MarshalIndent(results, "", getIndent(jsonIndentLen))
} else {
return json.Marshal(results) // compact json output
}
}

// Create an indent string in the length of @jsonIndentLength, maxed at 12 chars.
func getIndent(jsonIndentLength uint) string {
return strings.Repeat(" ", int(uintMin(jsonIndentLength, 12)))
}

// Return the minimum number
func uintMin(a uint, b uint) uint {
if a < b {
return a
}
return b
}

0 comments on commit a1c6eb6

Please sign in to comment.