Skip to content

Commit

Permalink
fix(terraform): improve detection of terraform files (#4984)
Browse files Browse the repository at this point in the history
* fix(terraform): improve detection of terraform files

* update defsec

---------

Co-authored-by: Simar <simar@linux.com>
Co-authored-by: knqyf263 <knqyf263@gmail.com>
  • Loading branch information
3 people committed Aug 31, 2023
1 parent 0c8919e commit 4547e27
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
12 changes: 2 additions & 10 deletions pkg/fanal/analyzer/config/terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package terraform

import (
"os"
"path/filepath"

"golang.org/x/exp/slices"

"github.com/aquasecurity/defsec/pkg/detection"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer/config"
"github.com/aquasecurity/trivy/pkg/misconf"
Expand All @@ -16,12 +14,6 @@ const (
version = 1
)

var requiredExts = []string{
".tf",
".tf.json",
".tfvars",
}

func init() {
analyzer.RegisterPostAnalyzer(analyzerType, newTerraformConfigAnalyzer)
}
Expand All @@ -42,5 +34,5 @@ func newTerraformConfigAnalyzer(opts analyzer.AnalyzerOptions) (analyzer.PostAna

// Required overrides config.Analyzer.Required() and checks if the given file is a Terraform file.
func (*terraformConfigAnalyzer) Required(filePath string, _ os.FileInfo) bool {
return slices.Contains(requiredExts, filepath.Ext(filePath))
return detection.IsTerraformFile(filePath)
}
17 changes: 16 additions & 1 deletion pkg/fanal/analyzer/config/terraform/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,25 @@ func TestConfigAnalyzer_Required(t *testing.T) {
want bool
}{
{
name: "happy path",
name: "tf",
filePath: "/path/to/main.tf",
want: true,
},
{
name: "tf.json",
filePath: "/path/to/main.tf.json",
want: true,
},
{
name: "tfvars",
filePath: "/path/to/some.tfvars",
want: true,
},
{
name: "json",
filePath: "/path/to/some.json",
want: false,
},
{
name: "hcl",
filePath: "/path/to/main.hcl",
Expand Down

0 comments on commit 4547e27

Please sign in to comment.