1 31 package org.objectweb.proactive.ext.security; 32 33 import java.io.IOException ; 34 import java.security.Provider ; 35 import java.security.Security ; 36 import java.security.cert.X509Certificate ; 37 import java.util.ArrayList ; 38 39 import org.apache.log4j.Logger; 40 import org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator; 41 import org.objectweb.proactive.core.xml.handler.BasicUnmarshaller; 42 import org.objectweb.proactive.core.xml.handler.UnmarshallerHandler; 43 import org.objectweb.proactive.core.xml.io.Attributes; 44 import org.xml.sax.SAXException ; 45 46 47 53 public class ProActiveSecurityDescriptorHandler 54 extends AbstractUnmarshallerDecorator { 55 protected PolicyServer policyServer; 56 protected X509Certificate applicationCertificate; 57 58 protected static Logger logger = Logger.getLogger(ProActiveSecurityDescriptorHandler.class.getName()); 59 protected static String PROACTIVE_SECURITY_TAG = "Policy"; 60 protected String RULE_TAG = "Rule"; 61 protected String ENTITY_TAG = "Entity"; 62 protected String RULES_TAG = "Rules"; 63 protected String PRIVATE_KEY_TAG = "PrivateKey"; 64 protected String CERTIFICATE_TAG = "Certificate"; 65 protected String TRUSTED_CERTIFICATION_AUTHORITY_TAG = "TrustedCertificationAuthority"; 66 protected String ENTITY_FROM_TAG = "From"; 67 protected String ENTITY_TO_TAG = "To"; 68 protected String RULE_COMMUNICATION_TAG = "Communication"; 69 protected String RULE_COMMUNICATION_TO_TAG = "Request"; 70 protected String RULE_COMMUNICATION_FROM_TAG = "Reply"; 71 protected String RULE_COMMUNICATION_MIGRATION_TAG = "Migration"; 72 protected String RULE_COMMUNICATION_AOCREATION_TAG = "OACreation"; 73 protected String RULE_COMMUNICATION_ATTRIBUTES_TAG = "Attributes"; 74 protected String RULE_MIGRATION_AUTHORIZED = "authorized"; 75 protected String RULE_MIGRATION_DENIED = "denied"; 76 protected String RULE_AOCREATION_AUTHORIZED = "authorized"; 77 protected String APPLICATION_NAME_TAG = "ApplicationName"; 78 81 public ProActiveSecurityDescriptorHandler() { 82 super(); 83 Provider myProvider = new org.bouncycastle.jce.provider.BouncyCastleProvider(); 84 Security.addProvider(myProvider); 85 policyServer = new PolicyServer(); 86 addHandler(APPLICATION_NAME_TAG,new SingleValueUnmarshaller()); 87 addHandler(PRIVATE_KEY_TAG, new SingleValueUnmarshaller()); 88 addHandler(CERTIFICATE_TAG, new SingleValueUnmarshaller()); 89 addHandler(RULES_TAG, new RulesHandler()); 90 } 91 92 95 protected void notifyEndActiveHandler(String name, 96 UnmarshallerHandler activeHandler) throws SAXException { 97 if (name.equals(PRIVATE_KEY_TAG)) { 98 policyServer.setApplicationPrivateKey((String ) activeHandler.getResultObject()); 99 } else if (name.equals(CERTIFICATE_TAG)) { 100 policyServer.setApplicationCertificate((String ) activeHandler.getResultObject()); 101 } else if (name.equals(RULES_TAG)) { 102 policyServer.setPolicies((ArrayList ) activeHandler.getResultObject()); 103 } else if (name.equals(APPLICATION_NAME_TAG)) { 104 policyServer.setApplicationName((String ) activeHandler.getResultObject()); 105 } 106 } 107 108 111 public Object getResultObject() throws SAXException { 112 return policyServer; 113 } 114 115 118 public void startContextElement(String name, Attributes attributes) 119 throws SAXException { 120 } 121 122 125 private class RulesHandler extends AbstractUnmarshallerDecorator { 126 127 RuleHandler ruleHandler = null; 128 private ArrayList policies; 129 130 public RulesHandler() { 131 super(); 132 policies = new ArrayList (); 133 ruleHandler = new RuleHandler(); 134 addHandler(RULE_TAG, ruleHandler); 135 } 136 137 public void startContextElement(String name, Attributes attributes) 138 throws org.xml.sax.SAXException { 139 if (name.equals(RULE_TAG)) { 140 } 143 } 144 145 146 protected void notifyEndActiveHandler(String name, 147 UnmarshallerHandler activeHandler) throws org.xml.sax.SAXException { 148 149 if (name.equals(RULE_TAG)) { 153 policies.add(activeHandler.getResultObject()); 154 } 156 addHandler(RULE_TAG, new RuleHandler()); 157 } 158 159 162 public Object getResultObject() throws SAXException { 163 return policies; 164 } 165 } 166 167 169 172 private static class InitialHandler extends AbstractUnmarshallerDecorator { 173 private ProActiveSecurityDescriptorHandler proActiveSecurityDescriptorHandler; 175 protected PolicyServer ps; 176 177 private InitialHandler() { 178 super(); 179 proActiveSecurityDescriptorHandler = new ProActiveSecurityDescriptorHandler(); 180 this.addHandler(PROACTIVE_SECURITY_TAG, 181 proActiveSecurityDescriptorHandler); 182 } 183 184 public Object getResultObject() throws org.xml.sax.SAXException { 185 return ps ; } 187 188 public void startContextElement(String name, Attributes attributes) 189 throws org.xml.sax.SAXException { 190 } 191 192 protected void notifyEndActiveHandler(String name, 193 UnmarshallerHandler activeHandler) throws org.xml.sax.SAXException { 194 if (name.equals(PROACTIVE_SECURITY_TAG)) { 195 ps = (PolicyServer) activeHandler.getResultObject(); 196 } 197 } 198 } 199 200 203 private class RuleHandler extends AbstractUnmarshallerDecorator { 204 private Policy policy; 205 206 public RuleHandler() { 207 super(); 208 policy = new Policy(); 209 addHandler(ENTITY_FROM_TAG, new EntityCollector()); 210 addHandler(ENTITY_TO_TAG, new EntityCollector()); 211 addHandler(RULE_COMMUNICATION_TAG, 212 new CommunicationCollectionHandler()); 213 addHandler(RULE_COMMUNICATION_AOCREATION_TAG, new SingleValueUnmarshaller()); 214 addHandler(RULE_COMMUNICATION_MIGRATION_TAG, new SingleValueUnmarshaller()); 215 } 216 217 public void startContextElement(String name, Attributes attributes) 218 throws org.xml.sax.SAXException { 219 policy = new Policy(); 220 } 221 222 225 protected void notifyEndActiveHandler(String name, 226 UnmarshallerHandler activeHandler) throws SAXException { 227 if (name.equals(ENTITY_FROM_TAG)) { 228 policy.setEntitiesFrom((ArrayList ) activeHandler.getResultObject()); 229 } else if (name.equals(ENTITY_TO_TAG)) { 230 policy.setEntitiesTo((ArrayList ) activeHandler.getResultObject()); 231 } else if (name.equals(RULE_COMMUNICATION_TAG)) { 232 policy.setCommunicationRules((Communication[]) activeHandler.getResultObject()); 233 } else if (name.equals(RULE_COMMUNICATION_AOCREATION_TAG)){ 234 String value = (String ) activeHandler.getResultObject(); 235 boolean b; 236 if (value.equals(RULE_AOCREATION_AUTHORIZED)) { 237 b = true; 238 } else { 239 b = false; 240 } 241 policy.setAocreation(b); 242 } else if (name.equals(RULE_COMMUNICATION_MIGRATION_TAG)){ 243 String value = (String ) activeHandler.getResultObject(); 244 boolean b; 245 if (value.equals(RULE_MIGRATION_AUTHORIZED)) { 246 b = true; 247 } else { 248 b = false; 249 } 250 policy.setMigration(b); 251 } 252 } 253 254 257 public Object getResultObject() throws SAXException { 258 return policy; 259 } 260 } 261 262 private class EntityCollector extends AbstractUnmarshallerDecorator { 264 private ArrayList entities; 265 266 public EntityCollector() { 267 entities = new ArrayList (); 268 addHandler(ENTITY_TAG, new EntityHandler()); 269 } 270 271 274 protected void notifyEndActiveHandler(String name, 275 UnmarshallerHandler activeHandler) throws SAXException { 276 entities.add(activeHandler.getResultObject()); 277 } 278 279 282 public Object getResultObject() throws SAXException { 283 return entities; 284 } 285 286 289 public void startContextElement(String name, Attributes attributes) 290 throws SAXException { 291 } 292 } 293 294 297 private class EntityHandler extends BasicUnmarshaller { 298 private Entity entity; 299 300 public EntityHandler() { 301 super(); 302 } 303 304 public void startContextElement(String name, Attributes attributes) 305 throws org.xml.sax.SAXException { 306 if (attributes.getValue("type").equals("VN")) { 307 entity = new EntityVirtualNode(attributes.getValue("name"),policyServer.getApplicationCertificate(),null); 308 } else if (attributes.getValue("type").equals("DefaultVirtualNode")) { 309 entity = new DefaultEntity(); 310 } 311 } 312 313 316 public Object getResultObject() throws SAXException { 317 return entity; 318 } 319 } 320 321 private class CommunicationCollectionHandler 323 extends AbstractUnmarshallerDecorator { 324 private Communication[] communication; 325 326 public CommunicationCollectionHandler() { 327 super(); 328 communication = new Communication[2]; 329 addHandler(RULE_COMMUNICATION_FROM_TAG, new CommunicationHandler()); 330 addHandler(RULE_COMMUNICATION_TO_TAG, new CommunicationHandler()); 331 } 332 333 336 protected void notifyEndActiveHandler(String name, 337 UnmarshallerHandler activeHandler) throws SAXException { 338 if (name.equals(RULE_COMMUNICATION_FROM_TAG)) { 339 communication[0] = (Communication) activeHandler.getResultObject(); 340 } else if (name.equals(RULE_COMMUNICATION_TO_TAG)) { 342 communication[1] = (Communication) activeHandler.getResultObject(); 343 } 344 } 345 346 349 public Object getResultObject() throws SAXException { 350 return communication; 351 } 352 353 356 public void startContextElement(String name, Attributes attributes) 357 throws SAXException { 358 } 359 } 360 361 364 private class CommunicationHandler extends AbstractUnmarshallerDecorator { 365 private Communication communication; 366 367 368 public CommunicationHandler() { 369 super(); 370 371 addHandler(RULE_COMMUNICATION_ATTRIBUTES_TAG, new CommunicationAttributesHandler()); 372 } 373 374 public void startContextElement(String name, Attributes attributes) 375 throws org.xml.sax.SAXException { 376 } 377 378 381 protected void notifyEndActiveHandler(String name, UnmarshallerHandler activeHandler) throws SAXException { 382 if (name.equals(RULE_COMMUNICATION_ATTRIBUTES_TAG)) { 383 communication = (Communication) activeHandler.getResultObject(); 384 } 385 386 } 387 388 391 public Object getResultObject() throws SAXException { 392 return communication; 394 } 395 } 396 397 400 private class CommunicationAttributesHandler extends BasicUnmarshaller { 401 private Communication communication; 402 403 public CommunicationAttributesHandler() { 404 super(); 405 } 406 407 public void startContextElement(String name, Attributes attributes) 408 throws org.xml.sax.SAXException { 409 communication = new Communication( 410 convert(attributes.getValue("authentication")), 411 convert(attributes.getValue("integrity")), 412 convert(attributes.getValue("confidentiality"))); 413 } 414 415 418 public Object getResultObject() throws SAXException { 419 return communication; 420 } 421 } 422 423 private class SingleValueUnmarshaller extends BasicUnmarshaller { 425 public void readValue(String value) throws org.xml.sax.SAXException { 426 setResultObject(value); 427 } 428 } 429 430 432 436 437 public static PolicyServer createPolicyServer(String xmlDescriptorUrl) 438 throws java.io.IOException , org.xml.sax.SAXException { 439 try { 441 InitialHandler h = new InitialHandler(); 442 String uri = xmlDescriptorUrl; 444 org.objectweb.proactive.core.xml.io.StreamReader sr = new org.objectweb.proactive.core.xml.io.StreamReader(new org.xml.sax.InputSource ( 445 uri), h); 446 sr.read(); 447 ((PolicyServer) h.getResultObject()).setFile(uri); 448 449 450 451 return (PolicyServer) h.getResultObject(); 452 } catch (org.xml.sax.SAXException e) { 453 e.printStackTrace(); 454 System.out.println( 455 "a problem occurs when getting the secuirty part of the ProActiveDescriptorHandler"); 456 throw e; 457 } 458 } 459 460 private int convert(String name) { 461 if (name == null) return Communication.OPTIONAL; 462 if (name.equals("required") || name.equals("allowed") || 463 name.equals("authorized")) { 464 return Communication.REQUIRED; 465 } else if (name.equals("denied")) { 466 return Communication.DENIED; 467 } else { 468 return Communication.OPTIONAL; 469 } 470 } 471 472 473 public static void main(String [] args) throws IOException { 474 InitialHandler h = new InitialHandler(); 475 String uri = "/net/home/acontes/dev/ProActive/descriptors/scurrav2.xml"; 477 org.objectweb.proactive.core.xml.io.StreamReader sr = new org.objectweb.proactive.core.xml.io.StreamReader(new org.xml.sax.InputSource ( 478 args[0]), h); 479 sr.read(); 480 } 481 } 482 | Popular Tags |