Skip to content

Commit

Permalink
custom_rules: merge all custom_rules pkgs into one (#850)
Browse files Browse the repository at this point in the history
This commit merge packages `entities/custom_rules` and
`services/custom_rules` into a single `customrules` package.

Signed-off-by: Matheus Alcantara <matheus.alcantara@zup.com.br>
  • Loading branch information
matheusalcantarazup authored Dec 7, 2021
1 parent 3cde903 commit 35f5a70
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/ZupIT/horusec-engine/text"
validation "github.com/go-ozzo/ozzo-validation/v4"

customrules "github.com/ZupIT/horusec/internal/enums/custom_rules"
"github.com/ZupIT/horusec/internal/services/engines/csharp"
"github.com/ZupIT/horusec/internal/services/engines/dart"
"github.com/ZupIT/horusec/internal/services/engines/java"
Expand All @@ -46,7 +45,7 @@ type CustomRule struct {
Language languages.Language `json:"language"`
Severity severities.Severity `json:"severity"`
Confidence confidence.Confidence `json:"confidence"`
Type customrules.MatchType `json:"type"`
Type MatchType `json:"type"`
Expressions []string `json:"expressions"`
}

Expand All @@ -61,20 +60,20 @@ func (c *CustomRule) Validate() error {
severities.Low, severities.Medium, severities.High, severities.Critical)),
validation.Field(&c.Confidence, validation.Required, validation.In(confidence.Low,
confidence.Medium, confidence.High)),
validation.Field(&c.Type, validation.Required, validation.In(customrules.Regular,
customrules.OrMatch, customrules.AndMatch, customrules.NotMatch)),
validation.Field(&c.Type, validation.Required, validation.In(Regular,
OrMatch, AndMatch, NotMatch)),
)
}

