3 D\ @sndZddlZddlZddlZddlZddlmZddlmZddl m Z ddl Z ej dZGdddeZdS) 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)parser) Translator)InvalidDocumentExceptioncfnlintc@sReZdZdZddZddZddZdd Zd d Ze d d Z e dddZ dS) Transformz Application Serverless Module tranform Wrappor. Based on code from AWS SAM CLI: https://github.com/awslabs/aws-sam-cli/blob/develop/samcli/commands/validate/lib/sam_template_validator.py cCs*||_||_||_|j|_tj|_dS)z, Initialize Transform class N) _filename _template_regionload_managed_policies_managed_policy_maprParser _sam_parser)selffilenametemplateZregionrk/private/var/folders/pf/wv4htv3x0qs2c2mp0dnn0kchsvlck3/T/pip-install-emcbgzcf/cfn-lint/cfnlint/transform.py__init__"s  zTransform.__init__cCs|jS)zGet the template)r)rrrrr-szTransform.templatecCs tjjdS)z Load the ManagedPolicies locally, based on the AWS-CLI: https://github.com/awslabs/aws-sam-cli/blob/develop/samcli/lib/samlib/default_managed_policies.json z$data/Serverless/ManagedPolicies.json)rhelpersZload_resources)rrrrr 1szTransform.load_managed_policiescCs|jjdi}x|jD]\}}|jd}|jd}|dkrHtjd||dkrf|jdrftjd||dkr|jd rd |d <tjd ||d krd |krd |krtjd|qd |d <qWdS)aZ Replaces the CodeUri in AWS::Serverless::Function and DefinitionUri in AWS::Serverless::Api to a fake S3 Uri. This is to support running the SAM Translator with valid values for these fields. If this in not done, the template is invalid in the eyes of SAM Translator (the translator does not support local paths) Z ResourcesTypeZ PropertieszAWS::Serverless::FunctionZCodeUriAWS::Serverless::LayerVersionZ ContentUrizAWS::Serverless::ApplicationLocationzAWS::Serverless::ApiZDefinitionBodyZAuthZ DefinitionUriN)r)rgetitemsr_update_to_s3_uri)rZ all_resources_resourceZ resource_typeZ resource_dictrrr_replace_local_codeuri8s"       z Transform._replace_local_codeuricCsHg}yltjdtjt|j|jd}|jtjd|j|jt j d<t j j |j|jid|_tjd|jWntk r}zDd}x8|jD].}|jt jdddd|jt j|j|jqWWYd d }~Xnttk rB}zVtjd t|tjd |d d d}|jt jdddd|jt j|jt|WYd d }~XnX|S)zP Transform the Template using the Serverless Application Model. zSAM Translator: %s)Zmanaged_policy_mapZ sam_parserz Setting AWS_DEFAULT_REGION to %sZAWS_DEFAULT_REGION)Z sam_templateZparameter_valueszTransformed template: %sz Error transforming template: {0}NzError transforming template: %szStack trace: %sT)exc_info)LOGGERdebug samtranslator __version__rr r rr osenvironrrZ convert_dict translaterrZcausesappendZMatchrZTransformErrorformatmessage Exceptionstr)rmatchesZsam_translatorer*causerrrtransform_templateVs6   ..zTransform.transform_templatecCst|tjo|jdS)a Checks the uri and determines if it is a valid S3 Uri Parameters ---------- uri str, required Uri to check Returns ------- bool Returns True if the uri given is an S3 uri, otherwise False zs3://) isinstancesix string_types startswith)urirrr is_s3_uris zTransform.is_s3_uris3://bucket/valuecCs0|j|d}t|ts tj|r$dS|||<dS)a  Updates the 'property_key' in the 'resource_property_dict' to the value of 's3_uri_value' Note: The function will mutate the resource_property_dict that is pass in Parameters ---------- property_key str, required Key in the resource_property_dict resource_property_dict dict, required Property dictionary of a Resource in the template to replace s3_uri_value str, optional Value to update the value of the property_key to .N)rr1dictrr6)Z property_keyZresource_property_dictZ s3_uri_valueZ uri_propertyrrrrs zTransform._update_to_s3_uriN)r7) __name__ __module__ __qualname____doc__rrr rr0 staticmethodr6rrrrrrs * r)r=r%loggingr2r#Zsamtranslator.parserrZ#samtranslator.translator.translatorrZsamtranslator.public.exceptionsrZcfnlint.helpersr getLoggerr!objectrrrrrs