-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(terraform): fix root module search (#6160)
Co-authored-by: simar7 <[email protected]>
- Loading branch information
Showing
8 changed files
with
334 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package parser | ||
|
||
import ( | ||
"context" | ||
"path" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/samber/lo" | ||
"github.com/zclconf/go-cty/cty" | ||
|
||
"github.com/aquasecurity/trivy/pkg/iac/terraform" | ||
) | ||
|
||
// FindRootModules takes a list of module paths and identifies the root local modules. | ||
// It builds a graph based on the module dependencies and determines the modules that have no incoming dependencies, | ||
// considering them as root modules. | ||
func (p *Parser) FindRootModules(ctx context.Context, dirs []string) ([]string, error) { | ||
for _, dir := range dirs { | ||
if err := p.ParseFS(ctx, dir); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
blocks, _, err := p.readBlocks(p.files) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
g := buildGraph(blocks, dirs) | ||
rootModules := g.rootModules() | ||
sort.Strings(rootModules) | ||
return rootModules, nil | ||
} | ||
|
||
type modulesGraph map[string][]string | ||
|
||
func buildGraph(blocks terraform.Blocks, paths []string) modulesGraph { | ||
moduleBlocks := blocks.OfType("module") | ||
|
||
graph := lo.SliceToMap(paths, func(p string) (string, []string) { | ||
return p, nil | ||
}) | ||
|
||
for _, block := range moduleBlocks { | ||
sourceVal := block.GetAttribute("source").Value() | ||
if sourceVal.Type() != cty.String { | ||
continue | ||
} | ||
|
||
source := sourceVal.AsString() | ||
if strings.HasPrefix(source, ".") { | ||
filename := block.GetMetadata().Range().GetFilename() | ||
dir := path.Dir(filename) | ||
graph[dir] = append(graph[dir], path.Join(dir, source)) | ||
} | ||
} | ||
|
||
return graph | ||
} | ||
|
||
func (g modulesGraph) rootModules() []string { | ||
incomingEdges := make(map[string]int) | ||
for _, neighbors := range g { | ||
for _, neighbor := range neighbors { | ||
incomingEdges[neighbor]++ | ||
} | ||
} | ||
|
||
var roots []string | ||
for module := range g { | ||
if incomingEdges[module] == 0 { | ||
roots = append(roots, module) | ||
} | ||
} | ||
|
||
return roots | ||
} |
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,71 @@ | ||
package parser | ||
|
||
import ( | ||
"context" | ||
"path" | ||
"testing" | ||
|
||
"github.com/aquasecurity/trivy/internal/testutil" | ||
"github.com/samber/lo" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/exp/maps" | ||
) | ||
|
||
func TestFindRootModules(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
files map[string]string | ||
expected []string | ||
}{ | ||
{ | ||
name: "multiple root modules", | ||
files: map[string]string{ | ||
"code/main.tf": ` | ||
module "this" { | ||
count = 0 | ||
source = "./modules/s3" | ||
}`, | ||
"code/modules/s3/main.tf": ` | ||
module "this" { | ||
source = "./modules/logging" | ||
} | ||
resource "aws_s3_bucket" "this" { | ||
bucket = "test" | ||
}`, | ||
"code/modules/s3/modules/logging/main.tf": ` | ||
resource "aws_s3_bucket" "this" { | ||
bucket = "test1" | ||
}`, | ||
"code/example/main.tf": ` | ||
module "this" { | ||
source = "../modules/s3" | ||
}`, | ||
}, | ||
expected: []string{"code", "code/example"}, | ||
}, | ||
{ | ||
name: "without module block", | ||
files: map[string]string{ | ||
"code/infra1/main.tf": `resource "test" "this" {}`, | ||
"code/infra2/main.tf": `resource "test" "this" {}`, | ||
}, | ||
expected: []string{"code/infra1", "code/infra2"}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
fsys := testutil.CreateFS(t, tt.files) | ||
parser := New(fsys, "", OptionStopOnHCLError(true)) | ||
|
||
modules := lo.Map(maps.Keys(tt.files), func(p string, _ int) string { | ||
return path.Dir(p) | ||
}) | ||
|
||
got, err := parser.FindRootModules(context.TODO(), modules) | ||
require.NoError(t, err) | ||
assert.Equal(t, tt.expected, got) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.