-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(misconf): load full Terraform module (#7925)
Signed-off-by: nikpivkin <nikita.pivkin@smartforce.io>
- Loading branch information
Showing
8 changed files
with
125 additions
and
53 deletions.
There are no files selected for viewing
133 changes: 93 additions & 40 deletions
133
pkg/iac/scanners/terraform/parser/resolvers/cache_integration_test.go
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 |
---|---|---|
@@ -1,119 +1,172 @@ | ||
//go:build unix | ||
|
||
package resolvers_test | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"io/fs" | ||
"net/http" | ||
"net/http/httptest" | ||
"path" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/aquasecurity/trivy/internal/gittest" | ||
"github.com/aquasecurity/trivy/pkg/iac/scanners/terraform/parser/resolvers" | ||
"github.com/aquasecurity/trivy/pkg/log" | ||
) | ||
|
||
type moduleResolver interface { | ||
Resolve(context.Context, fs.FS, resolvers.Options) (fs.FS, string, string, bool, error) | ||
} | ||
|
||
func TestResolveModuleFromCache(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping integration test in short mode") | ||
func testOptions(t *testing.T, source string) resolvers.Options { | ||
return resolvers.Options{ | ||
Source: source, | ||
OriginalSource: source, | ||
Version: "", | ||
OriginalVersion: "", | ||
AllowDownloads: true, | ||
CacheDir: t.TempDir(), | ||
Logger: log.WithPrefix("test"), | ||
} | ||
} | ||
|
||
func newRegistry(repoURL string) *httptest.Server { | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/v1/modules/terraform-aws-modules/s3-bucket/aws/download", func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("X-Terraform-Get", repoURL) | ||
w.WriteHeader(http.StatusNoContent) | ||
}) | ||
|
||
return httptest.NewTLSServer(mux) | ||
} | ||
|
||
func buildGitSource(repoURL string) string { return "git::" + repoURL } | ||
|
||
func TestResolveModuleFromCache(t *testing.T) { | ||
|
||
repo := "terraform-aws-s3-bucket" | ||
gs := gittest.NewServer(t, repo, "testdata/terraform-aws-s3-bucket") | ||
defer gs.Close() | ||
|
||
repoURL := gs.URL + "/" + repo + ".git" | ||
|
||
registry := newRegistry(buildGitSource(repoURL)) | ||
defer registry.Close() | ||
|
||
registryAddress := strings.TrimPrefix(registry.URL, "https://") | ||
|
||
tests := []struct { | ||
name string | ||
opts resolvers.Options | ||
firstResolver moduleResolver | ||
expectedSubdir string | ||
expectedString string | ||
}{ | ||
{ | ||
name: "registry", | ||
opts: resolvers.Options{ | ||
Name: "bucket", | ||
Source: "terraform-aws-modules/s3-bucket/aws", | ||
Version: "4.1.2", | ||
Source: registryAddress + "/terraform-aws-modules/s3-bucket/aws", | ||
Client: &http.Client{ | ||
Transport: &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
}, | ||
}, | ||
}, | ||
firstResolver: resolvers.Registry, | ||
expectedSubdir: ".", | ||
expectedString: "# AWS S3 bucket Terraform module", | ||
}, | ||
{ | ||
name: "registry with subdir", | ||
opts: resolvers.Options{ | ||
Name: "object", | ||
Source: "terraform-aws-modules/s3-bucket/aws//modules/object", | ||
Version: "4.1.2", | ||
Source: registryAddress + "/terraform-aws-modules/s3-bucket/aws//modules/object", | ||
Client: &http.Client{ | ||
Transport: &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
}, | ||
}, | ||
}, | ||
firstResolver: resolvers.Registry, | ||
expectedSubdir: "modules/object", | ||
expectedString: "# S3 bucket object", | ||
}, | ||
{ | ||
name: "remote", | ||
opts: resolvers.Options{ | ||
Name: "bucket", | ||
Source: "git::https://github.com/terraform-aws-modules/terraform-aws-s3-bucket.git?ref=v4.1.2", | ||
Source: buildGitSource(repoURL), | ||
}, | ||
firstResolver: resolvers.Remote, | ||
expectedSubdir: ".", | ||
expectedString: "# AWS S3 bucket Terraform module", | ||
}, | ||
{ | ||
name: "remote with subdir", | ||
opts: resolvers.Options{ | ||
Name: "object", | ||
Source: "git::https://github.com/terraform-aws-modules/terraform-aws-s3-bucket.git//modules/object?ref=v4.1.2", | ||
Source: buildGitSource(repoURL) + "//modules/object", | ||
}, | ||
firstResolver: resolvers.Remote, | ||
expectedSubdir: "modules/object", | ||
expectedString: "# S3 bucket object", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
|
||
tt.opts.AllowDownloads = true | ||
tt.opts.OriginalSource = tt.opts.Source | ||
tt.opts.OriginalVersion = tt.opts.Version | ||
tt.opts.AllowDownloads = true | ||
tt.opts.CacheDir = t.TempDir() | ||
tt.opts.Logger = log.WithPrefix("test") | ||
|
||
fsys, _, dir, _, err := tt.firstResolver.Resolve(context.Background(), nil, tt.opts) | ||
require.NoError(t, err) | ||
assert.Equal(t, tt.expectedSubdir, dir) | ||
|
||
fsys, _, _, applies, err := tt.firstResolver.Resolve(context.Background(), nil, tt.opts) | ||
b, err := fs.ReadFile(fsys, path.Join(dir, "README.md")) | ||
require.NoError(t, err) | ||
assert.True(t, applies) | ||
assert.Equal(t, tt.expectedString, string(b)) | ||
|
||
_, err = fs.Stat(fsys, "main.tf") | ||
_, _, dir, _, err = resolvers.Cache.Resolve(context.Background(), fsys, tt.opts) | ||
require.NoError(t, err) | ||
assert.Equal(t, tt.expectedSubdir, dir) | ||
|
||
_, _, _, applies, err = resolvers.Cache.Resolve(context.Background(), fsys, tt.opts) | ||
b, err = fs.ReadFile(fsys, path.Join(dir, "README.md")) | ||
require.NoError(t, err) | ||
assert.True(t, applies) | ||
assert.Equal(t, tt.expectedString, string(b)) | ||
}) | ||
} | ||
} | ||
|
||
func TestResolveModuleFromCacheWithDifferentSubdir(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping integration test in short mode") | ||
} | ||
repo := "terraform-aws-s3-bucket" | ||
gs := gittest.NewServer(t, repo, "testdata/terraform-aws-s3-bucket") | ||
defer gs.Close() | ||
|
||
cacheDir := t.TempDir() | ||
repoURL := gs.URL + "/" + repo + ".git" | ||
|
||
fsys, _, _, applies, err := resolvers.Remote.Resolve(context.Background(), nil, resolvers.Options{ | ||
Name: "object", | ||
Source: "git::https://github.com/terraform-aws-modules/terraform-aws-s3-bucket.git//modules/object?ref=v4.1.2", | ||
OriginalSource: "git::https://github.com/terraform-aws-modules/terraform-aws-s3-bucket.git//modules/object?ref=v4.1.2", | ||
AllowDownloads: true, | ||
CacheDir: cacheDir, | ||
}) | ||
fsys, _, dir, _, err := resolvers.Remote.Resolve( | ||
context.Background(), nil, | ||
testOptions(t, "git::"+repoURL+"//modules/object"), | ||
) | ||
require.NoError(t, err) | ||
assert.True(t, applies) | ||
|
||
_, err = fs.Stat(fsys, "main.tf") | ||
b, err := fs.ReadFile(fsys, path.Join(dir, "README.md")) | ||
require.NoError(t, err) | ||
assert.Equal(t, "# S3 bucket object", string(b)) | ||
|
||
_, _, _, applies, err = resolvers.Cache.Resolve(context.Background(), nil, resolvers.Options{ | ||
Name: "notification", | ||
Source: "git::https://github.com/terraform-aws-modules/terraform-aws-s3-bucket.git//modules/notification?ref=v4.1.2", | ||
OriginalSource: "git::https://github.com/terraform-aws-modules/terraform-aws-s3-bucket.git//modules/notification?ref=v4.1.2", | ||
CacheDir: cacheDir, | ||
}) | ||
fsys, _, dir, _, err = resolvers.Remote.Resolve( | ||
context.Background(), nil, | ||
testOptions(t, "git::"+repoURL+"//modules/notification"), | ||
) | ||
require.NoError(t, err) | ||
|
||
b, err = fs.ReadFile(fsys, path.Join(dir, "README.md")) | ||
require.NoError(t, err) | ||
assert.True(t, applies) | ||
assert.Equal(t, "# S3 bucket notification", string(b)) | ||
} |
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
1 change: 1 addition & 0 deletions
1
.../scanners/terraform/parser/resolvers/testdata/terraform-aws-s3-bucket/README.md
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 @@ | ||
# AWS S3 bucket Terraform module |
1 change: 1 addition & 0 deletions
1
...arser/resolvers/testdata/terraform-aws-s3-bucket/modules/notification/README.md
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 @@ | ||
# S3 bucket notification |
1 change: 1 addition & 0 deletions
1
...form/parser/resolvers/testdata/terraform-aws-s3-bucket/modules/object/README.md
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 @@ | ||
# S3 bucket object |