Skip to content
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

feat(terraform): support for-each meta-argument for data sources #47

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
51 changes: 51 additions & 0 deletions internal/adapters/terraform/aws/iam/policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,57 @@ func Test_adaptPolicies(t *testing.T) {
},
},
},
{
name: "aws_iam_policy_document with for_each meta-argument",
terraform: `locals {
sqs = {
sqs1 = "arn:aws:sqs:::*"
}
}

data "aws_iam_policy_document" "this" {
for_each = local.sqs

statement {
sid = each.key
actions = [
"sqs:CancelMessageMoveTask"
]
resources = [each.value]
}
}


resource "aws_iam_policy" "this" {
for_each = local.sqs
name = "test-${each.key}"
policy = data.aws_iam_policy_document.this[each.key].json
}`,
expected: []iam.Policy{
{
Metadata: defsecTypes.NewTestMetadata(),
Name: defsecTypes.String("test-sqs1", defsecTypes.NewTestMetadata()),
Builtin: defsecTypes.Bool(false, defsecTypes.NewTestMetadata()),
Document: iam.Document{
Metadata: defsecTypes.NewTestMetadata(),
IsOffset: true,
HasRefs: false,
Parsed: func() iamgo.Document {
builder := iamgo.NewPolicyBuilder()

sb := iamgo.NewStatementBuilder()
sb.WithEffect(iamgo.EffectAllow)
sb.WithSid("sqs1")
sb.WithActions([]string{"sqs:CancelMessageMoveTask"})
sb.WithResources([]string{"arn:aws:sqs:::*"})

builder.WithStatement(sb.Build())
return builder.Build()
}(),
},
},
},
},
}

for _, test := range tests {
Expand Down
8 changes: 7 additions & 1 deletion pkg/scanners/terraform/parser/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"reflect"
"time"

"golang.org/x/exp/slices"

"github.com/aquasecurity/defsec/pkg/debug"
"github.com/aquasecurity/defsec/pkg/terraform"
tfcontext "github.com/aquasecurity/defsec/pkg/terraform/context"
Expand Down Expand Up @@ -226,14 +228,18 @@ func (e *evaluator) expandDynamicBlock(b *terraform.Block) {
}
}

func isBlockSupportsForEachMetaArgument(block *terraform.Block) bool {
return slices.Contains([]string{"module", "resource", "data", "dynamic"}, block.Type())
}

func (e *evaluator) expandBlockForEaches(blocks terraform.Blocks) terraform.Blocks {
var forEachFiltered terraform.Blocks

for _, block := range blocks {

forEachAttr := block.GetAttribute("for_each")

if forEachAttr.IsNil() || block.IsCountExpanded() || (block.Type() != "resource" && block.Type() != "module" && block.Type() != "dynamic") {
if forEachAttr.IsNil() || block.IsCountExpanded() || !isBlockSupportsForEachMetaArgument(block) {
forEachFiltered = append(forEachFiltered, block)
continue
}
Expand Down
141 changes: 61 additions & 80 deletions pkg/scanners/terraform/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,10 @@ check "cats_mittens_is_special" {
})

parser := New(fs, "", OptionStopOnHCLError(true))
if err := parser.ParseFS(context.TODO(), "."); err != nil {
t.Fatal(err)
}
require.NoError(t, parser.ParseFS(context.TODO(), "."))
modules, _, err := parser.EvaluateAll(context.TODO())
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

blocks := modules[0].GetBlocks()

// variable
Expand Down Expand Up @@ -172,13 +169,11 @@ output "mod_result" {
})

parser := New(fs, "", OptionStopOnHCLError(true), options.ParserWithDebug(os.Stderr))
if err := parser.ParseFS(context.TODO(), "code"); err != nil {
t.Fatal(err)
}
require.NoError(t, parser.ParseFS(context.TODO(), "code"))

modules, _, err := parser.EvaluateAll(context.TODO())
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)

