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): Update required extensions for terraformplan #4523

Merged
merged 1 commit into from
Jun 1, 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
3 changes: 1 addition & 2 deletions pkg/fanal/analyzer/config/terraformplan/terraformplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ const (
)

var requiredExts = []string{
".tfplan.json",
".tf.json",
".json",
Copy link
Collaborator

Choose a reason for hiding this comment

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

It will match all JSON files. Is it OK?

}

func init() {
Expand Down
38 changes: 38 additions & 0 deletions pkg/fanal/analyzer/config/terraformplan/terraformplan_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package terraformplan

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestConfigAnalyzer_Required(t *testing.T) {
tests := []struct {
name string
filePath string
want bool
}{
{
name: "happy path",
filePath: "/path/to/tfplan.json",
want: true,
},
{
name: "hcl",
filePath: "/path/to/main.hcl",
want: false,
},
{
name: "yaml",
filePath: "deployment.yaml",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := terraformPlanConfigAnalyzer{}
got := a.Required(tt.filePath, nil)
assert.Equal(t, tt.want, got)
})
}
}
24 changes: 21 additions & 3 deletions pkg/misconf/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@ func TestScanner_Scan(t *testing.T) {
}
tests := []struct {
name string
scannerFunc func(filePatterns []string, opt ScannerOption) (*Scanner, error)
fields fields
files []file
wantFilePath string
wantFileType string
}{
{
name: "happy path. Dockerfile",
name: "happy path. Dockerfile",
scannerFunc: NewDockerfileScanner,
fields: fields{
opt: ScannerOption{},
},
Expand All @@ -94,7 +96,8 @@ func TestScanner_Scan(t *testing.T) {
wantFileType: types.Dockerfile,
},
{
name: "happy path. Dockerfile with custom file name",
name: "happy path. Dockerfile with custom file name",
scannerFunc: NewDockerfileScanner,
fields: fields{
filePatterns: []string{"dockerfile:dockerf"},
opt: ScannerOption{},
Expand All @@ -108,6 +111,21 @@ func TestScanner_Scan(t *testing.T) {
wantFilePath: "dockerf",
wantFileType: types.Dockerfile,
},
{
name: "happy path. terraform plan file",
scannerFunc: NewTerraformPlanScanner,
fields: fields{
opt: ScannerOption{},
},
files: []file{
{
path: "main.tfplan.json",
content: []byte(`{"format_version":"1.1","terraform_version":"1.4.6","planned_values":{"root_module":{"resources":[{"address":"aws_s3_bucket.my-bucket","mode":"managed","type":"aws_s3_bucket","name":"my-bucket","provider_name":"registry.terraform.io/hashicorp/aws","schema_version":0,"values":{"bucket":"evil","force_destroy":false,"tags":null,"timeouts":null},"sensitive_values":{"cors_rule":[],"grant":[],"lifecycle_rule":[],"logging":[],"object_lock_configuration":[],"replication_configuration":[],"server_side_encryption_configuration":[],"tags_all":{},"versioning":[],"website":[]}}]}},"resource_changes":[{"address":"aws_s3_bucket.my-bucket","mode":"managed","type":"aws_s3_bucket","name":"my-bucket","provider_name":"registry.terraform.io/hashicorp/aws","change":{"actions":["create"],"before":null,"after":{"bucket":"evil","force_destroy":false,"tags":null,"timeouts":null},"after_unknown":{"acceleration_status":true,"acl":true,"arn":true,"bucket_domain_name":true,"bucket_prefix":true,"bucket_regional_domain_name":true,"cors_rule":true,"grant":true,"hosted_zone_id":true,"id":true,"lifecycle_rule":true,"logging":true,"object_lock_configuration":true,"object_lock_enabled":true,"policy":true,"region":true,"replication_configuration":true,"request_payer":true,"server_side_encryption_configuration":true,"tags_all":true,"versioning":true,"website":true,"website_domain":true,"website_endpoint":true},"before_sensitive":false,"after_sensitive":{"cors_rule":[],"grant":[],"lifecycle_rule":[],"logging":[],"object_lock_configuration":[],"replication_configuration":[],"server_side_encryption_configuration":[],"tags_all":{},"versioning":[],"website":[]}}}],"configuration":{"provider_config":{"aws":{"name":"aws","full_name":"registry.terraform.io/hashicorp/aws","expressions":{"profile":{"constant_value":"foo-bar-123123123"},"region":{"constant_value":"us-west-1"}}}},"root_module":{"resources":[{"address":"aws_s3_bucket.my-bucket","mode":"managed","type":"aws_s3_bucket","name":"my-bucket","provider_config_key":"aws","expressions":{"bucket":{"constant_value":"evil"}},"schema_version":0}]}}}`),
},
},
wantFilePath: "main.tf",
wantFileType: types.TerraformPlan,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -118,7 +136,7 @@ func TestScanner_Scan(t *testing.T) {
require.NoError(t, err)
}

s, err := NewDockerfileScanner(tt.fields.filePatterns, tt.fields.opt)
s, err := tt.scannerFunc(tt.fields.filePatterns, tt.fields.opt)
require.NoError(t, err)

misconfs, err := s.Scan(context.Background(), fsys)
Expand Down