|
@@ -38,6 +38,7 @@ import java.nio.file.Path;
|
|
|
import java.nio.file.StandardCopyOption;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.HashSet;
|
|
|
import java.util.LinkedHashMap;
|
|
@@ -86,6 +87,7 @@ public class ElasticsearchNode implements TestClusterConfiguration {
|
|
|
private final Map<String, Supplier<CharSequence>> environment = new LinkedHashMap<>();
|
|
|
private final Map<String, File> extraConfigFiles = new HashMap<>();
|
|
|
final LinkedHashMap<String, String> defaultConfig = new LinkedHashMap<>();
|
|
|
+ private final List<Map<String, String>> credentials = new ArrayList<>();
|
|
|
|
|
|
private final Path confPathRepo;
|
|
|
private final Path configFile;
|
|
@@ -117,8 +119,7 @@ public class ElasticsearchNode implements TestClusterConfiguration {
|
|
|
esStdoutFile = confPathLogs.resolve("es.stdout.log");
|
|
|
esStderrFile = confPathLogs.resolve("es.stderr.log");
|
|
|
tmpDir = workingDir.resolve("tmp");
|
|
|
- waitConditions.put("http ports file", node -> Files.exists(((ElasticsearchNode) node).httpPortsFile));
|
|
|
- waitConditions.put("transport ports file", node -> Files.exists(((ElasticsearchNode)node).transportPortFile));
|
|
|
+ waitConditions.put("ports files", this::checkPortsFilesExistWithDelay);
|
|
|
}
|
|
|
|
|
|
public String getName() {
|
|
@@ -319,9 +320,25 @@ public class ElasticsearchNode implements TestClusterConfiguration {
|
|
|
|
|
|
copyExtraConfigFiles();
|
|
|
|
|
|
+ if (isSettingMissingOrTrue("xpack.security.enabled")) {
|
|
|
+ if (credentials.isEmpty()) {
|
|
|
+ user(Collections.emptyMap());
|
|
|
+ }
|
|
|
+ credentials.forEach(paramMap -> runElaticsearchBinScript(
|
|
|
+ "elasticsearch-users",
|
|
|
+ paramMap.entrySet().stream()
|
|
|
+ .flatMap(entry -> Stream.of(entry.getKey(), entry.getValue()))
|
|
|
+ .toArray(String[]::new)
|
|
|
+ ));
|
|
|
+ }
|
|
|
+
|
|
|
startElasticsearchProcess();
|
|
|
}
|
|
|
|
|
|
+ private boolean isSettingMissingOrTrue(String name) {
|
|
|
+ return Boolean.valueOf(settings.getOrDefault(name, () -> "false").get().toString());
|
|
|
+ }
|
|
|
+
|
|
|
private void copyExtraConfigFiles() {
|
|
|
extraConfigFiles.forEach((destination, from) -> {
|
|
|
if (Files.exists(from.toPath()) == false) {
|
|
@@ -375,6 +392,22 @@ public class ElasticsearchNode implements TestClusterConfiguration {
|
|
|
extraConfigFiles.put(destination, from);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void user(Map<String, String> userSpec) {
|
|
|
+ Set<String> keys = new HashSet<>(userSpec.keySet());
|
|
|
+ keys.remove("username");
|
|
|
+ keys.remove("password");
|
|
|
+ keys.remove("role");
|
|
|
+ if (keys.isEmpty() == false) {
|
|
|
+ throw new TestClustersException("Unknown keys in user definition " + keys + " for " + this);
|
|
|
+ }
|
|
|
+ Map<String,String> cred = new LinkedHashMap<>();
|
|
|
+ cred.put("useradd", userSpec.getOrDefault("username","test_user"));
|
|
|
+ cred.put("-p", userSpec.getOrDefault("password","x-pack-test-password"));
|
|
|
+ cred.put("-r", userSpec.getOrDefault("role", "superuser"));
|
|
|
+ credentials.add(cred);
|
|
|
+ }
|
|
|
+
|
|
|
private void runElaticsearchBinScriptWithInput(String input, String tool, String... args) {
|
|
|
try (InputStream byteArrayInputStream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8))) {
|
|
|
services.loggedExec(spec -> {
|
|
@@ -752,4 +785,21 @@ public class ElasticsearchNode implements TestClusterConfiguration {
|
|
|
public String toString() {
|
|
|
return "node{" + path + ":" + name + "}";
|
|
|
}
|
|
|
+
|
|
|
+ List<Map<String, String>> getCredentials() {
|
|
|
+ return credentials;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean checkPortsFilesExistWithDelay(TestClusterConfiguration node) {
|
|
|
+ if (Files.exists(httpPortsFile) && Files.exists(transportPortFile)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Thread.sleep(500);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ Thread.currentThread().interrupt();
|
|
|
+ throw new TestClustersException("Interrupted while waiting for ports files", e);
|
|
|
+ }
|
|
|
+ return Files.exists(httpPortsFile) && Files.exists(transportPortFile);
|
|
|
+ }
|
|
|
}
|