Skip to content

Commit ab0caec

Browse files
committed
KAFKA-20617: add validation for cluster-id in Formatter
This change adds validation for Uuids in Formatter which is used by `kafka-storage.sh` which allows users to fail fast before they inadvertently end up using an invalid cluster-id.
1 parent c298d4b commit ab0caec

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

metadata/src/main/java/org/apache/kafka/metadata/storage/Formatter.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,17 @@ public void run() throws Exception {
236236
if (clusterId == null) {
237237
throw new FormatterException("You must specify the cluster id.");
238238
}
239+
try {
240+
if (clusterId.contains("=")) {
241+
throw new FormatterException("The specified cluster id, " + clusterId + " contains padding and is invalid");
242+
}
243+
Uuid uuid = Uuid.fromString(clusterId);
244+
if (Uuid.RESERVED.contains(uuid)) {
245+
throw new FormatterException("The specified cluster id, " + clusterId + " is reserved");
246+
}
247+
} catch (IllegalArgumentException e) {
248+
throw new FormatterException("The specified cluster id, " + clusterId + " is invalid", e);
249+
}
239250
if (directories.isEmpty()) {
240251
throw new FormatterException("You must specify at least one directory to format");
241252
}

metadata/src/test/java/org/apache/kafka/metadata/storage/FormatterTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,4 +587,18 @@ public void testFormatWithNoInitialControllers() throws Exception {
587587
assertNotNull(logDirProps1);
588588
}
589589
}
590+
591+
@ParameterizedTest
592+
@ValueSource(strings = {"unrvTtQISjar0JUWGU/8Pg", "igNUVIdeSPO5JCZYFhOh7Q==", "AAAAAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAAAQ"})
593+
public void testFormatWithInvalidClusterId(String clusterId) throws Exception {
594+
try (TestEnv testEnv = new TestEnv(2)) {
595+
FormatterContext formatter1 = testEnv.newFormatter();
596+
formatter1.formatter.setClusterId(clusterId);
597+
String expectedPrefix = "The specified cluster id, " + clusterId;
598+
assertEquals(expectedPrefix,
599+
assertThrows(FormatterException.class,
600+
formatter1.formatter::run).
601+
getMessage().substring(0, expectedPrefix.length()));
602+
}
603+
}
590604
}

0 commit comments

Comments
 (0)