Skip to content

Commit

Permalink
Restrict group names in cluster.yaml to valid bash var name characters (
Browse files Browse the repository at this point in the history
#5038)

Closes #5033
  • Loading branch information
dlmarion authored Nov 7, 2024
1 parent 68e0406 commit ed4161e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.yaml.snakeyaml.Yaml;
Expand All @@ -38,6 +39,17 @@

public class ClusterConfigParser {

private static final Pattern GROUP_NAME_PATTERN =
Pattern.compile("^[a-zA-Z_]{1,}[a-zA-Z0-9_]{0,}$");

public static void validateGroupNames(Set<String> names) {
for (String name : names) {
if (!GROUP_NAME_PATTERN.matcher(name).matches()) {
throw new RuntimeException("Group name: " + name + " contains invalid characters");
}
}
}

private static final String PROPERTY_FORMAT = "%s=\"%s\"%n";
private static final String[] SECTIONS = new String[] {"manager", "monitor", "gc", "tserver"};

Expand Down Expand Up @@ -123,6 +135,7 @@ public static void outputShellVariables(Map<String,String> config, PrintStream o
Set<String> compactorQueues =
config.keySet().stream().filter(k -> k.startsWith(compactorPrefix))
.map(k -> k.substring(compactorPrefix.length())).collect(Collectors.toSet());
validateGroupNames(compactorQueues);

if (!compactorQueues.isEmpty()) {
out.printf(PROPERTY_FORMAT, "COMPACTION_QUEUES",
Expand All @@ -138,6 +151,7 @@ public static void outputShellVariables(Map<String,String> config, PrintStream o
String sserverPrefix = "sserver.";
Set<String> sserverGroups = config.keySet().stream().filter(k -> k.startsWith(sserverPrefix))
.map(k -> k.substring(sserverPrefix.length())).collect(Collectors.toSet());
validateGroupNames(sserverGroups);

if (!sserverGroups.isEmpty()) {
out.printf(PROPERTY_FORMAT, "SSERVER_GROUPS",
Expand All @@ -162,14 +176,19 @@ public static void main(String[] args) throws IOException {
System.exit(1);
}

if (args.length == 2) {
// Write to a file instead of System.out if provided as an argument
try (OutputStream os = Files.newOutputStream(Paths.get(args[1]), StandardOpenOption.CREATE);
PrintStream out = new PrintStream(os)) {
outputShellVariables(parseConfiguration(args[0]), new PrintStream(out));
try {
if (args.length == 2) {
// Write to a file instead of System.out if provided as an argument
try (OutputStream os = Files.newOutputStream(Paths.get(args[1]), StandardOpenOption.CREATE);
PrintStream out = new PrintStream(os)) {
outputShellVariables(parseConfiguration(args[0]), new PrintStream(out));
}
} else {
outputShellVariables(parseConfiguration(args[0]), System.out);
}
} else {
outputShellVariables(parseConfiguration(args[0]), System.out);
} catch (Exception e) {
System.err.println("Processing error: " + e.getMessage());
System.exit(1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -271,4 +272,20 @@ public void testFileWithUnknownSections() throws Exception {
assertTrue(exception.getMessage().contains("vserver"));
}
}

@Test
public void testGroupNamePattern() {
ClusterConfigParser.validateGroupNames(Set.of("a"));
ClusterConfigParser.validateGroupNames(Set.of("a", "b"));
ClusterConfigParser.validateGroupNames(Set.of("default", "reg_ular"));
ClusterConfigParser.validateGroupNames(Set.of("a1b2c3d4__"));
assertThrows(RuntimeException.class,
() -> ClusterConfigParser.validateGroupNames(Set.of("0abcde")));
assertThrows(RuntimeException.class,
() -> ClusterConfigParser.validateGroupNames(Set.of("a-b")));
assertThrows(RuntimeException.class,
() -> ClusterConfigParser.validateGroupNames(Set.of("a*b")));
assertThrows(RuntimeException.class,
() -> ClusterConfigParser.validateGroupNames(Set.of("a?b")));
}
}

0 comments on commit ed4161e

Please sign in to comment.