-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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): fix caching of modules in subdirectories #6814
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
114 changes: 114 additions & 0 deletions
114
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 |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package resolvers_test | ||
|
||
import ( | ||
"context" | ||
"io/fs" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/aquasecurity/trivy/pkg/iac/scanners/terraform/parser/resolvers" | ||
) | ||
|
||
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") | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
opts resolvers.Options | ||
firstResolver moduleResolver | ||
}{ | ||
{ | ||
name: "registry", | ||
opts: resolvers.Options{ | ||
Name: "bucket", | ||
Source: "terraform-aws-modules/s3-bucket/aws", | ||
Version: "4.1.2", | ||
}, | ||
firstResolver: resolvers.Registry, | ||
}, | ||
{ | ||
name: "registry with subdir", | ||
opts: resolvers.Options{ | ||
Name: "object", | ||
Source: "terraform-aws-modules/s3-bucket/aws//modules/object", | ||
Version: "4.1.2", | ||
}, | ||
firstResolver: resolvers.Registry, | ||
}, | ||
{ | ||
name: "remote", | ||
opts: resolvers.Options{ | ||
Name: "bucket", | ||
Source: "git::https://github.com/terraform-aws-modules/terraform-aws-s3-bucket.git?ref=v4.1.2", | ||
}, | ||
firstResolver: resolvers.Remote, | ||
}, | ||
{ | ||
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", | ||
}, | ||
firstResolver: resolvers.Remote, | ||
}, | ||
} | ||
|
||
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.CacheDir = t.TempDir() | ||
|
||
fsys, _, _, applies, err := tt.firstResolver.Resolve(context.Background(), nil, tt.opts) | ||
require.NoError(t, err) | ||
assert.True(t, applies) | ||
|
||
_, err = fs.Stat(fsys, "main.tf") | ||
require.NoError(t, err) | ||
|
||
_, _, _, applies, err = resolvers.Cache.Resolve(context.Background(), fsys, tt.opts) | ||
require.NoError(t, err) | ||
assert.True(t, applies) | ||
}) | ||
} | ||
} | ||
|
||
func TestResolveModuleFromCacheWithDifferentSubdir(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping integration test in short mode") | ||
} | ||
|
||
cacheDir := t.TempDir() | ||
|
||
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, | ||
}) | ||
require.NoError(t, err) | ||
assert.True(t, applies) | ||
|
||
_, err = fs.Stat(fsys, "main.tf") | ||
require.NoError(t, err) | ||
|
||
_, _, _, 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, | ||
}) | ||
require.NoError(t, err) | ||
assert.True(t, applies) | ||
} |
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,36 @@ | ||
package resolvers | ||
|
||
import "strings" | ||
|
||
func removeSubdirFromSource(src string) string { | ||
stop := len(src) | ||
if idx := strings.Index(src, "?"); idx > -1 { | ||
stop = idx | ||
} | ||
|
||
// Calculate an offset to avoid accidentally marking the scheme | ||
// as the dir. | ||
var offset int | ||
if idx := strings.Index(src[:stop], "://"); idx > -1 { | ||
offset = idx + 3 | ||
} | ||
|
||
// First see if we even have an explicit subdir | ||
idx := strings.Index(src[offset:stop], "//") | ||
if idx == -1 { | ||
return src | ||
} | ||
|
||
idx += offset | ||
subdir := src[idx+2:] | ||
src = src[:idx] | ||
|
||
// Next, check if we have query parameters and push them onto the | ||
// URL. | ||
if idx = strings.Index(subdir, "?"); idx > -1 { | ||
query := subdir[idx:] | ||
src += query | ||
} | ||
|
||
return src | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to keep
hash
? Can we inline it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
md5.Sum
returns an array of bytes,hex.EncodeToString
takes a slice of bytes. In order to convert an array to a slice, it must be addressable, so an intermediate variable is needed.