SEC-803: Removed use of websphere SubjectHelper class.

This commit is contained in:
Luke Taylor 2008-05-14 22:51:39 +00:00
parent d4defb10fe
commit 6493df13f8

View File

@ -24,171 +24,182 @@ import org.apache.commons.logging.LogFactory;
* @since 2.0 * @since 2.0
*/ */
final class WASSecurityHelper { final class WASSecurityHelper {
private static final Log logger = LogFactory.getLog(WASSecurityHelper.class); private static final Log logger = LogFactory.getLog(WASSecurityHelper.class);
private static final String USER_REGISTRY = "UserRegistry"; private static final String USER_REGISTRY = "UserRegistry";
private static Method getRunAsSubject = null; private static Method getRunAsSubject = null;
private static Method getWSCredentialFromSubject = null; private static Method getGroupsForUser = null;
private static Method getGroupsForUser = null; private static Method getSecurityName = null;
private static Method getSecurityName = null; // SEC-803
private static Class wsCredentialClass = null;
/**
* Get the security name for the given subject.
*
* @param subject
* The subject for which to retrieve the security name
* @return String the security name for the given subject
*/
private static final String getSecurityName(final Subject subject) {
if (logger.isDebugEnabled()) {
logger.debug("Determining Websphere security name for subject " + subject);
}
String userSecurityName = null;
if (subject != null) {
// SEC-803
Object credential = subject.getPublicCredentials(getWSCredentialClass()).iterator().next();
if (credential != null) {
userSecurityName = (String)invokeMethod(getSecurityNameMethod(),credential,null);
}
}
if (logger.isDebugEnabled()) {
logger.debug("Websphere security name is " + userSecurityName + " for subject " + subject);
}
return userSecurityName;
}
/** /**
* Get the security name for the given subject. * Get the current RunAs subject.
* *
* @param subject * @return Subject the current RunAs subject
* The subject for which to retrieve the security name */
* @return String the security name for the given subject private static final Subject getRunAsSubject() {
*/ logger.debug("Retrieving WebSphere RunAs subject");
private static final String getSecurityName(final Subject subject) { // get Subject: WSSubject.getCallerSubject ();
if (logger.isDebugEnabled()) { return (Subject) invokeMethod(getRunAsSubjectMethod(), null, new Object[] {});
logger.debug("Determining Websphere security name for subject " + subject); }
}
String userSecurityName = null;
if (subject != null) {
Object credential = invokeMethod(getWSCredentialFromSubjectMethod(),null,new Object[]{subject});
if (credential != null) {
userSecurityName = (String)invokeMethod(getSecurityNameMethod(),credential,null);
}
}
if (logger.isDebugEnabled()) {
logger.debug("Websphere security name is " + userSecurityName + " for subject " + subject);
}
return userSecurityName;
}
/** /**
* Get the current RunAs subject. * Get the WebSphere group names for the given subject.
* *
* @return Subject the current RunAs subject * @param subject
*/ * The subject for which to retrieve the WebSphere group names
private static final Subject getRunAsSubject() { * @return the WebSphere group names for the given subject
logger.debug("Retrieving WebSphere RunAs subject"); */
// get Subject: WSSubject.getCallerSubject (); private static final String[] getWebSphereGroups(final Subject subject) {
return (Subject) invokeMethod(getRunAsSubjectMethod(), null, new Object[] {}); return getWebSphereGroups(getSecurityName(subject));
} }
/** /**
* Get the WebSphere group names for the given subject. * Get the WebSphere group names for the given security name.
* *
* @param subject * @param securityName
* The subject for which to retrieve the WebSphere group names * The securityname for which to retrieve the WebSphere group names
* @return the WebSphere group names for the given subject * @return the WebSphere group names for the given security name
*/ */
private static final String[] getWebSphereGroups(final Subject subject) { private static final String[] getWebSphereGroups(final String securityName) {
return getWebSphereGroups(getSecurityName(subject)); Context ic = null;
} try {
// TODO: Cache UserRegistry object
ic = new InitialContext();
Object objRef = ic.lookup(USER_REGISTRY);
Object userReg = PortableRemoteObject.narrow(objRef, Class.forName ("com.ibm.websphere.security.UserRegistry"));
if (logger.isDebugEnabled()) {
logger.debug("Determining WebSphere groups for user " + securityName + " using WebSphere UserRegistry " + userReg);
}
final Collection groups = (Collection) invokeMethod(getGroupsForUserMethod(), userReg, new Object[]{ securityName });
if (logger.isDebugEnabled()) {
logger.debug("Groups for user " + securityName + ": " + groups.toString());
}
String[] result = new String[groups.size()];
return (String[]) groups.toArray(result);
} catch (Exception e) {
logger.error("Exception occured while looking up groups for user", e);
throw new RuntimeException("Exception occured while looking up groups for user", e);
} finally {
try {
ic.close();
} catch (NamingException e) {
logger.debug("Exception occured while closing context", e);
}
}
}
/** /**
* Get the WebSphere group names for the given security name. * @return
* */
* @param securityName public static final String[] getGroupsForCurrentUser() {
* The securityname for which to retrieve the WebSphere group names return getWebSphereGroups(getRunAsSubject());
* @return the WebSphere group names for the given security name }
*/
private static final String[] getWebSphereGroups(final String securityName) {
Context ic = null;
try {
// TODO: Cache UserRegistry object
ic = new InitialContext();
Object objRef = ic.lookup(USER_REGISTRY);
Object userReg = PortableRemoteObject.narrow(objRef, Class.forName ("com.ibm.websphere.security.UserRegistry"));
if (logger.isDebugEnabled()) {
logger.debug("Determining WebSphere groups for user " + securityName + " using WebSphere UserRegistry " + userReg);
}
final Collection groups = (Collection) invokeMethod(getGroupsForUserMethod(), userReg, new Object[]{ securityName });
if (logger.isDebugEnabled()) {
logger.debug("Groups for user " + securityName + ": " + groups.toString());
}
String[] result = new String[groups.size()];
return (String[]) groups.toArray(result);
} catch (Exception e) {
logger.error("Exception occured while looking up groups for user", e);
throw new RuntimeException("Exception occured while looking up groups for user", e);
} finally {
try {
ic.close();
} catch (NamingException e) {
logger.debug("Exception occured while closing context", e);
}
}
}
/** public static final String getCurrentUserName() {
* @return return getSecurityName(getRunAsSubject());
*/ }
public static final String[] getGroupsForCurrentUser() {
return getWebSphereGroups(getRunAsSubject()); private static final Object invokeMethod(Method method, Object instance, Object[] args)
} {
try {
return method.invoke(instance,args);
} catch (IllegalArgumentException e) {
logger.error("Error while invoking method "+method.getClass().getName()+"."+method.getName()+"("+ Arrays.asList(args)+")",e);
throw new RuntimeException("Error while invoking method "+method.getClass().getName()+"."+method.getName()+"("+Arrays.asList(args)+")",e);
} catch (IllegalAccessException e) {
logger.error("Error while invoking method "+method.getClass().getName()+"."+method.getName()+"("+Arrays.asList(args)+")",e);
throw new RuntimeException("Error while invoking method "+method.getClass().getName()+"."+method.getName()+"("+Arrays.asList(args)+")",e);
} catch (InvocationTargetException e) {
logger.error("Error while invoking method "+method.getClass().getName()+"."+method.getName()+"("+Arrays.asList(args)+")",e);
throw new RuntimeException("Error while invoking method "+method.getClass().getName()+"."+method.getName()+"("+Arrays.asList(args)+")",e);
}
}
public static final String getCurrentUserName() { private static final Method getMethod(String className, String methodName, String[] parameterTypeNames) {
return getSecurityName(getRunAsSubject()); try {
} Class c = Class.forName(className);
final int len = parameterTypeNames.length;
private static final Object invokeMethod(Method method, Object instance, Object[] args) Class[] parameterTypes = new Class[len];
{ for (int i = 0; i < len; i++) {
try { parameterTypes[i] = Class.forName(parameterTypeNames[i]);
return method.invoke(instance,args); }
} catch (IllegalArgumentException e) { return c.getDeclaredMethod(methodName, parameterTypes);
logger.error("Error while invoking method "+method.getClass().getName()+"."+method.getName()+"("+ Arrays.asList(args)+")",e); } catch (ClassNotFoundException e) {
throw new RuntimeException("Error while invoking method "+method.getClass().getName()+"."+method.getName()+"("+Arrays.asList(args)+")",e); logger.error("Required class"+className+" not found");
} catch (IllegalAccessException e) { throw new RuntimeException("Required class"+className+" not found",e);
logger.error("Error while invoking method "+method.getClass().getName()+"."+method.getName()+"("+Arrays.asList(args)+")",e); } catch (NoSuchMethodException e) {
throw new RuntimeException("Error while invoking method "+method.getClass().getName()+"."+method.getName()+"("+Arrays.asList(args)+")",e); logger.error("Required method "+methodName+" with parameter types ("+ Arrays.asList(parameterTypeNames) +") not found on class "+className);
} catch (InvocationTargetException e) { throw new RuntimeException("Required class"+className+" not found",e);
logger.error("Error while invoking method "+method.getClass().getName()+"."+method.getName()+"("+Arrays.asList(args)+")",e); }
throw new RuntimeException("Error while invoking method "+method.getClass().getName()+"."+method.getName()+"("+Arrays.asList(args)+")",e); }
}
}
private static final Method getMethod(String className, String methodName, String[] parameterTypeNames) { private static final Method getRunAsSubjectMethod() {
try { if (getRunAsSubject == null) {
Class c = Class.forName(className); getRunAsSubject = getMethod("com.ibm.websphere.security.auth.WSSubject", "getRunAsSubject", new String[] {});
final int len = parameterTypeNames.length; }
Class[] parameterTypes = new Class[len]; return getRunAsSubject;
for (int i = 0; i < len; i++) { }
parameterTypes[i] = Class.forName(parameterTypeNames[i]);
}
return c.getDeclaredMethod(methodName, parameterTypes);
} catch (ClassNotFoundException e) {
logger.error("Required class"+className+" not found");
throw new RuntimeException("Required class"+className+" not found",e);
} catch (NoSuchMethodException e) {
logger.error("Required method "+methodName+" with parameter types ("+ Arrays.asList(parameterTypeNames) +") not found on class "+className);
throw new RuntimeException("Required class"+className+" not found",e);
}
}
private static final Method getRunAsSubjectMethod() { private static final Method getGroupsForUserMethod() {
if (getRunAsSubject == null) { if (getGroupsForUser == null) {
getRunAsSubject = getMethod("com.ibm.websphere.security.auth.WSSubject", "getRunAsSubject", new String[] {}); getGroupsForUser = getMethod("com.ibm.websphere.security.UserRegistry", "getGroupsForUser", new String[] { "java.lang.String" });
} }
return getRunAsSubject; return getGroupsForUser;
} }
private static final Method getWSCredentialFromSubjectMethod() { private static final Method getSecurityNameMethod() {
if (getWSCredentialFromSubject == null) { if (getSecurityName == null) {
getWSCredentialFromSubject = getMethod("com.ibm.ws.security.auth.SubjectHelper", "getWSCredentialFromSubject", getSecurityName = getMethod("com.ibm.websphere.security.cred.WSCredential", "getSecurityName", new String[] {});
new String[] { "javax.security.auth.Subject" }); }
} return getSecurityName;
return getWSCredentialFromSubject; }
}
// SEC-803
private static final Method getGroupsForUserMethod() { private static final Class getWSCredentialClass() {
if (getGroupsForUser == null) { if (wsCredentialClass == null) {
getGroupsForUser = getMethod("com.ibm.websphere.security.UserRegistry", "getGroupsForUser", new String[] { "java.lang.String" }); wsCredentialClass = getClass("com.ibm.websphere.security.cred.WSCredential");
} }
return getGroupsForUser; return wsCredentialClass;
} }
private static final Method getSecurityNameMethod() { private static final Class getClass(String className) {
if (getSecurityName == null) { try {
getSecurityName = getMethod("com.ibm.websphere.security.cred.WSCredential", "getSecurityName", new String[] {}); return Class.forName(className);
} } catch (ClassNotFoundException e) {
return getSecurityName; logger.error("Required class " + className + " not found");
} throw new RuntimeException("Required class " + className + " not found",e);
}
}
} }