-
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): fix caching of modules in subdirectories (#6814)
- Loading branch information
Showing
10 changed files
with
263 additions
and
33 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
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.