From 2929f3ee359b642427b084a78db0d8852df76b46 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Sat, 26 Mar 2022 20:06:23 -0100 Subject: [PATCH] Logs - fix typo in log-group-name regex (#4972) --- moto/logs/responses.py | 11 +++++++---- tests/test_logs/test_logs.py | 32 +++++++++++++++++++------------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/moto/logs/responses.py b/moto/logs/responses.py index 39f1930ba..fe8900c8c 100644 --- a/moto/logs/responses.py +++ b/moto/logs/responses.py @@ -9,6 +9,9 @@ from .models import logs_backends # See http://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/Welcome.html +REGEX_LOG_GROUP_NAME = r"[-._\/#A-Za-z0-9]+" + + def validate_param( param_name, param_value, constraint, constraint_expression, pattern=None ): @@ -20,7 +23,7 @@ def validate_param( ) if pattern and param_value: try: - assert re.match(pattern, param_value) + assert re.fullmatch(pattern, param_value) except (AssertionError, TypeError): raise InvalidParameterException( constraint=f"Must match pattern: {pattern}", @@ -67,7 +70,7 @@ class LogsResponse(BaseResponse): "logGroupName", "Minimum length of 1. Maximum length of 512.", lambda x: 1 <= len(x) <= 512, - pattern="[.-_/#A-Za-z0-9]+", + pattern=REGEX_LOG_GROUP_NAME, ) metric_transformations = self._get_validated_param( "metricTransformations", "Fixed number of 1 item.", lambda x: len(x) == 1 @@ -90,7 +93,7 @@ class LogsResponse(BaseResponse): "logGroupName", "Minimum length of 1. Maximum length of 512", lambda x: x is None or 1 <= len(x) <= 512, - pattern="[.-_/#A-Za-z0-9]+", + pattern=REGEX_LOG_GROUP_NAME, ) metric_name = self._get_validated_param( "metricName", @@ -139,7 +142,7 @@ class LogsResponse(BaseResponse): "logGroupName", "Minimum length of 1. Maximum length of 512.", lambda x: 1 <= len(x) <= 512, - pattern="[.-_/#A-Za-z0-9]+$", + pattern=REGEX_LOG_GROUP_NAME, ) self.logs_backend.delete_metric_filter(filter_name, log_group_name) diff --git a/tests/test_logs/test_logs.py b/tests/test_logs/test_logs.py index 28d03741f..437be5a11 100644 --- a/tests/test_logs/test_logs.py +++ b/tests/test_logs/test_logs.py @@ -181,26 +181,32 @@ def test_describe_metric_filters_multiple_happy(): @mock_logs def test_delete_metric_filter(): - conn = boto3.client("logs", "us-west-2") + client = boto3.client("logs", "us-west-2") - response = put_metric_filter(conn, 1) - assert response["ResponseMetadata"]["HTTPStatusCode"] == 200 + lg_name = "/hello-world/my-cool-endpoint" + client.create_log_group(logGroupName=lg_name) + client.put_metric_filter( + logGroupName=lg_name, + filterName="my-cool-filter", + filterPattern="{ $.val = * }", + metricTransformations=[ + { + "metricName": "my-metric", + "metricNamespace": "my-namespace", + "metricValue": "$.value", + } + ], + ) - response = put_metric_filter(conn, 2) - assert response["ResponseMetadata"]["HTTPStatusCode"] == 200 - - response = conn.delete_metric_filter( - filterName="filterName", logGroupName="logGroupName1" + response = client.delete_metric_filter( + filterName="filterName", logGroupName=lg_name ) assert response["ResponseMetadata"]["HTTPStatusCode"] == 200 - response = conn.describe_metric_filters( + response = client.describe_metric_filters( filterNamePrefix="filter", logGroupName="logGroupName2" ) - assert response["metricFilters"][0]["filterName"] == "filterName2" - - response = conn.describe_metric_filters(logGroupName="logGroupName2") - assert response["metricFilters"][0]["filterName"] == "filterName2" + response.should.have.key("metricFilters").equals([]) @mock_logs