Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@ public void run() throws Exception {
if (clusterId == null) {
throw new FormatterException("You must specify the cluster id.");
}
try {
if (clusterId.contains("=")) {
throw new FormatterException("The specified cluster id, " + clusterId + " contains padding and is invalid");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our randomUuid method is possible to output the uuid contains = sign:

This will not generate a UUID equal to 0, 1, or one whose string representation starts with a dash ("-")

So is this the expected validation?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is possible because Uuid#toString does not use padding and = sign can only be present when padding is enabled

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this actually covers a real gap. The one in which a CID like igNUVIdeSPO5JCZYFhOh7Q== would pass the validation, but would be returned as igNUVIdeSPO5JCZYFhOh7Q by Uuid.toString(). I would just move it outside the try-catch block as it throws FormatterException directly and would bypass the catch anyway.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new FormatterException("The specified cluster id, " + clusterId + " contains padding and is invalid");
throw new FormatterException("The specified cluster id, " + clusterId + " is invalid: contains padding");

}
Uuid uuid = Uuid.fromString(clusterId);
if (Uuid.RESERVED.contains(uuid)) {
throw new FormatterException("The specified cluster id, " + clusterId + " is reserved");
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also validate the starting - sign.

Copy link
Copy Markdown
Contributor Author

@gaurav-narula gaurav-narula May 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these are cosmetic improvemenst that've been added over time but have no bearing on correctness. Failing validation on them would hinder migration from older clusters where - was allowed.

The motivation for avoiding - in the beginning was to avoid shell escaping issues when passing cluster id in CLI tools. https://issues.apache.org/jira/browse/KAFKA-13741

Lately, https://issues.apache.org/jira/browse/KAFKA-20072 avoided - altogether to allow easier copy-pasting.

Copy link
Copy Markdown
Contributor

@fvaleri fvaleri May 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Let's add a comment about this for future reference.

} catch (IllegalArgumentException e) {
throw new FormatterException("The specified cluster id, " + clusterId + " is invalid", e);
}
if (directories.isEmpty()) {
throw new FormatterException("You must specify at least one directory to format");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,4 +587,18 @@ public void testFormatWithNoInitialControllers() throws Exception {
assertNotNull(logDirProps1);
}
}

@ParameterizedTest
@ValueSource(strings = {"unrvTtQISjar0JUWGU/8Pg", "igNUVIdeSPO5JCZYFhOh7Q==", "AAAAAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAAAQ"})
public void testFormatWithInvalidClusterId(String clusterId) throws Exception {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no corresponding positive test showing that a valid CID passes validation. This would guard against the validation being too aggressive.

try (TestEnv testEnv = new TestEnv(2)) {
FormatterContext formatter1 = testEnv.newFormatter();
formatter1.formatter.setClusterId(clusterId);
String expectedPrefix = "The specified cluster id, " + clusterId;
assertEquals(expectedPrefix,
assertThrows(FormatterException.class,
formatter1.formatter::run).
getMessage().substring(0, expectedPrefix.length()));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't be easier to rewrite like:

assertTrue(message.startsWith(expectedPrefix))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm indifferent here - I followed the convention used in another test in the same class

}
}
}