-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathTestClustersPlugin.java
More file actions
357 lines (310 loc) · 16.4 KB
/
TestClustersPlugin.java
File metadata and controls
357 lines (310 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
package org.opensearch.gradle.testclusters;
import org.opensearch.gradle.DistributionDownloadPlugin;
import org.opensearch.gradle.JdkDownloadPlugin;
import org.opensearch.gradle.ReaperPlugin;
import org.opensearch.gradle.ReaperService;
import org.opensearch.gradle.VersionProperties;
import org.opensearch.gradle.info.BuildParams;
import org.opensearch.gradle.info.GlobalBuildInfoPlugin;
import org.opensearch.gradle.internal.InternalDistributionDownloadPlugin;
import org.opensearch.gradle.util.GradleUtils;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.execution.TaskActionListener;
import org.gradle.api.execution.TaskExecutionListener;
import org.gradle.api.file.ArchiveOperations;
import org.gradle.api.file.FileSystemOperations;
import org.gradle.api.invocation.Gradle;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.TaskState;
import org.gradle.api.tasks.testing.Test;
import javax.inject.Inject;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import static org.opensearch.gradle.util.GradleUtils.noop;
public class TestClustersPlugin implements Plugin<Project> {
public static final String EXTENSION_NAME = "testClusters";
public static final String THROTTLE_SERVICE_NAME = "testClustersThrottle";
private static final String LIST_TASK_NAME = "listTestClusters";
private static final String REGISTRY_SERVICE_NAME = "testClustersRegistry";
private static final Logger logger = Logging.getLogger(TestClustersPlugin.class);
@Inject
protected FileSystemOperations getFileSystemOperations() {
throw new UnsupportedOperationException();
}
@Inject
protected ArchiveOperations getArchiveOperations() {
throw new UnsupportedOperationException();
}
@Override
public void apply(Project project) {
project.getPluginManager().apply(JdkDownloadPlugin.class);
project.getRootProject().getPluginManager().apply(GlobalBuildInfoPlugin.class);
if (BuildParams.isInternal()) {
project.getPlugins().apply(InternalDistributionDownloadPlugin.class);
} else {
project.getPlugins().apply(DistributionDownloadPlugin.class);
}
project.getRootProject().getPluginManager().apply(ReaperPlugin.class);
ReaperService reaper = project.getRootProject().getExtensions().getByType(ReaperService.class);
// enable the DSL to describe clusters
NamedDomainObjectContainer<OpenSearchCluster> container = createTestClustersContainerExtension(project, reaper);
// provide a task to be able to list defined clusters.
createListClustersTask(project, container);
// register cluster registry as a global build service
project.getGradle().getSharedServices().registerIfAbsent(REGISTRY_SERVICE_NAME, TestClustersRegistry.class, noop());
// register throttle so we only run at most max-workers/2 nodes concurrently
project.getGradle()
.getSharedServices()
.registerIfAbsent(
THROTTLE_SERVICE_NAME,
TestClustersThrottle.class,
spec -> spec.getMaxParallelUsages().set(Math.max(1, project.getGradle().getStartParameter().getMaxWorkerCount() / 2))
);
// register cluster hooks
project.getRootProject().getPluginManager().apply(TestClustersHookPlugin.class);
// configure installed plugins from -PinstalledPlugins property
configureInstalledPlugins(project, container);
}
@SuppressWarnings("unchecked")
private void configureInstalledPlugins(Project project, NamedDomainObjectContainer<OpenSearchCluster> container) {
String installedPluginsProp = System.getProperty("installedPlugins");
boolean securityEnabled = "true".equals(System.getProperty("security.enabled"));
if (installedPluginsProp == null && !securityEnabled) {
return;
}
project.afterEvaluate(p -> {
container.configureEach(cluster -> {
List<String> plugins = new java.util.ArrayList<>();
// Parse explicit plugin list if provided
if (installedPluginsProp != null) {
try {
plugins.addAll((List<String>) groovy.util.Eval.me(installedPluginsProp));
} catch (Exception e) {
throw new IllegalArgumentException("Invalid installedPlugins format. Expected list like: ['plugin1','plugin2']", e);
}
}
// Auto-add security plugin if security is enabled and not already in list
if (securityEnabled && plugins.stream().noneMatch(p2 -> p2.contains("opensearch-security"))) {
plugins.add("opensearch-security");
}
for (String pluginName : plugins) {
// Check if it's a local project plugin
if (project.findProject(":plugins:" + pluginName) != null) {
cluster.plugin("plugins:" + pluginName);
} else {
// Resolve from Maven
project.getRepositories().mavenLocal();
project.getRepositories().maven(repo -> {
repo.setName("OpenSearch Snapshots");
repo.setUrl("https://ci.opensearch.org/ci/dbc/snapshots/maven/");
});
String version = VersionProperties.getOpenSearch().replace("-SNAPSHOT", ".0-SNAPSHOT");
String coords = pluginName.contains(":") ? pluginName : (pluginName + ":" + version);
if (!coords.contains("@")) {
coords += "@zip";
}
String fullCoords = coords.split(":").length < 3 ? "org.opensearch.plugin:" + coords : coords;
org.gradle.api.artifacts.Configuration config = project.getConfigurations()
.detachedConfiguration(project.getDependencies().create(fullCoords));
config.getResolutionStrategy().cacheChangingModulesFor(0, "seconds");
cluster.plugin(project.getLayout().file(project.provider(() -> config.getSingleFile())));
// Configure security if opensearch-security plugin is installed
if (pluginName.contains("opensearch-security")) {
configureSecurityPlugin(project, cluster);
}
}
}
});
});
}
private static final String[] SECURITY_CERT_FILES = {
"esnode.pem",
"esnode-key.pem",
"kirk.pem",
"kirk-key.pem",
"root-ca.pem",
"sample.pem",
"test-kirk.jks" };
private static final String SECURITY_CERTS_URL =
"https://raw.githubusercontent.com/opensearch-project/security/refs/heads/main/bwc-test/src/test/resources/security/";
private void configureSecurityPlugin(Project project, OpenSearchCluster cluster) {
File buildDir = project.getBuildDir();
File securityResourcesDir = new File(buildDir, "security-resources");
// Download certificates
for (String certFile : SECURITY_CERT_FILES) {
File localFile = new File(securityResourcesDir, certFile);
if (!localFile.exists()) {
try {
localFile.getParentFile().mkdirs();
try (
InputStream in = URI.create(SECURITY_CERTS_URL + certFile).toURL().openStream();
FileOutputStream out = new FileOutputStream(localFile)
) {
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
} catch (Exception e) {
throw new RuntimeException("Failed to download security certificate: " + certFile, e);
}
}
}
// Add security-resources directory to test classpath
project.getTasks().withType(Test.class).configureEach(testTask -> {
testTask.setClasspath(testTask.getClasspath().plus(project.files(securityResourcesDir)));
});
// Configure each node with certificates and security settings
cluster.getNodes().forEach(node -> {
// Add certificate files
Arrays.stream(SECURITY_CERT_FILES).forEach(cert -> node.extraConfigFile(cert, new File(securityResourcesDir, cert)));
// Security settings
node.setting("plugins.security.ssl.transport.pemcert_filepath", "esnode.pem");
node.setting("plugins.security.ssl.transport.pemkey_filepath", "esnode-key.pem");
node.setting("plugins.security.ssl.transport.pemtrustedcas_filepath", "root-ca.pem");
node.setting("plugins.security.ssl.transport.enforce_hostname_verification", "false");
node.setting("plugins.security.ssl.http.enabled", "true");
node.setting("plugins.security.ssl.http.pemcert_filepath", "esnode.pem");
node.setting("plugins.security.ssl.http.pemkey_filepath", "esnode-key.pem");
node.setting("plugins.security.ssl.http.pemtrustedcas_filepath", "root-ca.pem");
node.setting("plugins.security.allow_unsafe_democertificates", "true");
node.setting("plugins.security.allow_default_init_securityindex", "true");
node.setting("plugins.security.authcz.admin_dn", "\n - CN=kirk,OU=client,O=client,L=test,C=de");
node.setting("plugins.security.audit.type", "internal_opensearch");
node.setting("plugins.security.enable_snapshot_restore_privilege", "true");
node.setting("plugins.security.check_snapshot_restore_write_privileges", "true");
node.setting("plugins.security.restapi.roles_enabled", "[\"all_access\", \"security_rest_api_access\"]");
node.setting("plugins.security.system_indices.enabled", "true");
});
}
private NamedDomainObjectContainer<OpenSearchCluster> createTestClustersContainerExtension(Project project, ReaperService reaper) {
// Create an extensions that allows describing clusters
NamedDomainObjectContainer<OpenSearchCluster> container = project.container(
OpenSearchCluster.class,
name -> new OpenSearchCluster(
name,
project,
reaper,
new File(project.getBuildDir(), "testclusters"),
getFileSystemOperations(),
getArchiveOperations()
)
);
project.getExtensions().add(EXTENSION_NAME, container);
return container;
}
private void createListClustersTask(Project project, NamedDomainObjectContainer<OpenSearchCluster> container) {
// Task is never up to date so we can pass an lambda for the task action
project.getTasks().register(LIST_TASK_NAME, task -> {
task.setGroup("OpenSearch cluster formation");
task.setDescription("Lists all OpenSearch clusters configured for this project");
task.doLast(
(Task t) -> container.forEach(cluster -> logger.lifecycle(" * {}: {}", cluster.getName(), cluster.getNumberOfNodes()))
);
});
}
static class TestClustersHookPlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
if (project != project.getRootProject()) {
throw new IllegalStateException(this.getClass().getName() + " can only be applied to the root project.");
}
Provider<TestClustersRegistry> registryProvider = GradleUtils.getBuildService(
project.getGradle().getSharedServices(),
REGISTRY_SERVICE_NAME
);
TestClustersRegistry registry = registryProvider.get();
// When we know what tasks will run, we claim the clusters of those task to differentiate between clusters
// that are defined in the build script and the ones that will actually be used in this invocation of gradle
// we use this information to determine when the last task that required the cluster executed so that we can
// terminate the cluster right away and free up resources.
configureClaimClustersHook(project.getGradle(), registry);
// Before each task, we determine if a cluster needs to be started for that task.
configureStartClustersHook(project.getGradle(), registry);
// After each task we determine if there are clusters that are no longer needed.
configureStopClustersHook(project.getGradle(), registry);
}
private static void configureClaimClustersHook(Gradle gradle, TestClustersRegistry registry) {
// Once we know all the tasks that need to execute, we claim all the clusters that belong to those and count the
// claims so we'll know when it's safe to stop them.
gradle.getTaskGraph().whenReady(taskExecutionGraph -> {
taskExecutionGraph.getAllTasks()
.stream()
.filter(task -> task instanceof TestClustersAware)
.map(task -> (TestClustersAware) task)
.flatMap(task -> task.getClusters().stream())
.forEach(registry::claimCluster);
});
}
private static void configureStartClustersHook(Gradle gradle, TestClustersRegistry registry) {
gradle.addListener(new TaskActionListener() {
@Override
public void beforeActions(Task task) {
if (task instanceof TestClustersAware == false) {
return;
}
// we only start the cluster before the actions, so we'll not start it if the task is up-to-date
TestClustersAware awareTask = (TestClustersAware) task;
awareTask.beforeStart();
awareTask.getClusters().forEach(registry::maybeStartCluster);
}
@Override
public void afterActions(Task task) {}
});
}
private static void configureStopClustersHook(Gradle gradle, TestClustersRegistry registry) {
gradle.addListener(new TaskExecutionListener() {
@Override
public void afterExecute(Task task, TaskState state) {
if (task instanceof TestClustersAware == false) {
return;
}
// always unclaim the cluster, even if _this_ task is up-to-date, as others might not have been
// and caused the cluster to start.
((TestClustersAware) task).getClusters().forEach(cluster -> registry.stopCluster(cluster, state.getFailure() != null));
}
@Override
public void beforeExecute(Task task) {}
});
}
}
}