From 54595702dd5f7f6d8792d46299bf9f0af2e5c1fe Mon Sep 17 00:00:00 2001 From: Jorge Esteban Quilcate Otoya Date: Thu, 12 Oct 2023 17:51:21 +0300 Subject: [PATCH] fix: expand regex to include other characters GCS bucket names can contain hyphen, dots, etc. Not only words. --- .../storage/gcs/MetricCollector.java | 12 +-- .../storage/gcs/MetricCollectorRegexTest.java | 91 +++++++++++++++++++ 2 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 storage/gcs/src/test/java/io/aiven/kafka/tieredstorage/storage/gcs/MetricCollectorRegexTest.java diff --git a/storage/gcs/src/main/java/io/aiven/kafka/tieredstorage/storage/gcs/MetricCollector.java b/storage/gcs/src/main/java/io/aiven/kafka/tieredstorage/storage/gcs/MetricCollector.java index 379697100..26b9356d7 100644 --- a/storage/gcs/src/main/java/io/aiven/kafka/tieredstorage/storage/gcs/MetricCollector.java +++ b/storage/gcs/src/main/java/io/aiven/kafka/tieredstorage/storage/gcs/MetricCollector.java @@ -45,24 +45,24 @@ class MetricCollector { * *

That is, {@literal "/storage/v1/b//o/"}. */ - private static final Pattern OBJECT_METADATA_PATH_PATTERN = - Pattern.compile("^/storage/v1/b/\\w+/o/\\w+/?$"); + static final Pattern OBJECT_METADATA_PATH_PATTERN = + Pattern.compile("^/storage/v1/b/([^/]+)/o/([^/]+)/?$"); /** * The pattern for object download paths. * *

That is, {@literal "/download/storage/v1/b//o/"}. */ - private static final Pattern OBJECT_DOWNLOAD_PATH_PATTERN = - Pattern.compile("^/download/storage/v1/b/\\w+/o/\\w+/?$"); + static final Pattern OBJECT_DOWNLOAD_PATH_PATTERN = + Pattern.compile("^/download/storage/v1/b/([^/]+)/o/([^/]+)/?$"); /** * The pattern for object upload paths. * *

That is, {@literal "/upload/storage/v1/b//o"}. */ - private static final Pattern OBJECT_UPLOAD_PATH_PATTERN = - Pattern.compile("^/upload/storage/v1/b/\\w+/o/?$"); + static final Pattern OBJECT_UPLOAD_PATH_PATTERN = + Pattern.compile("^/upload/storage/v1/b/([^/]+)/o/?$"); private static final String METRIC_GROUP = "gcs-metrics"; diff --git a/storage/gcs/src/test/java/io/aiven/kafka/tieredstorage/storage/gcs/MetricCollectorRegexTest.java b/storage/gcs/src/test/java/io/aiven/kafka/tieredstorage/storage/gcs/MetricCollectorRegexTest.java new file mode 100644 index 000000000..ff3831620 --- /dev/null +++ b/storage/gcs/src/test/java/io/aiven/kafka/tieredstorage/storage/gcs/MetricCollectorRegexTest.java @@ -0,0 +1,91 @@ +/* + * Copyright 2023 Aiven Oy + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.aiven.kafka.tieredstorage.storage.gcs; + +import org.junit.jupiter.api.Test; + +import static io.aiven.kafka.tieredstorage.storage.gcs.MetricCollector.OBJECT_DOWNLOAD_PATH_PATTERN; +import static io.aiven.kafka.tieredstorage.storage.gcs.MetricCollector.OBJECT_METADATA_PATH_PATTERN; +import static io.aiven.kafka.tieredstorage.storage.gcs.MetricCollector.OBJECT_UPLOAD_PATH_PATTERN; +import static org.assertj.core.api.Assertions.assertThat; + +class MetricCollectorRegexTest { + + @Test + void validBucketName() { + final var bucketName = "test-bucket_with.all_chars123"; + final var prefix = "tiered-storage-demo"; + final var topic = "topic1"; + final var topicId = "_dCovw9-QaebnUIeIsIJvg"; + final var partition = "0"; + assertThat(OBJECT_METADATA_PATH_PATTERN.matcher( + "/storage/v1/b/" + + bucketName + + "/o/" + + prefix + "%2F" + + topic + "-" + topicId + "%2F" + partition + + "%2F" + + "00000000000000023511-mhTGflMpQJyceHAOQja1sw.indexes" + )).matches(); + assertThat(OBJECT_DOWNLOAD_PATH_PATTERN.matcher( + "/download/storage/v1/b/" + + bucketName + + "/o/" + + prefix + "%2F" + + topic + "-" + topicId + "%2F" + partition + + "%2F" + + "00000000000000023511-mhTGflMpQJyceHAOQja1sw.indexes" + )).matches(); + assertThat(OBJECT_UPLOAD_PATH_PATTERN.matcher( + "/upload/storage/v1/b/" + + bucketName + + "/o" + )).matches(); + } + + @Test + void invalidBucketName() { + final var bucketName = "test/invalid"; + final var prefix = "tiered-storage-demo"; + final var topic = "topic1"; + final var topicId = "_dCovw9-QaebnUIeIsIJvg"; + final var partition = "0"; + assertThat(OBJECT_METADATA_PATH_PATTERN.matcher( + "/storage/v1/b/" + + bucketName + + "/o/" + + prefix + "%2F" + + topic + "-" + topicId + "%2F" + partition + + "%2F" + + "00000000000000023511-mhTGflMpQJyceHAOQja1sw.indexes" + ).matches()).isFalse(); + assertThat(OBJECT_DOWNLOAD_PATH_PATTERN.matcher( + "/download/storage/v1/b/" + + bucketName + + "/o/" + + prefix + "%2F" + + topic + "-" + topicId + "%2F" + partition + + "%2F" + + "00000000000000023511-mhTGflMpQJyceHAOQja1sw.indexes" + ).matches()).isFalse(); + assertThat(OBJECT_UPLOAD_PATH_PATTERN.matcher( + "/upload/storage/v1/b/" + + bucketName + + "/o" + ).matches()).isFalse(); + } +}