/
TSPolicy.java
634 lines (554 loc) · 22.1 KB
/
TSPolicy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/*
* Copyright (c) 2007, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
/**
* $Id$
*
* @author Raja Perumal
* 08/14/02
*/
package com.sun.ts.tests.jacc.provider;
import java.io.File;
import java.security.CodeSource;
import java.security.Permission;
import java.security.PermissionCollection;
import java.security.ProtectionDomain;
import java.util.StringTokenizer;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import com.sun.ts.lib.util.sec.security.provider.PolicyFile;
import jakarta.security.jacc.EJBMethodPermission;
import jakarta.security.jacc.EJBRoleRefPermission;
import jakarta.security.jacc.PolicyConfiguration;
import jakarta.security.jacc.PolicyConfigurationFactory;
import jakarta.security.jacc.PolicyContext;
import jakarta.security.jacc.WebResourcePermission;
import jakarta.security.jacc.WebRoleRefPermission;
import jakarta.security.jacc.WebUserDataPermission;
/**
* This is a delegating Policy Implementation class which delegates the
* permission evaluation to vendor's policy implentation defined by
* "vendor.jakarta.security.jacc.policy.provider"
*
* In case the vendor doesn't provide Policy implementation default jdk policy
* will be used.
*
* Note: Since J2EE 1.3 appservers are not required to support JSR115, the 1.3
* policy implementation defined by "jakarta.security.auth.policy.provider" will
* not used for testing in this TCK
*/
public final class TSPolicy extends java.security.Policy {
private java.security.Policy policy = null;
public static final String VENDOR_POLICY_PROVIDER = "vendor.jakarta.security.jacc.policy.provider";
private static ClassLoader classLoader = null;
public static boolean POLICY_INSTALLED = false;
public static TSLogger logger = null;
boolean firstInvocationForJACCPermissions = true;
boolean multipleSetPolicyAllowed = false;
String[] tokenArray = new String[2];
String methodInterfaceName = null;
String methodName = null;
// This constructor will be used by vendor AppServer
public TSPolicy() {
initializeTSLogger();
if (!POLICY_INSTALLED)
loadPolicy();
// invoke equals and hashCode method on all JACC Permissions.
if (firstInvocationForJACCPermissions) {
jaccPermissionsEquals();
jaccPermissionsHashCode();
firstInvocationForJACCPermissions = false;
}
}
/**
* Evaluates the global policy and returns a PermissionCollection object
* specifying the set of permissions allowed for code from the specified code
* source.
*
* @param codesource
* the CodeSource associated with the caller. This encapsulates the
* original location of the code (where the code came from) and the
* public key(s) of its signer.
*
* @return the set of permissions allowed for code from <i>codesource</i>
* according to the policy.The returned set of permissions must be a
* new mutable instance and it must support heterogeneous Permission
* types.
*
*/
public PermissionCollection getPermissions(CodeSource codesource) {
if (logger.isLoggable(Level.FINER)) {
logger.entering("TSPolicy", "getPermissions");
}
// print permission collection as logger info ?
return policy.getPermissions(codesource);
}
/**
* Evaluates the global policy and returns a PermissionCollection object
* specifying the set of permissions allowed given the characteristics of the
* protection domain.
*
* @param domain
* the ProtectionDomain associated with the caller.
*
* @return the set of permissions allowed for the <i>domain</i> according to
* the policy.The returned set of permissions must be a new mutable
* instance and it must support heterogeneous Permission types.
*
* @see java.security.ProtectionDomain
* @see java.security.SecureClassLoader
* @since 1.4
*/
public PermissionCollection getPermissions(ProtectionDomain domain) {
if (logger.isLoggable(Level.FINER)) {
logger.entering("TSPolicy", "getPermissions");
}
// print permission collection as logger info ?
return policy.getPermissions(domain);
}
/**
* Evaluates the global policy for the permissions granted to the
* ProtectionDomain and tests whether the permission is granted.
*
* @param domain
* the ProtectionDomain to test
* @param permission
* the Permission object to be tested for implication.
*
* @return true if "permission" is a proper subset of a permission granted to
* this ProtectionDomain.
*
* @see java.security.ProtectionDomain
* @since 1.4
*/
public boolean implies(ProtectionDomain domain, Permission permission) {
// invoke policyContextKey1() only if
// permission instanceof WebResourcePermission and
// permission.getName().equals(/secured.jsp) // for secured.jsp
if ((permission instanceof WebResourcePermission)
&& (permission.getName().equals("/secured.jsp"))) {
logger.log(Level.INFO, "Calling policyContextKey1()");
policyContextKey1();
}
if (permission instanceof EJBMethodPermission) {
// Get method interface name (such as "Remote", "Home", etc..)
// and method name from permissions.getActions() string.
tokenArray = getTokensFromString(permission.getActions());
// Get method interface name and methodName from tokenArray
methodName = tokenArray[0];
methodInterfaceName = tokenArray[1];
// inovke policyContextKey2() and policyContextKey3() only if
// permission.getName().equals("jacc_providerContracts_JACCEntity") and
// methodInterfaceName.equals("Remote) and
// methodName.equals("getArg1")
if ((permission.getName().equals("jacc_providerContracts_JACCEntity"))
&& methodName.equals("getArg1")
&& methodInterfaceName.equals("Remote"))
{
logger.log(Level.INFO, "Calling policyContextKey2()");
// policyContextKey2();
logger.log(Level.INFO, "Calling policyContextKey3()");
policyContextKey3();
}
}
// If there is a PolicyContext.getContextID, verify new getPolicyConfiguration() methods work
String contextId = PolicyContext.getContextID();
if(contextId != null) {
try {
// Should be non-null PolicyConfiguration
PolicyConfigurationFactory pcf = PolicyConfigurationFactory.getPolicyConfigurationFactory();
PolicyConfiguration pc = pcf.getPolicyConfiguration();
if(pc != null) {
logger.log(Level.INFO, "PolicyConfigurationFactory.getPolicyConfiguration() : PASSED");
} else {
logger.log(Level.INFO, "PolicyConfigurationFactory.getPolicyConfiguration() : FAILED");
}
// Should be non-null PolicyConfiguration and match no-arg getPolicyConfiguration()
PolicyConfiguration pc2 = pcf.getPolicyConfiguration(contextId);
if(pc2 == null || !pc.equals(pc2)) {
logger.log(Level.INFO, "PolicyConfigurationFactory.getPolicyConfiguration(String) : FAILED");
} else {
logger.log(Level.INFO, "PolicyConfigurationFactory.getPolicyConfiguration(String) : PASSED");
}
} catch (Exception e) {
logger.log(Level.INFO, "PolicyConfigurationFactory.getPolicyConfiguration() : FAILED");
}
}
return policy.implies(domain, permission);
}
/**
* Refreshes/reloads the policy configuration. The behavior of this method
* depends on the implementation. For example, calling <code>refresh</code> on
* a file-based policy will cause the file to be re-read.
*
*/
public void refresh() {
policy.refresh();
if (logger != null)
logger.log(Level.INFO, "TSPolicy.refresh() invoked");
}
/**
* Loads vendor's policy implementation or JDK's default policy Note:
* LoadPolicy does not set the policy using setPolicy(policy), since setPolicy
* method will be called by the underlying provider, this task is delegated to
* the underlying provider.
*/
private void loadPolicy() {
String javaPolicy = System.getProperty(VENDOR_POLICY_PROVIDER);
if (javaPolicy == null) {
logger.log(Level.FINE, "Loading Default Policy");
policy = (java.security.Policy) new PolicyFile();
policy.refresh();
POLICY_INSTALLED = true;
logger.log(Level.INFO, "Default policy loaded");
} else {
try {
logger.log(Level.FINE, "Loading Policy = " + javaPolicy);
// Object obj = Class.forName(javaPolicy).newInstance();
classLoader = TSPolicy.class.getClassLoader();
Class clazz = classLoader.loadClass(javaPolicy);
Object obj = (Object) clazz.newInstance();
if (obj instanceof java.security.Policy) {
policy = (java.security.Policy) obj;
logger.log(Level.INFO, "vendor's policy loaded!");
POLICY_INSTALLED = true;
} else {
logger.log(Level.SEVERE,
"vendor's policy is not of type java.security.Policy");
throw new RuntimeException(
javaPolicy + "is not a type of java.security.Policy");
}
} catch (ClassNotFoundException cnfe) {
// problem with property value or classpath
logger.log(Level.SEVERE, "vendor's Policy instantiation error", cnfe);
throw new RuntimeException(cnfe);
} catch (IllegalAccessException iae) {
// problem with policy class definition
logger.log(Level.SEVERE, "vendor's Policy instantiation error", iae);
throw new RuntimeException(iae);
} catch (InstantiationException ie) {
// problem with policy instantiation
logger.log(Level.SEVERE, "vendor's Policy instantiation error", ie);
throw new RuntimeException(ie);
}
}
}
private static void initializeTSLogger() {
String logFileLocation = null;
if (logger != null)
return;
else {
try {
logFileLocation = System.getProperty("log.file.location");
if (logFileLocation != null) {
logger = TSLogger.getTSLogger("jacc");
boolean appendMode = true;
// clean the content of JACCLog.txt if it exists
File file = new File(logFileLocation + "/JACCLog.txt");
if (file.exists()) {
// delete the file, if it exists
file.delete();
}
// create a new file
System.out.println(
"XXXX: in initializeTSLogger() - about to create JACCLog.txt");
FileHandler fileHandler = new FileHandler(
logFileLocation + "/JACCLog.txt", appendMode);
fileHandler.setFormatter(new TSXMLFormatter());
logger.addHandler(fileHandler);
setTSLogger(logger);
} else {
// use default logging mechanism
logger = TSLogger.getTSLogger("jacc");
setTSLogger(logger);
logger.log(Level.SEVERE,
"log.file.location not set: Using default logger");
}
} catch (Exception e) {
throw new RuntimeException("TSLogger Initialization failed", e);
}
}
}
public static TSLogger getTSLogger() {
return logger;
}
public static void setTSLogger(TSLogger lgr) {
logger = lgr;
}
/**
* testName: policyContextKey1
*
* @assertion_ids: JACC:SPEC:99; JACC:JAVADOC:30
*
* @test_Strategy: 1) call
* PolicyContext.getContext("jakarta.servlet.http.HttpServletRequest")
* 2) verify the return value is an instance of
* HttpServletRequest
*
*/
private void policyContextKey1() {
try {
// Get HttpServletRequest object
jakarta.servlet.http.HttpServletRequest ctx = PolicyContext
.getContext("jakarta.servlet.http.HttpServletRequest");
logger.log(Level.INFO, "PolicyContext.getContext() " + "test passed for"
+ "jakarta.servlet.http.HttpServletRequest");
logger.log(Level.INFO, "PolicyContextKey1: PASSED");
} catch (ClassCastException e) {
logger.log(Level.INFO,
"PolicyContext.getContext()" + "returned incorrect value for key "
+ "jakarta.servlet.http.HttpServletRequest");
logger.log(Level.SEVERE, "PolicyContextKey1: FAILED");
} catch (Exception e) {
logger.log(Level.SEVERE, "PolicyContextKey1: FAILED");
}
}
/**
* testName: policyContextKey3
*
* @assertion_ids: JACC:SPEC:97; JACC:JAVADOC:30
*
* @test_Strategy: 1) call
* PolicyContext.getContext("javax.security.auth.Subject.container)
* 2) verify the return value is an instance of
* javax.security.auth.Subject
*
*/
private void policyContextKey3() {
try {
// Get Subject
javax.security.auth.Subject subject = PolicyContext
.getContext("javax.security.auth.Subject.container");
logger.log(Level.INFO, "PolicyContext.getContext() " + "test passed for"
+ "javax.security.auth.Subject.container");
logger.log(Level.INFO, "PolicyContextKey3: PASSED");
} catch (ClassCastException e) {
logger.log(Level.INFO,
"PolicyContext.getContext()" + "returned incorrect value for key "
+ "javax.security.auth.Subject.container");
logger.log(Level.INFO, "PolicyContextKey3: FAILED");
} catch (Exception e) {
logger.log(Level.SEVERE, "PolicyContextKey3: FAILED");
}
}
/**
* Gets the method interface name and method Name from the given permission's
* action string
*/
public String[] getTokensFromString(String actions) {
String[] array = new String[2];
StringTokenizer strtoken;
// Get first token and the remaining string
strtoken = new StringTokenizer(actions, ",");
if (actions.indexOf(",") > 0) {
// get methodName
array[0] = strtoken.nextToken();
if (strtoken.hasMoreTokens()) {
// get methodInterfaceName
array[1] = strtoken.nextToken();
}
}
return array;
}
/**
* testName: jaccPermissionsEquals
*
* assertion_ids: JACC:JAVADOC:4; JACC:JAVADOC:9; JACC:JAVADOC:43;
* JACC:JAVADOC:47; JACC:JAVADOC:53
*
* test_Strategy: 1) verify EJBMethodPermission.equals() or 2) verify
* EJBRoleRefPermission.equals() or 3) verify WebResourcePermission.equals()
* or 4) verify WebRoleRefPermission.equals() or 5) verify
* WebUserDataPermission.equals()
*
*/
private void jaccPermissionsEquals() {
try {
logger.log(Level.FINE, "Checking EJBMethodPermission.equals()");
EJBMethodPermission emp = new EJBMethodPermission("DummyEJB",
"dummyMethod,Home,String");
// call equals method onto itself
boolean result = emp.equals(emp);
if (result) {
logger.log(Level.INFO, "EJBMethodPermission.equals() : PASSED");
} else {
logger.log(Level.INFO, "EJBMethodPermission.equals() : FAILED");
logger.log(Level.INFO, "Calling EJBMethodPermission.equals()"
+ " onto itself returned false");
}
} catch (Exception e) {
logger.log(Level.SEVERE, "EJBMethodPermission.equals() : FAILED");
}
try {
EJBRoleRefPermission errp = new EJBRoleRefPermission("DummyEJB",
"dummyRole");
// call equals method onto itself
boolean result = errp.equals(errp);
if (result) {
logger.log(Level.INFO, "EJBRoleRefPermission.equals() : PASSED");
} else {
logger.log(Level.INFO, "EJBRoleRefPermission.equals() : FAILED");
logger.log(Level.INFO, "Calling EJBRoleRefPermission.equals()"
+ " onto itself returned false");
}
} catch (Exception e) {
logger.log(Level.SEVERE, "EJBRoleRefPermission.equals() : FAILED");
}
try {
WebResourcePermission wrp = new WebResourcePermission("/dummyEntry",
"POST");
// call equals method onto itself
boolean result = wrp.equals(wrp);
if (result) {
logger.log(Level.INFO, "WebResourcePermission.equals() : PASSED");
} else {
logger.log(Level.INFO, "WebResourcePermission.equals() : FAILED");
logger.log(Level.INFO, "Calling WebResourcePermission.equals()"
+ " onto itself returned false");
}
} catch (Exception e) {
logger.log(Level.SEVERE, "WebResourcePermission.equals() : FAILED");
}
try {
WebRoleRefPermission wrrp = new WebRoleRefPermission("dummyReosource",
"dummyRole");
// call equals method onto itself
boolean result = wrrp.equals(wrrp);
if (result) {
logger.log(Level.INFO, "WebRoleRefPermission.equals() : PASSED");
} else {
logger.log(Level.INFO, "WebRoleRefPermission.equals() : FAILED");
logger.log(Level.INFO, "Calling WebRoleRefPermission.equals()"
+ " onto itself returned false");
}
} catch (Exception e) {
logger.log(Level.SEVERE, "WebRoleRefPermission.equals() : FAILED");
}
try {
WebUserDataPermission wudp = new WebUserDataPermission(
"/dummyResource.jsp", "GET,POST:CONFIDENTIAL");
// call equals method onto itself
boolean result = wudp.equals(wudp);
if (result) {
logger.log(Level.INFO, "WebUserDataPermission.equals() : PASSED");
} else {
logger.log(Level.INFO, "WebUserDataPermission.equals() : FAILED");
logger.log(Level.INFO, "Calling WebUserDataPermission.equals()"
+ " onto itself returned false");
}
} catch (Exception e) {
logger.log(Level.SEVERE, "WebUserDataPermission.equals() : FAILED");
}
}
/**
* testName: jaccPermissionsHashCode
*
* assertion_ids: JACC:JAVADOC:6; JACC:JAVADOC:11; JACC:JAVADOC:42;
* JACC:JAVADOC:49; JACC:JAVADOC:55
*
* test_Strategy: 1) verify EJBMethodPermission.hashCode(); or 2) verify
* EJBRoleRefPermission.hashCode(); or 3) verify
* WebResourcePermission.hashCode() or 4) verify
* WebRoleRefPermission.hashCode() or 5) verify
* WebUserDataPermission.hashCode()
*/
private void jaccPermissionsHashCode() {
try {
EJBMethodPermission emp = new EJBMethodPermission("DummyEJB",
"dummyMethod,Home,String");
// Get EJBMethodPermission's hashcode
int hashCode1 = emp.hashCode();
// Get EJBMethodPermission's hashcode again
int hashCode2 = emp.hashCode();
if (hashCode1 == hashCode2) {
logger.log(Level.INFO, "EJBMethodPermission.hashCode() : PASSED");
} else {
logger.log(Level.INFO, "EJBMethodPermission.hashCode() : FAILED");
logger.log(Level.INFO, "EJBMethodPermission.hashCode()"
+ " returned different values within the same application.");
}
} catch (Exception e) {
logger.log(Level.SEVERE, "EJBMethodPermission.hashCode() : FAILED");
}
try {
EJBRoleRefPermission errp = new EJBRoleRefPermission("DummyEJB",
"dummyRole");
// Get EJBRoleRefPermission's hashcode
int hashCode3 = errp.hashCode();
// Get EJBRoleRefPermission's hashcode again
int hashCode4 = errp.hashCode();
if (hashCode3 == hashCode4) {
logger.log(Level.INFO, "EJBRoleRefPermission.hashCode() : PASSED");
} else {
logger.log(Level.INFO, "EJBRoleRefPermission.hashCode() : FAILED");
logger.log(Level.INFO, "EJBRoleRefPermission.hashCode()"
+ " returned different values within the same application.");
}
} catch (Exception e) {
logger.log(Level.SEVERE, "EJBRoleRefPermission.hashCode() : FAILED");
}
try {
WebResourcePermission wrp = new WebResourcePermission("/dummyEntry",
"POST");
// Get WebResourcePermission's hashcode
int hashCode5 = wrp.hashCode();
// Get WebResourcePermission's hashcode again
int hashCode6 = wrp.hashCode();
if (hashCode5 == hashCode6) {
logger.log(Level.INFO, "WebResourcePermission.hashCode() : PASSED");
} else {
logger.log(Level.INFO, "WebResourcePermission.hashCode() : FAILED");
logger.log(Level.INFO, "WebResourcePermission.hashCode()"
+ " returned different values within the same application.");
}
} catch (Exception e) {
logger.log(Level.SEVERE, "WebResourcePermission.hashCode() : FAILED");
}
try {
WebRoleRefPermission wrrp = new WebRoleRefPermission("dummyReosource",
"dummyRole");
// Get WebRoleRefPermission's hashcode
int hashCode7 = wrrp.hashCode();
// Get WebRoleRefPermission's hashcode again
int hashCode8 = wrrp.hashCode();
if (hashCode7 == hashCode8) {
logger.log(Level.INFO, "WebRoleRefPermission.hashCode() : PASSED");
} else {
logger.log(Level.INFO, "WebRoleRefPermission.hashCode() : FAILED");
logger.log(Level.INFO, "WebRoleRefPermission.hashCode()"
+ " returned different values within the same application.");
}
} catch (Exception e) {
logger.log(Level.SEVERE, "WebRoleRefPermission.hashCode() : FAILED");
}
try {
WebUserDataPermission wudp = new WebUserDataPermission(
"/dummyResource.jsp", "GET,POST:CONFIDENTIAL");
// Get WebUserDataPermission's hashcode
int hashCode9 = wudp.hashCode();
// Get WebUserDataPermission's hashcode again
int hashCode10 = wudp.hashCode();
if (hashCode9 == hashCode10) {
logger.log(Level.INFO, "WebUserDataPermission.hashCode() : PASSED");
} else {
logger.log(Level.INFO, "WebUserDataPermission.hashCode() : FAILED");
logger.log(Level.INFO, "WebUserDataPermission.hashCode()"
+ " returned different values within the same application.");
}
} catch (Exception e) {
logger.log(Level.SEVERE, "WebUserDataPermission.hashCode() : FAILED");
}
}
}