3 D\"@s8dZddlZddlmZddlmZGdddeZdS)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) RuleMatchc@sPeZdZdZdZdZdZdZddgZdd Z d d Z d d Z ddZ ddZ dS)SubzCheck if Sub values are correctZE1019zSub validation of parametersz3Making sure the sub function is properly configuredzdhttps://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-sub.htmlZ functionssubcCsBg}|j|}x.|D]&}t|tjr|j|j||||qW|S)z+Test if a string has appropriate parameters)Zget_sub_parameters isinstancesix string_typesextend_test_parameter)selfcfn sub_string parameterstreematchesZ string_paramsZ string_paramru/private/var/folders/pf/wv4htv3x0qs2c2mp0dnn0kchsvlck3/T/pip-install-emcbgzcf/cfn-lint/cfnlint/rules/functions/Sub.py _test_strings    zSub._test_stringcCsFi}|jjdi}t|trBx$|jD]\}}|jdd||<q&W|S)zGet all Parameter NamesZ ParametersTypeString)templategetrdictitems)r r resultsr param_nameZ param_valuesrrr_get_parameters*s  zSub._get_parametersc Cszddddddddd d g }g}xV|jD]H\}}|d d |g}t|tr@t|d krx|jD]\} } | |krd } |jt|| jdjtt |qh| dkr|j |j | |i|qh| dkrht| t r|j |j dj| |i|qht| t jrh|j |j | |i|qhWn&d} |jt|| jdjtt |q(t|t js(d} |jt|| jdjtt |q(W|S)z.Check parameters for appropriate configurationz Fn::Base64z Fn::FindInMap Fn::GetAttz Fn::GetAZszFn::ImportValuezFn::IfzFn::Joinz Fn::SelectzFn::SubRefNz1Sub parameter should use a valid function for {0}/.z.Sub parameter should be an object of 1 for {0}z8Sub parameter should be an object of 1 or string for {0})r)r)rrrlenappendrformatjoinmapstrr r listrr) r rr rZsupported_functionsrZparameter_nameZparameter_value_objZ param_treekeyvaluemessagerrr_test_parameters6sF     zSub._test_parametersc Csg}|j}ddddddddg}dd g}|} | j|j|j|} x|jD]\} } | j| qNW|| krd } || krd } | j||ks| j|jd s| j|jd rd}|jt||j |dj t t |x|jD]\}}x|jD]\}}||j ddkrT|dkrT|jdd krNd}|jt||j |dj t t |d } q||j ddkr|dj |j dddkr|jdd krd}|jt||j |dj t t |d } qWqW| sd}|jt||j |dj t t ||S)z Test a parameter z AWS::RegionzAWS::StackNamezAWS::URLSuffixz AWS::StackIdzAWS::PartitionzAWS::NotificationARNszAWS::AccountIdZCommaDelimitedListz.AWS::SSM::Parameter::ValueFTzAWS::SSM::Parameter::Valuer)r;rZcfnlintrrrrrrrs