Skip to content
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

Update ARN delimiter parsing in endpoint provider #2831

Merged
merged 2 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/next-release/bugfix-Endpointprovider-32427.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "bugfix",
"category": "Endpoint provider",
"description": "Updates ARN parsing ``resourceId`` delimiters"
}
4 changes: 2 additions & 2 deletions botocore/endpoint_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
VALID_HOST_LABEL_RE = re.compile(
r"^(?!-)[a-zA-Z\d-]{1,63}(?<!-)$",
)
ARN_DELIMITER_RE = re.compile(r"/|:")
CACHE_SIZE = 100
ARN_PARSER = ArnParser()
STRING_FORMATTER = Formatter()
Expand Down Expand Up @@ -236,8 +237,7 @@ def aws_parse_arn(self, value):
arn_dict["accountId"] = arn_dict.pop("account")

resource = arn_dict.pop("resource")
delimiter = ":" if ":" in resource else "/"
arn_dict["resourceId"] = resource.split(delimiter)
arn_dict["resourceId"] = re.split(ARN_DELIMITER_RE, resource)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if there is a difference, but why not use split on the compiled object?

Suggested change
arn_dict["resourceId"] = re.split(ARN_DELIMITER_RE, resource)
arn_dict["resourceId"] = ARN_DELIMITER_RE.split(resource)


return arn_dict

Expand Down
41 changes: 41 additions & 0 deletions tests/unit/test_endpoint_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,47 @@ def test_invalid_arn_returns_none(rule_lib):
assert rule_lib.aws_parse_arn("arn:aws:this-is-not-an-arn:foo") is None


@pytest.mark.parametrize(
"arn, expected_resource_id",
[
(
"arn:aws:s3:::myBucket/key",
{
"partition": "aws",
"service": "s3",
"region": "",
"accountId": "",
"resourceId": ["myBucket", "key"],
},
),
(
"arn:aws:kinesis:us-east-1:1234567890:stream/mystream:foo",
{
"partition": "aws",
"service": "kinesis",
"region": "us-east-1",
"accountId": "1234567890",
"resourceId": ["stream", "mystream", "foo"],
},
),
(
"arn:aws:s3:::myBucket",
{
"partition": "aws",
"service": "s3",
"region": "",
"accountId": "",
"region": "",
"resourceId": ["myBucket"],
},
),
],
)
def test_parse_arn_delimiters(rule_lib, arn, expected_resource_id):
parsed_arn = rule_lib.aws_parse_arn(arn)
assert parsed_arn == expected_resource_id


def test_uri_encode_none_returns_none(rule_lib):
assert rule_lib.uri_encode(None) is None

Expand Down