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

Commit

Permalink
fix: validate local duplicates and support custom rules (#122)
Browse files Browse the repository at this point in the history
* fix: validate local duplicates

* feat: support running custom rules

* fix: always run custom rules
  • Loading branch information
dimabru authored Oct 18, 2022
1 parent 86830d1 commit 060e56b
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 11 deletions.
47 changes: 44 additions & 3 deletions cmd/validate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/allero-io/allero/pkg/posthog"
"github.com/allero-io/allero/pkg/resultsPrinter"
"github.com/allero-io/allero/pkg/rulesConfig"
"github.com/allero-io/allero/pkg/rulesConfig/defaultRules"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -117,8 +118,10 @@ func validateOutputFlag(output string) bool {
}

func execute(deps *ValidateCommandDependencies, option *validateCommandOptions) error {
isLocal := option.localPathToValidate != ""
var err error
if option.localPathToValidate != "" {

if isLocal {
err = deps.LocalRepositoriesClient.Get(option.localPathToValidate)
if err == nil {
fmt.Printf("Running validation over %s\n", option.localPathToValidate)
Expand Down Expand Up @@ -163,7 +166,9 @@ func execute(deps *ValidateCommandDependencies, option *validateCommandOptions)
return err
}

if hasToken && !selectedRuleIds[rule.UniqueId] {
isCustomRule := rule.UniqueId >= 1000

if hasToken && !selectedRuleIds[rule.UniqueId] && !isCustomRule {
continue
} else if !hasToken && !rule.EnabledByDefault {
continue
Expand Down Expand Up @@ -208,7 +213,11 @@ func execute(deps *ValidateCommandDependencies, option *validateCommandOptions)
summary.URL = deps.ConfigurationManager.TokenGenerationUrl
}

err = resultsPrinter.PrintResults(ruleResultsById, summary, option.output, option.localPathToValidate != "")
if isLocal {
ruleResultsById = reduceLocalRuleResults(ruleResultsById)
}

err = resultsPrinter.PrintResults(ruleResultsById, summary, option.output, isLocal)
if err != nil {
return err
}
Expand All @@ -217,3 +226,35 @@ func execute(deps *ValidateCommandDependencies, option *validateCommandOptions)
}
return nil
}

func reduceLocalRuleResults(ruleResultsById map[int]*rulesConfig.RuleResult) map[int]*rulesConfig.RuleResult {
reducedRuleResultsById := map[int]*rulesConfig.RuleResult{}
for uniqueId, ruleResult := range ruleResultsById {
if ruleResult.Valid {
reducedRuleResultsById[uniqueId] = ruleResult
continue
}

schemaErrorsByScmPlatform := map[string][]*defaultRules.SchemaError{}
for _, schemaError := range ruleResult.SchemaErrors {
schemaErrorsByScmPlatform[schemaError.ScmPlatform] = append(schemaErrorsByScmPlatform[schemaError.ScmPlatform], schemaError)
}

maxErrors := 0
schemaErrors := []*defaultRules.SchemaError{}
for _, scmSchemaErrors := range schemaErrorsByScmPlatform {
if len(scmSchemaErrors) > maxErrors {
maxErrors = len(scmSchemaErrors)
schemaErrors = scmSchemaErrors
}
}

reducedRuleResultsById[uniqueId] = &rulesConfig.RuleResult{
RuleName: ruleResult.RuleName,
Valid: false,
SchemaErrors: schemaErrors,
FailureMessage: ruleResult.FailureMessage,
}
}
return reducedRuleResultsById
}
4 changes: 2 additions & 2 deletions pkg/rulesConfig/defaultRules/14-ensure-code-coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func githubErrorsRule14(githubData map[string]*githubConnector.GithubOwner) ([]*
schemaErrors = append(schemaErrors, &SchemaError{
ErrorLevel: 1,
RepositryName: repo.Name,
CiCdPlatform: "github-actions-workflows",
CiCdPlatform: "",
OwnerName: owner.Name,
ScmPlatform: "github",
})
Expand All @@ -116,7 +116,7 @@ func gitlabErrorsRule14(gitlabData map[string]*gitlabConnector.GitlabGroup) ([]*
schemaErrors = append(schemaErrors, &SchemaError{
ErrorLevel: 2,
RepositryName: project.Name,
CiCdPlatform: "gitlab-ci",
CiCdPlatform: "",
OwnerName: group.Name,
ScmPlatform: "gitlab",
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/rulesConfig/defaultRules/15-ensure-secrets-scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func githubErrorsRule15(githubData map[string]*githubConnector.GithubOwner) ([]*
schemaErrors = append(schemaErrors, &SchemaError{
ErrorLevel: 1,
RepositryName: repo.Name,
CiCdPlatform: "github-actions-workflows",
CiCdPlatform: "",
OwnerName: owner.Name,
ScmPlatform: "github",
})
Expand All @@ -141,7 +141,7 @@ func gitlabErrorsRule15(gitlabData map[string]*gitlabConnector.GitlabGroup) ([]*
schemaErrors = append(schemaErrors, &SchemaError{
ErrorLevel: 2,
RepositryName: project.Name,
CiCdPlatform: "gitlab-ci",
CiCdPlatform: "",
OwnerName: group.Name,
ScmPlatform: "gitlab",
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/rulesConfig/defaultRules/16-ensure-linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func githubErrorsRule16(githubData map[string]*githubConnector.GithubOwner) ([]*
schemaErrors = append(schemaErrors, &SchemaError{
ErrorLevel: 1,
RepositryName: repo.Name,
CiCdPlatform: "github-actions-workflows",
CiCdPlatform: "",
OwnerName: owner.Name,
ScmPlatform: "github",
})
Expand All @@ -122,7 +122,7 @@ func gitlabErrorsRule16(gitlabData map[string]*gitlabConnector.GitlabGroup) ([]*
schemaErrors = append(schemaErrors, &SchemaError{
ErrorLevel: 2,
RepositryName: project.Name,
CiCdPlatform: "gitlab-ci",
CiCdPlatform: "",
OwnerName: group.Name,
ScmPlatform: "gitlab",
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/rulesConfig/defaultRules/17-ensure-code-quality.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func githubErrorsRule17(githubData map[string]*githubConnector.GithubOwner) ([]*
schemaErrors = append(schemaErrors, &SchemaError{
ErrorLevel: 1,
RepositryName: repo.Name,
CiCdPlatform: "github-actions-workflows",
CiCdPlatform: "",
OwnerName: owner.Name,
ScmPlatform: "github",
})
Expand All @@ -118,7 +118,7 @@ func gitlabErrorsRule17(gitlabData map[string]*gitlabConnector.GitlabGroup) ([]*
schemaErrors = append(schemaErrors, &SchemaError{
ErrorLevel: 2,
RepositryName: project.Name,
CiCdPlatform: "gitlab-ci",
CiCdPlatform: "",
OwnerName: group.Name,
ScmPlatform: "gitlab",
})
Expand Down
16 changes: 16 additions & 0 deletions pkg/rulesConfig/rulesConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,26 @@ func (rc *RulesConfig) parseSchemaFieldGitlab(gitlabData map[string]*gitlabConne
func (rc *RulesConfig) GetAllRuleNames(scmPlatform string) []string {
alleroHomedir := fileManager.GetAlleroHomedir()
rulesPath := fmt.Sprintf("%s/rules/%s", alleroHomedir, scmPlatform)
customRulesPath := fmt.Sprintf("%s/rules/%s/custom", alleroHomedir, scmPlatform)

ruleNames := []string{}

files := fileManager.ReadFolder(rulesPath)
customFiles := fileManager.ReadFolder(customRulesPath)

for _, file := range files {
if strings.HasSuffix(file.Name(), ".json") {
ruleNames = append(ruleNames, strings.TrimSuffix(file.Name(), ".json"))
}
}

for _, file := range customFiles {
if strings.HasSuffix(file.Name(), ".json") {
ruleName := "custom/" + strings.TrimSuffix(file.Name(), ".json")
ruleNames = append(ruleNames, ruleName)
}
}

return ruleNames
}

Expand All @@ -243,6 +253,8 @@ func (rc *RulesConfig) GetSelectedRuleIds() (map[int]bool, error) {
}

func (rc *RulesConfig) GetRule(ruleName string, scmPlatform string) (*defaultRules.Rule, error) {
isCustomRule := strings.HasPrefix(ruleName, "custom/")

alleroHomedir := fileManager.GetAlleroHomedir()
ruleFilename := fmt.Sprintf("%s/rules/%s/%s.json", alleroHomedir, scmPlatform, ruleName)

Expand All @@ -257,6 +269,10 @@ func (rc *RulesConfig) GetRule(ruleName string, scmPlatform string) (*defaultRul
return nil, err
}

if isCustomRule {
rule.UniqueId = rule.UniqueId + 1000
}

return rule, rc.validateRuleStructure(ruleName, rule)
}

Expand Down

0 comments on commit 060e56b

Please sign in to comment.