Skip to content

Commit 1f010d7

Browse files
committed
Entitlements for JDK-wide global state
1 parent 9a4da3f commit 1f010d7

File tree

8 files changed

+734
-16
lines changed

8 files changed

+734
-16
lines changed

distribution/tools/server-cli/src/main/java/org/elasticsearch/server/cli/SystemJvmOptions.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,13 @@ private static Stream<String> maybeAttachEntitlementAgent(boolean useEntitlement
167167
} catch (IOException e) {
168168
throw new IllegalStateException("Failed to list entitlement jars in: " + dir, e);
169169
}
170+
String modulesThatCallBridge = "java.logging";
170171
return Stream.of(
171172
"-Des.entitlements.enabled=true",
172173
"-XX:+EnableDynamicAgentLoading",
173174
"-Djdk.attach.allowAttachSelf=true",
174175
"--patch-module=java.base=" + bridgeJar,
175-
"--add-exports=java.base/org.elasticsearch.entitlement.bridge=org.elasticsearch.entitlement"
176+
"--add-exports=java.base/org.elasticsearch.entitlement.bridge=org.elasticsearch.entitlement," + modulesThatCallBridge
176177
);
177178
}
178179
}

libs/entitlement/bridge/src/main/java/org/elasticsearch/entitlement/bridge/EntitlementChecker.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99

1010
package org.elasticsearch.entitlement.bridge;
1111

12+
import java.io.InputStream;
13+
import java.io.PrintStream;
14+
import java.io.PrintWriter;
15+
import java.net.ContentHandlerFactory;
16+
import java.net.DatagramSocketImplFactory;
17+
import java.net.FileNameMap;
18+
import java.net.SocketImplFactory;
1219
import java.net.URL;
1320
import java.net.URLStreamHandlerFactory;
1421
import java.util.List;
@@ -89,4 +96,73 @@ public interface EntitlementChecker {
8996

9097
void check$java_lang_ProcessBuilder$$startPipeline(Class<?> callerClass, List<ProcessBuilder> builders);
9198

99+
////////////////////
100+
//
101+
// JVM-wide state changes
102+
//
103+
104+
void check$java_lang_System$$setIn(Class<?> callerClass, InputStream in);
105+
106+
void check$java_lang_System$$setOut(Class<?> callerClass, PrintStream out);
107+
108+
void check$java_lang_System$$setErr(Class<?> callerClass, PrintStream err);
109+
110+
void check$java_lang_Runtime$addShutdownHook(Class<?> callerClass, Runtime runtime, Thread hook);
111+
112+
void check$java_lang_Runtime$removeShutdownHook(Class<?> callerClass, Runtime runtime, Thread hook);
113+
114+
void check$jdk_tools_jlink_internal_Jlink$(Class<?> callerClass);
115+
116+
void check$jdk_tools_jlink_internal_Main$$run(Class<?> callerClass, PrintWriter out, PrintWriter err, String... args);
117+
118+
void check$jdk_vm_ci_services_JVMCIServiceLocator$$getProviders(Class<?> callerClass, Class<?> service);
119+
120+
void check$jdk_vm_ci_services_Services$$load(Class<?> callerClass, Class<?> service);
121+
122+
void check$jdk_vm_ci_services_Services$$loadSingle(Class<?> callerClass, Class<?> service, boolean required);
123+
124+
void check$com_sun_tools_jdi_VirtualMachineManagerImpl$$virtualMachineManager(Class<?> callerClass);
125+
126+
void check$java_lang_Thread$$setDefaultUncaughtExceptionHandler(Class<?> callerClass, Thread.UncaughtExceptionHandler ueh);
127+
128+
void check$java_util_spi_LocaleServiceProvider$(Class<?> callerClass);
129+
130+
void check$java_text_spi_BreakIteratorProvider$(Class<?> callerClass);
131+
132+
void check$java_text_spi_CollatorProvider$(Class<?> callerClass);
133+
134+
void check$java_text_spi_DateFormatProvider$(Class<?> callerClass);
135+
136+
void check$java_text_spi_DateFormatSymbolsProvider$(Class<?> callerClass);
137+
138+
void check$java_text_spi_DecimalFormatSymbolsProvider$(Class<?> callerClass);
139+
140+
void check$java_text_spi_NumberFormatProvider$(Class<?> callerClass);
141+
142+
void check$java_util_spi_CalendarDataProvider$(Class<?> callerClass);
143+
144+
void check$java_util_spi_CalendarNameProvider$(Class<?> callerClass);
145+
146+
void check$java_util_spi_CurrencyNameProvider$(Class<?> callerClass);
147+
148+
void check$java_util_spi_LocaleNameProvider$(Class<?> callerClass);
149+
150+
void check$java_util_spi_TimeZoneNameProvider$(Class<?> callerClass);
151+
152+
void check$java_util_logging_LogManager$(Class<?> callerClass);
153+
154+
void check$java_net_DatagramSocket$$setDatagramSocketImplFactory(Class<?> callerClass, DatagramSocketImplFactory fac);
155+
156+
void check$java_net_HttpURLConnection$$setFollowRedirects(Class<?> callerClass, boolean set);
157+
158+
void check$java_net_ServerSocket$$setSocketFactory(Class<?> callerClass, SocketImplFactory fac);
159+
160+
void check$java_net_Socket$$setSocketImplFactory(Class<?> callerClass, SocketImplFactory fac);
161+
162+
void check$java_net_URL$$setURLStreamHandlerFactory(Class<?> callerClass, URLStreamHandlerFactory fac);
163+
164+
void check$java_net_URLConnection$$setFileNameMap(Class<?> callerClass, FileNameMap map);
165+
166+
void check$java_net_URLConnection$$setContentHandlerFactory(Class<?> callerClass, ContentHandlerFactory fac);
167+
92168
}

