Skip to content

Commit f403d93

Browse files
authored
fix(terraform): Fix for multiple checks (#7097)
* Fix CKV2_AWS_38 * fix for CKV_AZURE_43 * Handle plan files * Fix for 7090
1 parent d07d192 commit f403d93

File tree

8 files changed

+102
-27
lines changed

8 files changed

+102
-27
lines changed

checkov/arm/checks/resource/StorageAccountName.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
STO_NAME_REGEX = re.compile(r"^[a-z0-9]{3,24}$")
1111
VARIABLE_REFS = ("local.", "module.", "var.", "random_string.", "random_id.", "random_integer.", "random_pet.",
12-
"azurecaf_name", "each.")
12+
"azurecaf_name", "each.", "substring")
1313

1414

1515
class StorageAccountName(BaseResourceCheck):

checkov/terraform/checks/graph_checks/aws/Route53ZoneEnableDNSSECSigning.yaml

+20-18
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,30 @@ metadata:
33
id: "CKV2_AWS_38"
44
category: "NETWORKING"
55
definition:
6-
and :
7-
- cond_type: filter
8-
attribute: resource_type
9-
value:
10-
- aws_route53_zone
11-
operator: within
12-
- cond_type: connection
13-
resource_types:
14-
- aws_route53_zone
15-
connected_resource_types:
16-
- aws_route53_hosted_zone_dnssec
17-
- aws_route53_key_signing_key
18-
operator: exists
19-
- or:
6+
or:
7+
- and:
208
- cond_type: attribute
219
resource_types:
2210
- aws_route53_zone
23-
attribute: vpc
24-
operator: not_exists
11+
attribute: vpc # This indicates a private zone that can't have DNSSEC enabled
12+
operator: exists
13+
- cond_type: attribute
14+
resource_types:
15+
- aws_route53_zone
16+
attribute: vpc # This indicates a private zone that can't have DNSSEC enabled
17+
operator: not_equals
18+
value: []
19+
- and:
20+
- cond_type: filter
21+
attribute: resource_type
22+
value:
23+
- aws_route53_zone
24+
operator: within
2525
- cond_type: connection
2626
resource_types:
2727
- aws_route53_zone
2828
connected_resource_types:
29-
- aws_route53_zone_association
30-
operator: not_exists
29+
- aws_route53_hosted_zone_dnssec
30+
- aws_route53_key_signing_key
31+
- aws_route53_zone_association # This indicates a private zone that can't have DNSSEC enabled
32+
operator: exists

checkov/terraform/checks/resource/aws/S3AllowsAnyPrincipal.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,11 @@ def scan_resource_conf(self, conf):
8282
return CheckResult.UNKNOWN
8383

8484
if isinstance(policy_block, dict) and 'Statement' in policy_block.keys():
85-
for statement in force_list(policy_block['Statement']):
86-
if statement['Effect'] == 'Deny' or 'Principal' not in statement:
85+
statements = force_list(policy_block['Statement'])
86+
if all('Effect' not in statement for statement in statements):
87+
return CheckResult.UNKNOWN
88+
for statement in statements:
89+
if 'Effect' not in statement or statement['Effect'] == 'Deny' or 'Principal' not in statement:
8790
continue
8891
principal = statement['Principal']
8992
if principal == '*':
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
@description('Name of the environment')
2+
param environmentName string
3+
4+
@description('Name of the Storage account')
5+
param storageAccountName string = substring('abcdefgh${environmentName}${uniqueString(resourceGroup().id)}', 0, 24)
6+
7+
@description('Provide a location for the resources.')
8+
param location string = resourceGroup().location
9+
10+
resource dataStorageAccount 'Microsoft.Storage/storageAccounts@2021-09-01' = {
11+
name: storageAccountName
12+
location: location
13+
sku: {
14+
name: 'Standard_LRS'
15+
}
16+
kind: 'StorageV2'
17+
identity: {
18+
type: 'SystemAssigned'
19+
}
20+
properties: {
21+
accessTier: 'Hot'
22+
allowBlobPublicAccess: false
23+
allowSharedKeyAccess: true
24+
allowCrossTenantReplication: false
25+
isHnsEnabled: true
26+
allowedCopyScope: 'AAD'
27+
defaultToOAuthAuthentication: false
28+
encryption: {
29+
keySource: 'Microsoft.Storage'
30+
requireInfrastructureEncryption: false
31+
services: {
32+
blob: {
33+
enabled: true
34+
keyType: 'Account'
35+
}
36+
}
37+
}
38+
minimumTlsVersion: 'TLS1_2'
39+
largeFileSharesState: 'Disabled'
40+
sasPolicy: {
41+
expirationAction: 'Log'
42+
sasExpirationPeriod: '00.00:10:00'
43+
}
44+
supportsHttpsTrafficOnly: true
45+
networkAcls: {
46+
bypass: 'AzureServices'
47+
virtualNetworkRules: []
48+
ipRules: []
49+
defaultAction: 'Allow'
50+
}
51+
}
52+
}

