Skip to content

Commit

Permalink
fix: expand regex to include other characters
Browse files Browse the repository at this point in the history
GCS bucket names can contain hyphen, dots, etc. Not only words.
  • Loading branch information
jeqo committed Oct 13, 2023
1 parent 138200f commit 5459570
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,24 @@ class MetricCollector {
*
* <p>That is, {@literal "/storage/v1/b/<bucket>/o/<object>"}.
*/
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.
*
* <p>That is, {@literal "/download/storage/v1/b/<bucket>/o/<object>"}.
*/
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.
*
* <p>That is, {@literal "/upload/storage/v1/b/<bucket>/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";

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 5459570

Please sign in to comment.