Skip to content

Commit a4c2204

Browse files
committed
instrumentation for URL methods + tests
1 parent 6a3e259 commit a4c2204

File tree

7 files changed

+88
-11
lines changed

7 files changed

+88
-11
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,16 @@ public interface EntitlementChecker {
324324

325325
// URLConnection (java.net + sun.net.www)
326326

327+
void check$java_net_URL$openConnection(Class<?> callerClass, java.net.URL that);
328+
327329
void check$java_net_URL$openConnection(Class<?> callerClass, java.net.URL that, Proxy proxy);
328330

331+
void check$java_net_URL$openStream(Class<?> callerClass, java.net.URL that);
332+
333+
void check$java_net_URL$getContent(Class<?> callerClass, java.net.URL that);
334+
335+
void check$java_net_URL$getContent(Class<?> callerClass, java.net.URL that, Class<?>[] classes);
336+
329337
void check$java_net_URLConnection$getContentLength(Class<?> callerClass, java.net.URLConnection that);
330338

331339
void check$java_net_URLConnection$getContentLengthLong(Class<?> callerClass, java.net.URLConnection that);

libs/entitlement/qa/entitled-plugin/src/main/java/org/elasticsearch/entitlement/qa/entitled/EntitledActions.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.elasticsearch.core.SuppressForbidden;
1313

1414
import java.io.IOException;
15-
import java.net.MalformedURLException;
1615
import java.net.URI;
1716
import java.net.URLConnection;
1817
import java.nio.file.Files;

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import java.net.ServerSocket;
1919
import java.net.Socket;
2020
import java.net.SocketException;
21-
import java.net.URI;
22-
import java.net.URISyntaxException;
2321
import java.nio.ByteBuffer;
2422
import java.nio.channels.AsynchronousServerSocketChannel;
2523
import java.nio.channels.AsynchronousSocketChannel;
@@ -75,12 +73,6 @@ static void socketConnect() throws IOException {
7573
}
7674
}
7775

