Better handling when attribute PlugIns are mislabeled as DataConnector PlugIns. ...
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / aa / attrresolv / AttributeResolver.java
1 /* 
2  * The Shibboleth License, Version 1. 
3  * Copyright (c) 2002 
4  * University Corporation for Advanced Internet Development, Inc. 
5  * All rights reserved
6  * 
7  * 
8  * Redistribution and use in source and binary forms, with or without 
9  * modification, are permitted provided that the following conditions are met:
10  * 
11  * Redistributions of source code must retain the above copyright notice, this 
12  * list of conditions and the following disclaimer.
13  * 
14  * Redistributions in binary form must reproduce the above copyright notice, 
15  * this list of conditions and the following disclaimer in the documentation 
16  * and/or other materials provided with the distribution, if any, must include 
17  * the following acknowledgment: "This product includes software developed by 
18  * the University Corporation for Advanced Internet Development 
19  * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement 
20  * may appear in the software itself, if and wherever such third-party 
21  * acknowledgments normally appear.
22  * 
23  * Neither the name of Shibboleth nor the names of its contributors, nor 
24  * Internet2, nor the University Corporation for Advanced Internet Development, 
25  * Inc., nor UCAID may be used to endorse or promote products derived from this 
26  * software without specific prior written permission. For written permission, 
27  * please contact shibboleth@shibboleth.org
28  * 
29  * Products derived from this software may not be called Shibboleth, Internet2, 
30  * UCAID, or the University Corporation for Advanced Internet Development, nor 
31  * may Shibboleth appear in their name, without prior written permission of the 
32  * University Corporation for Advanced Internet Development.
33  * 
34  * 
35  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
36  * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
37  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
38  * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK 
39  * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE. 
40  * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY 
41  * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT, 
42  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
43  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
44  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
47  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  */
49
50 package edu.internet2.middleware.shibboleth.aa.attrresolv;
51
52 import java.io.IOException;
53 import java.io.InputStream;
54 import java.security.Principal;
55 import java.util.ArrayList;
56 import java.util.Arrays;
57 import java.util.HashMap;
58 import java.util.HashSet;
59 import java.util.Iterator;
60 import java.util.List;
61 import java.util.Map;
62 import java.util.Properties;
63 import java.util.Set;
64
65 import javax.naming.directory.Attributes;
66
67 import org.apache.log4j.Logger;
68 import org.apache.xerces.parsers.DOMParser;
69 import org.w3c.dom.Document;
70 import org.w3c.dom.Element;
71 import org.w3c.dom.Node;
72 import org.w3c.dom.NodeList;
73 import org.xml.sax.EntityResolver;
74 import org.xml.sax.ErrorHandler;
75 import org.xml.sax.InputSource;
76 import org.xml.sax.SAXException;
77 import org.xml.sax.SAXParseException;
78
79 import edu.internet2.middleware.shibboleth.aa.attrresolv.ResolverAttributeSet.ResolverAttributeIterator;
80 import edu.internet2.middleware.shibboleth.aa.attrresolv.provider.ValueHandler;
81 import edu.internet2.middleware.shibboleth.common.ShibResource;
82 import edu.internet2.middleware.shibboleth.common.ShibResource.ResourceNotAvailableException;
83
84 /**
85  * An engine for obtaining attribute values for specified principals.  Attributes values are
86  * resolved using a directed graph of pluggable attribute definitions and data connectors.
87  *   
88  * @author Walter Hoehn (wassa@columbia.edu)
89  *
90  */
91
92 public class AttributeResolver {
93
94         private static Logger log = Logger.getLogger(AttributeResolver.class.getName());
95         private HashMap plugIns = new HashMap();
96         private ResolverCache resolverCache = new ResolverCache();
97         public static final String resolverNamespace = "urn:mace:shibboleth:resolver:1.0";
98
99         public AttributeResolver(Properties properties) throws AttributeResolverException {
100                 if (properties
101                         .getProperty("edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver.ResolverConfig")
102                         == null) {
103                         log.error("No Attribute Resolver configuration file specified.");
104                         throw new AttributeResolverException("No Attribute Resolver configuration file specified.");
105                 }
106
107                 String configFile =
108                         properties.getProperty(
109                                 "edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver.ResolverConfig");
110
111                 try {
112                         ShibResource config = new ShibResource(configFile, this.getClass());
113                         DOMParser parser = new DOMParser();
114                         parser.setFeature("http://xml.org/sax/features/validation", true);
115                         parser.setFeature("http://apache.org/xml/features/validation/schema", true);
116                         parser.setEntityResolver(new EntityResolver() {
117                                 public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
118                                         if (systemId.endsWith("shibboleth-resolver-1.0.xsd")) {
119                                                 InputStream stream;
120                                                 try {
121                                                         return new InputSource(
122                                                                 new ShibResource("/schemas/shibboleth-resolver-1.0.xsd", this.getClass())
123                                                                         .getInputStream());
124                                                 } catch (IOException e) {
125                                                         throw new SAXException("Could not load entity: " + e);
126                                                 }
127                                         } else {
128                                                 return null;
129                                         }
130                                 }
131                         });
132
133                         parser.setErrorHandler(new ErrorHandler() {
134                                 public void error(SAXParseException arg0) throws SAXException {
135                                         throw new SAXException("Error parsing xml file: " + arg0);
136                                 }
137                                 public void fatalError(SAXParseException arg0) throws SAXException {
138                                         throw new SAXException("Error parsing xml file: " + arg0);
139                                 }
140                                 public void warning(SAXParseException arg0) throws SAXException {
141                                         throw new SAXException("Error parsing xml file: " + arg0);
142                                 }
143                         });
144                         parser.parse(new InputSource(config.getInputStream()));
145                         loadConfig(parser.getDocument());
146
147                 } catch (ResourceNotAvailableException e) {
148                         log.error("No Attribute Resolver configuration could be loaded from (" + configFile + "): " + e);
149                         throw new AttributeResolverException("No Attribute Resolver configuration found.");
150                 } catch (SAXException e) {
151                         log.error("Error parsing Attribute Resolver Configuration file: " + e);
152                         throw new AttributeResolverException("Error parsing Attribute Resolver Configuration file.");
153                 } catch (IOException e) {
154                         log.error("Error reading Attribute Resolver Configuration file: " + e);
155                         throw new AttributeResolverException("Error reading Attribute Resolver Configuration file.");
156                 }
157         }
158
159         private void loadConfig(Document document) throws AttributeResolverException {
160
161                 log.info("Configuring Attribute Resolver.");
162                 if (!document.getDocumentElement().getTagName().equals("AttributeResolver")) {
163                         log.error("Configuration must include <AttributeResolver> as the root node.");
164                         throw new AttributeResolverException("Cannot load Attribute Resolver.");
165                 }
166
167                 NodeList plugInNodes =
168                         document.getElementsByTagNameNS(resolverNamespace, "AttributeResolver").item(0).getChildNodes();
169                 if (plugInNodes.getLength() <= 0) {
170                         log.error("Configuration inclues no PlugIn definitions.");
171                         throw new AttributeResolverException("Cannot load Attribute Resolver.");
172                 }
173                 for (int i = 0; plugInNodes.getLength() > i; i++) {
174                         if (plugInNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
175                                 try {
176                                         log.info("Found a PlugIn. Loading...");
177                                         ResolutionPlugIn plugIn = ResolutionPlugInFactory.createPlugIn((Element) plugInNodes.item(i));
178                                         registerPlugIn(plugIn, plugIn.getId());
179                                 } catch (DuplicatePlugInException dpe) {
180                                         log.warn("Skipping PlugIn: " + dpe.getMessage());
181                                 } catch (ClassCastException cce) {
182                                         log.error("Problem realizing PlugIn configuration" + cce.getMessage());
183                                 } catch (AttributeResolverException are) {
184                                         log.warn("Skipping PlugIn: " + ((Element) plugInNodes.item(i)).getAttribute("id"));
185                                 }
186                         }
187                 }
188
189                 verifyPlugIns();
190                 log.info("Configuration complete.");
191         }
192
193         private void verifyPlugIns() throws AttributeResolverException {
194                 
195                 log.info("Verifying PlugIn graph consitency.");
196                 Set inconsistent = new HashSet();
197                 Iterator registered = plugIns.keySet().iterator();
198
199                 while (registered.hasNext()) {
200                         ResolutionPlugIn plugIn = lookupPlugIn((String) registered.next());
201                         log.debug("Checking PlugIn (" + plugIn.getId() + ") for consistency.");
202                         if (plugIn instanceof AttributeDefinitionPlugIn) {
203                                 verifyPlugIn((AttributeDefinitionPlugIn) plugIn, new HashSet(), inconsistent);
204                         }
205                 }
206                 
207                 if (!inconsistent.isEmpty()) {
208                         log.info("Unloading inconsistent PlugIns.");
209                         Iterator inconsistentIt = inconsistent.iterator();
210                         while (inconsistentIt.hasNext()) {
211                                 plugIns.remove(inconsistentIt.next());
212                         }
213                 }
214                 
215                 if (plugIns.size() < 1) {
216                         log.error("Failed to load any PlugIn definitions.");
217                         throw new AttributeResolverException("Cannot load Attribute Resolver.");
218                 }
219
220         }
221
222         private void verifyPlugIn(AttributeDefinitionPlugIn plugIn, Set verifyChain, Set inconsistent) {
223
224                 //Short-circuit if we have already found this PlugIn to be inconsistent
225                 if (inconsistent.contains(plugIn.getId())) {
226                         return;
227                 }
228
229                 //Make sure that we don't have a circular dependency
230                 if (verifyChain.contains(plugIn.getId())) {
231                         log.error(
232                                 "The PlugIn (" + plugIn.getId() + ") is inconsistent.  It is involved in a circular dependency chain.");
233                         inconsistent.add(plugIn.getId());
234                         return;
235                 }
236
237                 //Make sure all dependent Data Connectors are registered
238                 List depends = new ArrayList();
239                 depends.addAll(Arrays.asList(plugIn.getDataConnectorDependencyIds()));
240                 Iterator dependsIt = depends.iterator();
241                 while (dependsIt.hasNext()) {
242                         String key = (String) dependsIt.next();
243                         if (!plugIns.containsKey(key)) {
244                                 log.error(
245                                         "The PlugIn ("
246                                                 + plugIn.getId()
247                                                 + ") is inconsistent.  It depends on a PlugIn ("
248                                                 + key
249                                                 + ") that is not registered.");
250                                 inconsistent.add(plugIn.getId());
251                                 return;
252                         }
253                         
254                         ResolutionPlugIn dependent = lookupPlugIn(key);
255                         if (!(dependent instanceof DataConnectorPlugIn)) {
256                                 log.error(
257                                         "The PlugIn ("
258                                                 + plugIn.getId()
259                                                 + ") is inconsistent.  It depends on a PlugIn ("
260                                                 + key
261                                                 + ") that is mislabeled as an DataConnectorPlugIn.");
262                                 inconsistent.add(plugIn.getId());
263                                 return;
264                         }
265                 }
266
267                 //Recursively go through all AttributeDefinition dependencies and make sure all are registered and consistent.
268                 depends.clear();
269                 depends.addAll(Arrays.asList(plugIn.getAttributeDefinitionDependencyIds()));
270                 dependsIt = depends.iterator();
271                 while (dependsIt.hasNext()) {
272                         String key = (String) dependsIt.next();
273
274                         if (!plugIns.containsKey(key)) {
275                                 log.error(
276                                         "The PlugIn ("
277                                                 + plugIn.getId()
278                                                 + ") is inconsistent.  It depends on a PlugIn ("
279                                                 + key
280                                                 + ") that is not registered.");
281                                 inconsistent.add(plugIn.getId());
282                                 return;
283                         }
284
285                         ResolutionPlugIn dependent = lookupPlugIn(key);
286                         if (!(dependent instanceof AttributeDefinitionPlugIn)) {
287                                 log.error(
288                                         "The PlugIn ("
289                                                 + plugIn.getId()
290                                                 + ") is inconsistent.  It depends on a PlugIn ("
291                                                 + key
292                                                 + ") that is mislabeled as an AttributeDefinitionPlugIn.");
293                                 inconsistent.add(plugIn.getId());
294                                 return;
295                         }
296                         
297                         verifyChain.add(plugIn.getId());
298                         verifyPlugIn((AttributeDefinitionPlugIn) dependent, verifyChain, inconsistent);
299
300                         if (inconsistent.contains(key)) {
301                                 log.error(
302                                         "The PlugIn ("
303                                                 + plugIn.getId()
304                                                 + ") is inconsistent.  It depends on a PlugIn ("
305                                                 + key
306                                                 + ") that is not inconsistent.");
307                                 inconsistent.add(plugIn.getId());
308                                 return;
309                         }
310                 }
311         }
312
313         private void registerPlugIn(ResolutionPlugIn connector, String id) throws DuplicatePlugInException {
314
315                 if (plugIns.containsKey(id)) {
316                         log.error("A PlugIn is already registered with the Id (" + id + ").");
317                         throw new DuplicatePlugInException("Found a duplicate PlugIn Id.");
318                 }
319                 plugIns.put(id, connector);
320                 log.info("Registered PlugIn: (" + id + ")");
321
322         }
323
324         private ResolutionPlugIn lookupPlugIn(String id) {
325                 return (ResolutionPlugIn) plugIns.get(id);
326         }
327
328         /**
329          * Resolve a set of attributes for a particular principal and requester.
330          * 
331          * @param principal the <code>Principal</code> for which the attributes should be resolved
332          * @param requester the name of the requesting entity
333          * @param attributes the set of attributes to be resolved
334          */
335         public void resolveAttributes(Principal principal, String requester, ResolverAttributeSet attributes) {
336
337                 HashMap requestCache = new HashMap();
338                 ResolverAttributeIterator iterator = attributes.resolverAttributeIterator();
339
340                 while (iterator.hasNext()) {
341                         ResolverAttribute attribute = iterator.nextResolverAttribute();
342                         try {
343                                 if (lookupPlugIn(attribute.getName()) == null) {
344                                         log.warn("No PlugIn registered for attribute: (" + attribute.getName() + ")");
345                                         iterator.remove();
346                                 } else {
347                                         log.info("Resolving attribute: (" + attribute.getName() + ")");
348                                         if (attribute.resolved()) {
349                                                 log.debug(
350                                                         "Attribute ("
351                                                                 + attribute.getName()
352                                                                 + ") already resolved for this request.  No need for further resolution.");
353
354                                         } else {
355                                                 resolveAttribute(attribute, principal, requester, requestCache, attributes);
356                                         }
357
358                                         if (!attribute.hasValues()) {
359                                                 iterator.remove();
360                                         }
361                                 }
362                         } catch (ResolutionPlugInException rpe) {
363                                 log.error("Problem encountered while resolving attribute: (" + attribute.getName() + "): " + rpe);
364                                 iterator.remove();
365                         }
366                 }
367         }
368
369         private void resolveAttribute(
370                 ResolverAttribute attribute,
371                 Principal principal,
372                 String requester,
373                 Map requestCache,
374                 ResolverAttributeSet requestedAttributes)
375                 throws ResolutionPlugInException {
376
377                 AttributeDefinitionPlugIn currentDefinition = (AttributeDefinitionPlugIn) lookupPlugIn(attribute.getName());
378
379                 //Check to see if we have already resolved the attribute during this request
380                 if (requestCache.containsKey(currentDefinition.getId())) {
381                         log.debug(
382                                 "Attribute ("
383                                         + currentDefinition.getId()
384                                         + ") already resolved for this request, using cached version");
385                         attribute.resolveFromCached((ResolverAttribute) requestCache.get(currentDefinition.getId()));
386                         return;
387                 }
388
389                 //Check to see if we have a cached resolution for this attribute
390                 if (currentDefinition.getTTL() > 0) {
391                         ResolverAttribute cachedAttribute =
392                                 resolverCache.getResolvedAttribute(principal, currentDefinition.getId());
393                         if (cachedAttribute != null) {
394                                 log.debug(
395                                         "Attribute ("
396                                                 + currentDefinition.getId()
397                                                 + ") resolution cached from a previous request, using cached version");
398                                 attribute.resolveFromCached(cachedAttribute);
399                                 return;
400                         }
401                 }
402
403                 //Resolve all attribute dependencies
404                 String[] attributeDependencies = currentDefinition.getAttributeDefinitionDependencyIds();
405                 Dependencies depends = new Dependencies();
406
407                 boolean dependancyOnly = false;
408                 for (int i = 0; attributeDependencies.length > i; i++) {
409                         log.debug(
410                                 "Attribute (" + attribute.getName() + ") depends on attribute (" + attributeDependencies[i] + ").");
411                         ResolverAttribute dependant = requestedAttributes.getByName(attributeDependencies[i]);
412                         if (dependant == null) {
413                                 dependancyOnly = true;
414                                 dependant = new DependentOnlyResolutionAttribute(attributeDependencies[i]);
415                         }
416                         resolveAttribute(dependant, principal, requester, requestCache, requestedAttributes);
417                         depends.addAttributeResolution(attributeDependencies[i], dependant);
418
419                 }
420
421                 //Resolve all connector dependencies
422                 String[] connectorDependencies = currentDefinition.getDataConnectorDependencyIds();
423                 for (int i = 0; connectorDependencies.length > i; i++) {
424                         log.debug(
425                                 "Attribute (" + attribute.getName() + ") depends on connector (" + connectorDependencies[i] + ").");
426                         //Check to see if we have already resolved the connector during this request
427                         if (requestCache.containsKey(connectorDependencies[i])) {
428                                 log.debug(
429                                         "Connector ("
430                                                 + connectorDependencies[i]
431                                                 + ") already resolved for this request, using cached version");
432                                 depends.addConnectorResolution(
433                                         connectorDependencies[i],
434                                         (Attributes) requestCache.get(connectorDependencies[i]));
435                         } else {
436                                 //Check to see if we have a cached resolution for this attribute
437                                 if (((DataConnectorPlugIn) lookupPlugIn(connectorDependencies[i])).getTTL() > 0) {
438                                         Attributes cachedAttributes =
439                                                 resolverCache.getResolvedConnector(principal, connectorDependencies[i]);
440                                         if (cachedAttributes != null) {
441                                                 log.debug(
442                                                         "Connector ("
443                                                                 + connectorDependencies[i]
444                                                                 + ") resolution cached from a previous request, using cached version");
445                                                 depends.addConnectorResolution(connectorDependencies[i], cachedAttributes);
446                                         }
447                                 }
448
449                                 Attributes resolvedConnector =
450                                         ((DataConnectorPlugIn) lookupPlugIn(connectorDependencies[i])).resolve(principal);
451                                 requestCache.put(connectorDependencies[i], resolvedConnector);
452                                 depends.addConnectorResolution(connectorDependencies[i], resolvedConnector);
453
454                                 //Add attribute resolution to cache
455                                 if (((DataConnectorPlugIn) lookupPlugIn(connectorDependencies[i])).getTTL() > 0) {
456                                         resolverCache.cacheConnectorResolution(
457                                                 principal,
458                                                 connectorDependencies[i],
459                                                 ((DataConnectorPlugIn) lookupPlugIn(connectorDependencies[i])).getTTL(),
460                                                 resolvedConnector);
461                                 }
462                         }
463                 }
464
465                 //Resolve the attribute
466                 currentDefinition.resolve(attribute, principal, requester, depends);
467
468                 //If necessary, cache for this request
469                 if (dependancyOnly || !attribute.hasValues()) {
470                         requestCache.put(currentDefinition.getId(), attribute);
471                 }
472
473                 //Add attribute resolution to cache
474                 if (currentDefinition.getTTL() > 0) {
475                         resolverCache.cacheAttributeResolution(
476                                 principal,
477                                 attribute.getName(),
478                                 currentDefinition.getTTL(),
479                                 attribute);
480                 }
481         }
482
483         private class DuplicatePlugInException extends Exception {
484                 public DuplicatePlugInException(String message) {
485                         super(message);
486                 }
487         }
488
489         class DependentOnlyResolutionAttribute implements ResolverAttribute {
490                 String name;
491                 ArrayList values = new ArrayList();
492                 boolean resolved = false;
493
494                 DependentOnlyResolutionAttribute(String name) {
495                         this.name = name;
496                 }
497
498                 public String getName() {
499                         return name;
500                 }
501
502                 public boolean resolved() {
503                         return resolved;
504                 }
505
506                 public void setResolved() {
507                         resolved = true;
508                 }
509
510                 public void resolveFromCached(ResolverAttribute attribute) {
511                 }
512
513                 public void setLifetime(long lifetime) {
514                 }
515
516                 public long getLifetime() {
517                         return 0;
518                 }
519
520                 public void addValue(Object value) {
521                         values.add(value);
522                 }
523
524                 public Iterator getValues() {
525                         return values.iterator();
526                 }
527
528                 public boolean hasValues() {
529                         if (values.isEmpty()) {
530                                 return false;
531                         }
532                         return true;
533                 }
534
535                 public void registerValueHandler(ValueHandler handler) {
536                 }
537
538                 public ValueHandler getRegisteredValueHandler() {
539                         return null;
540                 }
541         }
542
543 }