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(apigatewayv2): managed exception NotFoundException #6590

Merged
merged 1 commit into from
Jan 17, 2025
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
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 @@
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(

Check warning on line 80 in prowler/providers/aws/services/apigatewayv2/apigatewayv2_service.py

View check run for this annotation

Codecov / codecov/patch

prowler/providers/aws/services/apigatewayv2/apigatewayv2_service.py#L80

Added line #L80 was not covered by tests
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]
Loading