require.Len(t, modules, 2)
rootModule := modules[0]
childModule := modules[1]
Expand Down Expand Up @@ -236,13 +231,9 @@ output "mod_result" {
})

parser := New(fs, "", OptionStopOnHCLError(true))
if err := parser.ParseFS(context.TODO(), "code"); err != nil {
t.Fatal(err)
}
require.NoError(t, parser.ParseFS(context.TODO(), "code"))
modules, _, err := parser.EvaluateAll(context.TODO())
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)
require.Len(t, modules, 2)
rootModule := modules[0]
childModule := modules[1]
Expand Down Expand Up @@ -285,13 +276,9 @@ resource "something" "blah" {
})

parser := New(fs, "", OptionStopOnHCLError(true))
if err := parser.ParseFS(context.TODO(), "code"); err != nil {
t.Fatal(err)
}
require.NoError(t, parser.ParseFS(context.TODO(), "code"))
modules, _, err := parser.EvaluateAll(context.TODO())
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)
require.Len(t, modules, 1)
rootModule := modules[0]

Expand All @@ -316,13 +303,9 @@ resource "something" "blah" {
})

parser := New(fs, "", OptionStopOnHCLError(true))
if err := parser.ParseFS(context.TODO(), "code"); err != nil {
t.Fatal(err)
}
require.NoError(t, parser.ParseFS(context.TODO(), "code"))
modules, _, err := parser.EvaluateAll(context.TODO())
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)
require.Len(t, modules, 1)
rootModule := modules[0]

Expand Down Expand Up @@ -363,13 +346,9 @@ resource "something" "blah" {
})

parser := New(fs, "", OptionStopOnHCLError(true))
if err := parser.ParseFS(context.TODO(), "code"); err != nil {
t.Fatal(err)
}
require.NoError(t, parser.ParseFS(context.TODO(), "code"))
modules, _, err := parser.EvaluateAll(context.TODO())
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)
require.Len(t, modules, 1)
rootModule := modules[0]

Expand Down Expand Up @@ -415,13 +394,9 @@ resource "something" "blah" {
})

parser := New(fs, "", OptionStopOnHCLError(true))
if err := parser.ParseFS(context.TODO(), "code"); err != nil {
t.Fatal(err)
}
require.NoError(t, parser.ParseFS(context.TODO(), "code"))
modules, _, err := parser.EvaluateAll(context.TODO())
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)
require.Len(t, modules, 1)
rootModule := modules[0]

Expand Down Expand Up @@ -460,13 +435,9 @@ resource "something" "blah" {
})

parser := New(fs, "", OptionStopOnHCLError(true))
if err := parser.ParseFS(context.TODO(), "code"); err != nil {
t.Fatal(err)
}
require.NoError(t, parser.ParseFS(context.TODO(), "code"))
modules, _, err := parser.EvaluateAll(context.TODO())
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)
require.Len(t, modules, 1)
rootModule := modules[0]

Expand Down Expand Up @@ -512,13 +483,9 @@ resource "something" "blah" {
})

parser := New(fs, "", OptionStopOnHCLError(true))
if err := parser.ParseFS(context.TODO(), "code"); err != nil {
t.Fatal(err)
}
require.NoError(t, parser.ParseFS(context.TODO(), "code"))
modules, _, err := parser.EvaluateAll(context.TODO())
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)
require.Len(t, modules, 1)
rootModule := modules[0]

Expand Down Expand Up @@ -560,13 +527,10 @@ resource "something" "blah" {
})

parser := New(fs, "", OptionStopOnHCLError(true))
if err := parser.ParseFS(context.TODO(), "code"); err != nil {
t.Fatal(err)
}
require.NoError(t, parser.ParseFS(context.TODO(), "code"))
modules, _, err := parser.EvaluateAll(context.TODO())
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

require.Len(t, modules, 1)
rootModule := modules[0]

Expand Down Expand Up @@ -607,9 +571,7 @@ resource "aws_s3_bucket" "default" {
})

