Skip to content

Commit

Permalink
Add severity to executable tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Ice3man543 committed Mar 13, 2021
1 parent f6fc4e5 commit d8bb580
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion v2/internal/runner/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (r *Runner) getParsedTemplatesFor(templatePaths, severities []string, workf
parsedTemplates[t.ID] = t
gologger.Info().Msgf("%s\n", r.templateLogMsg(t.ID, types.ToString(t.Info["name"]), types.ToString(t.Info["author"]), sev))
} else {
gologger.Error().Msgf("Excluding template %s due to severity filter (%s not in [%s])", t.ID, sev, severities)
gologger.Warning().Msgf("Excluding template %s due to severity filter (%s not in [%s])", t.ID, sev, severities)
}
}
return parsedTemplates, workflowCount
Expand Down
7 changes: 5 additions & 2 deletions v2/pkg/templates/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Parse(filePath string, options protocols.ExecuterOptions) (*Template, error
if !ok {
return nil, nil
}
if err := matchTemplateWithTags(types.ToString(templateTags), options.Options); err != nil {
if err := matchTemplateWithTags(types.ToString(templateTags), types.ToString(template.Info["severity"]), options.Options); err != nil {
return nil, nil
}
}
Expand Down Expand Up @@ -200,8 +200,11 @@ func (t *Template) parseWorkflowTemplate(workflow *workflows.WorkflowTemplate, o
}

// matchTemplateWithTags matches if the template matches a tag
func matchTemplateWithTags(tags string, options *types.Options) error {
func matchTemplateWithTags(tags, severity string, options *types.Options) error {
actualTags := strings.Split(tags, ",")
if severity != "" {
actualTags = append(actualTags, severity) // also add severity to tag
}

matched := false
mainLoop:
Expand Down
17 changes: 11 additions & 6 deletions v2/pkg/templates/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,30 @@ import (
)

func TestMatchTemplateWithTags(t *testing.T) {
err := matchTemplateWithTags("php,linux,symfony", &types.Options{Tags: []string{"php"}})
err := matchTemplateWithTags("php,linux,symfony", "", &types.Options{Tags: []string{"php"}})
require.Nil(t, err, "could not get php tag from input slice")

err = matchTemplateWithTags("lang:php,os:linux,cms:symfony", &types.Options{Tags: []string{"cms:symfony"}})
err = matchTemplateWithTags("lang:php,os:linux,cms:symfony", "", &types.Options{Tags: []string{"cms:symfony"}})
require.Nil(t, err, "could not get php tag from input key value")

err = matchTemplateWithTags("lang:php,os:linux,symfony", &types.Options{Tags: []string{"cms:symfony"}})
err = matchTemplateWithTags("lang:php,os:linux,symfony", "", &types.Options{Tags: []string{"cms:symfony"}})
require.NotNil(t, err, "could get key value tag from input key value")

err = matchTemplateWithTags("lang:php,os:linux,cms:jira", &types.Options{Tags: []string{"cms:symfony"}})
err = matchTemplateWithTags("lang:php,os:linux,cms:jira", "", &types.Options{Tags: []string{"cms:symfony"}})
require.NotNil(t, err, "could get key value tag from input key value")

t.Run("space", func(t *testing.T) {
err = matchTemplateWithTags("lang:php, os:linux, cms:symfony", &types.Options{Tags: []string{"cms:symfony"}})
err = matchTemplateWithTags("lang:php, os:linux, cms:symfony", "", &types.Options{Tags: []string{"cms:symfony"}})
require.Nil(t, err, "could get key value tag from input key value with space")
})

t.Run("comma-tags", func(t *testing.T) {
err = matchTemplateWithTags("lang:php,os:linux,cms:symfony", &types.Options{Tags: []string{"test,cms:symfony"}})
err = matchTemplateWithTags("lang:php,os:linux,cms:symfony", "", &types.Options{Tags: []string{"test,cms:symfony"}})
require.Nil(t, err, "could get key value tag from input key value with comma")
})

t.Run("severity", func(t *testing.T) {
err = matchTemplateWithTags("lang:php,os:linux,cms:symfony", "low", &types.Options{Tags: []string{"low"}})
require.Nil(t, err, "could get key value tag for severity")
})
}

0 comments on commit d8bb580

Please sign in to comment.