Skip to content

Commit 46a8e69

Browse files
authored
[Entitlements] Introducing runtime version-specific checks in IT tests (#120265)
1 parent 5b5f099 commit 46a8e69

File tree

4 files changed

+64
-24
lines changed

4 files changed

+64
-24
lines changed

libs/entitlement/qa/common/build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
99

10+
import org.elasticsearch.gradle.internal.precommit.CheckForbiddenApisTask
11+
1012
apply plugin: 'elasticsearch.build'
13+
apply plugin: 'elasticsearch.mrjar'
1114

1215
dependencies {
1316
implementation project(':server')
1417
implementation project(':libs:logging')
1518
}
19+
20+
tasks.withType(CheckForbiddenApisTask).configureEach {
21+
replaceSignatureFiles 'jdk-signatures'
22+
}

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

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,13 @@
4949
import java.net.URLClassLoader;
5050
import java.net.URLConnection;
5151
import java.net.URLStreamHandler;
52-
import java.net.spi.InetAddressResolver;
53-
import java.net.spi.InetAddressResolverProvider;
5452
import java.net.spi.URLStreamHandlerProvider;
5553
import java.security.NoSuchAlgorithmException;
5654
import java.util.List;
5755
import java.util.Map;
5856
import java.util.Set;
5957
import java.util.stream.Collectors;
58+
import java.util.stream.Stream;
6059

6160
import javax.net.ssl.HttpsURLConnection;
6261
import javax.net.ssl.SSLContext;
@@ -73,25 +72,25 @@ public class RestEntitlementsCheckAction extends BaseRestHandler {
7372
public static final Thread NO_OP_SHUTDOWN_HOOK = new Thread(() -> {}, "Shutdown hook for testing");
7473
private final String prefix;
7574

76-
record CheckAction(CheckedRunnable<Exception> action, boolean isAlwaysDeniedToPlugins) {
75+
record CheckAction(CheckedRunnable<Exception> action, boolean isAlwaysDeniedToPlugins, Integer fromJavaVersion) {
7776
/**
7877
* These cannot be granted to plugins, so our test plugins cannot test the "allowed" case.
79-
* Used both for always-denied entitlements as well as those granted only to the server itself.
78+
* Used both for always-denied entitlements and those granted only to the server itself.
8079
*/
8180
static CheckAction deniedToPlugins(CheckedRunnable<Exception> action) {
82-
return new CheckAction(action, true);
81+
return new CheckAction(action, true, null);
8382
}
8483

8584
static CheckAction forPlugins(CheckedRunnable<Exception> action) {
86-
return new CheckAction(action, false);
85+
return new CheckAction(action, false, null);
8786
}
8887

8988
static CheckAction alwaysDenied(CheckedRunnable<Exception> action) {
90-
return new CheckAction(action, true);
89+
return new CheckAction(action, true, null);
9190
}
9291
}
9392

94-
private static final Map<String, CheckAction> checkActions = Map.ofEntries(
93+
private static final Map<String, CheckAction> checkActions = Stream.of(
9594
entry("runtime_exit", deniedToPlugins(RestEntitlementsCheckAction::runtimeExit)),
9695
entry("runtime_halt", deniedToPlugins(RestEntitlementsCheckAction::runtimeHalt)),
9796
entry("system_exit", deniedToPlugins(RestEntitlementsCheckAction::systemExit)),
@@ -140,7 +139,10 @@ static CheckAction alwaysDenied(CheckedRunnable<Exception> action) {
140139

141140
entry("proxySelector_setDefault", alwaysDenied(RestEntitlementsCheckAction::setDefaultProxySelector)),
142141
entry("responseCache_setDefault", alwaysDenied(RestEntitlementsCheckAction::setDefaultResponseCache)),
143-
entry("createInetAddressResolverProvider", alwaysDenied(RestEntitlementsCheckAction::createInetAddressResolverProvider)),
142+
entry(
143+
"createInetAddressResolverProvider",
144+
new CheckAction(VersionSpecificNetworkChecks::createInetAddressResolverProvider, true, 18)
145+
),
144146
entry("createURLStreamHandlerProvider", alwaysDenied(RestEntitlementsCheckAction::createURLStreamHandlerProvider)),
145147
entry("createURLWithURLStreamHandler", alwaysDenied(RestEntitlementsCheckAction::createURLWithURLStreamHandler)),
146148
entry("createURLWithURLStreamHandler2", alwaysDenied(RestEntitlementsCheckAction::createURLWithURLStreamHandler2)),
@@ -156,7 +158,9 @@ static CheckAction alwaysDenied(CheckedRunnable<Exception> action) {
156158
entry("socket_connect", forPlugins(NetworkAccessCheckActions::socketConnect)),
157159
entry("server_socket_bind", forPlugins(NetworkAccessCheckActions::serverSocketBind)),
158160
entry("server_socket_accept", forPlugins(NetworkAccessCheckActions::serverSocketAccept))
159-
);
161+
)
162+
.filter(entry -> entry.getValue().fromJavaVersion() == null || Runtime.version().feature() >= entry.getValue().fromJavaVersion())
163+
.collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue));
160164

161165
private static void createURLStreamHandlerProvider() {
162166
var x = new URLStreamHandlerProvider() {
@@ -187,20 +191,6 @@ protected URLConnection openConnection(URL u) {
187191
});
188192
}
189193

190-
private static void createInetAddressResolverProvider() {
191-
var x = new InetAddressResolverProvider() {
192-
@Override
193-
public InetAddressResolver get(Configuration configuration) {
194-
return null;
195-
}
196-
197-
@Override
198-
public String name() {
199-
return "TEST";
200-
}
201-
};
202-
}
203-
204194
private static void setDefaultResponseCache() {
205195
ResponseCache.setDefault(null);
206196
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
class VersionSpecificNetworkChecks {
13+
static void createInetAddressResolverProvider() {}
14+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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.net.spi.InetAddressResolver;
13+
import java.net.spi.InetAddressResolverProvider;
14+
15+
class VersionSpecificNetworkChecks {
16+
static void createInetAddressResolverProvider() {
17+
var x = new InetAddressResolverProvider() {
18+
@Override
19+
public InetAddressResolver get(Configuration configuration) {
20+
return null;
21+
}
22+
23+
@Override
24+
public String name() {
25+
return "TEST";
26+
}
27+
};
28+
}
29+
}

0 commit comments

Comments
 (0)