func (c *CustomRule) GetRuleType() text.MatchType {
switch c.Type {
case customrules.Regular:
case Regular:
return text.Regular
case customrules.OrMatch:
case OrMatch:
return text.OrMatch
case customrules.AndMatch:
case AndMatch:
return text.AndMatch
case customrules.NotMatch:
case NotMatch:
return text.NotMatch
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
"github.com/ZupIT/horusec-devkit/pkg/enums/severities"
"github.com/ZupIT/horusec-engine/text"
"github.com/stretchr/testify/assert"

customrulesenum "github.com/ZupIT/horusec/internal/enums/custom_rules"
)

func TestValidate(t *testing.T) {
Expand All @@ -34,7 +32,7 @@ func TestValidate(t *testing.T) {
Description: "test",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customrulesenum.OrMatch,
Type: OrMatch,
Expressions: []string{""},
Language: languages.Leaks,
}
Expand All @@ -54,7 +52,7 @@ func TestValidate(t *testing.T) {
Description: "test",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customrulesenum.Regular,
Type: Regular,
Expressions: []string{""},
Language: languages.Java,
}
Expand All @@ -67,7 +65,7 @@ func TestValidate(t *testing.T) {
Description: "test",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customrulesenum.Regular,
Type: Regular,
Expressions: []string{""},
Language: languages.Leaks,
}
Expand All @@ -80,7 +78,7 @@ func TestValidate(t *testing.T) {
Description: "test",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customrulesenum.Regular,
Type: Regular,
Expressions: []string{""},
Language: languages.Python,
}
Expand All @@ -94,56 +92,56 @@ func TestValidateAllLanguages(t *testing.T) {
ID: "HS-CSHARP-10000",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customrulesenum.Regular,
Type: Regular,
Language: languages.CSharp,
},
{
ID: "HS-DART-10000",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customrulesenum.Regular,
Type: Regular,
Language: languages.Dart,
},
{
ID: "HS-JAVA-10000",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customrulesenum.Regular,
Type: Regular,
Language: languages.Java,
},
{
ID: "HS-KOTLIN-10000",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customrulesenum.Regular,
Type: Regular,
Language: languages.Kotlin,
},
{
ID: "HS-YAML-10000",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customrulesenum.Regular,
Type: Regular,
Language: languages.Yaml,
},
{
ID: "HS-LEAKS-10000",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customrulesenum.Regular,
Type: Regular,
Language: languages.Leaks,
},
{
ID: "HS-JAVASCRIPT-10000",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customrulesenum.Regular,
Type: Regular,
Language: languages.Javascript,
},
{
ID: "HS-NGINX-10000",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customrulesenum.Regular,
Type: Regular,
Language: languages.Nginx,
},
}
Expand All @@ -157,7 +155,7 @@ func TestValidateAllLanguages(t *testing.T) {
func TestGetRuleType(t *testing.T) {
t.Run("should return regular type", func(t *testing.T) {
customRule := CustomRule{
Type: customrulesenum.Regular,
Type: Regular,
}

assert.Equal(t, text.Regular, customRule.GetRuleType())
Expand All @@ -171,23 +169,23 @@ func TestGetRuleType(t *testing.T) {

t.Run("should return or type", func(t *testing.T) {
customRule := CustomRule{
Type: customrulesenum.OrMatch,
Type: OrMatch,
}

assert.Equal(t, text.OrMatch, customRule.GetRuleType())
})

t.Run("should return and type", func(t *testing.T) {
customRule := CustomRule{
Type: customrulesenum.AndMatch,
Type: AndMatch,
}

assert.Equal(t, text.AndMatch, customRule.GetRuleType())
})

t.Run("should return not type", func(t *testing.T) {
customRule := CustomRule{
Type: customrulesenum.NotMatch,
Type: NotMatch,
}

assert.Equal(t, text.NotMatch, customRule.GetRuleType())
Expand Down
7 changes: 3 additions & 4 deletions internal/services/custom_rules/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/ZupIT/horusec-engine/text"

"github.com/ZupIT/horusec/config"
customrules "github.com/ZupIT/horusec/internal/entities/custom_rules"
)

// Service represents the custom rule service responsible to
Expand Down Expand Up @@ -77,7 +76,7 @@ func (s *Service) loadCustomRules() *Service {
return s
}

func (s *Service) validateAndParseCustomRule(rule *customrules.CustomRule) {
func (s *Service) validateAndParseCustomRule(rule *CustomRule) {
if err := rule.Validate(); err != nil {
errMsg := fmt.Sprintf("{HORUSEC_CLI} invalid custom rule: %s", rule.String())
logger.LogError(errMsg, err)
Expand All @@ -89,7 +88,7 @@ func (s *Service) validateAndParseCustomRule(rule *customrules.CustomRule) {
)
}

func (s *Service) openCustomRulesJSONFile() (customRules []*customrules.CustomRule, err error) {
func (s *Service) openCustomRulesJSONFile() (customRules []*CustomRule, err error) {
file, err := os.Open(s.config.CustomRulesPath)
if err != nil {
return nil, err
Expand All @@ -103,7 +102,7 @@ func (s *Service) openCustomRulesJSONFile() (customRules []*customrules.CustomRu
return customRules, json.Unmarshal(bytes, &customRules)
}

func (s *Service) parseCustomRuleToTextRule(rule *customrules.CustomRule) text.TextRule {
func (s *Service) parseCustomRuleToTextRule(rule *CustomRule) text.TextRule {
return text.TextRule{
Metadata: engine.Metadata{
ID: rule.ID,
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions internal/services/formatters/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
commitauthor "github.com/ZupIT/horusec/internal/entities/commit_author"
dockerentity "github.com/ZupIT/horusec/internal/entities/docker"
"github.com/ZupIT/horusec/internal/helpers/messages"
custonrules "github.com/ZupIT/horusec/internal/services/custom_rules"
customrules "github.com/ZupIT/horusec/internal/services/custom_rules"
"github.com/ZupIT/horusec/internal/services/docker"
"github.com/ZupIT/horusec/internal/services/git"
"github.com/ZupIT/horusec/internal/utils/file"
Expand Down Expand Up @@ -71,7 +71,7 @@ func NewFormatterService(analysiss *analysis.Analysis, dockerSvc docker.Docker,
docker: dockerSvc,
git: git.New(cfg),
config: cfg,
customRules: custonrules.NewCustomRulesService(cfg),
customRules: customrules.NewCustomRulesService(cfg),
}
}

Expand Down

0 comments on commit 35f5a70

Please sign in to comment.