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

chore(deps): Bump trivy-checks #7417

Merged
merged 5 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions pkg/iac/rego/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ func (s *Scanner) filterModules(retriever *MetadataRetriever) error {
if err != nil {
return err
}

if !meta.hasAnyFramework(s.frameworks) {
continue
}

if len(meta.InputOptions.Selectors) == 0 {
s.logger.Warn(
"Module has no input selectors - it will be loaded for all inputs!",
Expand Down
60 changes: 43 additions & 17 deletions pkg/iac/rego/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ func NewStaticMetadata(pkgPath string, inputOpt InputOptions) *StaticMetadata {
Description: fmt.Sprintf("Rego module: %s", pkgPath),
Package: pkgPath,
InputOptions: inputOpt,
Frameworks: make(map[framework.Framework][]string),
Frameworks: map[framework.Framework][]string{
framework.Default: {},
},
}
}

func (sm *StaticMetadata) Update(meta map[string]any) error {
func (sm *StaticMetadata) update(meta map[string]any) error {
if sm.Frameworks == nil {
sm.Frameworks = make(map[framework.Framework][]string)
}
Expand Down Expand Up @@ -125,21 +127,31 @@ func (sm *StaticMetadata) Update(meta map[string]any) error {
}

func (sm *StaticMetadata) updateFrameworks(meta map[string]any) error {
if raw, ok := meta["frameworks"]; ok {
frameworks, ok := raw.(map[string]any)
raw, ok := meta["frameworks"]
if !ok {
return nil
}

frameworks, ok := raw.(map[string]any)
if !ok {
return fmt.Errorf("frameworks metadata is not an object, got %T", raw)
}

if len(frameworks) > 0 {
sm.Frameworks = make(map[framework.Framework][]string)
}

for fw, rawIDs := range frameworks {
ids, ok := rawIDs.([]any)
if !ok {
return fmt.Errorf("frameworks metadata is not an object, got %T", raw)
return fmt.Errorf("framework ids is not an array, got %T", rawIDs)
}
for fw, rawIDs := range frameworks {
ids, ok := rawIDs.([]any)
if !ok {
return fmt.Errorf("framework ids is not an array, got %T", rawIDs)
}
fr := framework.Framework(fw)
for _, id := range ids {
if str, ok := id.(string); ok {
sm.Frameworks[fr] = append(sm.Frameworks[fr], str)
}
fr := framework.Framework(fw)
for _, id := range ids {
if str, ok := id.(string); ok {
sm.Frameworks[fr] = append(sm.Frameworks[fr], str)
} else {
sm.Frameworks[fr] = []string{}
}
}
}
Expand All @@ -166,7 +178,7 @@ func (sm *StaticMetadata) FromAnnotations(annotations *ast.Annotations) error {
sm.References = append(sm.References, resource.Ref.String())
}
if custom := annotations.Custom; custom != nil {
if err := sm.Update(custom); err != nil {
if err := sm.update(custom); err != nil {
return err
}
}
Expand Down Expand Up @@ -329,7 +341,7 @@ func (m *MetadataRetriever) RetrieveMetadata(ctx context.Context, module *ast.Mo
return nil, fmt.Errorf("failed to parse metadata: not an object")
}

if err := metadata.Update(meta); err != nil {
if err := metadata.update(meta); err != nil {
return nil, err
}

Expand Down Expand Up @@ -436,3 +448,17 @@ func metadataFromRegoModule(module *ast.Module) (*StaticMetadata, error) {
}
return meta, nil
}

func (m *StaticMetadata) hasAnyFramework(frameworks []framework.Framework) bool {
if len(frameworks) == 0 {
frameworks = []framework.Framework{framework.Default}
}

for _, fr := range frameworks {
if _, exists := m.Frameworks[fr]; exists {
return true
}
}

return false
}
16 changes: 6 additions & 10 deletions pkg/iac/rego/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,9 @@ func Test_UpdateStaticMetadata(t *testing.T) {
Provider: "pr",
Service: "srvc",
Library: false,
Frameworks: map[framework.Framework][]string{
framework.Default: {"dd"},
},
}

require.NoError(t, sm.Update(
require.NoError(t, sm.update(
map[string]any{
"id": "i_n",
"avd_id": "a_n",
Expand Down Expand Up @@ -68,8 +65,7 @@ func Test_UpdateStaticMetadata(t *testing.T) {
Service: "srvc_n",
Library: true,
Frameworks: map[framework.Framework][]string{
framework.Default: {"dd"},
framework.ALL: {"aa"},
framework.ALL: {"aa"},
},
CloudFormation: &scan.EngineMetadata{},
Terraform: &scan.EngineMetadata{},
Expand All @@ -82,7 +78,7 @@ func Test_UpdateStaticMetadata(t *testing.T) {
sm := StaticMetadata{
References: []string{"r"},
}
require.NoError(t, sm.Update(map[string]any{
require.NoError(t, sm.update(map[string]any{
"related_resources": []map[string]any{
{
"ref": "r1_n",
Expand All @@ -107,7 +103,7 @@ func Test_UpdateStaticMetadata(t *testing.T) {
sm := StaticMetadata{
References: []string{"r"},
}
require.NoError(t, sm.Update(map[string]any{
require.NoError(t, sm.update(map[string]any{
"related_resources": []string{"r1_n", "r2_n"},
}))

Expand All @@ -125,7 +121,7 @@ func Test_UpdateStaticMetadata(t *testing.T) {
sm := StaticMetadata{
Deprecated: false,
}
require.NoError(t, sm.Update(map[string]any{
require.NoError(t, sm.update(map[string]any{
"deprecated": true,
}))

Expand All @@ -141,7 +137,7 @@ func Test_UpdateStaticMetadata(t *testing.T) {

t.Run("frameworks is not initialized", func(t *testing.T) {
sm := StaticMetadata{}
err := sm.Update(map[string]any{
err := sm.update(map[string]any{
"frameworks": map[string]any{"all": []any{"a", "b", "c"}},
})
require.NoError(t, err)
Expand Down
4 changes: 3 additions & 1 deletion pkg/iac/scanners/cloudformation/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ deny[res] {
Terraform: (*scan.TerraformCustomCheck)(nil),
},
RegoPackage: "data.builtin.dockerfile.DS006",
Frameworks: make(map[framework.Framework][]string),
Frameworks: map[framework.Framework][]string{
framework.Default: {},
},
}, results.GetFailed()[0].Rule())

failure := results.GetFailed()[0]
Expand Down
8 changes: 6 additions & 2 deletions pkg/iac/scanners/dockerfile/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ USER root
CustomChecks: scan.CustomChecks{
Terraform: (*scan.TerraformCustomCheck)(nil)},
RegoPackage: "data.builtin.dockerfile.DS006",
Frameworks: make(map[framework.Framework][]string),
Frameworks: map[framework.Framework][]string{
framework.Default: {},
},
},
results.GetFailed()[0].Rule(),
)
Expand Down Expand Up @@ -600,7 +602,9 @@ COPY --from=dep /binary /`
CustomChecks: scan.CustomChecks{
Terraform: (*scan.TerraformCustomCheck)(nil)},
RegoPackage: "data.builtin.dockerfile.DS006",
Frameworks: make(map[framework.Framework][]string),
Frameworks: map[framework.Framework][]string{
framework.Default: {},
},
},
results.GetFailed()[0].Rule(),
)
Expand Down
4 changes: 3 additions & 1 deletion pkg/iac/scanners/json/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ deny[res] {
Terraform: (*scan.TerraformCustomCheck)(nil),
},
RegoPackage: "data.builtin.json.lol",
Frameworks: make(map[framework.Framework][]string),
Frameworks: map[framework.Framework][]string{
framework.Default: {},
},
}, results.GetFailed()[0].Rule())
}
8 changes: 6 additions & 2 deletions pkg/iac/scanners/kubernetes/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ deny[res] {
CloudFormation: &scan.EngineMetadata{},
CustomChecks: scan.CustomChecks{Terraform: (*scan.TerraformCustomCheck)(nil)},
RegoPackage: "data.builtin.kubernetes.KSV011",
Frameworks: make(map[framework.Framework][]string),
Frameworks: map[framework.Framework][]string{
framework.Default: {},
},
}, results.GetFailed()[0].Rule())

failure := results.GetFailed()[0]
Expand Down Expand Up @@ -279,7 +281,9 @@ deny[res] {
CloudFormation: &scan.EngineMetadata{},
CustomChecks: scan.CustomChecks{Terraform: (*scan.TerraformCustomCheck)(nil)},
RegoPackage: "data.builtin.kubernetes.KSV011",
Frameworks: make(map[framework.Framework][]string),
Frameworks: map[framework.Framework][]string{
framework.Default: {},
},
}, results.GetFailed()[0].Rule())

failure := results.GetFailed()[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func Test_Scanning_Plan(t *testing.T) {
failedResults = append(failedResults, r)
}
}
assert.Len(t, results, 15)
Copy link
Contributor

Choose a reason for hiding this comment

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

Rego check is added successfully passed when no misconfigs are detected using it, unlike Go check where it was done manually.


assert.Len(t, failedResults, 9)

}
4 changes: 3 additions & 1 deletion pkg/iac/scanners/toml/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ deny[res] {
CustomChecks: scan.CustomChecks{
Terraform: (*scan.TerraformCustomCheck)(nil)},
RegoPackage: "data.builtin.toml.lol",
Frameworks: make(map[framework.Framework][]string),
Frameworks: map[framework.Framework][]string{
framework.Default: {},
},
},
results.GetFailed()[0].Rule(),
)
Expand Down
4 changes: 3 additions & 1 deletion pkg/iac/scanners/yaml/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ deny[res] {
CustomChecks: scan.CustomChecks{
Terraform: (*scan.TerraformCustomCheck)(nil)},
RegoPackage: "data.builtin.yaml.lol",
Frameworks: make(map[framework.Framework][]string),
Frameworks: map[framework.Framework][]string{
framework.Default: {},
},
},
results.GetFailed()[0].Rule(),
)
Expand Down
Loading