Skip to content

Commit 9b33eb5

Browse files
committed
Grant manage_threads to java.desktop for Tika
1 parent 7c957c3 commit 9b33eb5

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

libs/entitlement/src/main/java/org/elasticsearch/entitlement/bootstrap/HardcodedEntitlements.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,13 @@ private static List<Scope> createServerEntitlements(Path pidFile) {
114114
new FilesEntitlement(serverModuleFileDatas)
115115
)
116116
),
117-
new Scope("java.desktop", List.of(new LoadNativeLibrariesEntitlement())),
117+
new Scope(
118+
"java.desktop",
119+
List.of(
120+
new LoadNativeLibrariesEntitlement(),
121+
new ManageThreadsEntitlement() // For sun.java2d.Disposer
122+
)
123+
),
118124
new Scope(
119125
"java.xml",
120126
List.of(

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/PolicyManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public enum ComponentKind {
9494
* If this kind corresponds to a single component, this is that component's name;
9595
* otherwise null.
9696
*/
97-
final String componentName;
97+
public final String componentName;
9898

9999
ComponentKind(String componentName) {
100100
this.componentName = componentName;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.bootstrap;
11+
12+
import org.elasticsearch.test.ESTestCase;
13+
import org.elasticsearch.test.ESTestCase.WithEntitlementsOnTestCode;
14+
15+
import java.io.ByteArrayInputStream;
16+
17+
import javax.imageio.stream.MemoryCacheImageInputStream;
18+
19+
import static java.nio.charset.StandardCharsets.UTF_8;
20+
21+
@WithEntitlementsOnTestCode
22+
public class HardcodedEntitlementsTests extends ESTestCase {
23+
24+
/**
25+
* The Tika library can do some things we don't ordinarily want to allow.
26+
* <p>
27+
* Note that {@link MemoryCacheImageInputStream} doesn't even use {@code Disposer} in JDK 26,
28+
* so it's an open question how much effort this deserves.
29+
*/
30+
public void testTikaPDF() {
31+
new MemoryCacheImageInputStream(new ByteArrayInputStream("test test".getBytes(UTF_8)));
32+
}
33+
}

test/framework/src/main/java/org/elasticsearch/bootstrap/TestScopeResolver.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,24 @@
2424

2525
import static org.elasticsearch.entitlement.runtime.policy.PolicyManager.ALL_UNNAMED;
2626
import static org.elasticsearch.entitlement.runtime.policy.PolicyManager.ComponentKind.PLUGIN;
27+
import static org.elasticsearch.entitlement.runtime.policy.PolicyManager.ComponentKind.SERVER;
2728

2829
public record TestScopeResolver(Map<String, PolicyManager.PolicyScope> scopeMap) {
2930

3031
private static final Logger logger = LogManager.getLogger(TestScopeResolver.class);
3132

3233
PolicyManager.PolicyScope getScope(Class<?> callerClass) {
3334
var callerCodeSource = callerClass.getProtectionDomain().getCodeSource();
34-
assert callerCodeSource != null;
35+
if (callerCodeSource == null) {
36+
// This case happens for JDK modules. Usually those are trivially allowed, but some are excluded,
37+
// and those end up here.
38+
// We have no test build info for those modules, so for now, let's just guess.
39+
if (callerClass.getPackageName().equals("sun.java2d")) {
40+
return new PolicyManager.PolicyScope(SERVER, SERVER.componentName, "java.desktop");
41+
} else {
42+
throw new IllegalArgumentException("Cannot identify scope for JDK class [" + callerClass + "]");
43+
}
44+
}
3545

3646
var location = callerCodeSource.getLocation().toString();
3747
var scope = scopeMap.get(location);

test/framework/src/main/java/org/elasticsearch/entitlement/runtime/policy/TestPolicyManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ public final void clearModuleEntitlementsCache() {
9797

9898
@Override
9999
protected boolean isTrustedSystemClass(Class<?> requestingClass) {
100+
if (requestingClass.getPackageName().startsWith("sun.java2d")) {
101+
// This is part of the java.desktop module
102+
return false;
103+
}
100104
ClassLoader loader = requestingClass.getClassLoader();
101105
return loader == null || loader == ClassLoader.getPlatformClassLoader();
102106
}

0 commit comments

Comments
 (0)