Skip to content

Commit 514c1b2

Browse files
authored
Fix java.security.AccessControlException during OpenSearch server shutdown cycle (opensearch-project#17183) (opensearch-project#17204)
Signed-off-by: Andriy Redko <andriy.redko@aiven.io> (cherry picked from commit b9900ee)
1 parent d806b66 commit 514c1b2

File tree

4 files changed

+62
-6
lines changed

4 files changed

+62
-6
lines changed

modules/transport-netty4/src/main/java/org/opensearch/transport/SharedGroupFactory.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import java.util.concurrent.TimeUnit;
4747
import java.util.concurrent.atomic.AtomicBoolean;
4848

49-
import static org.opensearch.common.util.concurrent.OpenSearchExecutors.daemonThreadFactory;
49+
import static org.opensearch.common.util.concurrent.OpenSearchExecutors.privilegedDaemonThreadFactory;
5050

5151
/**
5252
* Creates and returns {@link io.netty.channel.EventLoopGroup} instances. It will return a shared group for
@@ -90,7 +90,7 @@ public synchronized SharedGroup getHttpGroup() {
9090
if (dedicatedHttpGroup == null) {
9191
NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(
9292
httpWorkerCount,
93-
daemonThreadFactory(settings, HttpServerTransport.HTTP_SERVER_WORKER_THREAD_NAME_PREFIX)
93+
privilegedDaemonThreadFactory(settings, HttpServerTransport.HTTP_SERVER_WORKER_THREAD_NAME_PREFIX)
9494
);
9595
dedicatedHttpGroup = new SharedGroup(new RefCountedGroup(eventLoopGroup));
9696
}
@@ -102,7 +102,7 @@ private SharedGroup getGenericGroup() {
102102
if (genericGroup == null) {
103103
EventLoopGroup eventLoopGroup = new NioEventLoopGroup(
104104
workerCount,
105-
daemonThreadFactory(settings, TcpTransport.TRANSPORT_WORKER_THREAD_NAME_PREFIX)
105+
privilegedDaemonThreadFactory(settings, TcpTransport.TRANSPORT_WORKER_THREAD_NAME_PREFIX)
106106
);
107107
this.genericGroup = new RefCountedGroup(eventLoopGroup);
108108
} else {

modules/transport-netty4/src/main/plugin-metadata/plugin-security.policy

+2-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* GitHub history for details.
3131
*/
3232

33-
grant codeBase "${codebase.netty-common}" {
33+
grant {
3434
// for reading the system-wide configuration for the backlog of established sockets
3535
permission java.io.FilePermission "/proc/sys/net/core/somaxconn", "read";
3636

@@ -39,9 +39,8 @@ grant codeBase "${codebase.netty-common}" {
3939

4040
// Netty sets custom classloader for some of its internal threads
4141
permission java.lang.RuntimePermission "*", "setContextClassLoader";
42-
};
42+
permission java.lang.RuntimePermission "getClassLoader";
4343

44-
grant codeBase "${codebase.netty-transport}" {
4544
// Netty NioEventLoop wants to change this, because of https://bugs.openjdk.java.net/browse/JDK-6427854
4645
// the bug says it only happened rarely, and that its fixed, but apparently it still happens rarely!
4746
permission java.util.PropertyPermission "sun.nio.ch.bugLevel", "write";

server/src/main/java/org/opensearch/common/util/concurrent/OpenSearchExecutors.java

+56
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import org.opensearch.common.unit.TimeValue;
4242
import org.opensearch.node.Node;
4343

44+
import java.security.AccessController;
45+
import java.security.PrivilegedAction;
4446
import java.util.List;
4547
import java.util.Optional;
4648
import java.util.concurrent.AbstractExecutorService;
@@ -337,6 +339,24 @@ public static ThreadFactory daemonThreadFactory(String namePrefix) {
337339
return new OpenSearchThreadFactory(namePrefix);
338340
}
339341

342+
public static ThreadFactory privilegedDaemonThreadFactory(Settings settings, String namePrefix) {
343+
return privilegedDaemonThreadFactory(threadName(settings, namePrefix));
344+
}
345+
346+
public static ThreadFactory privilegedDaemonThreadFactory(String nodeName, String namePrefix) {
347+
assert nodeName != null && false == nodeName.isEmpty();
348+
return privilegedDaemonThreadFactory(threadName(nodeName, namePrefix));
349+
}
350+
351+
public static ThreadFactory privilegedDaemonThreadFactory(String namePrefix) {
352+
return new PrivilegedOpenSearchThreadFactory(namePrefix);
353+
}
354+
355+
/**
356+
* A thread factory
357+
*
358+
* @opensearch.internal
359+
*/
340360
static class OpenSearchThreadFactory implements ThreadFactory {
341361

342362
final ThreadGroup group;
@@ -358,6 +378,42 @@ public Thread newThread(Runnable r) {
358378

359379
}
360380

381+
/**
382+
* A thread factory
383+
*
384+
* @opensearch.internal
385+
*/
386+
static class PrivilegedOpenSearchThreadFactory implements ThreadFactory {
387+
388+
final ThreadGroup group;
389+
final AtomicInteger threadNumber = new AtomicInteger(1);
390+
final String namePrefix;
391+
392+
@SuppressWarnings("removal")
393+
PrivilegedOpenSearchThreadFactory(String namePrefix) {
394+
this.namePrefix = namePrefix;
395+
SecurityManager s = System.getSecurityManager();
396+
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
397+
}
398+
399+
@Override
400+
public Thread newThread(Runnable r) {
401+
final Thread t = new Thread(group, new Runnable() {
402+
@SuppressWarnings({ "deprecation", "removal" })
403+
@Override
404+
public void run() {
405+
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
406+
r.run();
407+
return null;
408+
});
409+
}
410+
}, namePrefix + "[T#" + threadNumber.getAndIncrement() + "]", 0);
411+
t.setDaemon(true);
412+
return t;
413+
}
414+
415+
}
416+
361417
/**
362418
* Cannot instantiate.
363419
*/

server/src/main/resources/org/opensearch/bootstrap/security.policy

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ grant codeBase "${codebase.opensearch-secure-sm}" {
4646
grant codeBase "${codebase.opensearch}" {
4747
// needed for loading plugins which may expect the context class loader to be set
4848
permission java.lang.RuntimePermission "setContextClassLoader";
49+
permission java.lang.RuntimePermission "getClassLoader";
4950
};
5051

5152
//// Very special jar permissions:

0 commit comments

Comments
 (0)