|
1 | 1 | package terraform
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "fmt"
|
5 | 6 | "strings"
|
6 | 7 | "testing"
|
7 | 8 |
|
8 | 9 | "github.com/stretchr/testify/assert"
|
| 10 | + "github.com/stretchr/testify/require" |
9 | 11 |
|
| 12 | + "github.com/aquasecurity/trivy/internal/testutil" |
10 | 13 | "github.com/aquasecurity/trivy/pkg/iac/providers"
|
11 | 14 | "github.com/aquasecurity/trivy/pkg/iac/rules"
|
12 | 15 | "github.com/aquasecurity/trivy/pkg/iac/scan"
|
| 16 | + "github.com/aquasecurity/trivy/pkg/iac/scanners/options" |
13 | 17 | "github.com/aquasecurity/trivy/pkg/iac/severity"
|
14 | 18 | "github.com/aquasecurity/trivy/pkg/iac/terraform"
|
15 | 19 | )
|
@@ -748,3 +752,56 @@ func Test_IgnoreInlineByAVDID(t *testing.T) {
|
748 | 752 | }
|
749 | 753 | }
|
750 | 754 | }
|
| 755 | + |
| 756 | +func TestIgnoreRemoteTerraformResource(t *testing.T) { |
| 757 | + |
| 758 | + fsys := testutil.CreateFS(t, map[string]string{ |
| 759 | + "main.tf": `module "bucket" { |
| 760 | + source = "git::https://github.com/test/bucket" |
| 761 | +}`, |
| 762 | + ".terraform/modules/modules.json": `{ |
| 763 | + "Modules": [ |
| 764 | + { "Key": "", "Source": "", "Dir": "." }, |
| 765 | + { |
| 766 | + "Key": "bucket", |
| 767 | + "Source": "git::https://github.com/test/bucket", |
| 768 | + "Dir": ".terraform/modules/bucket" |
| 769 | + } |
| 770 | + ] |
| 771 | +} |
| 772 | +`, |
| 773 | + ".terraform/modules/bucket/main.tf": ` |
| 774 | +# trivy:ignore:test-0001 |
| 775 | +resource "aws_s3_bucket" "test" { |
| 776 | + bucket = "" |
| 777 | +} |
| 778 | +`, |
| 779 | + }) |
| 780 | + |
| 781 | + check := `# METADATA |
| 782 | +# title: Test |
| 783 | +# custom: |
| 784 | +# id: test-0001 |
| 785 | +# avdid: test-0001 |
| 786 | +
|
| 787 | +package user.test0001 |
| 788 | +
|
| 789 | +deny[res] { |
| 790 | + bucket := input.aws.s3.buckets[_] |
| 791 | + bucket.name.value == "" |
| 792 | + res := result.new("Empty bucket name!", bucket) |
| 793 | +}` |
| 794 | + |
| 795 | + localScanner := New( |
| 796 | + options.ScannerWithEmbeddedPolicies(false), |
| 797 | + options.ScannerWithEmbeddedLibraries(true), |
| 798 | + options.ScannerWithRegoOnly(true), |
| 799 | + options.ScannerWithPolicyNamespaces("user"), |
| 800 | + options.ScannerWithPolicyReader(strings.NewReader(check)), |
| 801 | + ScannerWithDownloadsAllowed(false), |
| 802 | + ScannerWithSkipCachedModules(true), |
| 803 | + ) |
| 804 | + results, err := localScanner.ScanFS(context.TODO(), fsys, ".") |
| 805 | + require.NoError(t, err) |
| 806 | + assert.Empty(t, results.GetFailed()) |
| 807 | +} |
0 commit comments