Skip to content

Commit e0ec857

Browse files
authored
Build: introduce keystoreFile for cluster config (#29491)
This commit introduces built in support for adding files to the keystore when configuring the integration test cluster for a project. In order to use this support, simply add `keystoreFile` followed by the secure setting name and the path to the source file inside the integTestCluster closure for a project. The built in support will handle the creation of the keystore and the addition of the file to the keystore.
1 parent fac0096 commit e0ec857

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterConfiguration.groovy

+11
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ class ClusterConfiguration {
141141

142142
Map<String, String> keystoreSettings = new HashMap<>()
143143

144+
Map<String, Object> keystoreFiles = new HashMap<>()
145+
144146
// map from destination path, to source file
145147
Map<String, Object> extraConfigFiles = new HashMap<>()
146148

@@ -167,6 +169,15 @@ class ClusterConfiguration {
167169
keystoreSettings.put(name, value)
168170
}
169171

172+
/**
173+
* Adds a file to the keystore. The name is the secure setting name, and the sourceFile
174+
* is anything accepted by project.file()
175+
*/
176+
@Input
177+
void keystoreFile(String name, Object sourceFile) {
178+
keystoreFiles.put(name, sourceFile)
179+
}
180+
170181
@Input
171182
void plugin(String path) {
172183
Project pluginProject = project.project(path)

buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterFormationTasks.groovy

+33-1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ class ClusterFormationTasks {
180180
setup = configureWriteConfigTask(taskName(prefix, node, 'configure'), project, setup, node, seedNode)
181181
setup = configureCreateKeystoreTask(taskName(prefix, node, 'createKeystore'), project, setup, node)
182182
setup = configureAddKeystoreSettingTasks(prefix, project, setup, node)
183+
setup = configureAddKeystoreFileTasks(prefix, project, setup, node)
183184

184185
if (node.config.plugins.isEmpty() == false) {
185186
if (node.nodeVersion == VersionProperties.elasticsearch) {
@@ -323,7 +324,7 @@ class ClusterFormationTasks {
323324

324325
/** Adds a task to create keystore */
325326
static Task configureCreateKeystoreTask(String name, Project project, Task setup, NodeInfo node) {
326-
if (node.config.keystoreSettings.isEmpty()) {
327+
if (node.config.keystoreSettings.isEmpty() && node.config.keystoreFiles.isEmpty()) {
327328
return setup
328329
} else {
329330
/*
@@ -357,6 +358,37 @@ class ClusterFormationTasks {
357358
return parentTask
358359
}
359360

361+
/** Adds tasks to add files to the keystore */
362+
static Task configureAddKeystoreFileTasks(String parent, Project project, Task setup, NodeInfo node) {
363+
Map<String, Object> kvs = node.config.keystoreFiles
364+
if (kvs.isEmpty()) {
365+
return setup
366+
}
367+
Task parentTask = setup
368+
/*
369+
* We have to delay building the string as the path will not exist during configuration which will fail on Windows due to getting
370+
* the short name requiring the path to already exist.
371+
*/
372+
final Object esKeystoreUtil = "${-> node.binPath().resolve('elasticsearch-keystore').toString()}"
373+
for (Map.Entry<String, Object> entry in kvs) {
374+
String key = entry.getKey()
375+
String name = taskName(parent, node, 'addToKeystore#' + key)
376+
String srcFileName = entry.getValue()
377+
Task t = configureExecTask(name, project, parentTask, node, esKeystoreUtil, 'add-file', key, srcFileName)
378+
t.doFirst {
379+
File srcFile = project.file(srcFileName)
380+
if (srcFile.isDirectory()) {
381+
throw new GradleException("Source for keystoreFile must be a file: ${srcFile}")
382+
}
383+
if (srcFile.exists() == false) {
384+
throw new GradleException("Source file for keystoreFile does not exist: ${srcFile}")
385+
}
386+
}
387+
parentTask = t
388+
}
389+
return parentTask
390+
}
391+
360392
static Task configureExtraConfigFilesTask(String name, Project project, Task setup, NodeInfo node) {
361393
if (node.config.extraConfigFiles.isEmpty()) {
362394
return setup

plugins/repository-gcs/build.gradle

+1-3
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@ task createServiceAccountFile() {
100100

101101
integTestCluster {
102102
dependsOn createServiceAccountFile, googleCloudStorageFixture
103-
setupCommand 'create-elasticsearch-keystore', 'bin/elasticsearch-keystore', 'create'
104-
setupCommand 'add-credentials-to-elasticsearch-keystore',
105-
'bin/elasticsearch-keystore', 'add-file', 'gcs.client.integration_test.credentials_file', "${serviceAccountFile.absolutePath}"
103+
keystoreFile 'gcs.client.integration_test.credentials_file', "${serviceAccountFile.absolutePath}"
106104

107105
/* Use a closure on the string to delay evaluation until tests are executed */
108106
setting 'gcs.client.integration_test.endpoint', "http://${ -> googleCloudStorageFixture.addressAndPort }"

0 commit comments

Comments
 (0)