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

fix(terraform): improve detection of terraform files #4984

Merged
merged 3 commits into from
Aug 31, 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
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