Skip to content

Commit

Permalink
Validate typename prefix is valid when creating ListType request whil…
Browse files Browse the repository at this point in the history
…e resolving hook target names (#1022)
  • Loading branch information
Brianwithay21 authored Sep 22, 2023
1 parent 2fee279 commit 0526b80
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/rpdk/core/type_name_resolver.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fnmatch
import logging
import os
import re

from botocore.exceptions import ClientError

Expand All @@ -13,6 +14,9 @@
REGISTRY_VISIBILITY_PRIVATE = "PRIVATE"
REGISTRY_VISIBILITY_PUBLIC = "PUBLIC"
REGISTRY_RESULTS_PAGE_SIZE = 100
TYPE_NAME_PREFIX_REGEX = (
r"([A-Za-z0-9]{2,64}::){0,2}([A-Za-z0-9]{2,64}:?){0,1}(:[A-Za-z0-9:]{2,64}){0,1}"
)


def contains_wildcard(pattern):
Expand Down Expand Up @@ -132,7 +136,7 @@ def _create_list_types_request(type_names):
prefix = prefix[:index]

req = {}
if prefix:
if prefix and re.fullmatch(TYPE_NAME_PREFIX_REGEX, prefix):
req["Filters"] = {"TypeNamePrefix": prefix}

return req
7 changes: 5 additions & 2 deletions tests/test_type_name_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,18 @@ def test_resolve_type_names_locally_no_local_info(resolver):
(["AWS::S?::Bucket"], "AWS::S"),
(["AWS::S?::*Bucket"], "AWS::S"),
(["AWS::*::Log*"], "AWS::"),
(["A*::S?::Bucket*"], "A"),
(["A*::S?::Bucket*"], None),
(["*::S?::Bucket*"], ""),
(["AWS::S?::BucketPolicy", "AWS::S3::Bucket"], "AWS::S"),
(["AWS::S*::Queue", "AWS::DynamoDB::*"], "AWS::"),
(["AwsCommunity::S3::BucketVersioningEnabled", "AWS::*"], None),
(["AWSSamples::*", f'AW{"S" * 64}*', "AWS::S3::Bucket"], "AWS"),
([f'AW{"S" * 64}*'], None),
],
)
def test_create_list_types_request(type_names, expected):
req = TypeNameResolver._create_list_types_request(type_names)
if not expected:
assert not req
assert req == {}
else:
assert req == {"Filters": {"TypeNamePrefix": expected}}

0 comments on commit 0526b80

Please sign in to comment.