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

feat(misconf): Support relative paths in terraform config #3991

Closed
wants to merge 8 commits into from
Closed
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
2 changes: 1 addition & 1 deletion pkg/fanal/analyzer/config/terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (a terraformConfigAnalyzer) Analyze(_ context.Context, input analyzer.Analy
types.MisconfPostHandler: {
{
Type: types.Terraform,
Path: input.FilePath,
Path: filepath.Join(input.Dir, input.FilePath),
Content: b,
},
},
Expand Down
3 changes: 2 additions & 1 deletion pkg/fanal/analyzer/config/terraform/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package terraform
import (
"bytes"
"context"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -30,7 +31,7 @@ func TestConfigAnalyzer_Analyze(t *testing.T) {
types.MisconfPostHandler: {
{
Type: types.Terraform,
Path: "main.tf",
Path: filepath.Clean("path/to/main.tf"),
Content: []byte{},
},
},
Expand Down
264 changes: 232 additions & 32 deletions pkg/fanal/artifact/local/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package local
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -350,6 +353,20 @@ func TestBuildPathsToSkip(t *testing.T) {
}
}

func getAbsCleanPath(path string) string {
p, _ := filepath.Abs(path)
switch runtime.GOOS {
case "windows":
if volume := filepath.VolumeName(p); volume != "" {
p = strings.TrimPrefix(filepath.ToSlash(p), volume+"/")
return filepath.FromSlash(p)
}
return strings.TrimPrefix(filepath.Clean(p), fmt.Sprintf("%c", os.PathSeparator))
default:
return strings.TrimPrefix(filepath.Clean(p), fmt.Sprintf("%c", os.PathSeparator))
}
}

func TestTerraformMisconfigurationScan(t *testing.T) {
type fields struct {
dir string
Expand Down Expand Up @@ -381,7 +398,7 @@ func TestTerraformMisconfigurationScan(t *testing.T) {
Misconfigurations: []types.Misconfiguration{
{
FileType: "terraform",
FilePath: "main.tf",
FilePath: getAbsCleanPath("testdata/misconfig/terraform/single-failure/src/main.tf"),
Failures: types.MisconfResults{
{
Namespace: "user.something",
Expand Down Expand Up @@ -415,10 +432,6 @@ func TestTerraformMisconfigurationScan(t *testing.T) {
want: types.ArtifactReference{
Name: "testdata/misconfig/terraform/single-failure/src",
Type: types.ArtifactFilesystem,
ID: "sha256:b3ae72efb468a0e17551fa4067c1f9d9dff9a1e520b9f8191f48829ab6e8356d",
BlobIDs: []string{
"sha256:b3ae72efb468a0e17551fa4067c1f9d9dff9a1e520b9f8191f48829ab6e8356d",
},
Comment on lines -418 to -421
Copy link
Member Author

@simar7 simar7 Apr 14, 2023

Choose a reason for hiding this comment

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

note: I've removed these assertions as the key is calculated over everything (including the file paths in the blob), which results in the key being different on different OS's (unix vs windows).

Arguably the content is more important in these tests, which is still asserted.

},
},
{
Expand All @@ -441,7 +454,7 @@ func TestTerraformMisconfigurationScan(t *testing.T) {
Misconfigurations: []types.Misconfiguration{
{
FileType: "terraform",
FilePath: "main.tf",
FilePath: getAbsCleanPath("testdata/misconfig/terraform/multiple-failures/src/more.tf"),
Failures: types.MisconfResults{
{
Namespace: "user.something",
Expand All @@ -458,13 +471,19 @@ func TestTerraformMisconfigurationScan(t *testing.T) {
References: []string{"https://trivy.dev/"},
},
CauseMetadata: types.CauseMetadata{
Resource: "aws_s3_bucket.one",
Resource: "aws_s3_bucket.three",
Provider: "Generic",
Service: "general",
StartLine: 1,
EndLine: 3,
},
},
},
},
{
FileType: "terraform",
FilePath: getAbsCleanPath("testdata/misconfig/terraform/multiple-failures/src/main.tf"),
Failures: types.MisconfResults{
{
Namespace: "user.something",
Query: "data.user.something.deny",
Expand All @@ -480,19 +499,13 @@ func TestTerraformMisconfigurationScan(t *testing.T) {
References: []string{"https://trivy.dev/"},
},
CauseMetadata: types.CauseMetadata{
Resource: "aws_s3_bucket.two",
Resource: "aws_s3_bucket.one",
Provider: "Generic",
Service: "general",
StartLine: 5,
EndLine: 7,
StartLine: 1,
EndLine: 3,
},
},
},
},
{
FileType: "terraform",
FilePath: "more.tf",
Failures: types.MisconfResults{
{
Namespace: "user.something",
Query: "data.user.something.deny",
Expand All @@ -508,11 +521,11 @@ func TestTerraformMisconfigurationScan(t *testing.T) {
References: []string{"https://trivy.dev/"},
},
CauseMetadata: types.CauseMetadata{
Resource: "aws_s3_bucket.three",
Resource: "aws_s3_bucket.two",
Provider: "Generic",
Service: "general",
StartLine: 1,
EndLine: 3,
StartLine: 5,
EndLine: 7,
},
},
},
Expand All @@ -525,10 +538,6 @@ func TestTerraformMisconfigurationScan(t *testing.T) {
want: types.ArtifactReference{
Name: "testdata/misconfig/terraform/multiple-failures/src",
Type: types.ArtifactFilesystem,
ID: "sha256:340197e6c02b644e4d1310647e5b2503c224caeaeb5be01187467b71827614ce",
BlobIDs: []string{
"sha256:340197e6c02b644e4d1310647e5b2503c224caeaeb5be01187467b71827614ce",
},
},
},
{
Expand All @@ -555,10 +564,6 @@ func TestTerraformMisconfigurationScan(t *testing.T) {
want: types.ArtifactReference{
Name: "testdata/misconfig/terraform/no-results/src",
Type: types.ArtifactFilesystem,
ID: "sha256:1694d46ecb8151fde496faca988441a78c4fe40ddb3049f4f59467282ab9853e",
BlobIDs: []string{
"sha256:1694d46ecb8151fde496faca988441a78c4fe40ddb3049f4f59467282ab9853e",
},
},
},
{
Expand All @@ -581,7 +586,7 @@ func TestTerraformMisconfigurationScan(t *testing.T) {
Misconfigurations: []types.Misconfiguration{
{
FileType: "terraform",
FilePath: ".",
FilePath: getAbsCleanPath("testdata/misconfig/terraform/passed/src"),
Successes: types.MisconfResults{
{
Namespace: "user.something",
Expand Down Expand Up @@ -611,10 +616,84 @@ func TestTerraformMisconfigurationScan(t *testing.T) {
want: types.ArtifactReference{
Name: "testdata/misconfig/terraform/passed/src",
Type: types.ArtifactFilesystem,
ID: "sha256:ee80e0b4d07abe98c88119a1dc3d5cfd3b5f3cfda3b52cf3e566d7abc15f072b",
BlobIDs: []string{
"sha256:ee80e0b4d07abe98c88119a1dc3d5cfd3b5f3cfda3b52cf3e566d7abc15f072b",
},
},
{
name: "multiple failures busted relative paths",
fields: fields{
dir: "./testdata/misconfig/terraform/busted-relative-paths/src/child/main.tf",
},
artifactOpt: artifact.Option{
MisconfScannerOption: config.ScannerOption{
RegoOnly: true,
Namespaces: []string{"user"},
PolicyPaths: []string{"./testdata/misconfig/terraform/busted-relative-paths/rego"},
},
},
putBlobExpectation: cache.ArtifactCachePutBlobExpectation{
Args: cache.ArtifactCachePutBlobArgs{
BlobIDAnything: true,
BlobInfo: types.BlobInfo{
SchemaVersion: 2,
Misconfigurations: []types.Misconfiguration{
{
FileType: "terraform",
FilePath: getAbsCleanPath("testdata/misconfig/terraform/busted-relative-paths/src/child/main.tf"),
Failures: types.MisconfResults{
{
Namespace: "user.something",
Query: "data.user.something.deny",
Message: "No buckets allowed!",
PolicyMetadata: types.PolicyMetadata{
ID: "TEST001",
AVDID: "AVD-TEST-0001",
Type: "Terraform Security Check",
Title: "Test policy",
Description: "This is a test policy.",
Severity: "LOW",
RecommendedActions: "Have a cup of tea.",
References: []string{"https://trivy.dev/"},
},
CauseMetadata: types.CauseMetadata{
Resource: "aws_s3_bucket.one",
Provider: "Generic",
Service: "general",
StartLine: 1,
EndLine: 3,
},
},
{
Namespace: "user.something",
Query: "data.user.something.deny",
Message: "No buckets allowed!",
PolicyMetadata: types.PolicyMetadata{
ID: "TEST001",
AVDID: "AVD-TEST-0001",
Type: "Terraform Security Check",
Title: "Test policy",
Description: "This is a test policy.",
Severity: "LOW",
RecommendedActions: "Have a cup of tea.",
References: []string{"https://trivy.dev/"},
},
CauseMetadata: types.CauseMetadata{
Resource: "aws_s3_bucket.two",
Provider: "Generic",
Service: "general",
StartLine: 5,
EndLine: 7,
},
},
},
},
},
},
},
Returns: cache.ArtifactCachePutBlobReturns{},
},
want: types.ArtifactReference{
Name: "testdata/misconfig/terraform/busted-relative-paths/src/child/main.tf",
Type: types.ArtifactFilesystem,
},
},
}
Expand All @@ -630,7 +709,10 @@ func TestTerraformMisconfigurationScan(t *testing.T) {

got, err := a.Inspect(context.Background())
require.NoError(t, err)
assert.Equal(t, tt.want, got)
require.NotNil(t, got)

assert.Contains(t, got.Name, tt.want.Name)
assert.Contains(t, got.Type, tt.want.Type)
})
}
}
Expand Down Expand Up @@ -1645,3 +1727,121 @@ func TestAzureARMMisconfigurationScan(t *testing.T) {
})
}
}

func TestMixedConfigurationScan(t *testing.T) {
type fields struct {
dir string
}
tests := []struct {
name string
fields fields
putBlobExpectation cache.ArtifactCachePutBlobExpectation
artifactOpt artifact.Option
want types.ArtifactReference
}{
{
name: "single failure each within terraform and cloudformation",
fields: fields{
dir: "./testdata/misconfig/mixed/src",
},
artifactOpt: artifact.Option{
MisconfScannerOption: config.ScannerOption{
RegoOnly: true,
Namespaces: []string{"user"},
PolicyPaths: []string{"./testdata/misconfig/mixed/rego"},
},
},
putBlobExpectation: cache.ArtifactCachePutBlobExpectation{
Args: cache.ArtifactCachePutBlobArgs{
BlobIDAnything: true,
BlobInfo: types.BlobInfo{
SchemaVersion: 2,
Misconfigurations: []types.Misconfiguration{
{
FileType: "cloudformation",
FilePath: "main.yaml",
Failures: types.MisconfResults{
{
Namespace: "user.something",
Query: "data.user.something.deny",
Message: "No buckets allowed!",
PolicyMetadata: types.PolicyMetadata{
ID: "TEST001",
AVDID: "AVD-TEST-0001",
Type: "CloudFormation Security Check",
Title: "Test policy",
Description: "This is a test policy.",
Severity: "LOW",
RecommendedActions: "Have a cup of tea.",
References: []string{"https://trivy.dev/"},
},
CauseMetadata: types.CauseMetadata{
Resource: "main.yaml:3-6",
Provider: "Generic",
Service: "general",
StartLine: 3,
EndLine: 6,
},
},
},
},
{
FileType: "terraform",
FilePath: getAbsCleanPath("testdata/misconfig/mixed/src/main.tf"),
Failures: types.MisconfResults{
{
Namespace: "user.something",
Query: "data.user.something.deny",
Message: "No buckets allowed!",
PolicyMetadata: types.PolicyMetadata{
ID: "TEST001",
AVDID: "AVD-TEST-0001",
Type: "Terraform Security Check",
Title: "Test policy",
Description: "This is a test policy.",
Severity: "LOW",
RecommendedActions: "Have a cup of tea.",
References: []string{"https://trivy.dev/"},
},
CauseMetadata: types.CauseMetadata{
Resource: "aws_s3_bucket.asd",
Provider: "Generic",
Service: "general",
StartLine: 1,
EndLine: 3,
},
},
},
},
},
},
},
Returns: cache.ArtifactCachePutBlobReturns{},
},
want: types.ArtifactReference{
Name: "testdata/misconfig/mixed/src",
Type: types.ArtifactFilesystem,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := new(cache.MockArtifactCache)
c.ApplyPutBlobExpectation(tt.putBlobExpectation)
tt.artifactOpt.DisabledHandlers = []types.HandlerType{
types.SystemFileFilteringPostHandler,
}
a, err := NewArtifact(tt.fields.dir, c, tt.artifactOpt)
require.NoError(t, err)

got, err := a.Inspect(context.Background())
require.NoError(t, err)
require.NotNil(t, got)

assert.Equal(t, tt.want.Name, got.Name)
assert.Equal(t, tt.want.Type, got.Type)
})
}

}
Loading