-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(terraform): do not scan local modules as root modules (#34)
* fix(terraform): do not scan local modules as root modules * chore: bump defsec * test: skip integration tests in short mode --------- Co-authored-by: simar7 <1254783+simar7@users.noreply.github.com>
- Loading branch information
Showing
8 changed files
with
340 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package parser | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/aquasecurity/trivy-iac/test/testutil" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_DefaultRegistry(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping integration test in short mode") | ||
} | ||
fs := testutil.CreateFS(t, map[string]string{ | ||
"code/test.tf": ` | ||
module "registry" { | ||
source = "terraform-aws-modules/vpc/aws" | ||
} | ||
`, | ||
}) | ||
|
||
parser := New(fs, "", OptionStopOnHCLError(true)) | ||
if err := parser.ParseFS(context.TODO(), "code"); err != nil { | ||
t.Fatal(err) | ||
} | ||
modules, _, err := parser.EvaluateAll(context.TODO()) | ||
require.NoError(t, err) | ||
require.Len(t, modules, 2) | ||
} | ||
|
||
func Test_SpecificRegistry(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping integration test in short mode") | ||
} | ||
fs := testutil.CreateFS(t, map[string]string{ | ||
"code/test.tf": ` | ||
module "registry" { | ||
source = "registry.terraform.io/terraform-aws-modules/vpc/aws" | ||
} | ||
`, | ||
}) | ||
|
||
parser := New(fs, "", OptionStopOnHCLError(true)) | ||
if err := parser.ParseFS(context.TODO(), "code"); err != nil { | ||
t.Fatal(err) | ||
} | ||
modules, _, err := parser.EvaluateAll(context.TODO()) | ||
require.NoError(t, err) | ||
require.Len(t, modules, 2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package terraform | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aquasecurity/defsec/pkg/scanners/options" | ||
"github.com/aquasecurity/trivy-iac/test/testutil" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_ScanRemoteModule(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping integration test in short mode") | ||
} | ||
fs := testutil.CreateFS(t, map[string]string{ | ||
"main.tf": ` | ||
module "s3_bucket" { | ||
source = "terraform-aws-modules/s3-bucket/aws" | ||
bucket = "my-s3-bucket" | ||
} | ||
`, | ||
"/rules/bucket_name.rego": ` | ||
# METADATA | ||
# schemas: | ||
# - input: schema.input | ||
# custom: | ||
# avd_id: AVD-AWS-0001 | ||
# input: | ||
# selector: | ||
# - type: cloud | ||
# subtypes: | ||
# - service: s3 | ||
# provider: aws | ||
package defsec.test.aws1 | ||
deny[res] { | ||
bucket := input.aws.s3.buckets[_] | ||
bucket.name.value == "" | ||
res := result.new("The name of the bucket must not be empty", bucket) | ||
}`, | ||
}) | ||
|
||
debugLog := bytes.NewBuffer([]byte{}) | ||
|
||
scanner := New( | ||
options.ScannerWithDebug(debugLog), | ||
options.ScannerWithPolicyFilesystem(fs), | ||
options.ScannerWithPolicyDirs("rules"), | ||
options.ScannerWithEmbeddedPolicies(false), | ||
options.ScannerWithEmbeddedLibraries(false), | ||
options.ScannerWithRegoOnly(true), | ||
ScannerWithAllDirectories(true), | ||
) | ||
|
||
results, err := scanner.ScanFS(context.TODO(), fs, ".") | ||
require.NoError(t, err) | ||
|
||
assert.Len(t, results.GetPassed(), 1) | ||
|
||
if t.Failed() { | ||
fmt.Printf("Debug logs:\n%s\n", debugLog.String()) | ||
} | ||
} | ||
|
||
func Test_ScanChildUseRemoteModule(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping integration test in short mode") | ||
} | ||
fs := testutil.CreateFS(t, map[string]string{ | ||
"main.tf": ` | ||
module "this" { | ||
source = "./modules/s3" | ||
bucket = "my-s3-bucket" | ||
} | ||
`, | ||
"modules/s3/main.tf": ` | ||
variable "bucket" { | ||
type = string | ||
} | ||
module "s3_bucket" { | ||
source = "github.com/terraform-aws-modules/terraform-aws-s3-bucket?ref=v3.15.1" | ||
bucket = var.bucket | ||
} | ||
`, | ||
"rules/bucket_name.rego": ` | ||
# METADATA | ||
# schemas: | ||
# - input: schema.input | ||
# custom: | ||
# avd_id: AVD-AWS-0001 | ||
# input: | ||
# selector: | ||
# - type: cloud | ||
# subtypes: | ||
# - service: s3 | ||
# provider: aws | ||
package defsec.test.aws1 | ||
deny[res] { | ||
bucket := input.aws.s3.buckets[_] | ||
bucket.name.value == "" | ||
res := result.new("The name of the bucket must not be empty", bucket) | ||
}`, | ||
}) | ||
|
||
debugLog := bytes.NewBuffer([]byte{}) | ||
|
||
scanner := New( | ||
options.ScannerWithDebug(debugLog), | ||
options.ScannerWithPolicyFilesystem(fs), | ||
options.ScannerWithPolicyDirs("rules"), | ||
options.ScannerWithEmbeddedPolicies(false), | ||
options.ScannerWithEmbeddedLibraries(false), | ||
options.ScannerWithRegoOnly(true), | ||
ScannerWithAllDirectories(true), | ||
) | ||
|
||
results, err := scanner.ScanFS(context.TODO(), fs, ".") | ||
require.NoError(t, err) | ||
|
||
assert.Len(t, results.GetPassed(), 1) | ||
|
||
if t.Failed() { | ||
fmt.Printf("Debug logs:\n%s\n", debugLog.String()) | ||
} | ||
} |
Oops, something went wrong.