Skip to content
This repository was archived by the owner on Jul 19, 2023. It is now read-only.

Commit 2bed2c0

Browse files
authoredNov 6, 2022
feat: print disabled rules (#124)
1 parent 1d0309a commit 2bed2c0

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed
 

‎cmd/validate/main.go

+24-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package validate
33
import (
44
"errors"
55
"fmt"
6+
"path/filepath"
67

78
"github.com/allero-io/allero/pkg/configurationManager"
89
localConnector "github.com/allero-io/allero/pkg/connectors/local"
@@ -59,7 +60,15 @@ allero validate ~/my-repo-dir Validate over local directory`,
5960
Args: cobra.MaximumNArgs(1),
6061
PreRun: func(cmd *cobra.Command, cmdArgs []string) {
6162
args := make(map[string]any)
63+
64+
if len(cmdArgs) > 0 {
65+
absolutePath, err := filepath.Abs(cmdArgs[0])
66+
if err == nil {
67+
cmdArgs[0] = absolutePath
68+
}
69+
}
6270
args["Args"] = cmdArgs
71+
6372
decodedToken, _ := deps.ConfigurationManager.ParseToken()
6473
if decodedToken != nil {
6574
args["User Email"] = decodedToken.Email
@@ -158,6 +167,7 @@ func execute(deps *ValidateCommandDependencies, option *validateCommandOptions)
158167
}
159168

160169
ruleResultsById := map[int]*rulesConfig.RuleResult{}
170+
disabledRules := map[string]bool{}
161171

162172
for scmPlatform, ruleNames := range ruleNamesByScmPlatform {
163173
for _, ruleName := range ruleNames {
@@ -168,9 +178,8 @@ func execute(deps *ValidateCommandDependencies, option *validateCommandOptions)
168178

169179
isCustomRule := rule.UniqueId >= 1000
170180

171-
if hasToken && !selectedRuleIds[rule.UniqueId] && !isCustomRule {
172-
continue
173-
} else if !hasToken && !rule.EnabledByDefault {
181+
if isRuleDisabled(hasToken, selectedRuleIds, rule, isCustomRule) {
182+
disabledRules[ruleName] = true
174183
continue
175184
}
176185

@@ -208,16 +217,13 @@ func execute(deps *ValidateCommandDependencies, option *validateCommandOptions)
208217

209218
summary.TotalRulesEvaluated = len(ruleResultsById)
210219
summary.TotalFailedRules = totalRulesFailed
211-
212-
if !hasToken {
213-
summary.URL = deps.ConfigurationManager.TokenGenerationUrl
214-
}
220+
summary.URL = deps.ConfigurationManager.TokenGenerationUrl
215221

216222
if isLocal {
217223
ruleResultsById = reduceLocalRuleResults(ruleResultsById)
218224
}
219225

220-
err = resultsPrinter.PrintResults(ruleResultsById, summary, option.output, isLocal)
226+
err = resultsPrinter.PrintResults(ruleResultsById, summary, disabledRules, option.output, isLocal)
221227
if err != nil {
222228
return err
223229
}
@@ -227,6 +233,16 @@ func execute(deps *ValidateCommandDependencies, option *validateCommandOptions)
227233
return nil
228234
}
229235

236+
func isRuleDisabled(hasToken bool, selectedRuleIds map[int]bool, rule *defaultRules.Rule, isCustomRule bool) bool {
237+
if hasToken && !selectedRuleIds[rule.UniqueId] && !isCustomRule {
238+
return true
239+
} else if !hasToken && !rule.EnabledByDefault {
240+
return true
241+
}
242+
243+
return false
244+
}
245+
230246
func reduceLocalRuleResults(ruleResultsById map[int]*rulesConfig.RuleResult) map[int]*rulesConfig.RuleResult {
231247
reducedRuleResultsById := map[int]*rulesConfig.RuleResult{}
232248
for uniqueId, ruleResult := range ruleResultsById {

‎pkg/resultsPrinter/resultsPrinter.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ const colorRed = "\033[31m"
2727
const colorReset = "\033[0m"
2828
const colorBlue = "\033[34m"
2929

30-
func PrintResults(ruleResults map[int]*rulesConfig.RuleResult, summary rulesConfig.OutputSummary, outputFormat string, localValidation bool) error {
30+
func PrintResults(ruleResults map[int]*rulesConfig.RuleResult, summary rulesConfig.OutputSummary, disabledRules map[string]bool, outputFormat string, localValidation bool) error {
3131
if localValidation {
3232
PatchLocalErrors(ruleResults)
3333
}
3434
if outputFormat == "" {
3535
printPretty(ruleResults, summary)
36-
printSummary(ruleResults, summary)
36+
printSummary(ruleResults, summary, disabledRules)
3737
} else if outputFormat == "csv" {
3838
return printCSV(ruleResults, summary)
3939
}
@@ -99,7 +99,7 @@ func unescapeValue(value string) string {
9999
return strings.ReplaceAll(value, "[ESCAPED_DOT]", ".")
100100
}
101101

102-
func printSummary(ruleResults map[int]*rulesConfig.RuleResult, summary rulesConfig.OutputSummary) {
102+
func printSummary(ruleResults map[int]*rulesConfig.RuleResult, summary rulesConfig.OutputSummary, disabledRules map[string]bool) {
103103
t := table.NewWriter()
104104
t.SetOutputMirror(os.Stdout)
105105
fmt.Println("Summary")
@@ -115,11 +115,18 @@ func printSummary(ruleResults map[int]*rulesConfig.RuleResult, summary rulesConf
115115
t.AppendRow([]interface{}{"Failed rules", summary.TotalFailedRules})
116116
t.Render()
117117

118-
if summary.URL != "" {
119-
fmt.Println()
120-
fmt.Println("Select your own rules:", string(colorBlue), summary.URL, string(colorReset))
118+
fmt.Println()
119+
120+
if len(disabledRules) > 0 {
121+
fmt.Println("The following rules are disabled:")
122+
for ruleName := range disabledRules {
123+
fmt.Println(string(colorBlue), "\r", ruleName, string(colorReset))
124+
}
121125
}
122126

127+
fmt.Println()
128+
fmt.Println("Click on the link to activate/deactivate rules:", string(colorBlue), summary.URL, string(colorReset))
129+
123130
if summary.TotalFailedRules > 0 {
124131
fmt.Println()
125132
fmt.Println("Failed rules summary:")

0 commit comments

Comments
 (0)
This repository has been archived.