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

fix(parser): apigw wss validation check_message_id; housekeeping #553

Merged
merged 7 commits into from
Jul 30, 2021
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
4 changes: 2 additions & 2 deletions aws_lambda_powertools/tracing/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def decorate(event, context, **kwargs):
# see #465
@overload
def capture_method(self, method: "AnyCallableT") -> "AnyCallableT":
...
... # pragma: no cover

@overload
def capture_method(
Expand All @@ -344,7 +344,7 @@ def capture_method(
capture_response: Optional[bool] = None,
capture_error: Optional[bool] = None,
) -> Callable[["AnyCallableT"], "AnyCallableT"]:
...
... # pragma: no cover

def capture_method(
self,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Any, Dict, Optional
from typing import Any, Dict, Optional, cast

from botocore.config import Config

Expand Down Expand Up @@ -56,11 +56,15 @@ def get_json_configuration(self) -> Dict[str, Any]:
parsed JSON dictionary
"""
try:
return self._conf_store.get(
name=self.configuration_name,
transform=TRANSFORM_TYPE,
max_age=self._cache_seconds,
) # parse result conf as JSON, keep in cache for self.max_age seconds
# parse result conf as JSON, keep in cache for self.max_age seconds
return cast(
dict,
self._conf_store.get(
name=self.configuration_name,
transform=TRANSFORM_TYPE,
max_age=self._cache_seconds,
),
)
except (GetParameterError, TransformParameterError) as exc:
error_str = f"unable to get AWS AppConfig configuration file, exception={str(exc)}"
self._logger.error(error_str)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ def idempotent(
try:
return idempotency_handler.handle()
except IdempotencyInconsistentStateError:
if i < max_handler_retries:
continue
else:
if i == max_handler_retries:
Copy link
Contributor

Choose a reason for hiding this comment

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

nice one!!

# Allow the exception to bubble up after max retries exceeded
raise

Expand Down Expand Up @@ -117,7 +115,6 @@ def __init__(
self.context = context
self.event = event
self.lambda_handler = lambda_handler
self.max_handler_retries = 2

def handle(self) -> Any:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def _update_record(self, data_record: DataRecord):
"ExpressionAttributeNames": expression_attr_names,
}

self.table.update_item(**kwargs)
self.table.update_item(**kwargs) # type: ignore

def _delete_record(self, data_record: DataRecord) -> None:
logger.debug(f"Deleting record for idempotency key: {data_record.idempotency_key}")
Expand Down
7 changes: 1 addition & 6 deletions aws_lambda_powertools/utilities/parameters/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""


from typing import Any, Dict, Optional
from typing import Dict, Optional

import boto3
from boto3.dynamodb.conditions import Key
Expand Down Expand Up @@ -141,11 +141,6 @@ class DynamoDBProvider(BaseProvider):
c Parameter value c
"""

table: Any = None
key_attr = None
sort_attr = None
value_attr = None

def __init__(
self,
table_name: str,
Expand Down
14 changes: 7 additions & 7 deletions aws_lambda_powertools/utilities/parser/models/apigw.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ class APIGatewayEventRequestContext(BaseModel):
routeKey: Optional[str]
operationName: Optional[str]

@root_validator
def check_message_id(cls, values):
michaelbrewer marked this conversation as resolved.
Show resolved Hide resolved
message_id, event_type = values.get("messageId"), values.get("eventType")
if message_id is not None and event_type != "MESSAGE":
raise TypeError("messageId is available only when the `eventType` is `MESSAGE`")
return values


class APIGatewayProxyEventModel(BaseModel):
version: Optional[str]
Expand All @@ -83,10 +90,3 @@ class APIGatewayProxyEventModel(BaseModel):
stageVariables: Optional[Dict[str, str]]
isBase64Encoded: bool
body: str

@root_validator()
def check_message_id(cls, values):
message_id, event_type = values.get("messageId"), values.get("eventType")
michaelbrewer marked this conversation as resolved.
Show resolved Hide resolved
if message_id is not None and event_type != "MESSAGE":
raise TypeError("messageId is available only when the `eventType` is `MESSAGE`")
return values
1 change: 0 additions & 1 deletion tests/functional/idempotency/test_idempotency.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@ def test_idempotent_lambda_expired_during_request(
lambda_apigw_event,
timestamp_expired,
lambda_response,
expected_params_update_item,
hashed_idempotency_key,
lambda_context,
):
Expand Down
44 changes: 44 additions & 0 deletions tests/functional/parser/test_apigw.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import pytest
from pydantic import ValidationError

from aws_lambda_powertools.utilities.parser import envelopes, event_parser
from aws_lambda_powertools.utilities.parser.models import APIGatewayProxyEventModel
from aws_lambda_powertools.utilities.typing import LambdaContext
Expand Down Expand Up @@ -100,3 +103,44 @@ def test_apigw_event():
assert request_context.operationName is None
assert identity.apiKey is None
assert identity.apiKeyId is None


def test_apigw_event_with_invalid_websocket_request():
# GIVEN an event with an eventType != MESSAGE and has a messageId
michaelbrewer marked this conversation as resolved.
Show resolved Hide resolved
event = {
"resource": "/",
"path": "/",
"httpMethod": "GET",
"headers": {},
"multiValueHeaders": {},
"isBase64Encoded": False,
"body": "Foo!",
"requestContext": {
"accountId": "1234",
"apiId": "myApi",
"httpMethod": "GET",
"identity": {
"sourceIp": "127.0.0.1",
},
"path": "/",
"protocol": "Https",
"requestId": "1234",
"requestTime": "2018-09-07T16:20:46Z",
"requestTimeEpoch": 1536992496000,
"resourcePath": "/",
"stage": "test",
"eventType": "DISCONNECT",
"messageId": "messageId",
},
}

# WHEN calling event_parser with APIGatewayProxyEventModel
with pytest.raises(ValidationError) as err:
handle_apigw_event(event, LambdaContext())

# THEN raise TypeError for invalid event
errors = err.value.errors()
assert len(errors) == 1
expected_msg = "messageId is available only when the `eventType` is `MESSAGE`"
assert errors[0]["msg"] == expected_msg
assert expected_msg in str(err.value)