Skip to content

Commit

Permalink
fix(apigatewayv2): managed exception NotFoundException (#6589)
Browse files Browse the repository at this point in the history
Co-authored-by: Hugo Pereira Brito <[email protected]>
  • Loading branch information
prowler-bot and HugoPBrito authored Jan 17, 2025
1 parent b2e322f commit 240f572
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from typing import Optional

from botocore.exceptions import ClientError
from pydantic import BaseModel

from prowler.lib.logger import logger
from prowler.lib.scan_filters.scan_filters import is_resource_filtered
from prowler.providers.aws.lib.service.service import AWSService


################## ApiGatewayV2
class ApiGatewayV2(AWSService):
def __init__(self, provider):
# Call AWSService's __init__
Expand Down Expand Up @@ -71,6 +71,15 @@ def _get_stages(self):
tags=[stage.get("Tags")],
)
)
except ClientError as error:
if error.response["Error"]["Code"] == "NotFoundException":
logger.warning(
f"{regional_client.region} -- {error.__class__.__name__}[{error.__traceback__.tb_lineno}]: {error}"
)
else:
logger.error(
f"{regional_client.region} -- {error.__class__.__name__}[{error.__traceback__.tb_lineno}]: {error}"
)
except Exception as error:
logger.error(
f"{error.__class__.__name__}:{error.__traceback__.tb_lineno} -- {error}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ def mock_make_api_call(self, operation_name, kwarg):
if operation_name == "GetAuthorizers":
return {"Items": [{"AuthorizerId": "authorizer-id", "Name": "test-authorizer"}]}
elif operation_name == "GetStages":
if kwarg["ApiId"] == "not-found-api":
raise botocore.exceptions.ClientError(
{
"Error": {
"Code": "NotFoundException",
"Message": "API not found",
}
},
"GetStages",
)
return {
"Items": [
{
Expand Down Expand Up @@ -120,3 +130,24 @@ def test_get_stages(self):
aws_provider = set_mocked_aws_provider([AWS_REGION_US_EAST_1])
apigatewayv2 = ApiGatewayV2(aws_provider)
assert apigatewayv2.apis[0].stages[0].logging is True

# Test ApiGatewayV2 Get Stages with NotFoundException
@mock_aws
@patch("prowler.providers.aws.services.apigatewayv2.apigatewayv2_service.logger")
def test_get_stages_not_found_exception(self, mock_logger):
# Generate ApiGatewayV2 Client
apigatewayv2_client = client("apigatewayv2", region_name=AWS_REGION_US_EAST_1)
# Create ApiGatewayV2 Rest API
apigatewayv2_client.create_api(Name="test-api", ProtocolType="HTTP")

aws_provider = set_mocked_aws_provider([AWS_REGION_US_EAST_1])
apigatewayv2 = ApiGatewayV2(aws_provider)

# Force API ID to trigger NotFoundException
apigatewayv2.apis[0].id = "not-found-api"

# Call _get_stages to trigger the exception
apigatewayv2._get_stages()

mock_logger.warning.assert_called_once()
assert "NotFoundException" in mock_logger.warning.call_args[0][0]

0 comments on commit 240f572

Please sign in to comment.