Skip to content

Commit

Permalink
fix: Allow ignores of implied IAM resources (#849)
Browse files Browse the repository at this point in the history
  • Loading branch information
liamg authored Aug 15, 2022
1 parent c5b4fd0 commit 4d89a77
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 5 deletions.
20 changes: 18 additions & 2 deletions internal/adapters/terraform/aws/iam/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ func adaptGroups(modules terraform.Modules) []iam.Group {
if err != nil {
continue
}
group := groupMap[groupBlock.ID()]
group, ok := groupMap[groupBlock.ID()]
if !ok {
group = iam.Group{
Metadata: groupBlock.GetMetadata(),
Name: groupBlock.GetAttribute("name").AsStringValueOrDefault("", groupBlock),
Users: nil,
Policies: nil,
}
}
group.Policies = append(group.Policies, policy)
groupMap[groupBlock.ID()] = group
}
Expand Down Expand Up @@ -54,7 +62,15 @@ func adaptGroups(modules terraform.Modules) []iam.Group {
if err != nil {
continue
}
group := groupMap[groupBlock.ID()]
group, ok := groupMap[groupBlock.ID()]
if !ok {
group = iam.Group{
Metadata: groupBlock.GetMetadata(),
Name: groupBlock.GetAttribute("name").AsStringValueOrDefault("", groupBlock),
Users: nil,
Policies: nil,
}
}
group.Policies = append(group.Policies, policy)
groupMap[groupBlock.ID()] = group
}
Expand Down
9 changes: 8 additions & 1 deletion internal/adapters/terraform/aws/iam/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ func adaptRoles(modules terraform.Modules) []iam.Role {
if err != nil {
continue
}
role := roleMap[roleBlock.ID()]
role, ok := roleMap[roleBlock.ID()]
if !ok {
role = iam.Role{
Metadata: roleBlock.GetMetadata(),
Name: roleBlock.GetAttribute("name").AsStringValueOrDefault("", roleBlock),
Policies: nil,
}
}
role.Policies = append(role.Policies, policy)
roleMap[roleBlock.ID()] = role
}
Expand Down
13 changes: 12 additions & 1 deletion internal/adapters/terraform/aws/iam/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,18 @@ func adaptUsers(modules terraform.Modules) []iam.User {
if err != nil {
continue
}
user := userMap[userBlock.ID()]
user, ok := userMap[userBlock.ID()]
if !ok {
user = iam.User{
Metadata: userBlock.GetMetadata(),
Name: userBlock.GetAttribute("name").AsStringValueOrDefault("", userBlock),
Groups: nil,
Policies: nil,
AccessKeys: nil,
MFADevices: nil,
LastAccess: defsecTypes.TimeUnresolvable(userBlock.GetMetadata()),
}
}
user.Policies = append(user.Policies, policy)
userMap[userBlock.ID()] = user
}
Expand Down
11 changes: 10 additions & 1 deletion internal/adapters/terraform/google/iam/org_iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@ func (a *adapter) adaptOrganizationMembers() {
if refBlock, err := a.modules.GetReferencedBlock(organizationAttr, iamBlock); err == nil {
if refBlock.TypeLabel() == "google_organization" {
a.addOrg(refBlock.ID())
org := a.orgs[refBlock.ID()]
org, ok := a.orgs[refBlock.ID()]
if !ok {
org = iam.Organization{
Metadata: refBlock.GetMetadata(),
Folders: nil,
Projects: nil,
Members: []iam.Member{member},
Bindings: nil,
}
}
org.Members = append(org.Members, member)
a.orgs[refBlock.ID()] = org
continue
Expand Down
51 changes: 51 additions & 0 deletions test/ignore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,54 @@ resource "bad" "my-rule" {
`, "testworkspace")
assert.Len(t, results.GetFailed(), 0)
}

func Test_IgnoreForImpliedIAMResource(t *testing.T) {
reg := rules.Register(exampleRule, nil)
defer rules.Deregister(reg)

results := scanHCL(t, `
terraform {
required_version = "~> 1.1.6"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.48"
}
}
}
# Retrieve an IAM group defined outside of this Terraform config.
# tfsec:ignore:aws-iam-enforce-mfa
data "aws_iam_group" "externally_defined_group" {
group_name = "group-name" # tfsec:ignore:aws-iam-enforce-mfa
}
# Create an IAM policy and attach it to the group.
# tfsec:ignore:aws-iam-enforce-mfa
resource "aws_iam_policy" "test_policy" {
name = "test-policy" # tfsec:ignore:aws-iam-enforce-mfa
policy = data.aws_iam_policy_document.test_policy.json # tfsec:ignore:aws-iam-enforce-mfa
}
# tfsec:ignore:aws-iam-enforce-mfa
resource "aws_iam_group_policy_attachment" "test_policy_attachment" {
group = data.aws_iam_group.externally_defined_group.group_name # tfsec:ignore:aws-iam-enforce-mfa
policy_arn = aws_iam_policy.test_policy.arn # tfsec:ignore:aws-iam-enforce-mfa
}
# tfsec:ignore:aws-iam-enforce-mfa
data "aws_iam_policy_document" "test_policy" {
statement {
sid = "PublishToCloudWatch" # tfsec:ignore:aws-iam-enforce-mfa
actions = [
"cloudwatch:PutMetricData", # tfsec:ignore:aws-iam-enforce-mfa
]
resources = ["*"] # tfsec:ignore:aws-iam-enforce-mfa
}
}
`)
assert.Len(t, results.GetFailed(), 0)
}

0 comments on commit 4d89a77

Please sign in to comment.