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

extending CIDR validation to more types #1058

Merged
merged 13 commits into from
Jul 29, 2019
67 changes: 32 additions & 35 deletions src/cfnlint/rules/parameters/Cidr.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,41 @@


class Cidr(CloudFormationLintRule):
"""Check Availability Zone parameter checks """
"""CIDR checks"""
id = 'W2509'
shortdesc = 'CIDR Parameters have allowed values'
description = 'Check if a parameter is being used as a CIDR. ' \
'If it is make sure it has allowed values regex comparisons'
source_url = 'https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html'
tags = ['parameters', 'availabilityzone']
tags = ['parameters', 'cidr']

def __init__(self):
"""Init"""
super(Cidr, self).__init__()
resource_type_specs = [
'AWS::EC2::ClientVpnAuthorizationRule',
'AWS::EC2::ClientVpnEndpoint',
'AWS::EC2::ClientVpnRoute',
'AWS::EC2::NetworkAclEntry',
'AWS::EC2::Route',
'AWS::EC2::SecurityGroupEgress',
'AWS::EC2::SecurityGroupIngress',
'AWS::EC2::Subnet',
'AWS::EC2::TransitGatewayRoute',
'AWS::EC2::VPC',
'AWS::EC2::VPCCidrBlock',
'AWS::EC2::VPNConnectionRoute',
'AWS::RDS::DBSecurityGroupIngress',
'AWS::EC2::NetworkAclEntry',
'AWS::EC2::SecurityGroupIngress',
'AWS::EC2::SecurityGroupEgress',
'AWS::Redshift::ClusterSecurityGroupIngress',
'AWS::EC2::VPCCidrBlock',
]

property_type_specs = [
'AWS::RDS::DBSecurityGroup.Ingress',
'AWS::EC2::SecurityGroup.Egress',
'AWS::SES::ReceiptFilter.IpFilter',
'AWS::EC2::SecurityGroup.Ingress',
'AWS::EC2::VPNConnection.VpnTunnelOptionsSpecification',
'AWS::MediaLive::InputSecurityGroup.InputWhitelistRuleCidr',
'AWS::RDS::DBSecurityGroup.Ingress',
'AWS::SES::ReceiptFilter.IpFilter',
]

for resource_type_spec in resource_type_specs:
Expand Down Expand Up @@ -79,34 +87,23 @@ def check(self, properties, resource_type, path, cfn):
"""Check itself"""
matches = []

