Skip to content

Commit

Permalink
Fix test cluster security setup on Windows (#93482)
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-vieira committed Feb 2, 2023
1 parent 43d9a6a commit b2d9a4e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -328,27 +328,10 @@ private void copyExtraConfigFiles() {
}

private void createKeystore() {
try {
Path executable = OS.conditional(
c -> c.onWindows(() -> distributionDir.resolve("bin").resolve("elasticsearch-keystore.bat"))
.onUnix(() -> distributionDir.resolve("bin").resolve("elasticsearch-keystore"))
);

if (spec.getKeystorePassword() == null || spec.getKeystorePassword().isEmpty()) {
ProcessUtils.exec(workingDir, executable, getEnvironmentVariables(), false, "-v", "create").waitFor();
} else {
ProcessUtils.exec(
spec.getKeystorePassword() + "\n" + spec.getKeystorePassword(),
workingDir,
executable,
getEnvironmentVariables(),
false,
"create",
"-p"
).waitFor();
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
if (spec.getKeystorePassword() == null || spec.getKeystorePassword().isEmpty()) {
runToolScript("elasticsearch-keystore", null, "-v", "create");
} else {
runToolScript("elasticsearch-keystore", spec.getKeystorePassword() + "\n" + spec.getKeystorePassword(), "create", "-p");
}
}

Expand All @@ -358,22 +341,7 @@ private void addKeystoreSettings() {
? value
: spec.getKeystorePassword() + "\n" + value;

try {
ProcessUtils.exec(
input,
workingDir,
OS.conditional(
c -> c.onWindows(() -> distributionDir.resolve("bin").resolve("elasticsearch-keystore.bat"))
.onUnix(() -> distributionDir.resolve("bin").resolve("elasticsearch-keystore"))
),
getEnvironmentVariables(),
false,
"add",
key
).waitFor();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
runToolScript("elasticsearch-keystore", input, "add", key);
});
}

Expand Down Expand Up @@ -422,22 +390,16 @@ private void configureSecurity() {

LOGGER.info("Creating users for node '{}'", spec.getName());
for (User user : spec.getUsers()) {
try {
ProcessUtils.exec(
workingDir,
distributionDir.resolve("bin").resolve("elasticsearch-users"),
getEnvironmentVariables(),
false,
"useradd",
user.getUsername(),
"-p",
user.getPassword(),
"-r",
user.getRole()
).waitFor();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
runToolScript(
"elasticsearch-users",
null,
"useradd",
user.getUsername(),
"-p",
user.getPassword(),
"-r",
user.getRole()
);
}
}
}
Expand Down Expand Up @@ -481,31 +443,14 @@ private void installPlugins() {
.map(p -> p.toUri().toString())
.toList();

Path pluginCommand = OS.conditional(
c -> c.onWindows(() -> distributionDir.resolve("bin").resolve("elasticsearch-plugin.bat"))
.onUnix(() -> distributionDir.resolve("bin").resolve("elasticsearch-plugin"))
);
if (spec.getVersion().onOrAfter("7.6.0")) {
try {
ProcessUtils.exec(
workingDir,
pluginCommand,
getEnvironmentVariables(),
false,
Stream.concat(Stream.of("install", "--batch"), toInstall.stream()).toArray(String[]::new)
).waitFor();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
runToolScript(
"elasticsearch-plugin",
null,
Stream.concat(Stream.of("install", "--batch"), toInstall.stream()).toArray(String[]::new)
);
} else {
toInstall.forEach(plugin -> {
try {
ProcessUtils.exec(workingDir, pluginCommand, getEnvironmentVariables(), false, "install", "--batch", plugin)
.waitFor();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
toInstall.forEach(plugin -> runToolScript("elasticsearch-plugin", null, "install", "--batch", plugin));
}
}
}
Expand Down Expand Up @@ -639,6 +584,22 @@ private Map<String, String> getJvmOptionsReplacements() {
);
}

private void runToolScript(String tool, String input, String... args) {
try {
ProcessUtils.exec(
input,
distributionDir,
distributionDir.resolve("bin")
.resolve(OS.<String>conditional(c -> c.onWindows(() -> tool + ".bat").onUnix(() -> tool))),
getEnvironmentVariables(),
false,
args
).waitFor();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

private String getServiceName() {
return baseWorkingDir.getFileName() + "-" + spec.getCluster().getName() + "-" + spec.getName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public static Process exec(
List<String> command = new ArrayList<>();
command.addAll(
OS.conditional(
c -> c.onWindows(() -> List.of("cmd", "/c", executable.toAbsolutePath().toString()))
.onUnix(() -> List.of(executable.toAbsolutePath().toString()))
c -> c.onWindows(() -> List.of("cmd", "/c", workingDir.relativize(executable).toString()))
.onUnix(() -> List.of(workingDir.relativize(executable).toString()))
)
);
command.addAll(Arrays.asList(args));
Expand Down

0 comments on commit b2d9a4e

Please sign in to comment.