Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

feat(terraform): add an option to skip cached modules #55

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/scanners/terraform/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ func ScannerWithDownloadsAllowed(allowed bool) options.ScannerOption {
}
}

func ScannerWithSkipCachedModules(b bool) options.ScannerOption {
return func(s options.ConfigurableScanner) {
if tf, ok := s.(ConfigurableTerraformScanner); ok {
tf.AddParserOptions(parser.OptionWithDownloads(b))
}
}
}

func ScannerWithConfigsFileSystem(fsys fs.FS) options.ScannerOption {
return func(s options.ConfigurableScanner) {
if tf, ok := s.(ConfigurableTerraformScanner); ok {
Expand Down
26 changes: 14 additions & 12 deletions pkg/scanners/terraform/parser/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@ const (
)

type evaluator struct {
filesystem fs.FS
ctx *tfcontext.Context
blocks terraform.Blocks
inputVars map[string]cty.Value
moduleMetadata *modulesMetadata
projectRootPath string // root of the current scan
modulePath string
moduleName string
ignores terraform.Ignores
parentParser *Parser
debug debug.Logger
allowDownloads bool
filesystem fs.FS
ctx *tfcontext.Context
blocks terraform.Blocks
inputVars map[string]cty.Value
moduleMetadata *modulesMetadata
projectRootPath string // root of the current scan
modulePath string
moduleName string
ignores terraform.Ignores
parentParser *Parser
debug debug.Logger
allowDownloads bool
skipCachedModules bool
}

func newEvaluator(
Expand All @@ -53,6 +54,7 @@ func newEvaluator(
ignores []terraform.Ignore,
logger debug.Logger,
allowDownloads bool,
skipCachedModules bool,
) *evaluator {

// create a context to store variables and make functions available
Expand Down
3 changes: 2 additions & 1 deletion pkg/scanners/terraform/parser/load_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,9 @@ func (e *evaluator) loadExternalModule(ctx context.Context, b *terraform.Block,
ModulePath: e.modulePath,
DebugLogger: e.debug.Extend("resolver"),
AllowDownloads: e.allowDownloads,
AllowCache: e.allowDownloads,
SkipCache: e.skipCachedModules,
}

filesystem, prefix, path, err := resolveModule(ctx, e.filesystem, opt)
if err != nil {
return nil, err
Expand Down
9 changes: 9 additions & 0 deletions pkg/scanners/terraform/parser/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type ConfigurableTerraformParser interface {
SetStopOnHCLError(bool)
SetWorkspaceName(string)
SetAllowDownloads(bool)
SetSkipCachedModules(bool)
SetConfigsFS(fsys fs.FS)
}

Expand Down Expand Up @@ -49,6 +50,14 @@ func OptionWithDownloads(allowed bool) options.ParserOption {
}
}

func OptionWithSkipCachedModules(b bool) options.ParserOption {
return func(p options.ConfigurableParser) {
if tf, ok := p.(ConfigurableTerraformParser); ok {
tf.SetSkipCachedModules(b)
}
}
}

func OptionWithConfigsFS(fsys fs.FS) options.ParserOption {
return func(s options.ConfigurableParser) {
if p, ok := s.(ConfigurableTerraformParser); ok {
Expand Down
44 changes: 25 additions & 19 deletions pkg/scanners/terraform/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,26 @@ var _ ConfigurableTerraformParser = (*Parser)(nil)

// Parser is a tool for parsing terraform templates at a given file system location
type Parser struct {
projectRoot string
moduleName string
modulePath string
moduleSource string
moduleFS fs.FS
moduleBlock *terraform.Block
files []sourceFile
tfvarsPaths []string
stopOnHCLError bool
workspaceName string
underlying *hclparse.Parser
children []*Parser
metrics Metrics
options []options.ParserOption
debug debug.Logger
allowDownloads bool
fsMap map[string]fs.FS
skipRequired bool
configsFS fs.FS
projectRoot string
moduleName string
modulePath string
moduleSource string
moduleFS fs.FS
moduleBlock *terraform.Block
files []sourceFile
tfvarsPaths []string
stopOnHCLError bool
workspaceName string
underlying *hclparse.Parser
children []*Parser
metrics Metrics
options []options.ParserOption
debug debug.Logger
allowDownloads bool
skipCachedModules bool
fsMap map[string]fs.FS
skipRequired bool
configsFS fs.FS
}

func (p *Parser) SetDebugWriter(writer io.Writer) {
Expand All @@ -84,6 +85,10 @@ func (p *Parser) SetAllowDownloads(b bool) {
p.allowDownloads = b
}

func (p *Parser) SetSkipCachedModules(b bool) {
p.skipCachedModules = b
}

func (p *Parser) SetSkipRequiredCheck(b bool) {
p.skipRequired = b
}
Expand Down Expand Up @@ -303,6 +308,7 @@ func (p *Parser) EvaluateAll(ctx context.Context) (terraform.Modules, cty.Value,
ignores,
p.debug.Extend("evaluator"),
p.allowDownloads,
p.skipCachedModules,
)
modules, fsMap, parseDuration := evaluator.EvaluateAll(ctx)
p.metrics.Counts.Modules = len(modules)
Expand Down
4 changes: 2 additions & 2 deletions pkg/scanners/terraform/parser/parser_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module "registry" {
`,
})

parser := New(fs, "", OptionStopOnHCLError(true))
parser := New(fs, "", OptionStopOnHCLError(true), OptionWithSkipCachedModules(true))
if err := parser.ParseFS(context.TODO(), "code"); err != nil {
t.Fatal(err)
}
Expand All @@ -41,7 +41,7 @@ module "registry" {
`,
})

parser := New(fs, "", OptionStopOnHCLError(true))
parser := New(fs, "", OptionStopOnHCLError(true), OptionWithSkipCachedModules(true))
if err := parser.ParseFS(context.TODO(), "code"); err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/scanners/terraform/parser/resolvers/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func locateCacheDir() (string, error) {
}

func (r *cacheResolver) Resolve(_ context.Context, _ fs.FS, opt Options) (filesystem fs.FS, prefix string, downloadPath string, applies bool, err error) {
if !opt.AllowCache {
if opt.SkipCache {
opt.Debug("Cache is disabled.")
return nil, "", "", false, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/scanners/terraform/parser/resolvers/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Options struct {
Source, OriginalSource, Version, OriginalVersion, WorkingDir, Name, ModulePath string
DebugLogger debug.Logger
AllowDownloads bool
AllowCache bool
SkipCache bool
RelativePath string
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/scanners/terraform/scanner_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ deny[res] {
options.ScannerWithEmbeddedLibraries(false),
options.ScannerWithRegoOnly(true),
ScannerWithAllDirectories(true),
ScannerWithSkipCachedModules(true),
)

results, err := scanner.ScanFS(context.TODO(), fs, ".")
Expand Down Expand Up @@ -117,6 +118,7 @@ deny[res] {
options.ScannerWithEmbeddedLibraries(false),
options.ScannerWithRegoOnly(true),
ScannerWithAllDirectories(true),
ScannerWithSkipCachedModules(true),
)

results, err := scanner.ScanFS(context.TODO(), fs, ".")
Expand Down