libs/entitlement/qa/common/src/main/java/module-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@
1212
requires org.elasticsearch.base;
1313
requires org.elasticsearch.logging;
1414

15+
// Modules we'll attempt to use in order to exercise entitlements
16+
requires java.logging;
17+
1518
exports org.elasticsearch.entitlement.qa.common;
1619
}
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
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 java.text.BreakIterator;
13+
import java.text.Collator;
14+
import java.text.DateFormat;
15+
import java.text.DateFormatSymbols;
16+
import java.text.DecimalFormatSymbols;
17+
import java.text.NumberFormat;
18+
import java.text.spi.BreakIteratorProvider;
19+
import java.text.spi.CollatorProvider;
20+
import java.text.spi.DateFormatProvider;
21+
import java.text.spi.DateFormatSymbolsProvider;
22+
import java.text.spi.DecimalFormatSymbolsProvider;
23+
import java.text.spi.NumberFormatProvider;
24+
import java.util.Locale;
25+
import java.util.Map;
26+
import java.util.spi.CalendarDataProvider;
27+
import java.util.spi.CalendarNameProvider;
28+
import java.util.spi.CurrencyNameProvider;
29+
import java.util.spi.LocaleNameProvider;
30+
import java.util.spi.LocaleServiceProvider;
31+
import java.util.spi.TimeZoneNameProvider;
32+
33+
/**
34+
* A collection of concrete subclasses that we can instantiate but that don't actually work.
35+
* <p>
36+
* A bit like Mockito but way more painful.
37+
*/
38+
class DummyImplementations {
39+
40+
static class DummyLocaleServiceProvider extends LocaleServiceProvider {
41+
42+
@Override
43+
public Locale[] getAvailableLocales() {
44+
throw unexpected();
45+
}
46+
}
47+
48+
static class DummyBreakIteratorProvider extends BreakIteratorProvider {
49+
50+
@Override
51+
public BreakIterator getWordInstance(Locale locale) {
52+
throw unexpected();
53+
}
54+
55+
@Override
56+
public BreakIterator getLineInstance(Locale locale) {
57+
throw unexpected();
58+
}
59+
60+
@Override
61+
public BreakIterator getCharacterInstance(Locale locale) {
62+
throw unexpected();
63+
}
64+
65+
@Override
66+
public BreakIterator getSentenceInstance(Locale locale) {
67+
throw unexpected();
68+
}
69+
70+
@Override
71+
public Locale[] getAvailableLocales() {
72+
throw unexpected();
73+
}
74+
}
75+
76+
static class DummyCollatorProvider extends CollatorProvider {
77+
78+
@Override
79+
public Collator getInstance(Locale locale) {
80+
throw unexpected();
81+
}
82+
83+
@Override
84+
public Locale[] getAvailableLocales() {
85+
throw unexpected();
86+
}
87+
}
88+
89+
static class DummyDateFormatProvider extends DateFormatProvider {
90+
91+
@Override
92+
public DateFormat getTimeInstance(int style, Locale locale) {
93+
throw unexpected();
94+
}
95+
96+
@Override
97+
public DateFormat getDateInstance(int style, Locale locale) {
98+
throw unexpected();
99+
}
100+
101+
@Override
102+
public DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale locale) {
103+
throw unexpected();
104+
}
105+
106+
@Override
107+
public Locale[] getAvailableLocales() {
108+
throw unexpected();
109+
}
110+
}
111+
112+
static class DummyDateFormatSymbolsProvider extends DateFormatSymbolsProvider {
113+
114+
@Override
115+
public DateFormatSymbols getInstance(Locale locale) {
116+
throw unexpected();
117+
}
118+
119+
@Override
120+
public Locale[] getAvailableLocales() {
121+
throw unexpected();
122+
}
123+
}
124+
125+
static class DummyDecimalFormatSymbolsProvider extends DecimalFormatSymbolsProvider {
126+
127+
@Override
128+
public DecimalFormatSymbols getInstance(Locale locale) {
129+
throw unexpected();
130+
}
131+
132+
@Override
133+
public Locale[] getAvailableLocales() {
134+
throw unexpected();
135+
}
136+
}
137+
138+
static class DummyNumberFormatProvider extends NumberFormatProvider {
139+
140+
@Override
141+
public NumberFormat getCurrencyInstance(Locale locale) {
142+
throw unexpected();
143+
}
144+
145+
@Override
146+
public NumberFormat getIntegerInstance(Locale locale) {
147+
throw unexpected();
148+
}
149+
150+
@Override
151+
public NumberFormat getNumberInstance(Locale locale) {
152+
throw unexpected();
153+
}
154+
155+
@Override
156+
public NumberFormat getPercentInstance(Locale locale) {
157+
throw unexpected();
158+
}
159+
160+
@Override
161+
public Locale[] getAvailableLocales() {
162+
throw unexpected();
163+
}
164+
}
165+
166+
static class DummyCalendarDataProvider extends CalendarDataProvider {
167+
168+
@Override
169+
public int getFirstDayOfWeek(Locale locale) {
170+
throw unexpected();
171+
}
172+
173+
@Override
174+
public int getMinimalDaysInFirstWeek(Locale locale) {
175+
throw unexpected();
176+
}
177+
178+
@Override
179+
public Locale[] getAvailableLocales() {
180+
throw unexpected();
181+
}
182+
}
183+
184+
static class DummyCalendarNameProvider extends CalendarNameProvider {
185+
186+
@Override
187+
public String getDisplayName(String calendarType, int field, int value, int style, Locale locale) {
188+
throw unexpected();
189+
}
190+
191+
@Override
192+
public Map<String, Integer> getDisplayNames(String calendarType, int field, int style, Locale locale) {
193+
throw unexpected();
194+
}
195+
196+
@Override
197+
public Locale[] getAvailableLocales() {
198+
throw unexpected();
199+
}
200+
}
201+
202+
static class DummyCurrencyNameProvider extends CurrencyNameProvider {
203+
204+
@Override
205+
public String getSymbol(String currencyCode, Locale locale) {
206+
throw unexpected();
207+
}
208+
209+
@Override
210+
public Locale[] getAvailableLocales() {
211+
throw unexpected();
212+
}
213+
}
214+
215+
static class DummyLocaleNameProvider extends LocaleNameProvider {
216+
217+
@Override
218+
public String getDisplayLanguage(String languageCode, Locale locale) {
219+
throw unexpected();
220+
}
221+
222+
@Override
223+
public String getDisplayCountry(String countryCode, Locale locale) {
224+
throw unexpected();
225+
}
226+
227+
@Override
228+
public String getDisplayVariant(String variant, Locale locale) {
229+
throw unexpected();
230+
}
231+
232+
@Override
233+
public Locale[] getAvailableLocales() {
234+
throw unexpected();
235+
}
236+
}
237+
238+
static class DummyTimeZoneNameProvider extends TimeZoneNameProvider {
239+
240+
@Override
241+
public String getDisplayName(String ID, boolean daylight, int style, Locale locale) {
242+
throw unexpected();
243+
}
244+
245+
@Override
246+
public Locale[] getAvailableLocales() {
247+
throw unexpected();
248+
}
249+
}
250+
251+
private static RuntimeException unexpected() {
252+
return new IllegalStateException("This method isn't supposed to be called");
253+
}
254+
255+
}

0 commit comments

Comments
 (0)