Skip to content

Commit 448dc17

Browse files
committed
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 df33e0f commit 448dc17

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) {
@@ -327,7 +328,7 @@ class ClusterFormationTasks {
327328

328329
/** Adds a task to create keystore */
329330
static Task configureCreateKeystoreTask(String name, Project project, Task setup, NodeInfo node) {
330-
if (node.config.keystoreSettings.isEmpty()) {
331+
if (node.config.keystoreSettings.isEmpty() && node.config.keystoreFiles.isEmpty()) {
331332
return setup
332333
} else {
333334
/*
@@ -361,6 +362,37 @@ class ClusterFormationTasks {
361362
return parentTask
362363
}
363364

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

plugins/repository-gcs/build.gradle

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

9595
integTestCluster {
9696
dependsOn createServiceAccountFile, googleCloudStorageFixture
97-
setupCommand 'create-elasticsearch-keystore', 'bin/elasticsearch-keystore', 'create'
98-
setupCommand 'add-credentials-to-elasticsearch-keystore',
99-
'bin/elasticsearch-keystore', 'add-file', 'gcs.client.integration_test.credentials_file', "${serviceAccountFile.absolutePath}"
97+
keystoreFile 'gcs.client.integration_test.credentials_file', "${serviceAccountFile.absolutePath}"
10098

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

0 commit comments

Comments
 (0)