parser := New(fs, "", OptionStopOnHCLError(true))
if err := parser.ParseFS(context.TODO(), "."); err != nil {
t.Fatal(err)
}
require.NoError(t, parser.ParseFS(context.TODO(), "."))
modules, _, err := parser.EvaluateAll(context.TODO())
require.NoError(t, err)
require.Len(t, modules, 1)
Expand Down Expand Up @@ -669,9 +631,7 @@ resource "aws_s3_bucket_server_side_encryption_configuration" "this2" {
})

parser := New(fs, "", OptionStopOnHCLError(true))
if err := parser.ParseFS(context.TODO(), "."); err != nil {
t.Fatal(err)
}
require.NoError(t, parser.ParseFS(context.TODO(), "."))
modules, _, err := parser.EvaluateAll(context.TODO())
assert.NoError(t, err)
assert.Len(t, modules, 1)
Expand Down Expand Up @@ -706,9 +666,7 @@ resource "aws_s3_bucket" "main" {
OptionWithTFVarsPaths("main.tfvars"),
)

if err := parser.ParseFS(context.TODO(), "."); err != nil {
t.Fatal(err)
}
require.NoError(t, parser.ParseFS(context.TODO(), "."))
modules, _, err := parser.EvaluateAll(context.TODO())
assert.NoError(t, err)
assert.Len(t, modules, 1)
Expand Down Expand Up @@ -740,9 +698,8 @@ resource "aws_s3_bucket" "this" {
})

parser := New(fs, "", OptionStopOnHCLError(true))
if err := parser.ParseFS(context.TODO(), "."); err != nil {
t.Fatal(err)
}
require.NoError(t, parser.ParseFS(context.TODO(), "."))

modules, _, err := parser.EvaluateAll(context.TODO())
assert.NoError(t, err)
assert.Len(t, modules, 1)
Expand Down Expand Up @@ -775,9 +732,8 @@ resource "aws_s3_bucket" "this" {
})

parser := New(fs, "", OptionStopOnHCLError(true))
if err := parser.ParseFS(context.TODO(), "."); err != nil {
t.Fatal(err)
}
require.NoError(t, parser.ParseFS(context.TODO(), "."))

modules, _, err := parser.EvaluateAll(context.TODO())
assert.NoError(t, err)
assert.Len(t, modules, 1)
Expand Down Expand Up @@ -829,11 +785,9 @@ policy_rules = {
`,
})

parser := New(fs, "", OptionStopOnHCLError(true))
parser.SetTFVarsPaths("main.tfvars")
if err := parser.ParseFS(context.TODO(), "."); err != nil {
t.Fatal(err)
}
parser := New(fs, "", OptionStopOnHCLError(true), OptionWithTFVarsPaths("main.tfvars"))
require.NoError(t, parser.ParseFS(context.TODO(), "."))

modules, _, err := parser.EvaluateAll(context.TODO())
assert.NoError(t, err)
assert.Len(t, modules, 1)
Expand All @@ -850,3 +804,30 @@ policy_rules = {
assert.Equal(t, "host() != 'google.com'", block.GetAttribute("session_matcher").AsStringValueOrDefault("", block).Value())
assert.Equal(t, 1001, block.GetAttribute("priority").AsIntValueOrDefault(0, block).Value())
}

func TestDataSourceWithForEachMetaArgument(t *testing.T) {
fs := testutil.CreateFS(t, map[string]string{
"main.tf": `
locals {
ports = [80, 8080]
}

data "http" "example" {
for_each = local.ports
url = "localhost:${each.key}"
}
`,
})

parser := New(fs, "", OptionStopOnHCLError(true))
require.NoError(t, parser.ParseFS(context.TODO(), "."))

modules, _, err := parser.EvaluateAll(context.TODO())
assert.NoError(t, err)
assert.Len(t, modules, 1)

rootModule := modules[0]

httpDataSources := rootModule.GetDatasByType("http")
assert.Len(t, httpDataSources, 2)
}