3 D\B@sdZddlZddlmZddlZddlZddlZddlZeje Z Gddde Z ddZ Gdd d eZGd d d eZGd d d eZGdddeZdS)a Copyright 2018 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)copyc@seZdZdZdS)ConditionParseErrorzG Error thrown when the template has poorly formatted condition N)__name__ __module__ __qualname____doc__rrl/private/var/folders/pf/wv4htv3x0qs2c2mp0dnn0kchsvlck3/T/pip-install-emcbgzcf/cfn-lint/cfnlint/conditions.pyrsrcCstjtj|ddjdjS)z Return a hash of an object T) sort_keyszutf-8)hashlibsha1jsondumpsencode hexdigest)objrrr get_hash!src@s(eZdZdZdZdZddZddZdS) EqualsValuez holds the values of a equals NcCspt|tr2t|dkr"t||_qltjdtn:t|tj rF||_ n&t|tj r^t ||_ ntjdtdS)Nz"Length of the object needs to be 1z'Equals value has to be string or object) isinstancedictlenrFunctionLOGGERdebugrsix string_typesString integer_typesstr)selfvaluerrr __init__+s        zEqualsValue.__init__cCs||j|jgkS)N)rr)r otherrrr __eq__;szEqualsValue.__eq__)rrrrrrr"r$rrrr r&s rc@s(eZdZdZdZdZddZddZdS)Equalsz Equals operator NcCsVt|trDt|dkr4t|d|_t|d|_qRtjdtntjdtdS)NrrzLength of Equals needs to be 2zEquals needs to be a list) rlistrrLeftRightrrr)r equalsrrr r"Ds    zEquals.__init__cCsBx<|jD]0\}}||jkr&||jkS||jkr ||jkSq WdS)z- Do an equals based on the provided scenario N)itemsr(r))r scenariosscenarior!rrr testPs    z Equals.test)rrrrr(r)r"r.rrrr r%?s  r%c@sNeZdZdZdZdZdZdZdZd ddZ ddZ ddZ d d Z d d Z dS) Conditionz Individual condition Nc Csg|_g|_g|_i|_|dk rn|jdij|i}y|j||Wqtk rjtjd|d|_ YqXn@|dk ry|j||Wn&tk rtjd|d|_ YnXdS)N ConditionszError parsing condition: %s) AndOrNotInfluenced_Equalsgetprocess_conditionrrrr%)r templatenameZ sub_conditionr!rrr r"cs"  zCondition.__init__cCs||jr(x|jD]}|j|sdSqWdS|jrPx|jD]}|j|r6dSq6WdS|jrpx|jD]}|j| SW|jj|S)z& Test a condition based on a scenario FT)r1r.r2r3r%)r r,aonrrr r.vs     zCondition.testcCs|jjrn|jj|jjs(t|j|jj<|jjrJ|j|jjj|jjn$|jjdk rn|j|jjj|jj|jjr|jj|jjst|j|jj<|jjr|j|jjj|jjn$|jjdk r|j|jjj|jjdS)z+ Get influenced equals from sub conditions N)r(rr4r5setr)addr)r equalrrr process_influenced_equals  z"Condition.process_influenced_equalc Cst|trt|dkrx|jD]\}}|tjjkrF|j|||_q"|tjj krb|j|||_ q"|tjj kr~|j|||_ q"|tjj krt|}|j|||_q"|dkr"|jdij|i}y|j||Wq"tk rtjd|Yq"Xq"WntjdtntjdtdS)z process condition rr/r0zError parsing condition: %szLength of the object must be 1zCondition has to be an objectN)rrrr+cfnlinthelpersZ FUNCTION_ANDprocess_functionr1Z FUNCTION_ORr2Z FUNCTION_NOTr3FUNCTION_EQUALSr%r?r5r6rrr)r r7r! func_nameZ func_valuer>rrr r6s.         zCondition.process_conditionc CsHg}x<|D]2}t|tr t|dkr x|jD]\}}|tjjkrht|}|j||j |q4|dkrt ||}|j |x|j jD]>\} } |j j | st |j | <x| D]} |j | j| qWqWq4t |d|}|j |xP|j jD]B\} } |j j | st |j | <x| D]} |j | j| qWqWq4Wq W|S)z Process Function rr/N)rrrr+r@rArCr%r?appendr/r4r5r<r=) r r7valuesresultsr!kvr> conditionZi_e_kZi_e_vs_vrrr rBs2              "zCondition.process_function)NN)rrrrr1r2r3r%r4r"r.r?r6rBrrrr r/[s r/c@s<eZdZdZdZdZdZddZddZddZ d d Z dS) r0z All the conditions NcCsi|_i|_i|_y|j|jtjj|_x(|jj diD]}t |j||j|<q:WxF|jj dij D]0\}}t |j dt rh|j d|jtd|i<qhWWn.tk r}ztjd|WYdd}~XnXdS)Nr0 ParametersZ AllowedValuesZRefz)While processing conditions got error: %s)r0r%rL_get_condition_equalsZsearch_deep_keysr@rArCr7r5r/r+rr'r Exceptionrr)r Zcfncondition_nameZparameter_nameparameter_valueserrrrr r"s zConditions.__init__c Csi}x|D]}|ddkr |d}|d }t|tr t|dkr d}d}d}d}t|dtrpt|d}n6t|dtjr|d}nt|dtjrt|d}t|dtrt|d}n8t|dtjr|d}nt|dtjrt|d}|rF||krg||<|r0||j |d|dn||j |d|d|r ||kr\g||<|rz||j |d|dq ||j |d|dq W|S) z Get Conditions based on Condition definitions Input: Cfn (Template) Output: List of hashes of Equal dict objects (Ref or FindInMap) rr0rr&Nr)r/TypeValuestring) rr'rrrrrrrrE) r r*rGr>rOZ dict_hash_1Zvalue_1Z dict_hash_2Zvalue_2rrr rMs`            z Conditions._get_condition_equalscCsfg}|s,x"|D]}i}|||<|j|qWx4|D],}x&|D]}t|}|||<|j|q.multiply_equalsTF) r0r5r4r+r<rEr=rYrLr.)r Z conditionsZmatched_equalsZmatched_conditionsrGrJZ equal_keyZ equal_valuesrKr\r,Zcon_hashr[r-Z r_conditionrrr get_scenarios:s<      /    zConditions.get_scenarios) rrrrr0r%rLr"rMrYr]rrrr r0s?r0)rr rr loggingrZcfnlint.helpersr@ getLoggerrrrNrrobjectrr%r/r0rrrr s  x