Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/security/auth/Subject/doAs/NestedActions.java
38860 views
1
/*
2
* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights
3
* reserved.
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
*
6
* This code is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License version 2 only, as
8
* published by the Free Software Foundation.
9
*
10
* This code is distributed in the hope that it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
* version 2 for more details (a copy is included in the LICENSE file that
14
* accompanied this code).
15
*
16
* You should have received a copy of the GNU General Public License version
17
* 2 along with this work; if not, write to the Free Software Foundation,
18
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
*
20
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21
* or visit www.oracle.com if you need additional information or have any
22
* questions.
23
*/
24
25
import jdk.testlibrary.ProcessTools;
26
27
import javax.security.auth.Subject;
28
import javax.security.auth.x500.X500Principal;
29
import java.io.*;
30
import java.security.*;
31
import java.util.ArrayList;
32
import java.util.Arrays;
33
import java.util.Collections;
34
import java.util.List;
35
import java.util.jar.JarEntry;
36
import java.util.jar.JarOutputStream;
37
import java.util.jar.Manifest;
38
import java.nio.file.Paths;
39
40
/**
41
* @test
42
* @bug 8048147
43
* @summary Check if proper AccessControlException is thrown
44
* in case of nested Subject.doAs() invocations
45
* when one of protection domains doesn't have permissions
46
*
47
* @library /lib/testlibrary
48
*
49
* @run main NestedActions jar NestedActionsACE.jar
50
* NestedActionsACE.class Utils.class
51
* @run main NestedActions jar NestedActionsPAE.jar
52
* NestedActionsPAE.class Utils.class
53
* @run main NestedActions jar NestedActionsOnePrincipal.jar
54
* NestedActionsOnePrincipal.class Utils.class
55
* @run main NestedActions jar NestedActionsTwoPrincipals.jar
56
* NestedActionsTwoPrincipals.class Utils.class
57
* @run main NestedActions jar WriteToFileAction.jar
58
* WriteToFileAction.class
59
* @run main NestedActions jar WriteToFileNegativeAction.jar
60
* WriteToFileNegativeAction.class
61
* @run main NestedActions jar WriteToFileExceptionAction.jar
62
* WriteToFileExceptionAction.class
63
* @run main NestedActions jar ReadFromFileAction.jar
64
* ReadFromFileAction.class
65
* @run main NestedActions jar ReadFromFileNegativeAction.jar
66
* ReadFromFileNegativeAction.class
67
* @run main NestedActions jar ReadFromFileExceptionAction.jar
68
* ReadFromFileExceptionAction.class
69
* @run main NestedActions jar ReadPropertyAction.jar
70
* ReadPropertyAction.class
71
* @run main NestedActions jar ReadPropertyNegativeAction.jar
72
* ReadPropertyNegativeAction.class
73
* @run main NestedActions jar ReadPropertyExceptionAction.jar
74
* ReadPropertyExceptionAction.class ReadPropertyException.class
75
*
76
* @run main NestedActions NestedActionsACE policy.expect.ace
77
* NestedActionsACE.jar WriteToFileNegativeAction.jar
78
* ReadFromFileNegativeAction.jar ReadPropertyNegativeAction.jar
79
* @run main NestedActions NestedActionsPAE policy.expect.pae
80
* NestedActionsPAE.jar WriteToFileExceptionAction.jar
81
* ReadFromFileExceptionAction.jar ReadPropertyExceptionAction.jar
82
* @run main NestedActions NestedActionsOnePrincipal policy.one.principal
83
* NestedActionsOnePrincipal.jar WriteToFileAction.jar
84
* ReadFromFileAction.jar ReadPropertyAction.jar
85
* @run main NestedActions NestedActionsTwoPrincipals policy.two.principals
86
* NestedActionsTwoPrincipals.jar WriteToFileAction.jar
87
* ReadFromFileAction.jar ReadPropertyAction.jar
88
*/
89
public class NestedActions {
90
91
static final String file = "NestedActions.tmp";
92
static final String PS = System.getProperty("path.separator");
93
static final String FS = System.getProperty("file.separator");
94
static final String TEST_CLASSES = System.getProperty("test.classes");
95
static final String TEST_SOURCES = System.getProperty("test.src");
96
static final String JAVA_OPTS = System.getProperty("test.java.opts");
97
static final String JAVA = System.getProperty("java.home")
98
+ FS + "bin" + FS + "java";
99
100
public static void main(String[] args) throws IOException {
101
if (args.length > 0) {
102
if ("jar".equals(args[0]) && args.length > 2) {
103
createJar(args[1],
104
Arrays.copyOfRange(args, 2, args.length));
105
} else {
106
runJava(args);
107
}
108
} else {
109
throw new RuntimeException("Wrong parameters");
110
}
111
}
112
113
static void createJar(String dest, String... files) throws IOException {
114
System.out.println("Create " + dest + " with the following content:");
115
try (JarOutputStream jos = new JarOutputStream(
116
new FileOutputStream(dest), new Manifest())) {
117
for (String file : files) {
118
System.out.println(" " + file);
119
jos.putNextEntry(new JarEntry(file));
120
try (FileInputStream fis = new FileInputStream(
121
TEST_CLASSES + FS + file)) {
122
jdk.testlibrary.Utils.transferTo(fis, jos);
123
}
124
}
125
}
126
}
127
128
129
130
static void runJava(String[] args) {
131
if (args == null || args.length < 3) {
132
throw new IllegalArgumentException("wrong parameters");
133
}
134
135
List<String> cmds = new ArrayList<>();
136
cmds.add(JAVA);
137
StringBuilder sb = new StringBuilder();
138
cmds.add("-classpath");
139
for (int i=2; i<args.length; i++) {
140
sb.append(args[i]).append(PS);
141
}
142
cmds.add(sb.toString());
143
if (JAVA_OPTS != null && !JAVA_OPTS.isEmpty()) {
144
Collections.addAll(cmds, JAVA_OPTS.trim().split("\\s+"));
145
}
146
cmds.add("-Djava.security.manager");
147
cmds.add("-Djava.security.policy=" + TEST_SOURCES + FS + args[1]);
148
cmds.add(args[0]);
149
try {
150
ProcessTools.executeCommand(cmds.toArray(new String[cmds.size()]))
151
.shouldHaveExitValue(0);
152
} catch (Throwable e) {
153
throw new RuntimeException(e);
154
}
155
}
156
}
157
158
/**
159
* Test for nested Subject.doAs() invocation:
160
*
161
* WriteToFileAction (CN=Duke principal) ->
162
* ReadFromFileAction (CN=Duke principal) ->
163
* ReadPropertyAction (CN=Duke principal)
164
*
165
* The test expects AccessControllException.
166
*/
167
class NestedActionsACE {
168
169
public static void main(String args[]) {
170
Subject subject = new Subject();
171
subject.getPrincipals().add(new X500Principal("CN=Duke"));
172
WriteToFileNegativeAction writeToFile
173
= new WriteToFileNegativeAction(NestedActions.file);
174
Subject.doAs(subject, writeToFile);
175
}
176
}
177
178
/**
179
* Test for nested Subject.doAs() invocation:
180
*
181
* WriteToFileAction (CN=Duke principal) ->
182
* ReadFromFileAction (CN=Duke principal) ->
183
* ReadPropertyAction (CN=Duke principal)
184
*
185
* The test expects PrivilegedActionException
186
* that caused by AccessControlEception.
187
*/
188
class NestedActionsPAE {
189
190
public static void main(String args[]) {
191
Subject subject = new Subject();
192
subject.getPrincipals().add(new X500Principal("CN=Duke"));
193
try {
194
WriteToFileExceptionAction writeToFile =
195
new WriteToFileExceptionAction(NestedActions.file);
196
Subject.doAs(subject, writeToFile);
197
throw new RuntimeException(
198
"Test failed: no PrivilegedActionException thrown");
199
} catch (PrivilegedActionException pae) {
200
System.out.println(
201
"PrivilegedActionException thrown as expected: "
202
+ pae);
203
204
// check if AccessControlException caused PrivilegedActionException
205
Throwable exception = pae.getException();
206
do {
207
if (!(exception instanceof PrivilegedActionException)) {
208
break;
209
}
210
exception = ((PrivilegedActionException) exception).
211
getException();
212
} while (true);
213
214
if (!(exception instanceof ReadPropertyException)) {
215
throw new RuntimeException(
216
"Test failed: PrivilegedActionException "
217
+ "was not caused by ReadPropertyException");
218
}
219
220
exception = exception.getCause();
221
if (!(exception instanceof AccessControlException)) {
222
throw new RuntimeException(
223
"Test failed: PrivilegedActionException "
224
+ "was not caused by ReadPropertyException");
225
}
226
227
System.out.println(
228
"Test passed: PrivilegedActionException "
229
+ "was caused by AccessControlException");
230
}
231
}
232
}
233
234
/**
235
* Test for nested Subject.doAs() invocation:
236
*
237
* WriteToFileAction (CN=Duke principal) ->
238
* ReadFromFileAction (CN=Duke principal) ->
239
* ReadPropertyAction (CN=Duke principal)
240
*/
241
class NestedActionsOnePrincipal {
242
243
public static void main(String args[]) {
244
Subject subject = new Subject();
245
subject.getPrincipals().add(new X500Principal("CN=Duke"));
246
WriteToFileAction writeToFile =
247
new WriteToFileAction(NestedActions.file);
248
Subject.doAs(subject, writeToFile);
249
}
250
}
251
252
/**
253
* Test for nested Subject.doAs() invocation:
254
*
255
* WriteToFileAction (CN=Duke principal) ->
256
* ReadFromFileAction (CN=Duke principal) ->
257
* ReadPropertyAction (CN=Java principal)
258
*/
259
class NestedActionsTwoPrincipals {
260
261
public static void main(String args[]) {
262
Subject subject = new Subject();
263
subject.getPrincipals().add(new X500Principal("CN=Duke"));
264
Subject anotherSubject = new Subject();
265
anotherSubject.getPrincipals().add(new X500Principal("CN=Java"));
266
ReadFromFileAction readFromFile
267
= new ReadFromFileAction(NestedActions.file, anotherSubject);
268
WriteToFileAction writeToFile
269
= new WriteToFileAction(NestedActions.file, readFromFile);
270
Subject.doAs(subject, writeToFile);
271
}
272
}
273
274
/**
275
* Helper class.
276
*/
277
class Utils {
278
279
static void readFile(String filename) {
280
System.out.println("ReadFromFileAction: try to read " + filename);
281
AccessControlContext acc = AccessController.getContext();
282
Subject subject = Subject.getSubject(acc);
283
System.out.println("principals = " + subject.getPrincipals());
284
try (FileInputStream fis = new FileInputStream(filename)) {
285
// do nothing
286
} catch (IOException e) {
287
throw new RuntimeException("Unexpected IOException", e);
288
}
289
}
290
291
static void writeFile(String filename) {
292
System.out.println("WriteToFileAction: try to write to " + filename);
293
AccessControlContext acc = AccessController.getContext();
294
Subject subject = Subject.getSubject(acc);
295
System.out.println("principals = " + subject.getPrincipals());
296
try (BufferedOutputStream bos = new BufferedOutputStream(
297
new FileOutputStream(filename))) {
298
bos.write(0);
299
bos.flush();
300
} catch (IOException e) {
301
throw new RuntimeException("Unexpected IOException", e);
302
}
303
}
304
305
}
306
307
class WriteToFileAction implements PrivilegedAction {
308
309
private final String filename;
310
private final PrivilegedAction nextAction;
311
312
WriteToFileAction(String filename, PrivilegedAction nextAction) {
313
this.filename = filename;
314
this.nextAction = nextAction;
315
}
316
317
WriteToFileAction(String filename) {
318
this(filename, new ReadFromFileAction(filename));
319
}
320
321
@Override
322
public Object run() {
323
Utils.writeFile(filename);
324
AccessControlContext acc = AccessController.getContext();
325
Subject subject = Subject.getSubject(acc);
326
return Subject.doAs(subject, nextAction);
327
}
328
329
}
330
331
class ReadFromFileAction implements PrivilegedAction {
332
333
private final String filename;
334
private final Subject anotherSubject;
335
336
ReadFromFileAction(String filename) {
337
this(filename, null);
338
}
339
340
ReadFromFileAction(String filename, Subject anotherSubject) {
341
this.filename = filename;
342
this.anotherSubject = anotherSubject;
343
}
344
345
@Override
346
public Object run() {
347
Utils.readFile(filename);
348
349
AccessControlContext acc = AccessController.getContext();
350
Subject subject = Subject.getSubject(acc);
351
ReadPropertyAction readProperty = new ReadPropertyAction();
352
if (anotherSubject != null) {
353
return Subject.doAs(anotherSubject, readProperty);
354
} else {
355
return Subject.doAs(subject, readProperty);
356
}
357
}
358
359
}
360
361
class ReadPropertyAction implements PrivilegedAction {
362
363
@Override
364
public java.lang.Object run() {
365
System.out.println("ReadPropertyAction: "
366
+ "try to read 'java.class.path' property");
367
368
AccessControlContext acc = AccessController.getContext();
369
Subject s = Subject.getSubject(acc);
370
System.out.println("principals = " + s.getPrincipals());
371
System.out.println("java.class.path = "
372
+ System.getProperty("java.class.path"));
373
374
return null;
375
}
376
377
}
378
379
class WriteToFileNegativeAction implements PrivilegedAction {
380
381
private final String filename;
382
383
public WriteToFileNegativeAction(String filename) {
384
this.filename = filename;
385
}
386
387
@Override
388
public Object run() {
389
AccessControlContext acc = AccessController.getContext();
390
Subject subject = Subject.getSubject(acc);
391
System.out.println("principals = " + subject.getPrincipals());
392
393
try {
394
Utils.writeFile(filename);
395
new File(filename).delete();
396
throw new RuntimeException(
397
"Test failed: no AccessControlException thrown");
398
} catch (AccessControlException ace) {
399
System.out.println(
400
"AccessControlException thrown as expected: "
401
+ ace.getMessage());
402
}
403
404
ReadFromFileNegativeAction readFromFile
405
= new ReadFromFileNegativeAction(filename);
406
return Subject.doAs(subject, readFromFile);
407
}
408
409
}
410
411
class ReadFromFileNegativeAction implements PrivilegedAction {
412
413
private final String filename;
414
415
public ReadFromFileNegativeAction(String filename) {
416
this.filename = filename;
417
}
418
419
@Override
420
public Object run() {
421
AccessControlContext acc = AccessController.getContext();
422
Subject subject = Subject.getSubject(acc);
423
System.out.println("principals = " + subject.getPrincipals());
424
425
try {
426
Utils.readFile(filename);
427
throw new RuntimeException(
428
"Test failed: no AccessControlException thrown");
429
} catch (AccessControlException ace) {
430
System.out.println(
431
"AccessControlException thrown as expected: "
432
+ ace.getMessage());
433
}
434
435
ReadPropertyNegativeAction readProperty =
436
new ReadPropertyNegativeAction();
437
return Subject.doAs(subject, readProperty);
438
}
439
440
}
441
442
class ReadPropertyNegativeAction implements PrivilegedAction {
443
444
@Override
445
public java.lang.Object run() {
446
System.out.println("Try to read 'java.class.path' property");
447
448
AccessControlContext acc = AccessController.getContext();
449
Subject s = Subject.getSubject(acc);
450
System.out.println("principals = " + s.getPrincipals());
451
452
try {
453
System.out.println("java.class.path = "
454
+ System.getProperty("java.class.path"));
455
throw new RuntimeException(
456
"Test failed: no AccessControlException thrown");
457
} catch (AccessControlException ace) {
458
System.out.println(
459
"AccessControlException thrown as expected: "
460
+ ace.getMessage());
461
}
462
463
return null;
464
}
465
466
}
467
468
class WriteToFileExceptionAction implements PrivilegedExceptionAction {
469
470
private final String filename;
471
472
WriteToFileExceptionAction(String filename) {
473
this.filename = filename;
474
}
475
476
@Override
477
public Object run() throws Exception {
478
Utils.writeFile(filename);
479
AccessControlContext acc = AccessController.getContext();
480
Subject subject = Subject.getSubject(acc);
481
ReadFromFileExceptionAction readFromFile =
482
new ReadFromFileExceptionAction(filename);
483
return Subject.doAs(subject, readFromFile);
484
}
485
486
}
487
488
class ReadFromFileExceptionAction implements PrivilegedExceptionAction {
489
490
private final String filename;
491
492
ReadFromFileExceptionAction(String filename) {
493
this.filename = filename;
494
}
495
496
@Override
497
public Object run() throws Exception {
498
Utils.readFile(filename);
499
AccessControlContext acc = AccessController.getContext();
500
Subject subject = Subject.getSubject(acc);
501
ReadPropertyExceptionAction readProperty =
502
new ReadPropertyExceptionAction();
503
return Subject.doAs(subject, readProperty);
504
}
505
506
}
507
508
class ReadPropertyExceptionAction implements PrivilegedExceptionAction {
509
510
@Override
511
public java.lang.Object run() throws Exception {
512
System.out.println("Try to read 'java.class.path' property");
513
514
AccessControlContext acc = AccessController.getContext();
515
Subject s = Subject.getSubject(acc);
516
System.out.println("principals = " + s.getPrincipals());
517
518
try {
519
System.out.println("java.class.path = "
520
+ System.getProperty("java.class.path"));
521
throw new RuntimeException(
522
"Test failed: no AccessControlException thrown");
523
} catch (AccessControlException ace) {
524
System.out.println(
525
"AccessControlException thrown as expected: "
526
+ ace.getMessage());
527
throw new ReadPropertyException(ace);
528
}
529
}
530
531
}
532
533
class ReadPropertyException extends Exception {
534
535
ReadPropertyException(Throwable cause) {
536
super(cause);
537
}
538
}
539
540