Skip to content

Commit 900e90d

Browse files
committed
Internal: Add versionless alias for rest client codebase in policy files (#26521)
Security manager policy files contains grants for specific codebases, where a codebase is a jar file. We use a system property containing the name of the jar file to resolve the jar file location when parsing the policy file. However, this means the version of the jars must be modified when versions of dependencies change. This is particularly messy for elasticsearch, where we now have a dependency on the rest client, and need to support both a snapshot version for testing and non snapshot for release. This commit adds an alias for the elasticsearch rest client without a version to be used in policy files. That allows the policy files to not care whether the rest client is a snapshot or release.
1 parent 649ec47 commit 900e90d

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

core/src/main/java/org/elasticsearch/bootstrap/Security.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
package org.elasticsearch.bootstrap;
2121

22+
import org.elasticsearch.Build;
2223
import org.elasticsearch.SecureSM;
24+
import org.elasticsearch.Version;
2325
import org.elasticsearch.common.SuppressForbidden;
2426
import org.elasticsearch.common.io.PathUtils;
2527
import org.elasticsearch.common.network.NetworkModule;
@@ -43,10 +45,12 @@
4345
import java.security.Permissions;
4446
import java.security.Policy;
4547
import java.security.URIParameter;
48+
import java.util.ArrayList;
4649
import java.util.Collections;
4750
import java.util.HashMap;
4851
import java.util.HashSet;
4952
import java.util.LinkedHashSet;
53+
import java.util.List;
5054
import java.util.Map;
5155
import java.util.Set;
5256

@@ -191,27 +195,39 @@ static Map<String,Policy> getPluginPermissions(Environment environment) throws I
191195
@SuppressForbidden(reason = "accesses fully qualified URLs to configure security")
192196
static Policy readPolicy(URL policyFile, Set<URL> codebases) {
193197
try {
198+
List<String> propertiesSet = new ArrayList<>();
194199
try {
195200
// set codebase properties
196201
for (URL url : codebases) {
197202
String shortName = PathUtils.get(url.toURI()).getFileName().toString();
198203
if (shortName.endsWith(".jar") == false) {
199204
continue; // tests :(
200205
}
201-
String previous = System.setProperty("codebase." + shortName, url.toString());
206+
String property = "codebase." + shortName;
207+
if (shortName.startsWith("elasticsearch-rest-client")) {
208+
// The rest client is currently the only example where we have an elasticsearch built artifact
209+
// which needs special permissions in policy files when used. This temporary solution is to
210+
// pass in an extra system property that omits the -version.jar suffix the other properties have.
211+
// That allows the snapshots to reference snapshot builds of the client, and release builds to
212+
// referenced release builds of the client, all with the same grant statements.
213+
final String esVersion = Version.CURRENT + (Build.CURRENT.isSnapshot() ? "-SNAPSHOT" : "");
214+
final int index = property.indexOf("-" + esVersion + ".jar");
215+
assert index >= 0;
216+
String restClientAlias = property.substring(0, index);
217+
propertiesSet.add(restClientAlias);
218+
System.setProperty(restClientAlias, url.toString());
219+
}
220+
propertiesSet.add(property);
221+
String previous = System.setProperty(property, url.toString());
202222
if (previous != null) {
203223
throw new IllegalStateException("codebase property already set: " + shortName + "->" + previous);
204224
}
205225
}
206226
return Policy.getInstance("JavaPolicy", new URIParameter(policyFile.toURI()));
207227
} finally {
208228
// clear codebase properties
209-
for (URL url : codebases) {
210-
String shortName = PathUtils.get(url.toURI()).getFileName().toString();
211-
if (shortName.endsWith(".jar") == false) {
212-
continue; // tests :(
213-
}
214-
System.clearProperty("codebase." + shortName);
229+
for (String property : propertiesSet) {
230+
System.clearProperty(property);
215231
}
216232
}
217233
} catch (NoSuchAlgorithmException | URISyntaxException e) {

core/src/main/resources/org/elasticsearch/bootstrap/test-framework.policy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ grant codeBase "${codebase.mocksocket-1.2.jar}" {
6363
permission java.net.SocketPermission "*", "accept,connect";
6464
};
6565

66-
grant codeBase "${codebase.elasticsearch-rest-client-6.0.0-rc1-SNAPSHOT.jar}" {
66+
grant codeBase "${codebase.elasticsearch-rest-client}" {
6767
// rest makes socket connections for rest tests
6868
permission java.net.SocketPermission "*", "connect";
6969
// rest client uses system properties which gets the default proxy

modules/reindex/src/main/plugin-metadata/plugin-security.policy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ grant {
2222
permission java.net.SocketPermission "*", "connect";
2323
};
2424

25-
grant codeBase "${codebase.elasticsearch-rest-client-6.0.0-rc1-SNAPSHOT.jar}" {
25+
grant codeBase "${codebase.elasticsearch-rest-client}" {
2626
// rest client uses system properties which gets the default proxy
2727
permission java.net.NetPermission "getProxySelector";
2828
};

0 commit comments

Comments
 (0)