From eb8cf40bf500dfd3a8347bd390cf9aec1dd458ea Mon Sep 17 00:00:00 2001 From: Guillaume Delacour Date: Fri, 26 Jun 2020 21:43:44 +0200 Subject: [PATCH] Improve template detection --- .automation/test/cfn/cfn_bad_3.json | 16 ++++++++++++++++ .automation/test/cfn/cfn_good_3.json | 16 ++++++++++++++++ Dockerfile | 2 +- lib/linter.sh | 18 ++++++++++++++++-- 4 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 .automation/test/cfn/cfn_bad_3.json create mode 100644 .automation/test/cfn/cfn_good_3.json diff --git a/.automation/test/cfn/cfn_bad_3.json b/.automation/test/cfn/cfn_bad_3.json new file mode 100644 index 00000000..37ae6b3a --- /dev/null +++ b/.automation/test/cfn/cfn_bad_3.json @@ -0,0 +1,16 @@ +{ + "Resources" : { + "myDNSRecord" : { + "Type" : "AWS::Route53::RecordSet", + "Properties" : { + "HostedZoneId" : "Z8VLZEXAMPLE", + "Name" : "test.example.com", + "ResourceRecords" : [ + "192.0.2.99" + ], + "Ttl" : 300, + "Type" : "A" + } + } + } +} diff --git a/.automation/test/cfn/cfn_good_3.json b/.automation/test/cfn/cfn_good_3.json new file mode 100644 index 00000000..f8602d7a --- /dev/null +++ b/.automation/test/cfn/cfn_good_3.json @@ -0,0 +1,16 @@ +{ + "Resources" : { + "myDNSRecord" : { + "Type" : "AWS::Route53::RecordSet", + "Properties" : { + "HostedZoneId" : "Z8VLZEXAMPLE", + "Name" : "test.example.com", + "ResourceRecords" : [ + "192.0.2.99" + ], + "TTL" : 300, + "Type" : "A" + } + } + } +} diff --git a/Dockerfile b/Dockerfile index e332654c..9806ee49 100644 --- a/Dockerfile +++ b/Dockerfile @@ -53,7 +53,7 @@ RUN mkdir -p /opt/microsoft/powershell/7 \ # Run Pip3 Installs # ##################### RUN pip3 --no-cache-dir install --upgrade --no-cache-dir \ - yamllint pylint yq cfn-lint + yamllint pylint yq cfn-lint shyaml #################### # Run NPM Installs # diff --git a/lib/linter.sh b/lib/linter.sh index 65f68b93..bf4a78e4 100755 --- a/lib/linter.sh +++ b/lib/linter.sh @@ -616,11 +616,25 @@ DetectOpenAPIFile() # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-formats.html DetectCloudFormationFile() { + # AWSTemplateFormatVersion is optional + # grep -E '("|)AWSTemplateFormatVersion("|):' if grep 'AWSTemplateFormatVersion' "${1}" > /dev/null; then return 0 - else - return 1 fi + if cat "${1}" | shyaml --quiet get-type AWSTemplateFormatVersion > /dev/null; then + return 0 + fi + + if cat "${1}" | jq -e 'has("Resources")' > /dev/null 2>&1; then + if cat "${1}" | jq ".Resources[].Type" 2>/dev/null | grep -q -E "(AWS|Alexa|Custom)"; then + return 0 + fi + fi + if cat "${1}" | shyaml values-0 Resources | grep -q -E "Type: (AWS|Alexa|Custom)"; then + return 0 + fi + + return 1 } ################################################################################