Skip to content

Commit 11f9fef

Browse files
authored
fix: Ensure AutoPublishCodeSha256 resolves to a string (#2173)
* Ensure AutoPublishCodeSha256 resolves to a string * Add unit test for AutoPublishCodeSha256 checking
1 parent 35ed6d0 commit 11f9fef

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

samtranslator/model/sam_resources.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ def to_cloudformation(self, **kwargs):
157157
code_sha256 = None
158158
if self.AutoPublishCodeSha256:
159159
code_sha256 = intrinsics_resolver.resolve_parameter_refs(self.AutoPublishCodeSha256)
160+
if not isinstance(code_sha256, string_types):
161+
raise InvalidResourceException(
162+
self.logical_id,
163+
"AutoPublishCodeSha256 must be a string",
164+
)
160165
lambda_version = self._construct_version(
161166
lambda_function, intrinsics_resolver=intrinsics_resolver, code_sha256=code_sha256
162167
)

samtranslator/translator/logical_id_generator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def __init__(self, prefix, data_obj=None, data_hash=None):
1616
1717
:param prefix: Prefix for the logicalId
1818
:param data_obj: Data object to trigger new changes on. If set to None, this is ignored
19+
:param data_hash: Pre-computed hash, must be a string
1920
"""
2021

2122
data_str = ""

tests/model/test_sam_resources.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,34 @@ def test_with_version_description(self):
235235
generateFunctionVersion = [x for x in cfnResources if isinstance(x, LambdaVersion)]
236236
self.assertEqual(generateFunctionVersion[0].Description, test_description)
237237

238+
@patch("boto3.session.Session.region_name", "ap-southeast-1")
239+
def test_with_autopublish_bad_hash(self):
240+
function = SamFunction("foo")
241+
test_description = "foobar"
242+
243+
function.Runtime = "foo"
244+
function.Handler = "bar"
245+
function.CodeUri = "s3://foobar/foo.zip"
246+
function.AutoPublishAlias = "live"
247+
function.AutoPublishCodeSha256 = {"Fn::Sub": "${parameter1}"}
248+
249+
with pytest.raises(InvalidResourceException):
250+
function.to_cloudformation(**self.kwargs)
251+
252+
@patch("boto3.session.Session.region_name", "ap-southeast-1")
253+
def test_with_autopublish_good_hash(self):
254+
function = SamFunction("foo")
255+
test_description = "foobar"
256+
257+
function.Runtime = "foo"
258+
function.Handler = "bar"
259+
function.CodeUri = "s3://foobar/foo.zip"
260+
function.AutoPublishAlias = "live"
261+
function.AutoPublishCodeSha256 = "08240bdc52933ca4f88d5f75fc88cd3228a48feffa9920c735602433b94767ad"
262+
263+
# confirm no exception thrown
264+
function.to_cloudformation(**self.kwargs)
265+
238266

239267
class TestOpenApi(TestCase):
240268
kwargs = {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Description: Dip Investigation
2+
Parameters:
3+
GitCommitInfo:
4+
Type: String
5+
Default: hashhash
6+
GitDirtInfo:
7+
Type: String
8+
Default: dirtyyy
9+
AWSTemplateFormatVersion: '2010-09-09'
10+
Resources:
11+
Function:
12+
Type: AWS::Serverless::Function
13+
Properties:
14+
VersionDescription:
15+
Fn::Sub: ${GitCommitInfo}-${GitDirtyInfo}
16+
MemorySize: 128
17+
Handler: loader
18+
Role:
19+
Ref: IamRole
20+
CodeUri: s3://some-bucket/somekey
21+
AutoPublishCodeSha256:
22+
Fn::Sub: ${GitCommitInfo}-${GitDirtInfo}-1
23+
Runtime: go1.x
24+
AutoPublishAlias: Alias1
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"errors": [
3+
{
4+
"errorMessage": "[Function] is invalid. AutoPublishCodeSha256 must be a string"
5+
}
6+
],
7+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [Function] is invalid. AutoPublishCodeSha256 must be a string"
8+
}

0 commit comments

Comments
 (0)