|
34 | 34 | import java.io.IOException; |
35 | 35 | import java.io.UncheckedIOException; |
36 | 36 | import java.net.DatagramSocket; |
37 | | -import java.net.DatagramSocketImpl; |
38 | | -import java.net.DatagramSocketImplFactory; |
39 | 37 | import java.net.HttpURLConnection; |
| 38 | +import java.net.MalformedURLException; |
| 39 | +import java.net.ProxySelector; |
| 40 | +import java.net.ResponseCache; |
40 | 41 | import java.net.ServerSocket; |
41 | 42 | import java.net.Socket; |
42 | 43 | import java.net.URL; |
43 | 44 | import java.net.URLClassLoader; |
44 | 45 | import java.net.URLConnection; |
| 46 | +import java.net.URLStreamHandler; |
| 47 | +import java.net.spi.URLStreamHandlerProvider; |
45 | 48 | import java.security.NoSuchAlgorithmException; |
46 | 49 | import java.util.List; |
47 | 50 | import java.util.Map; |
48 | 51 | import java.util.Set; |
49 | 52 | import java.util.stream.Collectors; |
| 53 | +import java.util.stream.Stream; |
50 | 54 |
|
51 | 55 | import javax.net.ssl.HttpsURLConnection; |
52 | 56 | import javax.net.ssl.SSLContext; |
| 57 | +import javax.net.ssl.SSLSession; |
| 58 | +import javax.net.ssl.SSLSocket; |
| 59 | +import javax.net.ssl.SSLSocketFactory; |
53 | 60 |
|
54 | 61 | import static java.util.Map.entry; |
55 | 62 | import static org.elasticsearch.entitlement.qa.common.RestEntitlementsCheckAction.CheckAction.alwaysDenied; |
56 | 63 | import static org.elasticsearch.entitlement.qa.common.RestEntitlementsCheckAction.CheckAction.deniedToPlugins; |
57 | 64 | import static org.elasticsearch.entitlement.qa.common.RestEntitlementsCheckAction.CheckAction.forPlugins; |
58 | 65 | import static org.elasticsearch.rest.RestRequest.Method.GET; |
59 | 66 |
|
| 67 | +@SuppressWarnings("unused") |
60 | 68 | public class RestEntitlementsCheckAction extends BaseRestHandler { |
61 | 69 | private static final Logger logger = LogManager.getLogger(RestEntitlementsCheckAction.class); |
62 | 70 | public static final Thread NO_OP_SHUTDOWN_HOOK = new Thread(() -> {}, "Shutdown hook for testing"); |
63 | 71 | private final String prefix; |
64 | 72 |
|
65 | | - record CheckAction(Runnable action, boolean isAlwaysDeniedToPlugins) { |
| 73 | + record CheckAction(Runnable action, boolean isAlwaysDeniedToPlugins, Integer fromJavaVersion) { |
66 | 74 | /** |
67 | 75 | * These cannot be granted to plugins, so our test plugins cannot test the "allowed" case. |
68 | | - * Used both for always-denied entitlements as well as those granted only to the server itself. |
| 76 | + * Used both for always-denied entitlements and those granted only to the server itself. |
69 | 77 | */ |
70 | 78 | static CheckAction deniedToPlugins(Runnable action) { |
71 | | - return new CheckAction(action, true); |
| 79 | + return new CheckAction(action, true, null); |
72 | 80 | } |
73 | 81 |
|
74 | 82 | static CheckAction forPlugins(Runnable action) { |
75 | | - return new CheckAction(action, false); |
| 83 | + return new CheckAction(action, false, null); |
76 | 84 | } |
77 | 85 |
|
78 | 86 | static CheckAction alwaysDenied(Runnable action) { |
79 | | - return new CheckAction(action, true); |
| 87 | + return new CheckAction(action, true, null); |
80 | 88 | } |
81 | 89 | } |
82 | 90 |
|
83 | | - private static final Map<String, CheckAction> checkActions = Map.ofEntries( |
| 91 | + private static final Map<String, CheckAction> checkActions = Stream.of( |
84 | 92 | entry("runtime_exit", deniedToPlugins(RestEntitlementsCheckAction::runtimeExit)), |
85 | 93 | entry("runtime_halt", deniedToPlugins(RestEntitlementsCheckAction::runtimeHalt)), |
86 | 94 | entry("system_exit", deniedToPlugins(RestEntitlementsCheckAction::systemExit)), |
@@ -125,8 +133,77 @@ static CheckAction alwaysDenied(Runnable action) { |
125 | 133 | entry("socket_setSocketImplFactory", alwaysDenied(RestEntitlementsCheckAction::socket$$setSocketImplFactory)), |
126 | 134 | entry("url_setURLStreamHandlerFactory", alwaysDenied(RestEntitlementsCheckAction::url$$setURLStreamHandlerFactory)), |
127 | 135 | entry("urlConnection_setFileNameMap", alwaysDenied(RestEntitlementsCheckAction::urlConnection$$setFileNameMap)), |
128 | | - entry("urlConnection_setContentHandlerFactory", alwaysDenied(RestEntitlementsCheckAction::urlConnection$$setContentHandlerFactory)) |
129 | | - ); |
| 136 | + entry("urlConnection_setContentHandlerFactory", alwaysDenied(RestEntitlementsCheckAction::urlConnection$$setContentHandlerFactory)), |
| 137 | + |
| 138 | + entry("proxySelector_setDefault", alwaysDenied(RestEntitlementsCheckAction::setDefaultProxySelector)), |
| 139 | + entry("responseCache_setDefault", alwaysDenied(RestEntitlementsCheckAction::setDefaultResponseCache)), |
| 140 | + entry( |
| 141 | + "createInetAddressResolverProvider", |
| 142 | + new CheckAction(VersionSpecificNetworkChecks::createInetAddressResolverProvider, true, 18) |
| 143 | + ), |
| 144 | + entry("createURLStreamHandlerProvider", alwaysDenied(RestEntitlementsCheckAction::createURLStreamHandlerProvider)), |
| 145 | + entry("createURLWithURLStreamHandler", alwaysDenied(RestEntitlementsCheckAction::createURLWithURLStreamHandler)), |
| 146 | + entry("createURLWithURLStreamHandler2", alwaysDenied(RestEntitlementsCheckAction::createURLWithURLStreamHandler2)), |
| 147 | + entry("sslSessionImpl_getSessionContext", alwaysDenied(RestEntitlementsCheckAction::sslSessionImplGetSessionContext)) |
| 148 | + ) |
| 149 | + .filter(entry -> entry.getValue().fromJavaVersion() == null || Runtime.version().feature() >= entry.getValue().fromJavaVersion()) |
| 150 | + .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 151 | + |
| 152 | + private static void createURLStreamHandlerProvider() { |
| 153 | + var x = new URLStreamHandlerProvider() { |
| 154 | + @Override |
| 155 | + public URLStreamHandler createURLStreamHandler(String protocol) { |
| 156 | + return null; |
| 157 | + } |
| 158 | + }; |
| 159 | + } |
| 160 | + |
| 161 | + private static void sslSessionImplGetSessionContext() { |
| 162 | + SSLSocketFactory factory = HttpsURLConnection.getDefaultSSLSocketFactory(); |
| 163 | + try (SSLSocket socket = (SSLSocket) factory.createSocket()) { |
| 164 | + SSLSession session = socket.getSession(); |
| 165 | + |
| 166 | + session.getSessionContext(); |
| 167 | + } catch (IOException e) { |
| 168 | + throw new RuntimeException(e); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + @SuppressWarnings("deprecation") |
| 173 | + private static void createURLWithURLStreamHandler() { |
| 174 | + try { |
| 175 | + var x = new URL("http", "host", 1234, "file", new URLStreamHandler() { |
| 176 | + @Override |
| 177 | + protected URLConnection openConnection(URL u) { |
| 178 | + return null; |
| 179 | + } |
| 180 | + }); |
| 181 | + } catch (MalformedURLException e) { |
| 182 | + throw new RuntimeException(e); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + @SuppressWarnings("deprecation") |
| 187 | + private static void createURLWithURLStreamHandler2() { |
| 188 | + try { |
| 189 | + var x = new URL(null, "spec", new URLStreamHandler() { |
| 190 | + @Override |
| 191 | + protected URLConnection openConnection(URL u) { |
| 192 | + return null; |
| 193 | + } |
| 194 | + }); |
| 195 | + } catch (MalformedURLException e) { |
| 196 | + throw new RuntimeException(e); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + private static void setDefaultResponseCache() { |
| 201 | + ResponseCache.setDefault(null); |
| 202 | + } |
| 203 | + |
| 204 | + private static void setDefaultProxySelector() { |
| 205 | + ProxySelector.setDefault(null); |
| 206 | + } |
130 | 207 |
|
131 | 208 | private static void setDefaultSSLContext() { |
132 | 209 | try { |
@@ -270,12 +347,7 @@ private static void setHttpsConnectionProperties() { |
270 | 347 | @SuppressForbidden(reason = "We're required to prevent calls to this forbidden API") |
271 | 348 | private static void datagramSocket$$setDatagramSocketImplFactory() { |
272 | 349 | try { |
273 | | - DatagramSocket.setDatagramSocketImplFactory(new DatagramSocketImplFactory() { |
274 | | - @Override |
275 | | - public DatagramSocketImpl createDatagramSocketImpl() { |
276 | | - throw new IllegalStateException(); |
277 | | - } |
278 | | - }); |
| 350 | + DatagramSocket.setDatagramSocketImplFactory(() -> { throw new IllegalStateException(); }); |
279 | 351 | } catch (IOException e) { |
280 | 352 | throw new IllegalStateException(e); |
281 | 353 | } |
|
0 commit comments