Skip to content

Commit bb673ba

Browse files
rjernstafoucret
authored andcommitted
Split out jvm and operating system actions in entitlement tests (elastic#125122)
This is part of continued cleanup to remove actions from RestEntitlementCheckAction.
1 parent 53c6308 commit bb673ba

File tree

3 files changed

+64
-44
lines changed

3 files changed

+64
-44
lines changed
Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,20 @@
1212
import org.elasticsearch.core.SuppressForbidden;
1313
import org.elasticsearch.entitlement.qa.entitled.EntitledPlugin;
1414

15+
import java.io.IOException;
16+
import java.net.URL;
17+
import java.net.URLClassLoader;
1518
import java.util.Locale;
1619
import java.util.TimeZone;
1720

21+
import static org.elasticsearch.entitlement.qa.test.EntitlementTest.ExpectedAccess.ALWAYS_DENIED;
22+
import static org.elasticsearch.entitlement.qa.test.EntitlementTest.ExpectedAccess.PLUGINS;
23+
1824
@SuppressForbidden(reason = "testing entitlements")
19-
class WritePropertiesCheckActions {
20-
private WritePropertiesCheckActions() {}
25+
@SuppressWarnings({ "unused" /* called via reflection */ })
26+
class JvmActions {
2127

28+
@EntitlementTest(expectedAccess = PLUGINS)
2229
static void setSystemProperty() {
2330
System.setProperty("es.entitlements.checkSetSystemProperty", "true");
2431
try {
@@ -29,24 +36,49 @@ static void setSystemProperty() {
2936

3037
}
3138

39+
@EntitlementTest(expectedAccess = PLUGINS)
3240
static void clearSystemProperty() {
3341
EntitledPlugin.selfTest(); // TODO: find a better home
3442
System.clearProperty("es.entitlements.checkClearSystemProperty");
3543
}
3644

45+
@EntitlementTest(expectedAccess = ALWAYS_DENIED)
3746
static void setSystemProperties() {
3847
System.setProperties(System.getProperties()); // no side effect in case if allowed (but shouldn't)
3948
}
4049

50+
@EntitlementTest(expectedAccess = ALWAYS_DENIED)
4151
static void setDefaultLocale() {
4252
Locale.setDefault(Locale.getDefault());
4353
}
4454

55+
@EntitlementTest(expectedAccess = ALWAYS_DENIED)
4556
static void setDefaultLocaleForCategory() {
4657
Locale.setDefault(Locale.Category.DISPLAY, Locale.getDefault(Locale.Category.DISPLAY));
4758
}
4859

60+
@EntitlementTest(expectedAccess = ALWAYS_DENIED)
4961
static void setDefaultTimeZone() {
5062
TimeZone.setDefault(TimeZone.getDefault());
5163
}
64+
65+
@EntitlementTest(expectedAccess = PLUGINS)
66+
static void createClassLoader() throws IOException {
67+
try (var classLoader = new URLClassLoader("test", new URL[0], RestEntitlementsCheckAction.class.getClassLoader())) {
68+
// intentionally empty, just let the loader close
69+
}
70+
}
71+
72+
@EntitlementTest(expectedAccess = ALWAYS_DENIED)
73+
static void createLogManager() {
74+
new java.util.logging.LogManager() {
75+
};
76+
}
77+
78+
@EntitlementTest(expectedAccess = ALWAYS_DENIED)
79+
static void thread$$setDefaultUncaughtExceptionHandler() {
80+
Thread.setDefaultUncaughtExceptionHandler(Thread.getDefaultUncaughtExceptionHandler());
81+
}
82+
83+
private JvmActions() {}
5284
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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.test;
11+
12+
import java.io.IOException;
13+
import java.util.List;
14+
15+
@SuppressWarnings({ "unused" /* called via reflection */ })
16+
class OperatingSystemActions {
17+
18+
static void processBuilder_start() throws IOException {
19+
new ProcessBuilder("").start();
20+
}
21+
22+
static void processBuilder_startPipeline() throws IOException {
23+
ProcessBuilder.startPipeline(List.of());
24+
}
25+
26+
private OperatingSystemActions() {}
27+
}

libs/entitlement/qa/entitlement-test-plugin/src/main/java/org/elasticsearch/entitlement/qa/test/RestEntitlementsCheckAction.java

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import java.net.Socket;
4141
import java.net.SocketException;
4242
import java.net.URL;
43-
import java.net.URLClassLoader;
4443
import java.net.URLConnection;
4544
import java.net.URLStreamHandler;
4645
import java.net.spi.URLStreamHandlerProvider;
@@ -63,7 +62,6 @@
6362
import static org.elasticsearch.entitlement.qa.test.EntitlementTest.ExpectedAccess.PLUGINS;
6463
import static org.elasticsearch.entitlement.qa.test.EntitlementTest.ExpectedAccess.SERVER_ONLY;
6564
import static org.elasticsearch.entitlement.qa.test.RestEntitlementsCheckAction.CheckAction.alwaysDenied;
66-
import static org.elasticsearch.entitlement.qa.test.RestEntitlementsCheckAction.CheckAction.deniedToPlugins;
6765
import static org.elasticsearch.entitlement.qa.test.RestEntitlementsCheckAction.CheckAction.forPlugins;
6866
import static org.elasticsearch.rest.RestRequest.Method.GET;
6967

@@ -95,26 +93,10 @@ static CheckAction alwaysDenied(CheckedRunnable<Exception> action) {
9593

9694
private static final Map<String, CheckAction> checkActions = Stream.of(
9795
Stream.<Entry<String, CheckAction>>of(
98-
entry("create_classloader", forPlugins(RestEntitlementsCheckAction::createClassLoader)),
99-
entry("processBuilder_start", deniedToPlugins(RestEntitlementsCheckAction::processBuilder_start)),
100-
entry("processBuilder_startPipeline", deniedToPlugins(RestEntitlementsCheckAction::processBuilder_startPipeline)),
10196
entry("set_https_connection_properties", forPlugins(RestEntitlementsCheckAction::setHttpsConnectionProperties)),
10297
entry("set_default_ssl_socket_factory", alwaysDenied(RestEntitlementsCheckAction::setDefaultSSLSocketFactory)),
10398
entry("set_default_hostname_verifier", alwaysDenied(RestEntitlementsCheckAction::setDefaultHostnameVerifier)),
10499
entry("set_default_ssl_context", alwaysDenied(RestEntitlementsCheckAction::setDefaultSSLContext)),
105-
entry(
106-
"thread_setDefaultUncaughtExceptionHandler",
107-
alwaysDenied(RestEntitlementsCheckAction::thread$$setDefaultUncaughtExceptionHandler)
108-
),
109-
entry("logManager", alwaysDenied(RestEntitlementsCheckAction::logManager$)),
110-
111-
entry("locale_setDefault", alwaysDenied(WritePropertiesCheckActions::setDefaultLocale)),
112-
entry("locale_setDefaultForCategory", alwaysDenied(WritePropertiesCheckActions::setDefaultLocaleForCategory)),
113-
entry("timeZone_setDefault", alwaysDenied(WritePropertiesCheckActions::setDefaultTimeZone)),
114-
115-
entry("system_setProperty", forPlugins(WritePropertiesCheckActions::setSystemProperty)),
116-
entry("system_clearProperty", forPlugins(WritePropertiesCheckActions::clearSystemProperty)),
117-
entry("system_setSystemProperties", alwaysDenied(WritePropertiesCheckActions::setSystemProperties)),
118100

119101
// This group is a bit nasty: if entitlements don't prevent these, then networking is
120102
// irreparably borked for the remainder of the test run.
@@ -211,7 +193,9 @@ static CheckAction alwaysDenied(CheckedRunnable<Exception> action) {
211193
getTestEntries(SpiActions.class),
212194
getTestEntries(SystemActions.class),
213195
getTestEntries(URLConnectionFileActions.class),
214-
getTestEntries(URLConnectionNetworkActions.class)
196+
getTestEntries(URLConnectionNetworkActions.class),
197+
getTestEntries(JvmActions.class),
198+
getTestEntries(OperatingSystemActions.class)
215199
)
216200
.flatMap(Function.identity())
217201
.filter(entry -> entry.getValue().fromJavaVersion() == null || Runtime.version().feature() >= entry.getValue().fromJavaVersion())
@@ -323,33 +307,10 @@ private static void setDefaultSSLSocketFactory() {
323307
HttpsURLConnection.setDefaultSSLSocketFactory(new DummyImplementations.DummySSLSocketFactory());
324308
}
325309

326-
private static void createClassLoader() throws IOException {
327-
try (var classLoader = new URLClassLoader("test", new URL[0], RestEntitlementsCheckAction.class.getClassLoader())) {
328-
logger.info("Created URLClassLoader [{}]", classLoader.getName());
329-
}
330-
}
331-
332-
private static void processBuilder_start() throws IOException {
333-
new ProcessBuilder("").start();
334-
}
335-
336-
private static void processBuilder_startPipeline() throws IOException {
337-
ProcessBuilder.startPipeline(List.of());
338-
}
339-
340310
private static void setHttpsConnectionProperties() {
341311
new DummyImplementations.DummyHttpsURLConnection().setSSLSocketFactory(new DummyImplementations.DummySSLSocketFactory());
342312
}
343313

344-
private static void thread$$setDefaultUncaughtExceptionHandler() {
345-
Thread.setDefaultUncaughtExceptionHandler(Thread.getDefaultUncaughtExceptionHandler());
346-
}
347-
348-
private static void logManager$() {
349-
new java.util.logging.LogManager() {
350-
};
351-
}
352-
353314
@SuppressWarnings("deprecation")
354315
@SuppressForbidden(reason = "We're required to prevent calls to this forbidden API")
355316
private static void datagramSocket$$setDatagramSocketImplFactory() throws IOException {

0 commit comments

Comments
 (0)