Skip to content

Commit

Permalink
revert some test changes, spec change for subject
Browse files Browse the repository at this point in the history
  • Loading branch information
wangweij committed Mar 4, 2024
1 parent 8f270d0 commit e57f725
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 26 deletions.
19 changes: 10 additions & 9 deletions src/java.base/share/classes/javax/security/auth/Subject.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,21 @@
* input type and exceptions thrown are slightly different.
*
* <p><b><a id="sm-allowed">These methods behave differently depending on
* whether a security manager is allowed or disallowed</a></b>:
* whether a security manager is
* <a href="../../../java/lang/SecurityManager.html#set-security-manager">allowed or disallowed</a></a></b>:
* <ul>
* <li>If a security manager is allowed, which means it is either already set
* or allowed to be set dynamically, a {@code Subject} object is associated
* with an {@code AccessControlContext} through a {@code doAs} or
* {@code callAs} call, and the subject can then be retrieved using the
* {@code getSubject(AccessControlContext)} method.
* <li>If a security manager is not allowed, which means it
* {@linkplain System#setSecurityManager is not set and not allowed to be set
* dynamically}, a {@code doAs} or {@code callAs} call binds a {@code Subject}
* object to the period of execution of an action, and the subject can be
* retrieved using the {@code current} method inside the action. This subject
* can be inherited by child threads if they are started and terminate within
* the execution of its parent thread using structured concurrency.
* {@code getSubject(AccessControlContext)} or {@code current} method.
* <li>If a security manager is not allowed, which means it is not set and
* not allowed to be set dynamically, a {@code doAs} or {@code callAs} call
* binds a {@code Subject} object to the period of execution of an action,
* and the subject can be retrieved using the {@code current} method inside
* the action. This subject can be inherited by child threads if they are
* started and terminate within the execution of its parent thread using
* structured concurrency.
* </ul>
*
* @since 1.4
Expand Down
3 changes: 2 additions & 1 deletion test/jdk/javax/management/monitor/ThreadPoolAccTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* @run main/othervm -Djava.security.manager=allow ThreadPoolAccTest
*/

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Date;
import java.util.Set;
Expand Down Expand Up @@ -66,7 +67,7 @@ public String getString() {
return "";
}
private void setPrincipal() {
Subject subject = Subject.current();
Subject subject = Subject.getSubject(AccessController.getContext());
Set<JMXPrincipal> principals = subject.getPrincipals(JMXPrincipal.class);
principal = principals.iterator().next().getName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
* @test
* @bug 8296244
* @enablePreview
* @summary Implement Subject.current and Subject.callAs using scoped values
* @summary Implement Subject.current and Subject.callAs using scoped values.
* Need @enablePreview to use StructuredTaskScope.
* @run main/othervm -Djava.security.manager=allow CallAsWithScopedValue false
* @run main/othervm -Djava.security.manager=disallow CallAsWithScopedValue true
*/
Expand Down
5 changes: 2 additions & 3 deletions test/jdk/javax/security/auth/Subject/Compat.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@

/*
* @test
* @bug 8296244
* @run main/othervm -Djava.security.manager=allow Compat
* @summary ensures the old implementation still works when SM is allowed
*/
public class Compat {

// static PrivilegedAction<AccessControlContext> action
// = () -> AccessController.getContext();

static PrivilegedExceptionAction<AccessControlContext> action
= () -> AccessController.getContext();

Expand Down
2 changes: 1 addition & 1 deletion test/jdk/javax/security/auth/Subject/UnsupportedSV.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static void t1() throws Exception {
s.getPrincipals().add(new UserPrincipal("Duke"));

// TODO: Still has no way to reject the following code.
// Here, AccessController::getContext returns a plan ACC without
// Here, AccessController::getContext returns a plain ACC without
// the subject inside.
AccessControlContext acc2 = Subject.callAs(s, AccessController::getContext);
Subject ns = AccessController.doPrivileged(
Expand Down
35 changes: 24 additions & 11 deletions test/jdk/javax/security/auth/Subject/doAs/NestedActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.AccessControlContext;
import java.security.AccessControlException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
Expand Down Expand Up @@ -280,7 +282,8 @@ class Utils {

static void readFile(String filename) {
System.out.println("ReadFromFileAction: try to read " + filename);
Subject subject = Subject.current();
AccessControlContext acc = AccessController.getContext();
Subject subject = Subject.getSubject(acc);
System.out.println("principals = " + subject.getPrincipals());
try (FileInputStream fis = new FileInputStream(filename)) {
// do nothing
Expand All @@ -291,7 +294,8 @@ static void readFile(String filename) {

static void writeFile(String filename) {
System.out.println("WriteToFileAction: try to write to " + filename);
Subject subject = Subject.current();
AccessControlContext acc = AccessController.getContext();
Subject subject = Subject.getSubject(acc);
System.out.println("principals = " + subject.getPrincipals());
try (BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(filename))) {
Expand Down Expand Up @@ -321,7 +325,8 @@ class WriteToFileAction implements PrivilegedAction {
@Override
public Object run() {
Utils.writeFile(filename);
Subject subject = Subject.current();
AccessControlContext acc = AccessController.getContext();
Subject subject = Subject.getSubject(acc);
return Subject.doAs(subject, nextAction);
}

Expand All @@ -345,7 +350,8 @@ class ReadFromFileAction implements PrivilegedAction {
public Object run() {
Utils.readFile(filename);

Subject subject = Subject.current();
AccessControlContext acc = AccessController.getContext();
Subject subject = Subject.getSubject(acc);
ReadPropertyAction readProperty = new ReadPropertyAction();
if (anotherSubject != null) {
return Subject.doAs(anotherSubject, readProperty);
Expand All @@ -363,7 +369,8 @@ public java.lang.Object run() {
System.out.println("ReadPropertyAction: "
+ "try to read 'java.class.path' property");

Subject s = Subject.current();
AccessControlContext acc = AccessController.getContext();
Subject s = Subject.getSubject(acc);
System.out.println("principals = " + s.getPrincipals());
System.out.println("java.class.path = "
+ System.getProperty("java.class.path"));
Expand All @@ -383,7 +390,8 @@ public WriteToFileNegativeAction(String filename) {

@Override
public Object run() {
Subject subject = Subject.current();
AccessControlContext acc = AccessController.getContext();
Subject subject = Subject.getSubject(acc);
System.out.println("principals = " + subject.getPrincipals());

try {
Expand Down Expand Up @@ -414,7 +422,8 @@ public ReadFromFileNegativeAction(String filename) {

@Override
public Object run() {
Subject subject = Subject.current();
AccessControlContext acc = AccessController.getContext();
Subject subject = Subject.getSubject(acc);
System.out.println("principals = " + subject.getPrincipals());

try {
Expand All @@ -440,7 +449,8 @@ class ReadPropertyNegativeAction implements PrivilegedAction {
public java.lang.Object run() {
System.out.println("Try to read 'java.class.path' property");

Subject s = Subject.current();
AccessControlContext acc = AccessController.getContext();
Subject s = Subject.getSubject(acc);
System.out.println("principals = " + s.getPrincipals());

try {
Expand Down Expand Up @@ -470,7 +480,8 @@ class WriteToFileExceptionAction implements PrivilegedExceptionAction {
@Override
public Object run() throws Exception {
Utils.writeFile(filename);
Subject subject = Subject.current();
AccessControlContext acc = AccessController.getContext();
Subject subject = Subject.getSubject(acc);
ReadFromFileExceptionAction readFromFile =
new ReadFromFileExceptionAction(filename);
return Subject.doAs(subject, readFromFile);
Expand All @@ -489,7 +500,8 @@ class ReadFromFileExceptionAction implements PrivilegedExceptionAction {
@Override
public Object run() throws Exception {
Utils.readFile(filename);
Subject subject = Subject.current();
AccessControlContext acc = AccessController.getContext();
Subject subject = Subject.getSubject(acc);
ReadPropertyExceptionAction readProperty =
new ReadPropertyExceptionAction();
return Subject.doAs(subject, readProperty);
Expand All @@ -503,7 +515,8 @@ class ReadPropertyExceptionAction implements PrivilegedExceptionAction {
public java.lang.Object run() throws Exception {
System.out.println("Try to read 'java.class.path' property");

Subject s = Subject.current();
AccessControlContext acc = AccessController.getContext();
Subject s = Subject.getSubject(acc);
System.out.println("principals = " + s.getPrincipals());

try {
Expand Down

0 comments on commit e57f725

Please sign in to comment.