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(misconf): do not filter Terraform plan JSON by name #7406

Merged
merged 2 commits into from
Aug 29, 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
22 changes: 11 additions & 11 deletions docs/docs/coverage/iac/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ Trivy scans Infrastructure as Code (IaC) files for

## Supported configurations

| Config type | File patterns |
|-------------------------------------|-----------------------------------------------|
| [Kubernetes](kubernetes.md) | \*.yml, \*.yaml, \*.json |
| [Docker](docker.md) | Dockerfile, Containerfile |
| [Terraform](terraform.md) | \*.tf, \*.tf.json, \*.tfvars |
| [Terraform Plan](terraform.md) | tfplan, \*.tfplan, \*.tfplan.json, \*.tf.json |
| [CloudFormation](cloudformation.md) | \*.yml, \*.yaml, \*.json |
| [Azure ARM Template](azure-arm.md) | \*.json |
| [Helm](helm.md) | \*.yaml, \*.tpl, \*.tar.gz, etc. |
| [YAML][json-and-yaml] | \*.yaml, \*.yml |
| [JSON][json-and-yaml] | \*.json |
| Config type | File patterns |
|-------------------------------------|----------------------------------|
| [Kubernetes](kubernetes.md) | \*.yml, \*.yaml, \*.json |
| [Docker](docker.md) | Dockerfile, Containerfile |
| [Terraform](terraform.md) | \*.tf, \*.tf.json, \*.tfvars |
| [Terraform Plan](terraform.md) | tfplan, \*.tfplan, \*.json |
| [CloudFormation](cloudformation.md) | \*.yml, \*.yaml, \*.json |
| [Azure ARM Template](azure-arm.md) | \*.json |
| [Helm](helm.md) | \*.yaml, \*.tpl, \*.tar.gz, etc. |
| [YAML][json-and-yaml] | \*.yaml, \*.yml |
| [JSON][json-and-yaml] | \*.json |

[misconf]: ../../scanner/misconfiguration/index.md
[secret]: ../../scanner/secret.md
Expand Down
16 changes: 10 additions & 6 deletions pkg/iac/detection/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,17 @@ func init() {

contents := make(map[string]any)
err := json.NewDecoder(r).Decode(&contents)
if err == nil {
if _, ok := contents["terraform_version"]; ok {
_, stillOk := contents["format_version"]
return stillOk
if err != nil {
return false
}

for _, k := range []string{"terraform_version", "format_version"} {
if _, ok := contents[k]; !ok {
return false
}
}

return true
}
return false
}
Expand Down Expand Up @@ -150,8 +155,7 @@ func init() {
return false
}

return (sniff.Parameters != nil && len(sniff.Parameters) > 0) ||
(sniff.Resources != nil && len(sniff.Resources) > 0)
return len(sniff.Parameters) > 0 || len(sniff.Resources) > 0
simar7 marked this conversation as resolved.
Show resolved Hide resolved
}

matchers[FileTypeDockerfile] = func(name string, _ io.ReadSeeker) bool {
Expand Down
38 changes: 19 additions & 19 deletions pkg/iac/scanners/terraformplan/tfjson/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"io"
"io/fs"

"github.com/bmatcuk/doublestar/v4"

"github.com/aquasecurity/trivy/pkg/iac/framework"
"github.com/aquasecurity/trivy/pkg/iac/scan"
"github.com/aquasecurity/trivy/pkg/iac/scanners/options"
Expand All @@ -17,11 +15,6 @@ import (
"github.com/aquasecurity/trivy/pkg/log"
)

var tfPlanExts = []string{
"**/*tfplan.json",
"**/*tf.json",
}

type Scanner struct {
parser *parser.Parser
logger *log.Logger
Expand Down Expand Up @@ -93,25 +86,32 @@ func (s *Scanner) Name() string {
return "Terraform Plan JSON"
}

func (s *Scanner) ScanFS(ctx context.Context, inputFS fs.FS, dir string) (scan.Results, error) {
var filesFound []string
func (s *Scanner) ScanFS(ctx context.Context, fsys fs.FS, dir string) (scan.Results, error) {

var results scan.Results

for _, ext := range tfPlanExts {
files, err := doublestar.Glob(inputFS, ext, doublestar.WithFilesOnly())
walkFn := func(path string, d fs.DirEntry, err error) error {
if err != nil {
return nil, fmt.Errorf("unable to scan for terraform plan files: %w", err)
return err
}
filesFound = append(filesFound, files...)
}

var results scan.Results
for _, f := range filesFound {
res, err := s.ScanFile(f, inputFS)
if d.IsDir() {
return nil
}

res, err := s.ScanFile(path, fsys)
if err != nil {
return nil, err
return fmt.Errorf("failed to scan %s: %w", path, err)
}

results = append(results, res...)
return nil
}

if err := fs.WalkDir(fsys, dir, walkFn); err != nil {
return nil, err
}

return results, nil
}

Expand Down Expand Up @@ -148,7 +148,7 @@ func (s *Scanner) Scan(reader io.Reader) (scan.Results, error) {

planFS, err := planFile.ToFS()
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to convert plan to FS: %w", err)
}

scanner := terraformScanner.New(s.options...)
Expand Down