-
Notifications
You must be signed in to change notification settings - Fork 1k
Allow SDK to run in environments prohibiting use of sun.misc.Unsafe #4902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...ed-deps/src/test/java/io/opentelemetry/sdk/trace/internal/JcToolsSecurityManagerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.sdk.trace.internal; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import io.opentelemetry.internal.testing.slf4j.SuppressLogger; | ||
| import java.security.AccessController; | ||
| import java.security.PrivilegedAction; | ||
| import java.util.Queue; | ||
| import java.util.concurrent.ArrayBlockingQueue; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.condition.EnabledOnJre; | ||
| import org.junit.jupiter.api.condition.JRE; | ||
|
|
||
| public class JcToolsSecurityManagerTest { | ||
|
|
||
| @Test | ||
| @EnabledOnJre({JRE.JAVA_8, JRE.JAVA_11, JRE.JAVA_17}) | ||
| @SuppressLogger(JcTools.class) | ||
| void newFixedSizeQueue_SunMiscProhibited() { | ||
|
rrva marked this conversation as resolved.
|
||
| assertThat(System.getSecurityManager()).isNull(); | ||
| SunMiscProhibitedSecurityManager testingSecurityManager = | ||
| new SunMiscProhibitedSecurityManager(); | ||
| try { | ||
| System.setSecurityManager(testingSecurityManager); | ||
| Queue<Object> queue = | ||
| AccessController.doPrivileged( | ||
| (PrivilegedAction<Queue<Object>>) () -> JcTools.newFixedSizeQueue(10)); | ||
| assertThat(queue).isInstanceOf(ArrayBlockingQueue.class); | ||
| } finally { | ||
| System.setSecurityManager(null); | ||
| } | ||
| } | ||
| } | ||
111 changes: 111 additions & 0 deletions
111
...s/src/test/java/io/opentelemetry/sdk/trace/internal/SunMiscProhibitedSecurityManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.sdk.trace.internal; | ||
|
|
||
| import java.io.FileDescriptor; | ||
| import java.net.InetAddress; | ||
| import java.security.AccessControlException; | ||
| import java.security.Permission; | ||
|
|
||
| /** | ||
| * A security manager which disallows access to classes in sun.misc. Running the tests with a | ||
| * standard security manager is too invasive. | ||
| */ | ||
| public class SunMiscProhibitedSecurityManager extends SecurityManager { | ||
|
|
||
| public SunMiscProhibitedSecurityManager() {} | ||
|
|
||
| @Override | ||
| protected Class<?>[] getClassContext() { | ||
| return super.getClassContext(); | ||
| } | ||
|
|
||
| @Override | ||
| public void checkPermission(Permission perm) { | ||
| if (perm.getName().equals("accessClassInPackage.sun.misc")) { | ||
| throw new AccessControlException("access denied " + perm, perm); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void checkPermission(Permission perm, Object context) {} | ||
|
|
||
| @Override | ||
| public void checkCreateClassLoader() {} | ||
|
|
||
| @Override | ||
| public void checkAccess(Thread t) {} | ||
|
|
||
| @Override | ||
| public void checkAccess(ThreadGroup g) {} | ||
|
|
||
| @Override | ||
| public void checkExit(int status) {} | ||
|
|
||
| @Override | ||
| public void checkExec(String cmd) {} | ||
|
|
||
| @Override | ||
| public void checkLink(String lib) {} | ||
|
|
||
| @Override | ||
| public void checkRead(FileDescriptor fd) {} | ||
|
|
||
| @Override | ||
| public void checkRead(String file) {} | ||
|
|
||
| @Override | ||
| public void checkRead(String file, Object context) {} | ||
|
|
||
| @Override | ||
| public void checkWrite(FileDescriptor fd) {} | ||
|
|
||
| @Override | ||
| public void checkWrite(String file) {} | ||
|
|
||
| @Override | ||
| public void checkDelete(String file) {} | ||
|
|
||
| @Override | ||
| public void checkConnect(String host, int port) {} | ||
|
|
||
| @Override | ||
| public void checkConnect(String host, int port, Object context) {} | ||
|
|
||
| @Override | ||
| public void checkListen(int port) {} | ||
|
|
||
| @Override | ||
| public void checkAccept(String host, int port) {} | ||
|
|
||
| @Override | ||
| public void checkMulticast(InetAddress maddr) {} | ||
|
|
||
| @Override | ||
| public void checkPropertiesAccess() {} | ||
|
|
||
| @Override | ||
| public void checkPropertyAccess(String key) {} | ||
|
|
||
| @Override | ||
| public void checkPrintJobAccess() {} | ||
|
|
||
| @Override | ||
| public void checkPackageAccess(String pkg) { | ||
| if (pkg.equals("sun.misc")) { | ||
| super.checkPackageAccess(pkg); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void checkPackageDefinition(String pkg) {} | ||
|
|
||
| @Override | ||
| public void checkSetFactory() {} | ||
|
|
||
| @Override | ||
| public void checkSecurityAccess(String target) {} | ||
| } |
41 changes: 41 additions & 0 deletions
41
...c/test/java/io/opentelemetry/sdk/trace/internal/SunMiscProhibitedSecurityManagerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.sdk.trace.internal; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThatNoException; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| import java.security.AccessControlException; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class SunMiscProhibitedSecurityManagerTest { | ||
|
|
||
| @Test | ||
| public void checkPackageAccess_ProhibitsSunMisc() { | ||
| SunMiscProhibitedSecurityManager sm = new SunMiscProhibitedSecurityManager(); | ||
| assertThatThrownBy(() -> sm.checkPackageAccess("sun.misc")) | ||
| .isInstanceOf(AccessControlException.class) | ||
| .hasMessage( | ||
| "access denied (\"java.lang.RuntimePermission\" \"accessClassInPackage.sun.misc\")"); | ||
| } | ||
|
|
||
| @Test | ||
| public void checkPackageAccess_ProhibitsSunMiscRuntimePermission() { | ||
| SunMiscProhibitedSecurityManager sm = new SunMiscProhibitedSecurityManager(); | ||
|
|
||
| assertThatThrownBy( | ||
| () -> sm.checkPermission(new RuntimePermission("accessClassInPackage.sun.misc"))) | ||
| .isInstanceOf(AccessControlException.class) | ||
| .hasMessage( | ||
| "access denied (\"java.lang.RuntimePermission\" \"accessClassInPackage.sun.misc\")"); | ||
| } | ||
|
|
||
| @Test | ||
| public void checkPackageAccess_AllowsOtherPackage() { | ||
| SunMiscProhibitedSecurityManager sm = new SunMiscProhibitedSecurityManager(); | ||
| assertThatNoException().isThrownBy(() -> sm.checkPackageAccess("io.opentelemetry.sdk")); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.