Skip to content

Commit

Permalink
feat(rego): Add aliasing support (#1515)
Browse files Browse the repository at this point in the history
* feat(checks): Add aliasing support
Fixes: aquasecurity/trivy#5691

Signed-off-by: Simar <[email protected]>

* fix lint

---------

Signed-off-by: Simar <[email protected]>
  • Loading branch information
simar7 authored Jan 10, 2024
1 parent 72340d1 commit 9a50155
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
15 changes: 14 additions & 1 deletion pkg/rego/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type StaticMetadata struct {
AVDID string
Title string
ShortCode string
Aliases []string
Description string
Severity string
RecommendedActions string
Expand Down Expand Up @@ -99,6 +100,8 @@ func (sm *StaticMetadata) Update(meta map[string]any) error {
}
}

sm.updateAliases(meta)

var err error
if sm.CloudFormation, err = NewEngineMetadata("cloud_formation", meta); err != nil {
return err
Expand All @@ -111,6 +114,16 @@ func (sm *StaticMetadata) Update(meta map[string]any) error {
return nil
}

func (sm *StaticMetadata) updateAliases(meta map[string]any) {
if raw, ok := meta["aliases"]; ok {
if aliases, ok := raw.([]interface{}); ok {
for _, a := range aliases {
sm.Aliases = append(sm.Aliases, fmt.Sprintf("%s", a))
}
}
}
}

func (sm *StaticMetadata) FromAnnotations(annotations *ast.Annotations) error {
sm.Title = annotations.Title
sm.Description = annotations.Description
Expand Down Expand Up @@ -191,7 +204,7 @@ func (m StaticMetadata) ToRule() scan.Rule {

return scan.Rule{
AVDID: m.AVDID,
Aliases: []string{m.ID},
Aliases: append(m.Aliases, m.ID),
ShortCode: m.ShortCode,
Summary: m.Title,
Explanation: m.Description,
Expand Down
3 changes: 3 additions & 0 deletions pkg/rego/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func Test_UpdateStaticMetadata(t *testing.T) {
AVDID: "a",
Title: "t",
ShortCode: "sc",
Aliases: []string{"a", "b", "c"},
Description: "d",
Severity: "s",
RecommendedActions: "ra",
Expand All @@ -36,6 +37,7 @@ func Test_UpdateStaticMetadata(t *testing.T) {
"avd_id": "a_n",
"title": "t_n",
"short_code": "sc_n",
"aliases": []any{"a_n", "b_n", "c_n"},
"description": "d_n",
"service": "srvc_n",
"provider": "pr_n",
Expand All @@ -54,6 +56,7 @@ func Test_UpdateStaticMetadata(t *testing.T) {
AVDID: "a_n",
Title: "t_n",
ShortCode: "sc_n",
Aliases: []string{"a", "b", "c", "a_n", "b_n", "c_n"},
Description: "d_n",
Severity: "S_N",
RecommendedActions: "ra_n",
Expand Down

0 comments on commit 9a50155

Please sign in to comment.