| 1 22 23 package org.continuent.sequoia.controller.jmx; 24 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Set ; 30 31 import javax.management.InstanceNotFoundException ; 32 import javax.management.MBeanException ; 33 import javax.management.MBeanServer ; 34 import javax.management.ObjectName ; 35 import javax.management.ReflectionException ; 36 import javax.security.auth.Subject ; 37 38 import org.continuent.sequoia.common.jmx.JMXPrincipalWithPassword; 39 import org.continuent.sequoia.common.jmx.JmxConstants; 40 import org.continuent.sequoia.common.log.Trace; 41 42 49 public class AuthenticatingMBeanServer extends ChainedMBeanServer 50 { 51 52 53 static Trace logger = Trace 54 .getLogger("org.continuent.sequoia.controller.jmx.AuthenticatingMBeanServer"); 55 56 62 public void setMBeanServer(MBeanServer server) 63 { 64 super.setMBeanServer(server); 65 } 66 67 71 public Object invoke(ObjectName name, String operationName, Object [] params, 72 String [] signature) throws InstanceNotFoundException , MBeanException , 73 ReflectionException  74 { 75 if (JmxConstants.mbeanNeedAuthentication(name) 76 && (operationName.equalsIgnoreCase("checkAdminAuthentication") == false)) 77 { 78 boolean authenticationOk = false; 82 String username = null; 83 String password = null; 84 85 Subject subject = Subject.getSubject(java.security.AccessController 86 .getContext()); 87 if (subject == null || subject.getPrincipals().size() == 0) 88 { 89 username = (String ) params[0]; 90 password = (String ) params[1]; 91 authenticationOk = authenticate(name, username, password); 92 if (!authenticationOk) 93 throw new MBeanException (new Exception ( 94 "Authentication failed (username,password) invalid")); 95 96 if (logger.isDebugEnabled()) 97 logger 98 .debug("Authentication with username and password was successfull"); 99 100 return super.invoke(name, operationName, cleanO(params), 103 cleanS(signature)); 104 } 105 else 106 { 107 Set principals = subject.getPrincipals(JMXPrincipalWithPassword.class); 108 for (Iterator it = principals.iterator(); it.hasNext();) 109 { 110 JMXPrincipalWithPassword principal = (JMXPrincipalWithPassword) it 111 .next(); 112 username = principal.getName(); 113 password = principal.getPassword(); 114 authenticationOk = authenticate(name, username, password); 115 if (authenticationOk) 116 break; 117 } 118 119 if (principals.size() == 0 && logger.isDebugEnabled()) 120 throw new MBeanException (new Exception ( 121 "Authentication failed : no principal")); 122 123 if (!authenticationOk) 124 throw new MBeanException (new Exception ( 125 "Authentication failed : principal invalid")); 126 if (logger.isDebugEnabled()) 127 logger.debug("Authentication with principal was successfull"); 128 return super.invoke(name, operationName, params, signature); 129 } 130 } 131 else 132 { 133 if (logger.isDebugEnabled()) 134 logger.debug("no authentication required"); 135 136 return super.invoke(name, operationName, params, signature); 137 } 138 } 139 140 private boolean authenticate(ObjectName name, String username, String password) 141 { 142 try 143 { 144 String type = name.getKeyProperty("type"); 145 boolean vdb = JmxConstants.VIRTUALDATABASE_TYPE_VALUE.equals(type); 146 if (vdb) 147 return ((Boolean ) invoke(name, "checkAdminAuthentication", 148 new Object []{username, password}, new String []{"java.lang.String", 149 "java.lang.String"})).booleanValue(); 150 else 151 { 152 boolean backend = JmxConstants.DATABASEBACKEND_TYPE_VALUE.equals(type); 153 if (backend) 154 { 155 String virtualDataBaseName = name 156 .getKeyProperty(JmxConstants.VIRTUALDATABASE_PROPERTY); 157 if (virtualDataBaseName == null) 158 { 159 return false; 160 } 161 ObjectName vdbName = JmxConstants 163 .getVirtualDataBaseObjectName(virtualDataBaseName); 164 return ((Boolean ) invoke(vdbName, "checkAdminAuthentication", 165 new Object []{username, password}, new String []{ 166 "java.lang.String", "java.lang.String"})).booleanValue(); 167 } 168 else 169 return true; 171 } 172 } 173 catch (Exception e) 174 { 175 if (logger.isDebugEnabled()) 176 { 177 logger.debug("authentication failed with exception ", e); 178 } 179 return false; 180 } 181 } 182 183 private static Object [] cleanO(Object [] params) 184 { 185 List o = Arrays.asList(params); 186 o = o.subList(2, o.size()); 187 return (new ArrayList (o).toArray()); 188 } 189 190 private static String [] cleanS(String [] params) 191 { 192 List o = Arrays.asList(params); 193 o = o.subList(2, o.size()); 194 String [] s = new String [o.size()]; 195 return (String []) new ArrayList (o).toArray(s); 196 } 197 } | Popular Tags |