Skip to content

Commit bb930cc

Browse files
prenx4xMufaddal Makati
andauthored
Fix: Regex issue and Keyerror crash (#1794)
* Fix: Regex issue when basepath is / * fix: keyerror in open_api when method is ANY * fixed python 2.7 compatibility issue Co-authored-by: Mufaddal Makati <[email protected]>
1 parent bd3fe59 commit bb930cc

File tree

6 files changed

+48
-7
lines changed

6 files changed

+48
-7
lines changed

samtranslator/model/api/http_api_generator.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,16 @@ def _construct_basepath_mappings(self, basepaths, http_api):
340340
invalid_regex = r"[^0-9a-zA-Z\/\-\_]+"
341341
if re.search(invalid_regex, path) is not None:
342342
raise InvalidResourceException(self.logical_id, "Invalid Basepath name provided.")
343-
# ignore leading and trailing `/` in the path name
344-
m = re.search(r"[a-zA-Z0-9]+[\-\_]?[a-zA-Z0-9]+", path)
345-
path = m.string[m.start(0) : m.end(0)]
346-
if path is None:
347-
raise InvalidResourceException(self.logical_id, "Invalid Basepath name provided.")
343+
344+
if path == "/":
345+
path = ""
346+
else:
347+
# ignore leading and trailing `/` in the path name
348+
m = re.search(r"[a-zA-Z0-9]+[\-\_]?[a-zA-Z0-9]+", path)
349+
path = m.string[m.start(0) : m.end(0)]
350+
if path is None:
351+
raise InvalidResourceException(self.logical_id, "Invalid Basepath name provided.")
352+
348353
logical_id = "{}{}{}".format(self.logical_id, re.sub(r"[\-\_]+", "", path), "ApiMapping")
349354
basepath_mapping = ApiGatewayV2ApiMapping(logical_id, attributes=self.passthrough_resource_attributes)
350355
basepath_mapping.DomainName = ref(self.domain.get("ApiDomainName"))

samtranslator/open_api/open_api.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,14 @@ def set_path_default_authorizer(self, path, default_authorizer, authorizers, api
336336
if normalized_method_name != "options":
337337
normalized_method_name = self._normalize_method_name(method_name)
338338
# It is possible that the method could have two definitions in a Fn::If block.
339+
if normalized_method_name not in self.get_path(path):
340+
raise InvalidDocumentException(
341+
[
342+
InvalidTemplateException(
343+
"Could not find {} in {} within DefinitionBody.".format(normalized_method_name, path)
344+
)
345+
]
346+
)
339347
for method_definition in self.get_method_contents(self.get_path(path)[normalized_method_name]):
340348
# If no integration given, then we don't need to process this definition (could be AWS::NoValue)
341349
if not self.method_definition_has_integration(method_definition):

tests/model/api/test_http_api_generator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,14 @@ def test_basepaths(self):
237237
self.kwargs["domain"] = {
238238
"DomainName": "example.com",
239239
"CertificateArn": "some-url",
240-
"BasePath": ["one-1", "two_2", "three"],
240+
"BasePath": ["one-1", "two_2", "three", "/"],
241241
"Route53": {"HostedZoneId": "xyz", "HostedZoneName": "abc", "IpV6": True},
242242
}
243243
http_api = HttpApiGenerator(**self.kwargs)._construct_http_api()
244244
domain, basepath, route = HttpApiGenerator(**self.kwargs)._construct_api_domain(http_api)
245245
self.assertIsNotNone(domain, None)
246246
self.assertIsNotNone(basepath, None)
247-
self.assertEqual(len(basepath), 3)
247+
self.assertEqual(len(basepath), 4)
248248
self.assertIsNotNone(route, None)
249249
self.assertEqual(route.HostedZoneName, None)
250250
self.assertEqual(route.HostedZoneId, "xyz")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Resources:
2+
SomeHttpApi:
3+
Type: AWS::Serverless::HttpApi
4+
Properties:
5+
DefinitionBody:
6+
paths:
7+
"/{domain}":
8+
any:
9+
responses: {}
10+
"/{domain/}{id}":
11+
any:
12+
responses: {}
13+
openapi: 3.0.1
14+
Auth:
15+
Authorizers:
16+
OAuth2Authorizer:
17+
AuthorizationScopes:
18+
- email
19+
JwtConfiguration:
20+
audience:
21+
- randomnumber
22+
issuer: https://some/issuer
23+
IdentitySource: "$request.headers.Authorization"
24+
DefaultAuthorizer: OAuth2Authorizer
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Structure of the SAM template is invalid. Could not find x-amazon-apigateway-any-method in /{domain} within DefinitionBody."
3+
}

tests/translator/test_translator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ def _generate_new_deployment_hash(self, logical_id, dict_to_hash, rest_api_to_sw
665665
"error_httpapi_mtls_configuration_invalid_field",
666666
"error_httpapi_mtls_configuration_invalid_type",
667667
"error_resource_policy_not_dict",
668+
"error_implicit_http_api_auth_any_method",
668669
],
669670
)
670671
@patch("boto3.session.Session.region_name", "ap-southeast-1")

0 commit comments

Comments
 (0)