Skip to content

Commit

Permalink
ImportValue cannot be used inside the Conditions section (#696)
Browse files Browse the repository at this point in the history
  • Loading branch information
kddejong authored and Chuck Meyer committed Mar 3, 2019
1 parent a5667f0 commit cffb4d3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/cfnlint/rules/functions/ImportValue.py
Expand Up @@ -45,9 +45,17 @@ def match(self, cfn):
'Ref'
]

unsupported_locations = [
'Conditions'
]

for iv_obj in iv_objs:
iv_value = iv_obj[-1]
tree = iv_obj[:-1]
if iv_obj[0] in unsupported_locations:
message = 'ImportValue cannot be used inside {0} at {1}'
matches.append(RuleMatch(
tree, message.format(iv_obj[0], '/'.join(map(str, tree[:-1])))))
if isinstance(iv_value, dict):
if len(iv_value) == 1:
for key, _ in iv_value.items():
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/templates/bad/functions/import_value.yaml
@@ -0,0 +1,14 @@
---
AWSTemplateFormatVersion: "2010-09-09"
Conditions:
primaryRegion: !Equals [!Ref 'AWS::Region', !ImportValue PrimaryRegion]
Resources:
subnet:
Type: AWS::EC2::Subnet
Properties:
CidrBlock:
Fn::ImportValue:
Fn::ImportValue: CidrBlock
VpcId:
Fn::ImportValue:
- PrimaryRegion
34 changes: 34 additions & 0 deletions test/rules/functions/test_import_value.py
@@ -0,0 +1,34 @@
"""
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.
"""
from cfnlint.rules.functions.ImportValue import ImportValue # pylint: disable=E0401
from .. import BaseRuleTestCase


class TestImportValue(BaseRuleTestCase):
"""Test Rules Get Att """
def setUp(self):
"""Setup"""
super(TestImportValue, self).setUp()
self.collection.register(ImportValue())

def test_file_positive(self):
"""Test Positive"""
self.helper_file_positive()

def test_file_negative(self):
"""Test failure"""
self.helper_file_negative('test/fixtures/templates/bad/functions/import_value.yaml', 3)

0 comments on commit cffb4d3

Please sign in to comment.