forked from Checkmarx/kics
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(query): support of Terraform modules for "S3 Bucket Allows Put …
…Action From All Principals" Checkmarx#4274
- Loading branch information
1 parent
8e45487
commit e9772dc
Showing
8 changed files
with
153 additions
and
71 deletions.
There are no files selected for viewing
48 changes: 36 additions & 12 deletions
48
assets/queries/terraform/aws/s3_bucket_allows_delete_action_from_all_principals/query.rego
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 |
---|---|---|
@@ -1,24 +1,48 @@ | ||
package Cx | ||
|
||
import data.generic.common as commonLib | ||
import data.generic.terraform as terraLib | ||
import data.generic.common as common_lib | ||
import data.generic.terraform as terra_lib | ||
|
||
pl := {"aws_s3_bucket_policy", "aws_s3_bucket"} | ||
|
||
CxPolicy[result] { | ||
pl := {"aws_s3_bucket_policy", "aws_s3_bucket"} | ||
resource := input.document[i].resource[pl[r]][name] | ||
resourceType := pl[r] | ||
resource := input.document[i].resource[resourceType][name] | ||
|
||
policy := commonLib.json_unmarshal(resource.policy) | ||
statement := policy.Statement[_] | ||
delete_action_from_all_principals(resource.policy) | ||
|
||
statement.Effect == "Allow" | ||
terraLib.anyPrincipal(statement) | ||
commonLib.containsOrInArrayContains(statement.Action, "delete") | ||
result := { | ||
"documentId": input.document[i].id, | ||
"searchKey": sprintf("%s[%s].policy", [resourceType, name]), | ||
"issueType": "IncorrectValue", | ||
"keyExpectedValue": sprintf("%s[%s].policy.Action is not a 'Delete' action", [resourceType, name]), | ||
"keyActualValue": sprintf("%s[%s].policy.Action is a 'Delete' action", [resourceType, name]), | ||
"searchLine": common_lib.build_search_line(["resource", resourceType, name, "policy"], []), | ||
} | ||
} | ||
|
||
CxPolicy[result] { | ||
module := input.document[i].module[name] | ||
resourceType := pl[r] | ||
keyToCheck := common_lib.get_module_equivalent_key("aws", module.source, resourceType, "policy") | ||
|
||
delete_action_from_all_principals(module[keyToCheck]) | ||
|
||
result := { | ||
"documentId": input.document[i].id, | ||
"searchKey": sprintf("%s[%s].policy.Action", [pl[r], name]), | ||
"searchKey": sprintf("module[%s].policy", [name]), | ||
"issueType": "IncorrectValue", | ||
"keyExpectedValue": sprintf("%s[%s].policy.Action is not a 'Delete' action", [pl[r], name]), | ||
"keyActualValue": sprintf("%s[%s].policy.Action is a 'Delete' action", [pl[r], name]), | ||
"keyExpectedValue": "'policy.Statement.Action' is not a 'Delete' action", | ||
"keyActualValue": "'policy.Statement.Action' is a 'Delete' action", | ||
"searchLine": common_lib.build_search_line(["module", name, "policy"], []), | ||
} | ||
} | ||
|
||
delete_action_from_all_principals(policyValue){ | ||
policy := common_lib.json_unmarshal(policyValue) | ||
statement := policy.Statement[_] | ||
|
||
statement.Effect == "Allow" | ||
terra_lib.anyPrincipal(statement) | ||
common_lib.containsOrInArrayContains(statement.Action, "delete") | ||
} |
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
29 changes: 29 additions & 0 deletions
29
...ueries/terraform/aws/s3_bucket_allows_delete_action_from_all_principals/test/negative2.tf
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,29 @@ | ||
module "s3_bucket" { | ||
source = "terraform-aws-modules/s3-bucket/aws" | ||
version = "3.7.0" | ||
|
||
bucket = "my-s3-bucket" | ||
acl = "private" | ||
|
||
versioning = { | ||
enabled = true | ||
} | ||
|
||
policy = <<POLICY | ||
{ | ||
"Version": "2012-10-17", | ||
"Id": "MYBUCKETPOLICY", | ||
"Statement": [ | ||
{ | ||
"Sid": "IPAllow", | ||
"Effect": "Deny", | ||
"Action": "s3:*", | ||
"Resource": "arn:aws:s3:::my_tf_test_bucket/*", | ||
"Condition": { | ||
"IpAddress": {"aws:SourceIp": "8.8.8.8/32"} | ||
} | ||
} | ||
] | ||
} | ||
POLICY | ||
} |
51 changes: 0 additions & 51 deletions
51
...queries/terraform/aws/s3_bucket_allows_delete_action_from_all_principals/test/positive.tf
This file was deleted.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
...ueries/terraform/aws/s3_bucket_allows_delete_action_from_all_principals/test/positive1.tf
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,22 @@ | ||
resource "aws_s3_bucket_policy" "positive1" { | ||
bucket = aws_s3_bucket.b.id | ||
|
||
policy = <<POLICY | ||
{ | ||
"Version": "2012-10-17", | ||
"Id": "MYBUCKETPOLICY", | ||
"Statement": [ | ||
{ | ||
"Sid": "IPAllow", | ||
"Effect": "Allow", | ||
"Principal": "*", | ||
"Action": "s3:DeleteObject", | ||
"Resource": "arn:aws:s3:::my_tf_test_bucket/*", | ||
"Condition": { | ||
"IpAddress": {"aws:SourceIp": "8.8.8.8/32"} | ||
} | ||
} | ||
] | ||
} | ||
POLICY | ||
} |
24 changes: 24 additions & 0 deletions
24
...ueries/terraform/aws/s3_bucket_allows_delete_action_from_all_principals/test/positive2.tf
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,24 @@ | ||
resource "aws_s3_bucket_policy" "positive2" { | ||
bucket = aws_s3_bucket.b.id | ||
|
||
policy = <<POLICY | ||
{ | ||
"Version": "2012-10-17", | ||
"Id": "MYBUCKETPOLICY", | ||
"Statement": [ | ||
{ | ||
"Sid": "IPAllow", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"AWS": "*" | ||
}, | ||
"Action": "s3:DeleteObject", | ||
"Resource": "arn:aws:s3:::my_tf_test_bucket/*", | ||
"Condition": { | ||
"IpAddress": {"aws:SourceIp": "8.8.8.8/32"} | ||
} | ||
} | ||
] | ||
} | ||
POLICY | ||
} |
30 changes: 30 additions & 0 deletions
30
...ueries/terraform/aws/s3_bucket_allows_delete_action_from_all_principals/test/positive3.tf
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,30 @@ | ||
module "s3_bucket" { | ||
source = "terraform-aws-modules/s3-bucket/aws" | ||
version = "3.7.0" | ||
|
||
bucket = "my-s3-bucket" | ||
acl = "private" | ||
|
||
versioning = { | ||
enabled = true | ||
} | ||
|
||
policy = <<POLICY | ||
{ | ||
"Version": "2012-10-17", | ||
"Id": "MYBUCKETPOLICY", | ||
"Statement": [ | ||
{ | ||
"Sid": "IPAllow", | ||
"Effect": "Allow", | ||
"Principal": "*", | ||
"Action": "s3:DeleteObject", | ||
"Resource": "arn:aws:s3:::my_tf_test_bucket/*", | ||
"Condition": { | ||
"IpAddress": {"aws:SourceIp": "8.8.8.8/32"} | ||
} | ||
} | ||
] | ||
} | ||
POLICY | ||
} |
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