tests/terraform/checks/resource/aws/example_S3AllowsAnyPrincipal/main.tf

+13
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,17 @@ resource "aws_s3_bucket" "pass_w_condition6" {
483483
]
484484
}
485485
POLICY
486+
}
487+
488+
# Handle error
489+
resource "aws_s3_bucket_policy" "logs" {
490+
bucket = aws_s3_bucket.logs.id
491+
policy = jsonencode({
492+
Version = "2012-10-17"
493+
Statement = concat(
494+
jsondecode(data.aws_iam_policy_document.logs-cloudtrail-policy-acl-check.json).Statement,
495+
jsondecode(data.aws_iam_policy_document.s3-logs-cloudtrail-policy-write.json).Statement,
496+
jsondecode(data.aws_iam_policy_document.s3-logs-vpc-flow-logs-policy.json).Statement,
497+
)
498+
})
486499
}

tests/terraform/graph/checks/resources/Route53ZoneEnableDNSSECSigning/expected.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ pass:
22
- "aws_route53_zone.pass"
33
- "aws_route53_zone.private_with_inline_vpc"
44
- "aws_route53_zone.private_with_zone_association"
5+
- "aws_route53_zone.pass_signing_key"
56
fail:
6-
- "aws_route53_zone.fail"
7+
- "aws_route53_zone.fail2"

tests/terraform/graph/checks/resources/Route53ZoneEnableDNSSECSigning/main.tf

+6-5
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ resource "aws_route53_zone_association" "private" {
3434
vpc_id = "vpc-1a2b3c4d"
3535
}
3636

37-
#fail
38-
resource "aws_route53_zone" "fail" {
39-
name = "fail"
37+
#pass with signing key
38+
resource "aws_route53_zone" "pass_signing_key" {
39+
name = "pass"
4040
}
41+
4142
resource "aws_route53_key_signing_key" "fail" {
42-
hosted_zone_id = aws_route53_zone.fail.id
43-
key_management_service_arn = aws_kms_key.fail.arn
43+
hosted_zone_id = aws_route53_zone.pass_signing_key.id
44+
key_management_service_arn = aws_kms_key.pass_signing_key.arn
4445
name = "pass"
4546
}
4647

tests/terraform/graph/checks/test_yaml_policies.py

+3
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,9 @@ def test_OSSBucketPublic(self):
568568
def test_Route53ZoneHasMatchingQueryLog(self):
569569
self.go("Route53ZoneHasMatchingQueryLog")
570570

571+
def test_Route53ZoneEnableDNSSECSigning(self):
572+
self.go("Route53ZoneEnableDNSSECSigning")
573+
571574

572575
def test_registry_load(self):
573576
registry = Registry(parser=GraphCheckParser(), checks_dir=str(

0 commit comments

Comments
 (0)