1 7 8 package org.jboss.security; 9 10 import java.security.Principal ; 11 import java.util.ArrayList ; 12 import java.util.HashMap ; 13 import javax.security.auth.Subject ; 14 15 import org.jboss.logging.Logger; 16 17 44 public final class SecurityAssociation 45 { 46 private static Logger log = Logger.getLogger(SecurityAssociation.class); 47 50 private static boolean trace; 51 54 private static boolean server; 55 58 private static Principal principal; 59 62 private static Object credential; 63 64 67 private static ThreadLocal threadPrincipal; 68 71 private static ThreadLocal threadCredential; 72 75 private static ThreadLocal threadContextMap; 76 77 81 private static RunAsThreadLocalStack threadRunAsStacks = new RunAsThreadLocalStack(); 82 86 private static SubjectThreadLocalStack threadSubjectStacks = new SubjectThreadLocalStack(); 87 88 91 private static final RuntimePermission getPrincipalInfoPermission = 92 new RuntimePermission ("org.jboss.security.SecurityAssociation.getPrincipalInfo"); 93 96 private static final RuntimePermission getSubjectPermission = 97 new RuntimePermission ("org.jboss.security.SecurityAssociation.getSubject"); 98 102 private static final RuntimePermission setPrincipalInfoPermission = 103 new RuntimePermission ("org.jboss.security.SecurityAssociation.setPrincipalInfo"); 104 107 private static final RuntimePermission setServerPermission = 108 new RuntimePermission ("org.jboss.security.SecurityAssociation.setServer"); 109 112 private static final RuntimePermission setRunAsIdentity = 113 new RuntimePermission ("org.jboss.security.SecurityAssociation.setRunAsRole"); 114 117 private static final RuntimePermission getContextInfo = 118 new RuntimePermission ("org.jboss.security.SecurityAssociation.accessContextInfo", "get"); 119 122 private static final RuntimePermission setContextInfo = 123 new RuntimePermission ("org.jboss.security.SecurityAssociation.accessContextInfo", "set"); 124 125 static 126 { 127 boolean useThreadLocal = true; 128 try 129 { 130 useThreadLocal = Boolean.getBoolean("org.jboss.security.SecurityAssociation.ThreadLocal"); 131 } 132 catch (SecurityException e) 133 { 134 } 136 137 trace = log.isTraceEnabled(); 138 if (useThreadLocal) 139 { 140 threadPrincipal = new ThreadLocal (); 141 threadCredential = new ThreadLocal (); 142 threadContextMap = new ThreadLocal () 143 { 144 protected Object initialValue() 145 { 146 return new HashMap (); 147 } 148 }; 149 } 150 else 151 { 152 threadPrincipal = new InheritableThreadLocal (); 153 threadCredential = new InheritableThreadLocal (); 154 threadContextMap = new InheritableThreadLocal () 155 { 156 protected Object initialValue() 157 { 158 return new HashMap (); 159 } 160 }; 161 } 162 } 163 164 173 public static Principal getPrincipal() 174 { 175 SecurityManager sm = System.getSecurityManager(); 176 if (sm != null) 177 sm.checkPermission(getPrincipalInfoPermission); 178 179 if (server) 180 return (Principal ) threadPrincipal.get(); 181 else 182 return principal; 183 } 184 185 194 public static Principal getCallerPrincipal() 195 { 196 SecurityManager sm = System.getSecurityManager(); 197 if (sm != null) 198 sm.checkPermission(getPrincipalInfoPermission); 199 200 if (peekRunAsIdentity(1) != null) 201 return peekRunAsIdentity(1); 202 if (server) 203 return (Principal ) threadPrincipal.get(); 204 else 205 return principal; 206 } 207 208 217 public static Object getCredential() 218 { 219 SecurityManager sm = System.getSecurityManager(); 220 if (sm != null) 221 sm.checkPermission(getPrincipalInfoPermission); 222 223 if (server) 224 return threadCredential.get(); 225 else 226 return credential; 227 } 228 229 241 public static Subject getSubject() 242 { 243 SecurityManager sm = System.getSecurityManager(); 244 if (sm != null) 245 sm.checkPermission(getSubjectPermission); 246 247 SubjectContext sc = threadSubjectStacks.peek(); 248 Subject subject = null; 249 if( sc != null ) 250 subject = sc.getSubject(); 251 return subject; 252 } 253 254 262 public static void setPrincipal(Principal principal) 263 { 264 SecurityManager sm = System.getSecurityManager(); 265 if (sm != null) 266 sm.checkPermission(setPrincipalInfoPermission); 267 268 if (trace) 269 log.trace("setPrincipal, p=" + principal + ", server=" + server); 270 if (server) 271 threadPrincipal.set(principal); 272 else 273 SecurityAssociation.principal = principal; 274 SubjectContext sc = threadSubjectStacks.peek(); 276 if( sc == null ) 277 { 278 sc = new SubjectContext(); 280 threadSubjectStacks.push(sc); 281 } 282 else if( (sc.getFlags() & SubjectContext.PRINCIPAL_WAS_SET) != 0 ) 283 { 284 sc = new SubjectContext(); 286 threadSubjectStacks.push(sc); 287 } 288 sc.setPrincipal(principal); 289 } 290 291 302 public static void setCredential(Object credential) 303 { 304 SecurityManager sm = System.getSecurityManager(); 305 if (sm != null) 306 sm.checkPermission(setPrincipalInfoPermission); 307 308 if (server) 309 threadCredential.set(credential); 310 else 311 SecurityAssociation.credential = credential; 312 SubjectContext sc = threadSubjectStacks.peek(); 314 if( sc == null ) 315 { 316 sc = new SubjectContext(); 318 threadSubjectStacks.push(sc); 319 } 320 else if( (sc.getFlags() & SubjectContext.CREDENTIAL_WAS_SET) != 0 ) 321 { 322 sc = new SubjectContext(); 324 threadSubjectStacks.push(sc); 325 } 326 sc.setCredential(credential); 327 } 328 329 337 public static void setSubject(Subject subject) 338 { 339 SecurityManager sm = System.getSecurityManager(); 340 if (sm != null) 341 sm.checkPermission(setPrincipalInfoPermission); 342 343 if (trace) 344 log.trace("setSubject, s=" + subject + ", server=" + server); 345 SubjectContext sc = threadSubjectStacks.peek(); 347 if( sc == null ) 348 { 349 sc = new SubjectContext(); 351 threadSubjectStacks.push(sc); 352 } 353 else if( (sc.getFlags() & SubjectContext.SUBJECT_WAS_SET) != 0 ) 354 { 355 sc = new SubjectContext(); 357 threadSubjectStacks.push(sc); 358 } 359 sc.setSubject(subject); 360 } 361 362 371 public static Object getContextInfo(Object key) 372 { 373 SecurityManager sm = System.getSecurityManager(); 374 if (sm != null) 375 sm.checkPermission(getContextInfo); 376 377 HashMap contextInfo = (HashMap ) threadContextMap.get(); 378 return contextInfo.get(key); 379 } 380 381 391 public static Object setContextInfo(Object key, Object value) 392 { 393 SecurityManager sm = System.getSecurityManager(); 394 if (sm != null) 395 sm.checkPermission(setContextInfo); 396 397 HashMap contextInfo = (HashMap ) threadContextMap.get(); 398 return contextInfo.put(key, value); 399 } 400 401 413 public static void pushSubjectContext(Subject subject, 414 Principal principal, Object credential) 415 { 416 SecurityManager sm = System.getSecurityManager(); 417 if (sm != null) 418 sm.checkPermission(setPrincipalInfoPermission); 419 420 if (trace) 421 log.trace("pushSubjectContext, subject=" + subject + ", principal="+principal); 422 if (server) 424 { 425 threadPrincipal.set(principal); 426 threadCredential.set(credential); 427 } 428 else 429 { 430 SecurityAssociation.principal = principal; 431 SecurityAssociation.credential = credential; 432 } 433 SubjectContext sc = new SubjectContext(subject, principal, credential); 435 threadSubjectStacks.push(sc); 436 } 437 443 public static void dupSubjectContext() 444 { 445 SecurityManager sm = System.getSecurityManager(); 446 if (sm != null) 447 sm.checkPermission(setPrincipalInfoPermission); 448 449 if (trace) 450 log.trace("dupSubjectContext"); 451 threadSubjectStacks.dup(); 452 } 453 454 462 public static SubjectContext popSubjectContext() 463 { 464 SecurityManager sm = System.getSecurityManager(); 465 if (sm != null) 466 sm.checkPermission(setPrincipalInfoPermission); 467 468 SubjectContext sc = threadSubjectStacks.pop(); 469 return sc; 470 } 471 472 479 public static void clear() 480 { 481 SecurityManager sm = System.getSecurityManager(); 482 if (sm != null) 483 sm.checkPermission(setPrincipalInfoPermission); 484 485 if (trace) 486 log.trace("clear, server=" + server); 487 if (server == true) 488 { 489 threadPrincipal.set(null); 490 threadCredential.set(null); 491 } 492 else 493 { 494 SecurityAssociation.principal = null; 495 SecurityAssociation.credential = null; 496 } 497 threadSubjectStacks.clear(); 499 } 500 501 504 public static void pushRunAsIdentity(RunAsIdentity runAs) 505 { 506 SecurityManager sm = System.getSecurityManager(); 507 if (sm != null) 508 sm.checkPermission(setRunAsIdentity); 509 if (trace) 510 log.trace("pushRunAsIdentity, runAs=" + runAs); 511 threadRunAsStacks.push(runAs); 512 } 513 514 517 public static RunAsIdentity popRunAsIdentity() 518 { 519 SecurityManager sm = System.getSecurityManager(); 520 if (sm != null) 521 sm.checkPermission(setRunAsIdentity); 522 RunAsIdentity runAs = threadRunAsStacks.pop(); 523 if (trace) 524 log.trace("popRunAsIdentity, runAs=" + runAs); 525 return runAs; 526 } 527 528 532 public static RunAsIdentity peekRunAsIdentity() 533 { 534 return peekRunAsIdentity(0); 535 } 536 537 544 public static RunAsIdentity peekRunAsIdentity(int depth) 545 { 546 RunAsIdentity runAs = threadRunAsStacks.peek(depth); 547 return runAs; 548 } 549 550 563 public static void setServer() 564 { 565 SecurityManager sm = System.getSecurityManager(); 566 if (sm != null) 567 sm.checkPermission(setServerPermission); 568 569 server = true; 570 } 571 572 577 private static class RunAsThreadLocalStack extends ThreadLocal 578 { 579 protected Object initialValue() 580 { 581 return new ArrayList (); 582 } 583 584 void push(RunAsIdentity runAs) 585 { 586 ArrayList stack = (ArrayList ) super.get(); 587 stack.add(runAs); 588 } 589 590 RunAsIdentity pop() 591 { 592 ArrayList stack = (ArrayList ) super.get(); 593 RunAsIdentity runAs = null; 594 int lastIndex = stack.size() - 1; 595 if (lastIndex >= 0) 596 runAs = (RunAsIdentity) stack.remove(lastIndex); 597 return runAs; 598 } 599 600 605 RunAsIdentity peek(int depth) 606 { 607 ArrayList stack = (ArrayList ) super.get(); 608 RunAsIdentity runAs = null; 609 final int stackSize = stack.size(); 610 do 611 { 612 int index = stackSize - 1 - depth; 613 if( index >= 0 ) 614 runAs = (RunAsIdentity) stack.get(index); 615 depth ++; 616 } 617 while (runAs == null && depth <= stackSize - 1); 618 return runAs; 619 } 620 } 621 622 625 public static class SubjectContext 626 { 627 public static final int SUBJECT_WAS_SET = 1; 628 public static final int PRINCIPAL_WAS_SET = 2; 629 public static final int CREDENTIAL_WAS_SET = 4; 630 631 private Subject subject; 632 private Principal principal; 633 private Object credential; 634 private int flags; 635 636 public SubjectContext() 637 { 638 this.flags = 0; 639 } 640 public SubjectContext(Subject s, Principal p, Object cred) 641 { 642 this.subject = s; 643 this.principal = p; 644 this.credential = cred; 645 this.flags = SUBJECT_WAS_SET | PRINCIPAL_WAS_SET | CREDENTIAL_WAS_SET; 646 } 647 648 public Subject getSubject() 649 { 650 return subject; 651 } 652 public void setSubject(Subject subject) 653 { 654 this.subject = subject; 655 this.flags |= SUBJECT_WAS_SET; 656 } 657 658 public Principal getPrincipal() 659 { 660 return principal; 661 } 662 public void setPrincipal(Principal principal) 663 { 664 this.principal = principal; 665 this.flags |= PRINCIPAL_WAS_SET; 666 } 667 668 public Object getCredential() 669 { 670 return credential; 671 } 672 public void setCredential(Object credential) 673 { 674 this.credential = credential; 675 this.flags |= CREDENTIAL_WAS_SET; 676 } 677 678 public int getFlags() 679 { 680 return this.flags; 681 } 682 } 683 684 private static class SubjectThreadLocalStack extends ThreadLocal 685 { 686 protected Object initialValue() 687 { 688 return new ArrayList (); 689 } 690 691 void push(SubjectContext context) 692 { 693 ArrayList stack = (ArrayList ) super.get(); 694 stack.add(context); 695 } 696 697 SubjectContext dup() 698 { 699 ArrayList stack = (ArrayList ) super.get(); 700 SubjectContext context = null; 701 int lastIndex = stack.size() - 1; 702 if (lastIndex >= 0) 703 { 704 context = (SubjectContext) stack.get(lastIndex); 705 stack.add(context); 706 } 707 return context; 708 } 709 710 SubjectContext pop() 711 { 712 ArrayList stack = (ArrayList ) super.get(); 713 SubjectContext context = null; 714 int lastIndex = stack.size() - 1; 715 if (lastIndex >= 0) 716 context = (SubjectContext) stack.remove(lastIndex); 717 return context; 718 } 719 720 725 SubjectContext peek() 726 { 727 ArrayList stack = (ArrayList ) super.get(); 728 SubjectContext context = null; 729 int lastIndex = stack.size() - 1; 730 if (lastIndex >= 0) 731 context = (SubjectContext) stack.get(lastIndex); 732 return context; 733 } 734 737 void clear() 738 { 739 ArrayList stack = (ArrayList ) super.get(); 740 stack.clear(); 741 } 742 } 743 744 } 745 | Popular Tags |