78-
static void urlOpenConnectionWithProxy() throws URISyntaxException, IOException {
79-
var url = new URI("http://localhost").toURL();
80-
var urlConnection = url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(0)));
81-
assert urlConnection != null;
82-
}
83-
8476
static void createLDAPCertStore() {
8577
try {
8678
// We pass down null params to provoke a InvalidAlgorithmParameterException

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ static CheckAction alwaysDenied(CheckedRunnable<Exception> action) {
143143
entry("server_socket_bind", forPlugins(NetworkAccessCheckActions::serverSocketBind)),
144144
entry("server_socket_accept", forPlugins(NetworkAccessCheckActions::serverSocketAccept)),
145145

146-
entry("url_open_connection_proxy", forPlugins(NetworkAccessCheckActions::urlOpenConnectionWithProxy)),
147146
entry("http_client_send", forPlugins(VersionSpecificNetworkChecks::httpClientSend)),
148147
entry("http_client_send_async", forPlugins(VersionSpecificNetworkChecks::httpClientSendAsync)),
149148
entry("create_ldap_cert_store", forPlugins(NetworkAccessCheckActions::createLDAPCertStore)),

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@
1010
package org.elasticsearch.entitlement.qa.test;
1111

1212
import org.elasticsearch.core.CheckedConsumer;
13+
import org.elasticsearch.core.SuppressForbidden;
1314
import org.elasticsearch.entitlement.qa.entitled.EntitledActions;
1415

1516
import java.io.IOException;
1617
import java.io.InputStream;
1718
import java.net.ConnectException;
1819
import java.net.HttpURLConnection;
20+
import java.net.InetSocketAddress;
1921
import java.net.MalformedURLException;
22+
import java.net.Proxy;
2023
import java.net.URI;
24+
import java.net.URISyntaxException;
2125
import java.net.URL;
2226
import java.net.URLConnection;
2327

@@ -75,6 +79,46 @@ private static void withJdkHttpConnection(CheckedConsumer<HttpURLConnection, Exc
7579
}
7680
}
7781

82+
@EntitlementTest(expectedAccess = PLUGINS)
83+
static void urlOpenConnection() throws Exception {
84+
URI.create("http://127.0.0.1:12345/").toURL().openConnection();
85+
}
86+
87+
@EntitlementTest(expectedAccess = PLUGINS)
88+
@SuppressForbidden(reason = "just testing, not a real connection")
89+
static void urlOpenConnectionWithProxy() throws URISyntaxException, IOException {
90+
var url = new URI("http://localhost").toURL();
91+
var urlConnection = url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(0)));
92+
assert urlConnection != null;
93+
}
94+
95+
@EntitlementTest(expectedAccess = PLUGINS)
96+
static void urlOpenStream() throws Exception {
97+
try {
98+
URI.create("http://127.0.0.1:12345/").toURL().openStream().close();
99+
} catch (java.net.ConnectException e) {
100+
// It's OK, it means we passed entitlement checks, and we tried to connect
101+
}
102+
}
103+
104+
@EntitlementTest(expectedAccess = PLUGINS)
105+
static void urlGetContent() throws Exception {
106+
try {
107+
URI.create("http://127.0.0.1:12345/").toURL().getContent();
108+
} catch (java.net.ConnectException e) {
109+
// It's OK, it means we passed entitlement checks, and we tried to connect
110+
}
111+
}
112+
113+
@EntitlementTest(expectedAccess = PLUGINS)
114+
static void urlGetContentWithClasses() throws Exception {
115+
try {
116+
URI.create("http://127.0.0.1:12345/").toURL().getContent(new Class<?>[] { String.class });
117+
} catch (java.net.ConnectException e) {
118+
// It's OK, it means we passed entitlement checks, and we tried to connect
119+
}
120+
}
121+
78122
@EntitlementTest(expectedAccess = PLUGINS)
79123
static void baseUrlConnectionGetContentLength() throws Exception {
80124
withPlainNetworkConnection(URLConnection::getContentLength);

libs/entitlement/qa/src/javaRestTest/java/org/elasticsearch/entitlement/qa/EntitlementsTestRule.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class EntitlementsTestRule implements TestRule {
3434
// entitlements that test methods may use, see EntitledActions
3535
private static final PolicyBuilder ENTITLED_POLICY = (builder, tempDir) -> {
3636
builder.value("manage_threads");
37+
builder.value("outbound_network");
3738
builder.value(
3839
Map.of(
3940
"files",

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/api/ElasticsearchEntitlementChecker.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,9 +640,37 @@ public ElasticsearchEntitlementChecker(PolicyManager policyManager) {
640640
policyManager.checkOutboundNetworkAccess(callerClass);
641641
}
642642

643+
@Override
644+
public void check$java_net_URL$openConnection(Class<?> callerClass, java.net.URL that) {
645+
if (isNetworkUrl(that)) {
646+
policyManager.checkOutboundNetworkAccess(callerClass);
647+
}
648+
}
649+
643650
@Override
644651
public void check$java_net_URL$openConnection(Class<?> callerClass, URL that, Proxy proxy) {
645-
if (proxy.type() != Proxy.Type.DIRECT) {
652+
if (proxy.type() != Proxy.Type.DIRECT || isNetworkUrl(that)) {
653+
policyManager.checkOutboundNetworkAccess(callerClass);
654+
}
655+
}
656+
657+
@Override
658+
public void check$java_net_URL$openStream(Class<?> callerClass, java.net.URL that) {
659+
if (isNetworkUrl(that)) {
660+
policyManager.checkOutboundNetworkAccess(callerClass);
661+
}
662+
}
663+
664+
@Override
665+
public void check$java_net_URL$getContent(Class<?> callerClass, java.net.URL that) {
666+
if (isNetworkUrl(that)) {
667+
policyManager.checkOutboundNetworkAccess(callerClass);
668+
}
669+
}
670+
671+
@Override
672+
public void check$java_net_URL$getContent(Class<?> callerClass, java.net.URL that, Class<?>[] classes) {
673+
if (isNetworkUrl(that)) {
646674
policyManager.checkOutboundNetworkAccess(callerClass);
647675
}
648676
}
@@ -653,6 +681,12 @@ public ElasticsearchEntitlementChecker(PolicyManager policyManager) {
653681
"sun.net.www.protocol.mailto.MailToURLConnection"
654682
);
655683

684+
private static final Set<String> NETWORK_PROTOCOLS = Set.of("http", "https", "ftp", "mailto");
685+
686+
private static boolean isNetworkUrl(java.net.URL url) {
687+
return NETWORK_PROTOCOLS.contains(url.getProtocol());
688+
}
689+
656690
private static boolean isNetworkUrlConnection(java.net.URLConnection urlConnection) {
657691
var connectionClass = urlConnection.getClass();
658692
return HttpURLConnection.class.isAssignableFrom(connectionClass)

0 commit comments

Comments
 (0)