-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: package/deploy failure when Location/TemplateURL is virtual host S3 URL #2785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6560821
Rename is_s3_url() to is_s3_protocol_url()
aahung d523c4c
Add is_s3_url() to handle all kinds of s3 urls
aahung c5dbfb2
Use the new is_s3_url() in CloudFormationStackResource
aahung 8e9fa7c
Move all constants to top
aahung e637e3d
Merge branch 'develop' into parse-all-types-s3-urls
aahung 6b86814
Merge branch 'develop' into parse-all-types-s3-urls
aahung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| import logging | ||
| import os | ||
| import platform | ||
| import re | ||
| import shutil | ||
| import tempfile | ||
| import uuid | ||
|
|
@@ -22,6 +23,29 @@ | |
|
|
||
| LOG = logging.getLogger(__name__) | ||
|
|
||
| # https://docs.aws.amazon.com/AmazonS3/latest/dev-retired/UsingBucket.html | ||
| _REGION_PATTERN = r"[a-zA-Z0-9-]+" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets move these to the top of file, if they are constants, after all the imports.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
| _DOT_AMAZONAWS_COM_PATTERN = r"\.amazonaws\.com" | ||
aahung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| _S3_URL_REGEXS = [ | ||
| # Path-Style (and ipv6 dualstack) | ||
| # - https://s3.Region.amazonaws.com/bucket-name/key name | ||
| # - https://s3.amazonaws.com/bucket-name/key name (old, without region) | ||
| # - https://s3.dualstack.us-west-2.amazonaws.com/... | ||
| re.compile(rf"http(s)?://s3(.dualstack)?(\.{_REGION_PATTERN})?{_DOT_AMAZONAWS_COM_PATTERN}/.+/.+"), | ||
| # Virtual Hosted-Style (including two legacies) | ||
| # https://docs.aws.amazon.com/AmazonS3/latest/userguide/VirtualHosting.html | ||
| # - Virtual Hosted-Style: https://bucket-name.s3.Region.amazonaws.com/key name | ||
| # - Virtual Hosted-Style (Legacy: a dash between S3 and the Region): https://bucket-name.s3-Region.amazonaws.com/... | ||
| # - Virtual Hosted-Style (Legacy Global Endpoint): https://my-bucket.s3.amazonaws.com/... | ||
| re.compile(rf"http(s)?://.+\.s3((.|-){_REGION_PATTERN})?{_DOT_AMAZONAWS_COM_PATTERN}/.+"), | ||
| # S3 access point: | ||
| # - https://AccessPointName-AccountId.s3-accesspoint.region.amazonaws.com | ||
| re.compile(rf"http(s)?://.+-\d+\.s3-accesspoint\.{_REGION_PATTERN}{_DOT_AMAZONAWS_COM_PATTERN}/.+/.+"), | ||
| # S3 protocol URL: | ||
| # - s3://bucket-name/key-name | ||
| re.compile(r"s3://.+/.+"), | ||
| ] | ||
|
|
||
|
|
||
| def is_path_value_valid(path): | ||
| return isinstance(path, str) | ||
|
|
@@ -33,14 +57,25 @@ def make_abs_path(directory, path): | |
| return path | ||
|
|
||
|
|
||
| def is_s3_url(url): | ||
| def is_s3_protocol_url(url): | ||
| """ | ||
| Check whether url is a valid path in the form of "s3://..." | ||
| """ | ||
| try: | ||
| S3Uploader.parse_s3_url(url) | ||
| return True | ||
| except ValueError: | ||
| return False | ||
|
|
||
|
|
||
| def is_s3_url(url: str) -> bool: | ||
| """ | ||
| Check whether a URL is a S3 access URL | ||
| specified at https://docs.aws.amazon.com/AmazonS3/latest/dev-retired/UsingBucket.html | ||
| """ | ||
| return any(regex.match(url) for regex in _S3_URL_REGEXS) | ||
|
|
||
|
|
||
| def is_local_folder(path): | ||
| return is_path_value_valid(path) and os.path.isdir(path) | ||
|
|
||
|
|
@@ -122,7 +157,7 @@ def upload_local_artifacts( | |
| # Build the root directory and upload to S3 | ||
| local_path = parent_dir | ||
|
|
||
| if is_s3_url(local_path): | ||
| if is_s3_protocol_url(local_path): | ||
| # A valid CloudFormation template will specify artifacts as S3 URLs. | ||
| # This check is supporting the case where your resource does not | ||
| # refer to local artifacts | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| from unittest import TestCase | ||
|
|
||
| from parameterized import parameterized | ||
|
|
||
| from samcli.lib.package import utils | ||
|
|
||
|
|
||
| class TestPackageUtils(TestCase): | ||
| @parameterized.expand( | ||
| [ | ||
| # path like | ||
| "https://s3.us-west-2.amazonaws.com/bucket-name/some/path/object.html", | ||
| "http://s3.amazonaws.com/bucket-name/some/path/object.html", | ||
| "https://s3.dualstack.us-west-2.amazonaws.com/bucket-name/some/path/object.html", | ||
| # virual host | ||
| "http://bucket-name.s3.us-west-2.amazonaws.com/some/path/object.html", | ||
| "https://bucket-name.s3-us-west-2.amazonaws.com/some/path/object.html", | ||
| "https://bucket-name.s3.amazonaws.com/some/path/object.html", | ||
| # access point | ||
| "https://access-name-123456.s3-accesspoint.us-west-2.amazonaws.com/some/path/object.html", | ||
| "http://access-name-899889.s3-accesspoint.us-east-1.amazonaws.com/some/path/object.html", | ||
| # s3:// | ||
| "s3://bucket-name/path/to/object", | ||
| ] | ||
| ) | ||
| def test_is_s3_url(self, url): | ||
| self.assertTrue(utils.is_s3_url(url)) | ||
|
|
||
| @parameterized.expand( | ||
| [ | ||
| # path like | ||
| "https://s3.$region.amazonaws.com/bucket-name/some/path/object.html", # invalid region | ||
| "https://s3.amazonaws.com/object.html", # no bucket | ||
| # virual host | ||
| "https://bucket-name.s3-us-west-2.amazonaws.com/", # no object | ||
| # access point | ||
| "https://access-name.s3-accesspoint.us-west-2.amazonaws.com/some/path/object.html", # no account id | ||
| # s3:// | ||
| "s3://bucket-name", # no object | ||
| "s3:://bucket-name", # typo | ||
| ] | ||
| ) | ||
| def test_is_not_s3_url(self, url): | ||
| self.assertFalse(utils.is_s3_url(url)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
explain:
self.uploader.s3.meta.endpoint_urllooks likehttps://s3.us-west-2.amazonaws.com. I think originally @sriram-mv 's intention is to make sure the url is in the same region. (@sriram-mv correct me if wrong) I think we don't need to handle it here because if the url lacks region info (likes3://bucket-name/key), we let it pass through. So we can have a consistent behavior here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, we still pass through if there is no region information within the url.