Skip to content

Commit 73689dc

Browse files
authored
[Entitlements] Moving and refactoring IT tests (#118254) (#118467)
1 parent c20f2e7 commit 73689dc

File tree

21 files changed

+537
-169
lines changed

21 files changed

+537
-169
lines changed

qa/entitlements/build.gradle renamed to libs/entitlement/qa/build.gradle

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,14 @@
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
99

10-
apply plugin: 'elasticsearch.base-internal-es-plugin'
1110
apply plugin: 'elasticsearch.internal-java-rest-test'
1211
// Necessary to use tests in Serverless
1312
apply plugin: 'elasticsearch.internal-test-artifact'
1413

15-
esplugin {
16-
name 'entitlement-qa'
17-
description 'A test module that triggers entitlement checks'
18-
classname 'org.elasticsearch.test.entitlements.EntitlementsCheckPlugin'
19-
}
20-
2114
dependencies {
22-
clusterPlugins project(':qa:entitlements')
15+
javaRestTestImplementation project(':libs:entitlement:qa:common')
16+
clusterPlugins project(':libs:entitlement:qa:entitlement-allowed')
17+
clusterPlugins project(':libs:entitlement:qa:entitlement-allowed-nonmodular')
18+
clusterPlugins project(':libs:entitlement:qa:entitlement-denied')
19+
clusterPlugins project(':libs:entitlement:qa:entitlement-denied-nonmodular')
2320
}
24-
25-
tasks.named("javadoc").configure {
26-
// There seems to be some problem generating javadoc on a QA project that has a module definition
27-
enabled = false
28-
}
29-
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
apply plugin: 'elasticsearch.build'
11+
12+
dependencies {
13+
implementation project(':server')
14+
implementation project(':libs:logging')
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
module org.elasticsearch.entitlement.qa.common {
11+
requires org.elasticsearch.server;
12+
requires org.elasticsearch.base;
13+
requires org.elasticsearch.logging;
14+
15+
exports org.elasticsearch.entitlement.qa.common;
16+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.entitlement.qa.common;
11+
12+
import org.elasticsearch.client.internal.node.NodeClient;
13+
import org.elasticsearch.common.Strings;
14+
import org.elasticsearch.core.SuppressForbidden;
15+
import org.elasticsearch.logging.LogManager;
16+
import org.elasticsearch.logging.Logger;
17+
import org.elasticsearch.rest.BaseRestHandler;
18+
import org.elasticsearch.rest.RestRequest;
19+
import org.elasticsearch.rest.RestResponse;
20+
import org.elasticsearch.rest.RestStatus;
21+
22+
import java.io.IOException;
23+
import java.io.UncheckedIOException;
24+
import java.net.URL;
25+
import java.net.URLClassLoader;
26+
import java.util.List;
27+
import java.util.Map;
28+
import java.util.Set;
29+
import java.util.stream.Collectors;
30+
31+
import static java.util.Map.entry;
32+
import static org.elasticsearch.rest.RestRequest.Method.GET;
33+
34+
public class RestEntitlementsCheckAction extends BaseRestHandler {
35+
private static final Logger logger = LogManager.getLogger(RestEntitlementsCheckAction.class);
36+
private final String prefix;
37+
38+
private record CheckAction(Runnable action, boolean isServerOnly) {
39+
40+
static CheckAction serverOnly(Runnable action) {
41+
return new CheckAction(action, true);
42+
}
43+
44+
static CheckAction serverAndPlugin(Runnable action) {
45+
return new CheckAction(action, false);
46+
}
47+
}
48+
49+
private static final Map<String, CheckAction> checkActions = Map.ofEntries(
50+
entry("system_exit", CheckAction.serverOnly(RestEntitlementsCheckAction::systemExit)),
51+
entry("create_classloader", CheckAction.serverAndPlugin(RestEntitlementsCheckAction::createClassLoader))
52+
);
53+
54+
@SuppressForbidden(reason = "Specifically testing System.exit")
55+
private static void systemExit() {
56+
logger.info("Calling System.exit(123);");
57+
System.exit(123);
58+
}
59+
60+
private static void createClassLoader() {
61+
logger.info("Calling new URLClassLoader");
62+
try (var classLoader = new URLClassLoader("test", new URL[0], RestEntitlementsCheckAction.class.getClassLoader())) {
63+
logger.info("Created URLClassLoader [{}]", classLoader.getName());
64+
} catch (IOException e) {
65+
throw new UncheckedIOException(e);
66+
}
67+
}
68+
69+
public RestEntitlementsCheckAction(String prefix) {
70+
this.prefix = prefix;
71+
}
72+
73+
public static Set<String> getServerAndPluginsCheckActions() {
74+
return checkActions.entrySet()
75+
.stream()
76+
.filter(kv -> kv.getValue().isServerOnly() == false)
77+
.map(Map.Entry::getKey)
78+
.collect(Collectors.toSet());
79+
}
80+
81+
public static Set<String> getAllCheckActions() {
82+
return checkActions.keySet();
83+
}
84+
85+
@Override
86+
public List<Route> routes() {
87+
return List.of(new Route(GET, "/_entitlement/" + prefix + "/_check"));
88+
}
89+
90+
@Override
91+
public String getName() {
92+
return "check_" + prefix + "_action";
93+
}
94+
95+
@Override
96+
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) {
97+
logger.info("RestEntitlementsCheckAction rest handler [{}]", request.path());
98+
var actionName = request.param("action");
99+
if (Strings.isNullOrEmpty(actionName)) {
100+
throw new IllegalArgumentException("Missing action parameter");
101+
}
102+
var checkAction = checkActions.get(actionName);
103+
if (checkAction == null) {
104+
throw new IllegalArgumentException(Strings.format("Unknown action [%s]", actionName));
105+
}
106+
107+
return channel -> {
108+
checkAction.action().run();
109+
channel.sendResponse(new RestResponse(RestStatus.OK, Strings.format("Succesfully executed action [%s]", actionName)));
110+
};
111+
}
112+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
apply plugin: 'elasticsearch.base-internal-es-plugin'
11+
12+
esplugin {
13+
name 'entitlement-allowed-nonmodular'
14+
description 'A non-modular test module that invokes entitlement checks that are supposed to be granted'
15+
classname 'org.elasticsearch.entitlement.qa.nonmodular.EntitlementAllowedNonModularPlugin'
16+
}
17+
18+
dependencies {
19+
implementation project(':libs:entitlement:qa:common')
20+
}
21+
22+
tasks.named("javadoc").configure {
23+
enabled = false
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
package org.elasticsearch.entitlement.qa.nonmodular;
10+
11+
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
12+
import org.elasticsearch.cluster.node.DiscoveryNodes;
13+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
14+
import org.elasticsearch.common.settings.ClusterSettings;
15+
import org.elasticsearch.common.settings.IndexScopedSettings;
16+
import org.elasticsearch.common.settings.Settings;
17+
import org.elasticsearch.common.settings.SettingsFilter;
18+
import org.elasticsearch.entitlement.qa.common.RestEntitlementsCheckAction;
19+
import org.elasticsearch.features.NodeFeature;
20+
import org.elasticsearch.plugins.ActionPlugin;
21+
import org.elasticsearch.plugins.Plugin;
22+
import org.elasticsearch.rest.RestController;
23+
import org.elasticsearch.rest.RestHandler;
24+
25+
import java.util.List;
26+
import java.util.function.Predicate;
27+
import java.util.function.Supplier;
28+
29+
public class EntitlementAllowedNonModularPlugin extends Plugin implements ActionPlugin {
30+
31+
@Override
32+
public List<RestHandler> getRestHandlers(
33+
final Settings settings,
34+
NamedWriteableRegistry namedWriteableRegistry,
35+
final RestController restController,
36+
final ClusterSettings clusterSettings,
37+
final IndexScopedSettings indexScopedSettings,
38+
final SettingsFilter settingsFilter,
39+
final IndexNameExpressionResolver indexNameExpressionResolver,
40+
final Supplier<DiscoveryNodes> nodesInCluster,
41+
Predicate<NodeFeature> clusterSupportsFeature
42+
) {
43+
return List.of(new RestEntitlementsCheckAction("allowed_nonmodular"));
44+
}
45+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALL-UNNAMED:
2+
- create_class_loader
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
apply plugin: 'elasticsearch.base-internal-es-plugin'
11+
12+
esplugin {
13+
name 'entitlement-allowed'
14+
description 'A test module that invokes entitlement checks that are supposed to be granted'
15+
classname 'org.elasticsearch.entitlement.qa.EntitlementAllowedPlugin'
16+
}
17+
18+
dependencies {
19+
implementation project(':libs:entitlement:qa:common')
20+
}
21+
22+
tasks.named("javadoc").configure {
23+
enabled = false
24+
}
25+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
module org.elasticsearch.entitlement.qa.allowed {
11+
requires org.elasticsearch.server;
12+
requires org.elasticsearch.base;
13+
requires org.elasticsearch.logging;
14+
requires org.elasticsearch.entitlement.qa.common;
15+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
package org.elasticsearch.entitlement.qa;
10+
11+
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
12+
import org.elasticsearch.cluster.node.DiscoveryNodes;
13+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
14+
import org.elasticsearch.common.settings.ClusterSettings;
15+
import org.elasticsearch.common.settings.IndexScopedSettings;
16+
import org.elasticsearch.common.settings.Settings;
17+
import org.elasticsearch.common.settings.SettingsFilter;
18+
import org.elasticsearch.entitlement.qa.common.RestEntitlementsCheckAction;
19+
import org.elasticsearch.features.NodeFeature;
20+
import org.elasticsearch.plugins.ActionPlugin;
21+
import org.elasticsearch.plugins.Plugin;
22+
import org.elasticsearch.rest.RestController;
23+
import org.elasticsearch.rest.RestHandler;
24+
25+
import java.util.List;
26+
import java.util.function.Predicate;
27+
import java.util.function.Supplier;
28+
29+
public class EntitlementAllowedPlugin extends Plugin implements ActionPlugin {
30+
31+
@Override
32+
public List<RestHandler> getRestHandlers(
33+
final Settings settings,
34+
NamedWriteableRegistry namedWriteableRegistry,
35+
final RestController restController,
36+
final ClusterSettings clusterSettings,
37+
final IndexScopedSettings indexScopedSettings,
38+
final SettingsFilter settingsFilter,
39+
final IndexNameExpressionResolver indexNameExpressionResolver,
40+
final Supplier<DiscoveryNodes> nodesInCluster,
41+
Predicate<NodeFeature> clusterSupportsFeature
42+
) {
43+
return List.of(new RestEntitlementsCheckAction("allowed"));
44+
}
45+
}

0 commit comments

Comments
 (0)