matches.extend(
cfn.check_value(
properties, 'CIDRIP', path,
check_value=None, check_ref=self.check_cidr_ref,
check_find_in_map=None, check_split=None, check_join=None
)
)
matches.extend(
cfn.check_value(
properties, 'Cidr', path,
check_value=None, check_ref=self.check_cidr_ref,
check_find_in_map=None, check_split=None, check_join=None
)
)
matches.extend(
cfn.check_value(
properties, 'CidrBlock', path,
check_value=None, check_ref=self.check_cidr_ref,
check_find_in_map=None, check_split=None, check_join=None
)
)
matches.extend(
cfn.check_value(
properties, 'CidrIp', path,
check_value=None, check_ref=self.check_cidr_ref,
check_find_in_map=None, check_split=None, check_join=None
for cidrString in [
'CIDRIP',
'Cidr',
'CidrBlock',
'CidrIp',
'ClientCidrBlock',
'DestinationCidrBlock',
'TargetNetworkCidr',
'TunnelInsideCidr',
]:
matches.extend(
cfn.check_value(
properties, cidrString, path,
check_value=None, check_ref=self.check_cidr_ref,
check_find_in_map=None, check_split=None, check_join=None
)
)
)

return matches

Expand Down
65 changes: 31 additions & 34 deletions src/cfnlint/rules/parameters/CidrAllowedValues.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


class CidrAllowedValues(CloudFormationLintRule):
"""Check Availability Zone parameter checks """
"""CIDR checks"""
id = 'E2004'
shortdesc = 'CIDR Allowed Values should be a Cidr Range'
description = 'Check if a parameter is being used as a CIDR. ' \
Expand All @@ -34,21 +34,29 @@ def __init__(self):
"""Init"""
super(CidrAllowedValues, self).__init__()
resource_type_specs = [
'AWS::EC2::ClientVpnAuthorizationRule',
'AWS::EC2::ClientVpnEndpoint',
'AWS::EC2::ClientVpnRoute',
'AWS::EC2::NetworkAclEntry',
'AWS::EC2::Route',
'AWS::EC2::SecurityGroupEgress',
'AWS::EC2::SecurityGroupIngress',
'AWS::EC2::Subnet',
'AWS::EC2::TransitGatewayRoute',
'AWS::EC2::VPC',
'AWS::EC2::VPCCidrBlock',
'AWS::EC2::VPNConnectionRoute',
'AWS::RDS::DBSecurityGroupIngress',
'AWS::EC2::NetworkAclEntry',
'AWS::EC2::SecurityGroupIngress',
'AWS::EC2::SecurityGroupEgress',
'AWS::Redshift::ClusterSecurityGroupIngress',
'AWS::EC2::VPCCidrBlock',
]

property_type_specs = [
'AWS::RDS::DBSecurityGroup.Ingress',
'AWS::EC2::SecurityGroup.Egress',
'AWS::SES::ReceiptFilter.IpFilter',
'AWS::EC2::SecurityGroup.Ingress',
'AWS::EC2::VPNConnection.VpnTunnelOptionsSpecification',
'AWS::MediaLive::InputSecurityGroup.InputWhitelistRuleCidr',
'AWS::RDS::DBSecurityGroup.Ingress',
'AWS::SES::ReceiptFilter.IpFilter',
]

for resource_type_spec in resource_type_specs:
Expand Down Expand Up @@ -77,34 +85,23 @@ def check(self, properties, resource_type, path, cfn):
"""Check itself"""
matches = []

matches.extend(
cfn.check_value(
properties, 'CIDRIP', path,
check_value=None, check_ref=self.check_cidr_ref,
check_find_in_map=None, check_split=None, check_join=None
)
)
matches.extend(
cfn.check_value(
properties, 'Cidr', path,
check_value=None, check_ref=self.check_cidr_ref,
check_find_in_map=None, check_split=None, check_join=None
)
)
matches.extend(
cfn.check_value(
properties, 'CidrBlock', path,
check_value=None, check_ref=self.check_cidr_ref,
check_find_in_map=None, check_split=None, check_join=None
)
)
matches.extend(
cfn.check_value(
properties, 'CidrIp', path,
check_value=None, check_ref=self.check_cidr_ref,
check_find_in_map=None, check_split=None, check_join=None
for cidrString in [
'CIDRIP',
'Cidr',
'CidrBlock',
'CidrIp',
'ClientCidrBlock',
'DestinationCidrBlock',
'TargetNetworkCidr',
'TunnelInsideCidr',
]:
matches.extend(
cfn.check_value(
properties, cidrString, path,
check_value=None, check_ref=self.check_cidr_ref,
check_find_in_map=None, check_split=None, check_join=None
)
)
)

return matches

Expand Down
25 changes: 25 additions & 0 deletions test/fixtures/results/quickstart/nist_vpc_management.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
[
{
"Filename": "test/fixtures/templates/quickstart/nist_vpc_management.yaml",
"Level": "Warning",
"Location": {
"End": {
"ColumnNumber": 18,
"LineNumber": 267
},
"Path": [
"Parameters",
"pProductionCIDR"
],
"Start": {
"ColumnNumber": 3,
"LineNumber": 267
}
},
"Message": "AllowedPattern and/or AllowedValues for Parameter should be specified at Parameters/pProductionCIDR. Example for AllowedPattern: \"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/([0-9]|[1-2][0-9]|3[0-2]))$\"",
"Rule": {
"Description": "Check if a parameter is being used as a CIDR. If it is make sure it has allowed values regex comparisons",
"Id": "W2509",
"ShortDescription": "CIDR Parameters have allowed values",
"Source": "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html"
}
},
kddejong marked this conversation as resolved.
Show resolved Hide resolved
{
"Filename": "test/fixtures/templates/quickstart/nist_vpc_management.yaml",
"Level": "Warning",
Expand Down
2 changes: 1 addition & 1 deletion test/module/cfn_json/test_cfn_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def setUp(self):
},
"vpc_management": {
"filename": 'test/fixtures/templates/quickstart/vpc-management.json',
"failures": 31
"failures": 35
kddejong marked this conversation as resolved.
Show resolved Hide resolved
},
"vpc": {
"filename": 'test/fixtures/templates/quickstart/vpc.json',
Expand Down
2 changes: 1 addition & 1 deletion test/rules/parameters/test_cidr.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_file_negative_nist_app(self):

def test_file_negative_nist_mgmt(self):
"""Failure test"""
self.helper_file_negative('test/fixtures/templates/quickstart/nist_vpc_management.yaml', 6)
self.helper_file_negative('test/fixtures/templates/quickstart/nist_vpc_management.yaml', 7)
kddejong marked this conversation as resolved.
Show resolved Hide resolved

def test_file_negative_nist_prod(self):
"""Failure test"""
Expand Down