Skip to content

Commit 71ab9b8

Browse files
jadhavmanojkeetonian
authored andcommitted
fix: fix Internal transform failure on bad Condition syntax (#908)
1 parent b0fea81 commit 71ab9b8

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

samtranslator/sdk/resource.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from enum import Enum
2+
from samtranslator.model.exceptions import InvalidDocumentException, InvalidTemplateException
3+
from samtranslator.model.types import is_str
24

35

46
class SamResource(object):
@@ -30,7 +32,15 @@ def valid(self):
3032
3133
:return: True, if the resource is valid
3234
"""
33-
# As long as the type is valid.
35+
# As long as the type is valid and type string.
36+
# validate the condition should be string
37+
38+
if self.condition:
39+
40+
if not is_str()(self.condition, should_raise=False):
41+
raise InvalidDocumentException([
42+
InvalidTemplateException("Every Condition member must be a string.")])
43+
3444
return SamResourceType.has_value(self.type)
3545

3646
def to_dict(self):
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: 'AWS::Serverless-2016-10-31'
3+
Conditions:
4+
MyCondition:
5+
Fn::Equals:
6+
- true
7+
- false
8+
9+
Resources:
10+
ConditionFunction:
11+
Type: 'AWS::Serverless::Function'
12+
Condition:
13+
Ref: MyCondition
14+
Properties:
15+
CodeUri: s3://sam-demo-bucket/hello.zip
16+
Handler: hello.handler
17+
Runtime: python2.7
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"errors": [
3+
{
4+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Structure of the SAM template is invalid. Every Condition member must be a string."
5+
}
6+
],
7+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Structure of the SAM template is invalid. Every Condition member must be a string."
8+
}

tests/translator/test_translator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ class TestTranslatorEndToEnd(TestCase):
252252
'implicit_api_with_many_conditions',
253253
'implicit_and_explicit_api_with_conditions',
254254
'api_with_cors_and_conditions_no_definitionbody',
255-
'api_with_auth_and_conditions_all_max'
255+
'api_with_auth_and_conditions_all_max',
256256
],
257257
[
258258
("aws", "ap-southeast-1"),
@@ -436,7 +436,8 @@ def _generate_new_deployment_hash(self, logical_id, dict_to_hash, rest_api_to_sw
436436
'error_function_policy_template_with_missing_parameter',
437437
'error_function_policy_template_invalid_value',
438438
'error_function_with_unknown_policy_template',
439-
'error_function_with_invalid_policy_statement'
439+
'error_function_with_invalid_policy_statement',
440+
'error_function_with_invalid_condition_name'
440441
])
441442
@patch('boto3.session.Session.region_name', 'ap-southeast-1')
442443
@patch('samtranslator.plugins.application.serverless_app_plugin.ServerlessAppPlugin._sar_service_call', mock_sar_service_call)

0 commit comments

Comments
 (0)