Skip to content

Commit

Permalink
refactor(misconf): simplify the retrieval of module annotations (#6528)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikpivkin committed Apr 20, 2024
1 parent 9873cf3 commit 0d7d97d
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions pkg/iac/rego/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/mitchellh/mapstructure"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/rego"
"github.com/samber/lo"

"github.com/aquasecurity/trivy/pkg/iac/framework"
"github.com/aquasecurity/trivy/pkg/iac/providers"
Expand All @@ -16,6 +17,8 @@ import (
iacTypes "github.com/aquasecurity/trivy/pkg/iac/types"
)

const annotationScopePackage = "package"

type StaticMetadata struct {
ID string
AVDID string
Expand Down Expand Up @@ -234,17 +237,9 @@ func NewMetadataRetriever(compiler *ast.Compiler) *MetadataRetriever {
}

func (m *MetadataRetriever) findPackageAnnotations(module *ast.Module) *ast.Annotations {
annotationSet := m.compiler.GetAnnotationSet()
if annotationSet == nil {
return nil
}
for _, annotation := range annotationSet.Flatten() {
if annotation.GetPackage().Path.String() != module.Package.Path.String() || annotation.Annotations.Scope != "package" {
continue
}
return annotation.Annotations
}
return nil
return lo.FindOrElse(module.Annotations, nil, func(a *ast.Annotations) bool {
return a.Scope == annotationScopePackage
})
}

func (m *MetadataRetriever) RetrieveMetadata(ctx context.Context, module *ast.Module, contents ...any) (*StaticMetadata, error) {
Expand Down

0 comments on commit 0d7d97d

Please sign in to comment.