Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add the occurrences field #1383

Merged
merged 3 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/scan/flat.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type FlatResult struct {
Warning bool `json:"warning"`
Status Status `json:"status"`
Resource string `json:"resource"`
Occurrences []Occurrence `json:"occurrences,omitempty"`
Location FlatRange `json:"location"`
}

Expand Down Expand Up @@ -60,6 +61,7 @@ func (r *Result) Flatten() FlatResult {
Severity: r.rule.Severity,
Status: r.status,
Resource: resMetadata.Reference(),
Occurrences: r.Occurrences(),
Warning: r.IsWarning(),
Location: FlatRange{
Filename: rng.GetFilename(),
Expand Down
28 changes: 28 additions & 0 deletions pkg/scan/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,31 @@ func rawToString(raw interface{}) string {
return "?"
}
}

type Occurrence struct {
Resource string `json:"resource"`
Filename string `json:"filename"`
StartLine int `json:"start_line"`
EndLine int `json:"end_line"`
}

func (r *Result) Occurrences() []Occurrence {
var occurrences []Occurrence

mod := &r.metadata

for {
mod = mod.Parent()
if mod == nil {
break
}
parentRange := mod.Range()
occurrences = append(occurrences, Occurrence{
Resource: mod.Reference(),
Filename: parentRange.GetFilename(),
StartLine: parentRange.GetStartLine(),
EndLine: parentRange.GetEndLine(),
})
}
return occurrences
}
Comment on lines +347 to +366
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test for this? We can probably add it in pkg/scanners/cloud/aws/scanner_test.go.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simar7 How good is this place for the test? this file refers to tests of aws services. Since the method is public and converts the result to another structure, maybe it's worth adding it to pkg/scan/result_test.go?

56 changes: 56 additions & 0 deletions pkg/scan/result_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package scan_test

import (
"testing"

"github.com/aquasecurity/defsec/pkg/scan"
"github.com/aquasecurity/defsec/pkg/types"
"github.com/stretchr/testify/assert"
)

func Test_Occurrences(t *testing.T) {
tests := []struct {
name string
factory func() *scan.Result
expected []scan.Occurrence
}{
{
name: "happy",
factory: func() *scan.Result {
r := scan.Result{}
causeResourceMeta := types.NewMetadata(types.NewRange(
"main.tf", 1, 13, "", nil,
), "module.aws-security-groups[\"db1\"]")

parentMeta := types.NewMetadata(types.NewRange(
"terraform-aws-modules/security-group/aws/main.tf", 191, 227, "", nil,
), "aws_security_group_rule.ingress_with_cidr_blocks[0]").WithParent(causeResourceMeta)

r.OverrideMetadata(types.NewMetadata(types.NewRange(
"terraform-aws-modules/security-group/aws/main.tf", 197, 204, "", nil,
), "aws_security_group_rule.ingress_with_cidr_blocks").WithParent(parentMeta))
return &r
},
expected: []scan.Occurrence{
{
Resource: "aws_security_group_rule.ingress_with_cidr_blocks[0]",
Filename: "terraform-aws-modules/security-group/aws/main.tf",
StartLine: 191,
EndLine: 227,
},
{
Resource: "module.aws-security-groups[\"db1\"]",
Filename: "main.tf",
StartLine: 1,
EndLine: 13,
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, tt.factory().Occurrences())
})
}
}