Path: blob/master/test/jdk/javax/security/auth/Subject/doAs/NestedActions.java
41161 views
/*1* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.BufferedOutputStream;24import java.io.File;25import java.io.FileInputStream;26import java.io.FileOutputStream;27import java.io.IOException;28import java.security.AccessControlContext;29import java.security.AccessControlException;30import java.security.AccessController;31import java.security.PrivilegedAction;32import java.security.PrivilegedActionException;33import java.security.PrivilegedExceptionAction;34import java.util.ArrayList;35import java.util.Arrays;36import java.util.Collections;37import java.util.List;38import java.util.jar.JarEntry;39import java.util.jar.JarOutputStream;40import java.util.jar.Manifest;41import javax.security.auth.Subject;42import javax.security.auth.x500.X500Principal;43import jdk.test.lib.process.ProcessTools;4445/**46* @test47* @bug 804814748* @summary Check if proper AccessControlException is thrown49* in case of nested Subject.doAs() invocations50* when one of protection domains doesn't have permissions51*52* @library /test/lib53*54* @run main NestedActions jar NestedActionsACE.jar55* NestedActionsACE.class Utils.class56* @run main NestedActions jar NestedActionsPAE.jar57* NestedActionsPAE.class Utils.class58* @run main NestedActions jar NestedActionsOnePrincipal.jar59* NestedActionsOnePrincipal.class Utils.class60* @run main NestedActions jar NestedActionsTwoPrincipals.jar61* NestedActionsTwoPrincipals.class Utils.class62* @run main NestedActions jar WriteToFileAction.jar63* WriteToFileAction.class64* @run main NestedActions jar WriteToFileNegativeAction.jar65* WriteToFileNegativeAction.class66* @run main NestedActions jar WriteToFileExceptionAction.jar67* WriteToFileExceptionAction.class68* @run main NestedActions jar ReadFromFileAction.jar69* ReadFromFileAction.class70* @run main NestedActions jar ReadFromFileNegativeAction.jar71* ReadFromFileNegativeAction.class72* @run main NestedActions jar ReadFromFileExceptionAction.jar73* ReadFromFileExceptionAction.class74* @run main NestedActions jar ReadPropertyAction.jar75* ReadPropertyAction.class76* @run main NestedActions jar ReadPropertyNegativeAction.jar77* ReadPropertyNegativeAction.class78* @run main NestedActions jar ReadPropertyExceptionAction.jar79* ReadPropertyExceptionAction.class ReadPropertyException.class80*81* @run main NestedActions NestedActionsACE policy.expect.ace82* NestedActionsACE.jar WriteToFileNegativeAction.jar83* ReadFromFileNegativeAction.jar ReadPropertyNegativeAction.jar84* @run main NestedActions NestedActionsPAE policy.expect.pae85* NestedActionsPAE.jar WriteToFileExceptionAction.jar86* ReadFromFileExceptionAction.jar ReadPropertyExceptionAction.jar87* @run main NestedActions NestedActionsOnePrincipal policy.one.principal88* NestedActionsOnePrincipal.jar WriteToFileAction.jar89* ReadFromFileAction.jar ReadPropertyAction.jar90* @run main NestedActions NestedActionsTwoPrincipals policy.two.principals91* NestedActionsTwoPrincipals.jar WriteToFileAction.jar92* ReadFromFileAction.jar ReadPropertyAction.jar93*/94public class NestedActions {9596static final String file = "NestedActions.tmp";97static final String PS = System.getProperty("path.separator");98static final String FS = System.getProperty("file.separator");99static final String TEST_CLASSES = System.getProperty("test.classes");100static final String TEST_SOURCES = System.getProperty("test.src");101static final String JAVA_OPTS = System.getProperty("test.java.opts");102static final String JAVA = System.getProperty("java.home")103+ FS + "bin" + FS + "java";104105public static void main(String[] args) throws IOException {106if (args.length > 0) {107if ("jar".equals(args[0]) && args.length > 2) {108createJar(args[1],109Arrays.copyOfRange(args, 2, args.length));110} else {111runJava(args);112}113} else {114throw new RuntimeException("Wrong parameters");115}116}117118static void createJar(String dest, String... files) throws IOException {119System.out.println("Create " + dest + " with the following content:");120try (JarOutputStream jos = new JarOutputStream(121new FileOutputStream(dest), new Manifest())) {122for (String file : files) {123System.out.println(" " + file);124jos.putNextEntry(new JarEntry(file));125try (FileInputStream fis = new FileInputStream(126TEST_CLASSES + FS + file)) {127fis.transferTo(jos);128}129}130}131}132133static void runJava(String[] args) {134if (args == null || args.length < 3) {135throw new IllegalArgumentException("wrong parameters");136}137138List<String> cmds = new ArrayList<>();139cmds.add(JAVA);140StringBuilder sb = new StringBuilder();141cmds.add("-classpath");142for (int i=2; i<args.length; i++) {143sb.append(args[i]).append(PS);144}145cmds.add(sb.toString());146if (JAVA_OPTS != null && !JAVA_OPTS.isEmpty()) {147Collections.addAll(cmds, JAVA_OPTS.trim().split("\\s+"));148}149cmds.add("-Djava.security.manager");150cmds.add("-Djava.security.policy=" + TEST_SOURCES + FS + args[1]);151cmds.add(args[0]);152try {153ProcessTools.executeCommand(cmds.toArray(new String[cmds.size()]))154.shouldHaveExitValue(0);155} catch (Throwable e) {156throw new RuntimeException(e);157}158}159}160161/**162* Test for nested Subject.doAs() invocation:163*164* WriteToFileAction (CN=Duke principal) ->165* ReadFromFileAction (CN=Duke principal) ->166* ReadPropertyAction (CN=Duke principal)167*168* The test expects AccessControllException.169*/170class NestedActionsACE {171172public static void main(String args[]) {173Subject subject = new Subject();174subject.getPrincipals().add(new X500Principal("CN=Duke"));175WriteToFileNegativeAction writeToFile176= new WriteToFileNegativeAction(NestedActions.file);177Subject.doAs(subject, writeToFile);178}179}180181/**182* Test for nested Subject.doAs() invocation:183*184* WriteToFileAction (CN=Duke principal) ->185* ReadFromFileAction (CN=Duke principal) ->186* ReadPropertyAction (CN=Duke principal)187*188* The test expects PrivilegedActionException189* that caused by AccessControlEception.190*/191class NestedActionsPAE {192193public static void main(String args[]) {194Subject subject = new Subject();195subject.getPrincipals().add(new X500Principal("CN=Duke"));196try {197WriteToFileExceptionAction writeToFile =198new WriteToFileExceptionAction(NestedActions.file);199Subject.doAs(subject, writeToFile);200throw new RuntimeException(201"Test failed: no PrivilegedActionException thrown");202} catch (PrivilegedActionException pae) {203System.out.println(204"PrivilegedActionException thrown as expected: "205+ pae);206207// check if AccessControlException caused PrivilegedActionException208Throwable exception = pae.getException();209do {210if (!(exception instanceof PrivilegedActionException)) {211break;212}213exception = ((PrivilegedActionException) exception).214getException();215} while (true);216217if (!(exception instanceof ReadPropertyException)) {218throw new RuntimeException(219"Test failed: PrivilegedActionException "220+ "was not caused by ReadPropertyException");221}222223exception = exception.getCause();224if (!(exception instanceof AccessControlException)) {225throw new RuntimeException(226"Test failed: PrivilegedActionException "227+ "was not caused by ReadPropertyException");228}229230System.out.println(231"Test passed: PrivilegedActionException "232+ "was caused by AccessControlException");233}234}235}236237/**238* Test for nested Subject.doAs() invocation:239*240* WriteToFileAction (CN=Duke principal) ->241* ReadFromFileAction (CN=Duke principal) ->242* ReadPropertyAction (CN=Duke principal)243*/244class NestedActionsOnePrincipal {245246public static void main(String args[]) {247Subject subject = new Subject();248subject.getPrincipals().add(new X500Principal("CN=Duke"));249WriteToFileAction writeToFile =250new WriteToFileAction(NestedActions.file);251Subject.doAs(subject, writeToFile);252}253}254255/**256* Test for nested Subject.doAs() invocation:257*258* WriteToFileAction (CN=Duke principal) ->259* ReadFromFileAction (CN=Duke principal) ->260* ReadPropertyAction (CN=Java principal)261*/262class NestedActionsTwoPrincipals {263264public static void main(String args[]) {265Subject subject = new Subject();266subject.getPrincipals().add(new X500Principal("CN=Duke"));267Subject anotherSubject = new Subject();268anotherSubject.getPrincipals().add(new X500Principal("CN=Java"));269ReadFromFileAction readFromFile270= new ReadFromFileAction(NestedActions.file, anotherSubject);271WriteToFileAction writeToFile272= new WriteToFileAction(NestedActions.file, readFromFile);273Subject.doAs(subject, writeToFile);274}275}276277/**278* Helper class.279*/280class Utils {281282static void readFile(String filename) {283System.out.println("ReadFromFileAction: try to read " + filename);284AccessControlContext acc = AccessController.getContext();285Subject subject = Subject.getSubject(acc);286System.out.println("principals = " + subject.getPrincipals());287try (FileInputStream fis = new FileInputStream(filename)) {288// do nothing289} catch (IOException e) {290throw new RuntimeException("Unexpected IOException", e);291}292}293294static void writeFile(String filename) {295System.out.println("WriteToFileAction: try to write to " + filename);296AccessControlContext acc = AccessController.getContext();297Subject subject = Subject.getSubject(acc);298System.out.println("principals = " + subject.getPrincipals());299try (BufferedOutputStream bos = new BufferedOutputStream(300new FileOutputStream(filename))) {301bos.write(0);302bos.flush();303} catch (IOException e) {304throw new RuntimeException("Unexpected IOException", e);305}306}307308}309310class WriteToFileAction implements PrivilegedAction {311312private final String filename;313private final PrivilegedAction nextAction;314315WriteToFileAction(String filename, PrivilegedAction nextAction) {316this.filename = filename;317this.nextAction = nextAction;318}319320WriteToFileAction(String filename) {321this(filename, new ReadFromFileAction(filename));322}323324@Override325public Object run() {326Utils.writeFile(filename);327AccessControlContext acc = AccessController.getContext();328Subject subject = Subject.getSubject(acc);329return Subject.doAs(subject, nextAction);330}331332}333334class ReadFromFileAction implements PrivilegedAction {335336private final String filename;337private final Subject anotherSubject;338339ReadFromFileAction(String filename) {340this(filename, null);341}342343ReadFromFileAction(String filename, Subject anotherSubject) {344this.filename = filename;345this.anotherSubject = anotherSubject;346}347348@Override349public Object run() {350Utils.readFile(filename);351352AccessControlContext acc = AccessController.getContext();353Subject subject = Subject.getSubject(acc);354ReadPropertyAction readProperty = new ReadPropertyAction();355if (anotherSubject != null) {356return Subject.doAs(anotherSubject, readProperty);357} else {358return Subject.doAs(subject, readProperty);359}360}361362}363364class ReadPropertyAction implements PrivilegedAction {365366@Override367public java.lang.Object run() {368System.out.println("ReadPropertyAction: "369+ "try to read 'java.class.path' property");370371AccessControlContext acc = AccessController.getContext();372Subject s = Subject.getSubject(acc);373System.out.println("principals = " + s.getPrincipals());374System.out.println("java.class.path = "375+ System.getProperty("java.class.path"));376377return null;378}379380}381382class WriteToFileNegativeAction implements PrivilegedAction {383384private final String filename;385386public WriteToFileNegativeAction(String filename) {387this.filename = filename;388}389390@Override391public Object run() {392AccessControlContext acc = AccessController.getContext();393Subject subject = Subject.getSubject(acc);394System.out.println("principals = " + subject.getPrincipals());395396try {397Utils.writeFile(filename);398new File(filename).delete();399throw new RuntimeException(400"Test failed: no AccessControlException thrown");401} catch (AccessControlException ace) {402System.out.println(403"AccessControlException thrown as expected: "404+ ace.getMessage());405}406407ReadFromFileNegativeAction readFromFile408= new ReadFromFileNegativeAction(filename);409return Subject.doAs(subject, readFromFile);410}411412}413414class ReadFromFileNegativeAction implements PrivilegedAction {415416private final String filename;417418public ReadFromFileNegativeAction(String filename) {419this.filename = filename;420}421422@Override423public Object run() {424AccessControlContext acc = AccessController.getContext();425Subject subject = Subject.getSubject(acc);426System.out.println("principals = " + subject.getPrincipals());427428try {429Utils.readFile(filename);430throw new RuntimeException(431"Test failed: no AccessControlException thrown");432} catch (AccessControlException ace) {433System.out.println(434"AccessControlException thrown as expected: "435+ ace.getMessage());436}437438ReadPropertyNegativeAction readProperty =439new ReadPropertyNegativeAction();440return Subject.doAs(subject, readProperty);441}442443}444445class ReadPropertyNegativeAction implements PrivilegedAction {446447@Override448public java.lang.Object run() {449System.out.println("Try to read 'java.class.path' property");450451AccessControlContext acc = AccessController.getContext();452Subject s = Subject.getSubject(acc);453System.out.println("principals = " + s.getPrincipals());454455try {456System.out.println("java.class.path = "457+ System.getProperty("java.class.path"));458throw new RuntimeException(459"Test failed: no AccessControlException thrown");460} catch (AccessControlException ace) {461System.out.println(462"AccessControlException thrown as expected: "463+ ace.getMessage());464}465466return null;467}468469}470471class WriteToFileExceptionAction implements PrivilegedExceptionAction {472473private final String filename;474475WriteToFileExceptionAction(String filename) {476this.filename = filename;477}478479@Override480public Object run() throws Exception {481Utils.writeFile(filename);482AccessControlContext acc = AccessController.getContext();483Subject subject = Subject.getSubject(acc);484ReadFromFileExceptionAction readFromFile =485new ReadFromFileExceptionAction(filename);486return Subject.doAs(subject, readFromFile);487}488489}490491class ReadFromFileExceptionAction implements PrivilegedExceptionAction {492493private final String filename;494495ReadFromFileExceptionAction(String filename) {496this.filename = filename;497}498499@Override500public Object run() throws Exception {501Utils.readFile(filename);502AccessControlContext acc = AccessController.getContext();503Subject subject = Subject.getSubject(acc);504ReadPropertyExceptionAction readProperty =505new ReadPropertyExceptionAction();506return Subject.doAs(subject, readProperty);507}508509}510511class ReadPropertyExceptionAction implements PrivilegedExceptionAction {512513@Override514public java.lang.Object run() throws Exception {515System.out.println("Try to read 'java.class.path' property");516517AccessControlContext acc = AccessController.getContext();518Subject s = Subject.getSubject(acc);519System.out.println("principals = " + s.getPrincipals());520521try {522System.out.println("java.class.path = "523+ System.getProperty("java.class.path"));524throw new RuntimeException(525"Test failed: no AccessControlException thrown");526} catch (AccessControlException ace) {527System.out.println(528"AccessControlException thrown as expected: "529+ ace.getMessage());530throw new ReadPropertyException(ace);531}532}533534}535536class ReadPropertyException extends Exception {537538ReadPropertyException(Throwable cause) {539super(cause);540}541}542543544