Logs - fix typo in log-group-name regex (#4972)

This commit is contained in:
Bert Blommers 2022-03-26 20:06:23 -01:00 committed by GitHub
parent 1f9f4af1c4
commit 2929f3ee35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 17 deletions

View File

@ -9,6 +9,9 @@ from .models import logs_backends
# See http://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/Welcome.html # See http://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/Welcome.html
REGEX_LOG_GROUP_NAME = r"[-._\/#A-Za-z0-9]+"
def validate_param( def validate_param(
param_name, param_value, constraint, constraint_expression, pattern=None param_name, param_value, constraint, constraint_expression, pattern=None
): ):
@ -20,7 +23,7 @@ def validate_param(
) )
if pattern and param_value: if pattern and param_value:
try: try:
assert re.match(pattern, param_value) assert re.fullmatch(pattern, param_value)
except (AssertionError, TypeError): except (AssertionError, TypeError):
raise InvalidParameterException( raise InvalidParameterException(
constraint=f"Must match pattern: {pattern}", constraint=f"Must match pattern: {pattern}",
@ -67,7 +70,7 @@ class LogsResponse(BaseResponse):
"logGroupName", "logGroupName",
"Minimum length of 1. Maximum length of 512.", "Minimum length of 1. Maximum length of 512.",
lambda x: 1 <= len(x) <= 512, lambda x: 1 <= len(x) <= 512,
pattern="[.-_/#A-Za-z0-9]+", pattern=REGEX_LOG_GROUP_NAME,
) )
metric_transformations = self._get_validated_param( metric_transformations = self._get_validated_param(
"metricTransformations", "Fixed number of 1 item.", lambda x: len(x) == 1 "metricTransformations", "Fixed number of 1 item.", lambda x: len(x) == 1
@ -90,7 +93,7 @@ class LogsResponse(BaseResponse):
"logGroupName", "logGroupName",
"Minimum length of 1. Maximum length of 512", "Minimum length of 1. Maximum length of 512",
lambda x: x is None or 1 <= len(x) <= 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( metric_name = self._get_validated_param(
"metricName", "metricName",
@ -139,7 +142,7 @@ class LogsResponse(BaseResponse):
"logGroupName", "logGroupName",
"Minimum length of 1. Maximum length of 512.", "Minimum length of 1. Maximum length of 512.",
lambda x: 1 <= len(x) <= 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) self.logs_backend.delete_metric_filter(filter_name, log_group_name)

View File

@ -181,26 +181,32 @@ def test_describe_metric_filters_multiple_happy():
@mock_logs @mock_logs
def test_delete_metric_filter(): 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) lg_name = "/hello-world/my-cool-endpoint"
assert response["ResponseMetadata"]["HTTPStatusCode"] == 200 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) response = client.delete_metric_filter(
assert response["ResponseMetadata"]["HTTPStatusCode"] == 200 filterName="filterName", logGroupName=lg_name
response = conn.delete_metric_filter(
filterName="filterName", logGroupName="logGroupName1"
) )
assert response["ResponseMetadata"]["HTTPStatusCode"] == 200 assert response["ResponseMetadata"]["HTTPStatusCode"] == 200
response = conn.describe_metric_filters( response = client.describe_metric_filters(
filterNamePrefix="filter", logGroupName="logGroupName2" filterNamePrefix="filter", logGroupName="logGroupName2"
) )
assert response["metricFilters"][0]["filterName"] == "filterName2" response.should.have.key("metricFilters").equals([])
response = conn.describe_metric_filters(logGroupName="logGroupName2")
assert response["metricFilters"][0]["filterName"] == "filterName2"
@mock_logs @mock_logs