Skip to content

Commit 0ed405d

Browse files
committed
URLConnectionNetworkActions tests
1 parent 304e1ab commit 0ed405d

File tree

3 files changed

+139
-2
lines changed

3 files changed

+139
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ static CheckAction alwaysDenied(CheckedRunnable<Exception> action) {
194194
getTestEntries(NioFileSystemActions.class),
195195
getTestEntries(PathActions.class),
196196
getTestEntries(SpiActions.class),
197-
getTestEntries(SystemActions.class)
197+
getTestEntries(SystemActions.class),
198+
getTestEntries(URLConnectionNetworkActions.class)
198199
)
199200
.flatMap(Function.identity())
200201
.filter(entry -> entry.getValue().fromJavaVersion() == null || Runtime.version().feature() >= entry.getValue().fromJavaVersion())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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 org.elasticsearch.core.CheckedConsumer;
13+
14+
import java.io.IOException;
15+
import java.io.InputStream;
16+
import java.net.ConnectException;
17+
import java.net.HttpURLConnection;
18+
import java.net.MalformedURLException;
19+
import java.net.URI;
20+
import java.net.URL;
21+
import java.net.URLConnection;
22+
23+
import static org.elasticsearch.entitlement.qa.test.EntitlementTest.ExpectedAccess.PLUGINS;
24+
25+
@SuppressWarnings("unused") // everything is called via reflection
26+
class URLConnectionNetworkActions {
27+
28+
private static final URL HTTP_URL;
29+
30+
static {
31+
try {
32+
HTTP_URL = URI.create("http://127.0.0.1/").toURL();
33+
} catch (MalformedURLException e) {
34+
throw new RuntimeException(e);
35+
}
36+
}
37+
38+
private static void withPlainNetworkConnection(CheckedConsumer<URLConnection, Exception> connectionConsumer) throws Exception {
39+
// Create a HttpURLConnection with minimal overrides to test calling directly into URLConnection methods as much as possible
40+
var conn = new HttpURLConnection(HTTP_URL) {
41+
@Override
42+
public void connect() {}
43+
44+
@Override
45+
public void disconnect() {}
46+
47+
@Override
48+
public boolean usingProxy() {
49+
return false;
50+
}
51+
52+
@Override
53+
public InputStream getInputStream() throws IOException {
54+
// Mock an attempt to call connect
55+
throw new ConnectException();
56+
}
57+
};
58+
59+
try {
60+
connectionConsumer.accept(conn);
61+
} catch (java.net.ConnectException e) {
62+
// It's OK, it means we passed entitlement checks, and we tried to connect
63+
}
64+
}
65+
66+
private static void withJdkHttpConnection(CheckedConsumer<HttpURLConnection, Exception> connectionConsumer) throws Exception {
67+
var conn = URI.create("http://127.0.0.1:12345/").toURL().openConnection();
68+
// Be sure we got the connection implementation we want
69+
assert HttpURLConnection.class.isAssignableFrom(conn.getClass());
70+
try {
71+
connectionConsumer.accept((HttpURLConnection) conn);
72+
} catch (java.net.ConnectException e) {
73+
// It's OK, it means we passed entitlement checks, and we tried to connect
74+
}
75+
}
76+
77+
@EntitlementTest(expectedAccess = PLUGINS)
78+
static void urlConnectionGetContentLength() throws Exception {
79+
withPlainNetworkConnection(URLConnection::getContentLength);
80+
withJdkHttpConnection(URLConnection::getContentLength);
81+
}
82+
83+
@EntitlementTest(expectedAccess = PLUGINS)
84+
static void urlConnectionGetContentType() throws Exception {
85+
withPlainNetworkConnection(URLConnection::getContentType);
86+
withJdkHttpConnection(URLConnection::getContentType);
87+
}
88+
89+
@EntitlementTest(expectedAccess = PLUGINS)
90+
static void urlConnectionGetContentEncoding() throws Exception {
91+
withPlainNetworkConnection(URLConnection::getContentEncoding);
92+
withJdkHttpConnection(URLConnection::getContentEncoding);
93+
}
94+
95+
@EntitlementTest(expectedAccess = PLUGINS)
96+
static void urlConnectionGetExpiration() throws Exception {
97+
withPlainNetworkConnection(URLConnection::getExpiration);
98+
withJdkHttpConnection(URLConnection::getExpiration);
99+
}
100+
101+
@EntitlementTest(expectedAccess = PLUGINS)
102+
static void urlConnectionGetDate() throws Exception {
103+
withPlainNetworkConnection(URLConnection::getDate);
104+
withJdkHttpConnection(URLConnection::getDate);
105+
}
106+
107+
@EntitlementTest(expectedAccess = PLUGINS)
108+
static void urlConnectionGetLastModified() throws Exception {
109+
withPlainNetworkConnection(URLConnection::getLastModified);
110+
withJdkHttpConnection(URLConnection::getLastModified);
111+
}
112+
113+
@EntitlementTest(expectedAccess = PLUGINS)
114+
static void urlConnectionGetHeaderFieldInt() throws Exception {
115+
withPlainNetworkConnection(conn -> conn.getHeaderFieldInt("field", 0));
116+
withJdkHttpConnection(conn -> conn.getHeaderFieldInt("field", 0));
117+
}
118+
119+
@EntitlementTest(expectedAccess = PLUGINS)
120+
static void urlConnectionGetHeaderFieldLong() throws Exception {
121+
withPlainNetworkConnection(conn -> conn.getHeaderFieldLong("field", 0));
122+
withJdkHttpConnection(conn -> conn.getHeaderFieldLong("field", 0));
123+
}
124+
125+
@EntitlementTest(expectedAccess = PLUGINS)
126+
static void urlConnectionGetContent() throws Exception {
127+
withPlainNetworkConnection(URLConnection::getContent);
128+
withJdkHttpConnection(URLConnection::getContent);
129+
}
130+
131+
@EntitlementTest(expectedAccess = PLUGINS)
132+
static void urlConnectionGetContentWithClasses() throws Exception {
133+
withPlainNetworkConnection(conn -> conn.getContent(new Class<?>[] { String.class }));
134+
withJdkHttpConnection(conn -> conn.getContent(new Class<?>[] { String.class }));
135+
}
136+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ public ElasticsearchEntitlementChecker(PolicyManager policyManager) {
655655

656656
private static boolean isNetworkUrlConnection(java.net.URLConnection urlConnection) {
657657
var connectionClass = urlConnection.getClass();
658-
return connectionClass.isAssignableFrom(HttpURLConnection.class)
658+
return HttpURLConnection.class.isAssignableFrom(connectionClass)
659659
|| ADDITIONAL_NETWORK_URL_CONNECT_CLASS_NAMES.contains(connectionClass.getName());
660660
}
661661

0 commit comments

Comments
 (0)