3 D\V@s0dZddlmZddlmZGdddeZdS)a Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. )CloudFormationLintRule) RuleMatchc@sPeZdZdZdZdZdZdZddgZdd Z d d Z d d Z ddZ ddZ dS)SecurityGroupIngressz.Check if EC2 Security Group Ingress PropertiesZE2506z.Resource EC2 Security Group Ingress PropertieszSee if EC2 Security Group Ingress Properties are set correctly. Check that "SourceSecurityGroupId" or "SourceSecurityGroupName" are are exclusive and using the type of Ref or GetAtt zmhttps://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-security-group-ingress.html resourcesZ securitygroupcCs8g}|jds4d}|jt||jdjtt||S)zCheck VPC Valueszsg-z2Security Group Id must have a valid Identifier {0}/) startswithappendrformatjoinmapstr)selfvaluepathmatchesmessager/private/var/folders/pf/wv4htv3x0qs2c2mp0dnn0kchsvlck3/T/pip-install-emcbgzcf/cfn-lint/cfnlint/rules/resources/ectwo/SecurityGroupIngress.pycheck_sgid_values  z%SecurityGroupIngress.check_sgid_valuec Cs g}dddg}||krp|j|}|jd}||krpd|dg} d} |jt| | jdjtt|djtt| ||kr|j|i} | jdd } | d krd } |jt|| jdjtt|nD| jd i} | jd d}|sd} |jt|| jdjtt||S)zCheck ref for VPCz7AWS::SSM::Parameter::ValuezAWS::EC2::SecurityGroup::IdStringTypeZ Parametersz;Security Group Id Parameter should be of type [{0}] for {1}z, rzAWS::EC2::SecurityGroupzMSecurity Group Id resources should be of type AWS::EC2::SecurityGroup for {0} PropertiesVpcIdNzNSecurity Group Id should reference a VPC based AWS::EC2::SecurityGroup for {0})getrrr r r r )r rr parametersrrZ allowed_typesZparameter_propertiesZparameter_type path_errorrresource resource_typeZresource_propertiesZ vpc_propertyrrrcheck_sgid_ref(s<         z#SecurityGroupIngress.check_sgid_refcKs.g}d}|jt||jdjtt||S)z'Automatic failure for certain functionsz,Use Ref, FindInMap, or string values for {0}r)rrr r r r )r rrkwargsrrrrrcheck_sgid_failOs z$SecurityGroupIngress.check_sgid_failc Csg}|r~|jddrL|dddg}d}|jt||jdjtt||j|j|d|dd|j |j d|j |j dnD|jddr|dddg}d}|jt||jdjtt||S)zCheck ingress ruleZSourceSecurityGroupNameNzLSourceSecurityGroupName shouldn't be specified for Vpc Security Group at {0}rZSourceSecurityGroupId)objkeyr check_valueZ check_refZcheck_find_in_mapZ check_splitZ check_joinzNSourceSecurityGroupId shouldn't be specified for Non-Vpc Security Group at {0}) rrrr r r r extendr$rrr!)r vpc_id propertiesrcfnrrrrrrcheck_ingress_ruleXs(   z'SecurityGroupIngress.check_ingress_rulec Csg}|jdd}x|jD]t\}}|jdi}|r|jdd}|jd}t|trx:t|D].\} } d|dd| g} |j|j|| | |dq\WqWd}|jd d}xj|jD]^\}}|jdi}|jd d} d|dg} | rd }nd}|rd|dg} |j|j||| |dqW|S) z4Check EC2 Security Group Ingress Resource ParameterszAWS::EC2::SecurityGroup)rrrNrZ Resources)r&r'rr(zAWS::EC2::SecurityGroupIngressZGroupIdz vpc-1234567) get_resourcesitemsr isinstancelist enumerater%r)) r r(rr resource_nameZresource_objectr'r&Z ingress_rulesindexZ ingress_rulerZgroup_idrrrmatch{sF          zSecurityGroupIngress.matchN)__name__ __module__ __qualname____doc__idZ shortdesc descriptionZ source_urltagsrrr!r)r1rrrrrs ' #rN)r5Zcfnlintrrrrrrrs