-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
terraform.go
46 lines (37 loc) · 1.18 KB
/
terraform.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package terraform
import (
"os"
"path/filepath"
"golang.org/x/exp/slices"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer/config"
"github.com/aquasecurity/trivy/pkg/misconf"
)
const (
analyzerType = analyzer.TypeTerraform
version = 1
)
var requiredExts = []string{
".tf",
".tf.json",
".tfvars",
}
func init() {
analyzer.RegisterPostAnalyzer(analyzerType, newTerraformConfigAnalyzer)
}
// terraformConfigAnalyzer is an analyzer for detecting misconfigurations in Terraform files.
// It embeds config.Analyzer so it can implement analyzer.PostAnalyzer.
type terraformConfigAnalyzer struct {
*config.Analyzer
}
func newTerraformConfigAnalyzer(opts analyzer.AnalyzerOptions) (analyzer.PostAnalyzer, error) {
a, err := config.NewAnalyzer(analyzerType, version, misconf.NewTerraformScanner, opts)
if err != nil {
return nil, err
}
return &terraformConfigAnalyzer{Analyzer: a}, nil
}
// 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))
}