3 D\#@sLdZddlZddlZddlmZddlmZddlmZGdddeZdS)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. N)CloudFormationLintRule) RuleMatch) REGEX_CIDRc@s@eZdZdZdZdZdZdZddgZdd Z d d Z d d Z dS)Cidrz Check if Cidr values are correctZE1024zCidr validation of parametersz9Making sure the function CIDR is a list with valid valueszehttps://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-cidr.htmlZ functionsZcidrc Csg}|jj|i}|rd|g}|jd}|dkr|jd}|jd}| s\|dks\|dkrd} |jt|dg| jd jtt|dg| s|dks|dkrd } |jt|dg| jd jtt|dgn&d } |jt|| jd jtt||S) zCheck Count Parameter if used ParametersTypeNumberMaxValueMinValuez?Parameter for Cidr count have MinValue between 1 and 256 at {0}/z?Parameter for Cidr count have MaxValue between 1 and 256 at {0}z6Parameter for Cidr count have be of Type Number at {0})get_parametersgetappendrformatjoinmapstr) selfcfnparameter_namematches parameter_objtreeparameter_type max_value min_valuemessagerv/private/var/folders/pf/wv4htv3x0qs2c2mp0dnn0kchsvlck3/T/pip-install-emcbgzcf/cfn-lint/cfnlint/rules/functions/Cidr.pycheck_parameter_count s(   (*zCidr.check_parameter_countc Csg}|jj|i}|rd|g}|jd}|dkr|jd}|jd}| s\|dks\|dkrd} |jt|dg| jd jtt|dg| s|dks|dkrd } |jt|dg| jd jtt|dgn&d } |jt|| jd jtt||S) z Check SizeMask Parameter if usedrrrr r r r z_Parameter for Cidr sizeMask have MinValue between 1 and 128 (for ipv6) and 32 (for ipv4) at {0}r z\Parameter for Cidr count have MaxValue between 1 and 128 (for ipv6) and 32 (for ipv4) at {0}z6Parameter for Cidr count have be of Type Number at {0})rrrrrrrr) rrrrrrrrrrrrr check_parameter_size_mask9s(   (*zCidr.check_parameter_size_maskc Csg}|jd}dddddg}g}g}x|D]}|d}|dd} t|trt|dkrx|d } |d} t|d kr|d } nd} t| trt| dkrx^| jD]R\} }| |krd }|jt| ddd g|jd j t t | ddd gqWnt| t j t jfrltjt| sd}|jt| ddd g|jd j t t | ddd gnBd}|jt| ddd g|jd j t t | ddd gt| trBt| dkrx| jD]j\} }| |kr&d}|jt| dddg|jd j t t | dddg| dkr|j|qWnPt| t jsd}|jt| dddg|jd j t t | dddgt| tr&t| dkrvx| jD]j\} }| |kr d}|jt| ddd g|jd j t t | ddd g| dkr|j|qWnPt| t jsd}|jt| ddd g|jd j t t | ddd gn&d}|jt| |jd j t t | q,d}|jt| |jd j t t | q,Wx$t|D]}|j|j||qWx$t|D]}|j|j||qW|S)zCheck CloudFormation CidrzFn::Cidrz Fn::SelectZRefz Fn::GetAttzFn::SubzFn::ImportValuer NrzECidr ipBlock should be Cidr Range, Ref, GetAtt, Sub or Select for {0}r z8Cidr ipBlock should be a Cidr Range based string for {0}z'Cidr ipBlock should be a string for {0}z0Cidr count should be Int, Ref, or Select for {0}z"Cidr count should be a int for {0}z3Cidr sizeMask should be Int, Ref, or Select for {0}z%Cidr sizeMask should be a int for {0}z0Cidr should be a list of 2 or 3 elements for {0}r%)r#r$)Zsearch_deep_keys isinstancelistlendictitemsrrrrrrsix text_type string_typesrematchr integer_typessetextendr!r")rrrZ cidr_objsZsupported_functionsZcount_parametersZsize_mask_parametersZcidr_objZcidr_value_objrZ ip_block_objZ count_objZ size_mask_objZ index_key_rZ index_valueZcount_parameterZsize_mask_parameterrrr r/Ts      >:8  8 8  8 : z Cidr.matchN) __name__ __module__ __qualname____doc__idZ shortdesc descriptionZ source_urltagsr!r"r/rrrr rsr) r7r.r+ZcfnlintrrZcfnlint.helpersrrrrrr s