Skip to content

Commit 3fa2415

Browse files
authored
[Entitlements] Move some checks that use version-specific API (#120397)
1 parent 110b206 commit 3fa2415

File tree

5 files changed

+105
-36
lines changed

5 files changed

+105
-36
lines changed

libs/entitlement/qa/common/src/main/java/org/elasticsearch/entitlement/qa/common/NetworkAccessCheckActions.java

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
import java.net.SocketException;
2121
import java.net.URI;
2222
import java.net.URISyntaxException;
23-
import java.net.http.HttpClient;
24-
import java.net.http.HttpRequest;
25-
import java.net.http.HttpResponse;
2623
import java.nio.ByteBuffer;
2724
import java.nio.channels.AsynchronousServerSocketChannel;
2825
import java.nio.channels.AsynchronousSocketChannel;
@@ -84,37 +81,6 @@ static void urlOpenConnectionWithProxy() throws URISyntaxException, IOException
8481
assert urlConnection != null;
8582
}
8683

87-
static void httpClientSend() throws InterruptedException {
88-
try (HttpClient httpClient = HttpClient.newBuilder().build()) {
89-
// Shutdown the client, so the send action will shortcut before actually executing any network operation
90-
// (but after it run our check in the prologue)
91-
httpClient.shutdown();
92-
try {
93-
httpClient.send(HttpRequest.newBuilder(URI.create("http://localhost")).build(), HttpResponse.BodyHandlers.discarding());
94-
} catch (IOException e) {
95-
// Expected, since we shut down the client.
96-
// "send" will be called and exercise the Entitlement check, we don't care if it fails afterward for this known reason.
97-
}
98-
}
99-
}
100-
101-
static void httpClientSendAsync() {
102-
try (HttpClient httpClient = HttpClient.newBuilder().build()) {
103-
// Shutdown the client, so the send action will return before actually executing any network operation
104-
// (but after it run our check in the prologue)
105-
httpClient.shutdown();
106-
var future = httpClient.sendAsync(
107-
HttpRequest.newBuilder(URI.create("http://localhost")).build(),
108-
HttpResponse.BodyHandlers.discarding()
109-
);
110-
assert future.isCompletedExceptionally();
111-
future.exceptionally(ex -> {
112-
assert ex instanceof IOException;
113-
return null;
114-
});
115-
}
116-
}
117-
11884
static void createLDAPCertStore() throws NoSuchAlgorithmException {
11985
try {
12086
// We pass down null params to provoke a InvalidAlgorithmParameterException

libs/entitlement/qa/common/src/main/java/org/elasticsearch/entitlement/qa/common/RestEntitlementsCheckAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ static CheckAction alwaysDenied(CheckedRunnable<Exception> action) {
160160
entry("server_socket_accept", forPlugins(NetworkAccessCheckActions::serverSocketAccept)),
161161

162162
entry("url_open_connection_proxy", forPlugins(NetworkAccessCheckActions::urlOpenConnectionWithProxy)),
163-
entry("http_client_send", forPlugins(NetworkAccessCheckActions::httpClientSend)),
164-
entry("http_client_send_async", forPlugins(NetworkAccessCheckActions::httpClientSendAsync)),
163+
entry("http_client_send", forPlugins(VersionSpecificNetworkChecks::httpClientSend)),
164+
entry("http_client_send_async", forPlugins(VersionSpecificNetworkChecks::httpClientSendAsync)),
165165
entry("create_ldap_cert_store", forPlugins(NetworkAccessCheckActions::createLDAPCertStore)),
166166

167167
entry("server_socket_channel_bind", forPlugins(NetworkAccessCheckActions::serverSocketChannelBind)),

libs/entitlement/qa/common/src/main/java/org/elasticsearch/entitlement/qa/common/VersionSpecificNetworkChecks.java

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

1010
package org.elasticsearch.entitlement.qa.common;
1111

12+
import java.io.IOException;
13+
import java.net.URI;
14+
import java.net.http.HttpClient;
15+
import java.net.http.HttpRequest;
16+
import java.net.http.HttpResponse;
17+
1218
class VersionSpecificNetworkChecks {
1319
static void createInetAddressResolverProvider() {}
20+
21+
static void httpClientSend() throws InterruptedException {
22+
HttpClient httpClient = HttpClient.newBuilder().build();
23+
try {
24+
httpClient.send(HttpRequest.newBuilder(URI.create("http://localhost")).build(), HttpResponse.BodyHandlers.discarding());
25+
} catch (IOException e) {
26+
// Expected, the send action may fail with these parameters (but after it run the entitlement check in the prologue)
27+
}
28+
}
29+
30+
static void httpClientSendAsync() {
31+
HttpClient httpClient = HttpClient.newBuilder().build();
32+
httpClient.sendAsync(HttpRequest.newBuilder(URI.create("http://localhost")).build(), HttpResponse.BodyHandlers.discarding());
33+
}
1434
}

libs/entitlement/qa/common/src/main18/java/org/elasticsearch/entitlement/qa/common/VersionSpecificNetworkChecks.java

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

1010
package org.elasticsearch.entitlement.qa.common;
1111

12+
import java.io.IOException;
13+
import java.net.URI;
14+
import java.net.http.HttpClient;
15+
import java.net.http.HttpRequest;
16+
import java.net.http.HttpResponse;
1217
import java.net.spi.InetAddressResolver;
1318
import java.net.spi.InetAddressResolverProvider;
1419

@@ -26,4 +31,18 @@ public String name() {
2631
}
2732
};
2833
}
34+
35+
static void httpClientSend() throws InterruptedException {
36+
HttpClient httpClient = HttpClient.newBuilder().build();
37+
try {
38+
httpClient.send(HttpRequest.newBuilder(URI.create("http://localhost")).build(), HttpResponse.BodyHandlers.discarding());
39+
} catch (IOException e) {
40+
// Expected, the send action may fail with these parameters (but after it run the entitlement check in the prologue)
41+
}
42+
}
43+
44+
static void httpClientSendAsync() {
45+
HttpClient httpClient = HttpClient.newBuilder().build();
46+
httpClient.sendAsync(HttpRequest.newBuilder(URI.create("http://localhost")).build(), HttpResponse.BodyHandlers.discarding());
47+
}
2948
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.io.IOException;
13+
import java.net.URI;
14+
import java.net.http.HttpClient;
15+
import java.net.http.HttpRequest;
16+
import java.net.http.HttpResponse;
17+
import java.net.spi.InetAddressResolver;
18+
import java.net.spi.InetAddressResolverProvider;
19+
20+
class VersionSpecificNetworkChecks {
21+
static void createInetAddressResolverProvider() {
22+
var x = new InetAddressResolverProvider() {
23+
@Override
24+
public InetAddressResolver get(Configuration configuration) {
25+
return null;
26+
}
27+
28+
@Override
29+
public String name() {
30+
return "TEST";
31+
}
32+
};
33+
}
34+
35+
static void httpClientSend() throws InterruptedException {
36+
try (HttpClient httpClient = HttpClient.newBuilder().build()) {
37+
// Shutdown the client, so the send action will shortcut before actually executing any network operation
38+
// (but after it run our check in the prologue)
39+
httpClient.shutdown();
40+
try {
41+
httpClient.send(HttpRequest.newBuilder(URI.create("http://localhost")).build(), HttpResponse.BodyHandlers.discarding());
42+
} catch (IOException e) {
43+
// Expected, since we shut down the client
44+
}
45+
}
46+
}
47+
48+
static void httpClientSendAsync() {
49+
try (HttpClient httpClient = HttpClient.newBuilder().build()) {
50+
// Shutdown the client, so the send action will return before actually executing any network operation
51+
// (but after it run our check in the prologue)
52+
httpClient.shutdown();
53+
var future = httpClient.sendAsync(
54+
HttpRequest.newBuilder(URI.create("http://localhost")).build(),
55+
HttpResponse.BodyHandlers.discarding()
56+
);
57+
assert future.isCompletedExceptionally();
58+
future.exceptionally(ex -> {
59+
assert ex instanceof IOException;
60+
return null;
61+
});
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)