Skip to content

Commit cb40354

Browse files
authored
Add explicit "roles.yml" support to testclusters (#82137)
Previously, within tests, the file "roles.yml" (that is used to define security roles in a cluster) would need to be configured using `extraConfigFile`. This is effective, but means that there can only be a single source of security roles for the testcluster. This change introduces an explicit "securityRoles" setting in testclusters that will concatenate the provided files into a single "roles.yml" in the config directory. This makes it possible for testclusters itself to define standard roles as well as having each test define additional roles it may need. Relates: #81400
1 parent 1e57062 commit cb40354

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed

build-tools/src/main/java/org/elasticsearch/gradle/testclusters/ElasticsearchCluster.java

+5
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,11 @@ public void user(Map<String, String> userSpec) {
390390
nodes.all(node -> node.user(userSpec));
391391
}
392392

393+
@Override
394+
public void rolesFile(File rolesYml) {
395+
nodes.all(node -> node.rolesFile(rolesYml));
396+
}
397+
393398
private void writeUnicastHostsFiles() {
394399
String unicastUris = nodes.stream().flatMap(node -> node.getAllTransportPortURI().stream()).collect(Collectors.joining("\n"));
395400
nodes.forEach(node -> {

build-tools/src/main/java/org/elasticsearch/gradle/testclusters/ElasticsearchNode.java

+46-10
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ public class ElasticsearchNode implements TestClusterConfiguration {
144144
private final LazyPropertyMap<String, File> extraConfigFiles = new LazyPropertyMap<>("Extra config files", this, FileEntry::new);
145145
private final LazyPropertyList<FileCollection> extraJarConfigurations = new LazyPropertyList<>("Extra jar files", this);
146146
private final List<Map<String, String>> credentials = new ArrayList<>();
147+
private final List<File> roleFiles = new ArrayList<>();
147148
final LinkedHashMap<String, String> defaultConfig = new LinkedHashMap<>();
148149

149150
private final Path confPathRepo;
@@ -561,16 +562,7 @@ public synchronized void start() {
561562
}
562563
}
563564

564-
if (credentials.isEmpty() == false) {
565-
logToProcessStdout("Setting up " + credentials.size() + " users");
566-
567-
credentials.forEach(
568-
paramMap -> runElasticsearchBinScript(
569-
getVersion().onOrAfter("6.3.0") ? "elasticsearch-users" : "x-pack/users",
570-
paramMap.entrySet().stream().flatMap(entry -> Stream.of(entry.getKey(), entry.getValue())).toArray(String[]::new)
571-
)
572-
);
573-
}
565+
configureSecurity();
574566

575567
if (cliSetup.isEmpty() == false) {
576568
logToProcessStdout("Running " + cliSetup.size() + " setup commands");
@@ -672,6 +664,39 @@ private void copyExtraJars() {
672664
});
673665
}
674666

667+
private void configureSecurity() {
668+
if (credentials.isEmpty() == false) {
669+
logToProcessStdout("Setting up " + credentials.size() + " users");
670+
671+
credentials.forEach(
672+
paramMap -> runElasticsearchBinScript(
673+
getVersion().onOrAfter("6.3.0") ? "elasticsearch-users" : "x-pack/users",
674+
paramMap.entrySet().stream().flatMap(entry -> Stream.of(entry.getKey(), entry.getValue())).toArray(String[]::new)
675+
)
676+
);
677+
}
678+
if (roleFiles.isEmpty() == false) {
679+
logToProcessStdout("Setting up roles.yml");
680+
681+
Path dst = configFile.getParent().resolve("roles.yml");
682+
roleFiles.forEach(from -> {
683+
if (Files.exists(from.toPath()) == false) {
684+
throw new TestClustersException(
685+
"Can't create roles.yml config file from " + from + " for " + this + " as it does not exist"
686+
);
687+
}
688+
try {
689+
final Path source = from.toPath();
690+
final String content = Files.readString(source, StandardCharsets.UTF_8);
691+
Files.writeString(dst, content + System.lineSeparator(), StandardCharsets.UTF_8, StandardOpenOption.APPEND);
692+
LOGGER.info("Appended roles file {} to {}", source, dst);
693+
} catch (IOException e) {
694+
throw new UncheckedIOException("Can't append roles file " + from + " to " + dst, e);
695+
}
696+
});
697+
}
698+
}
699+
675700
private void installModules() {
676701
logToProcessStdout("Installing " + modules.size() + " modules");
677702
for (Provider<File> module : modules) {
@@ -730,6 +755,11 @@ public void user(Map<String, String> userSpec) {
730755
credentials.add(cred);
731756
}
732757

758+
@Override
759+
public void rolesFile(File rolesYml) {
760+
roleFiles.add(rolesYml);
761+
}
762+
733763
private void runElasticsearchBinScriptWithInput(String input, String tool, CharSequence... args) {
734764
if (Files.exists(getDistroDir().resolve("bin").resolve(tool)) == false
735765
&& Files.exists(getDistroDir().resolve("bin").resolve(tool + ".bat")) == false) {
@@ -1373,6 +1403,12 @@ private List<FileTree> getDistributionFiles(Action<PatternFilterable> patternFil
13731403
return files;
13741404
}
13751405

1406+
@InputFiles
1407+
@PathSensitive(PathSensitivity.RELATIVE)
1408+
public List<File> getRoleFiles() {
1409+
return roleFiles;
1410+
}
1411+
13761412
@Nested
13771413
public List<?> getKeystoreSettings() {
13781414
return keystoreSettings.getNormalizedCollection();

build-tools/src/main/java/org/elasticsearch/gradle/testclusters/TestClusterConfiguration.java

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ public interface TestClusterConfiguration {
9595

9696
void user(Map<String, String> userSpec);
9797

98+
void rolesFile(File rolesYml);
99+
98100
String getHttpSocketURI();
99101

100102
String getTransportPortURI();

x-pack/plugin/security/qa/security-basic/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ testClusters.configureEach {
2727
setting 'xpack.security.authc.token.enabled', 'true'
2828
setting 'xpack.security.authc.api_key.enabled', 'true'
2929

30-
extraConfigFile 'roles.yml', file('src/javaRestTest/resources/roles.yml')
30+
rolesFile file('src/javaRestTest/resources/roles.yml')
3131
user username: "admin_user", password: "admin-password"
3232
user username: "security_test_user", password: "security-test-password", role: "security_test_role"
3333
user username: "api_key_admin", password: "security-test-password", role: "api_key_admin_role"

0 commit comments

Comments
 (0)