Implement metadata lookup by artifact, refactored metadata indexing.
authorScott Cantor <cantor.2@osu.edu>
Thu, 3 Aug 2006 01:00:26 +0000 (01:00 +0000)
committerScott Cantor <cantor.2@osu.edu>
Thu, 3 Aug 2006 01:00:26 +0000 (01:00 +0000)
saml/SAMLArtifact.h
saml/saml1/core/SAMLArtifactType0001.h
saml/saml2/core/SAML2ArtifactType0004.h
saml/saml2/metadata/MetadataProvider.h
saml/saml2/metadata/impl/FilesystemMetadataProvider.cpp
saml/saml2/metadata/impl/MetadataProvider.cpp
samltest/SAMLArtifactType0002Test.h
samltest/saml2/metadata/FilesystemMetadataProviderTest.h

index 5281205..18ebcaf 100644 (file)
@@ -63,7 +63,7 @@ namespace opensaml {
 \r
         /**\r
          * Returns the binary type code of the artifact.\r
-         * The result is NOT null-terminated.\r
+         * The result MAY contain embedded null characters.\r
          * \r
          * @return the binary type code\r
          */\r
@@ -73,7 +73,7 @@ namespace opensaml {
         \r
         /**\r
          * Returns the binary artifact data following the type code.\r
-         * The result is NOT null-terminated.\r
+         * The result MAY contain embedded null characters.\r
          * \r
          * @return the binary artifact data\r
          */\r
@@ -82,18 +82,18 @@ namespace opensaml {
         }\r
         \r
         /**\r
-         * Returns the binary data that identifies the source of the artifact.\r
-         * The exact form this takes depends on the type.\r
-         * The result is NOT null-terminated.\r
+         * Returns a string that identifies the source of the artifact.\r
+         * The exact form this takes depends on the type but should match\r
+         * the syntax needed for metadata lookup.\r
          * \r
-         * @return the binary source data\r
+         * @return null-terminated source string\r
          */\r
         virtual std::string getSource() const=0;\r
         \r
         /**\r
          * Returns the binary data that references the message (2.0) or assertion (1.x)\r
          * The exact form this takes depends on the type.\r
-         * The result is NOT null-terminated.\r
+         * The result MAY contain embedded null characters.\r
          * \r
          * @return the binary reference data\r
          */\r
index d61020c..2fdf887 100644 (file)
@@ -64,6 +64,16 @@ namespace opensaml {
             }\r
             \r
             virtual std::string getSource() const {\r
+                return toHex(getSourceID());\r
+            }\r
+\r
+            /**\r
+             * Returns the binary data that identifies the source.\r
+             * The result MAY contain embedded null characters.\r
+             * \r
+             * @return the binary source ID\r
+             */\r
+            virtual std::string getSourceID() const {\r
                 return m_raw.substr(TYPECODE_LENGTH,SOURCEID_LENGTH);                   // bytes 3-22\r
             }\r
             \r
index 7a0375c..caa429d 100644 (file)
@@ -66,6 +66,16 @@ namespace opensaml {
             }\r
             \r
             virtual std::string getSource() const {\r
+                return toHex(getSourceID());\r
+            }\r
+\r
+            /**\r
+             * Returns the binary data that identifies the source.\r
+             * The result MAY contain embedded null characters.\r
+             * \r
+             * @return the binary source ID\r
+             */\r
+            virtual std::string getSourceID() const {\r
                 return m_raw.substr(TYPECODE_LENGTH + INDEX_LENGTH, SOURCEID_LENGTH);    // bytes 5-24\r
             }\r
             \r
index 4e24bd7..7f5d386 100644 (file)
@@ -27,6 +27,8 @@
 #include <saml/saml2/metadata/MetadataFilter.h>
 
 namespace opensaml {
+    
+    class SAML_API SAMLArtifact;
 
     namespace saml2md {
         
@@ -34,7 +36,8 @@ namespace opensaml {
          * Supplies an individual source of metadata.
          * 
          * The source can be a local file, remote service, or the result of a
-         * dynamic lookup, can include local caching, etc.
+         * dynamic lookup, can include local caching, etc. Providers
+         * <strong>MUST</strong> be locked before any lookup operations.
          */
         class SAML_API MetadataProvider : public virtual xmltooling::Lockable
         {
@@ -119,7 +122,7 @@ namespace opensaml {
              * 
              * @return the entity's metadata or NULL if there is no metadata or no valid metadata
              */
-            virtual const EntityDescriptor* getEntityDescriptor(const XMLCh* id, bool requireValidMetadata=true) const=0;
+            virtual const EntityDescriptor* getEntityDescriptor(const XMLCh* id, bool requireValidMetadata=true) const;
 
             /**
              * Gets the metadata for a given entity. If a valid entity is returned,
@@ -131,7 +134,18 @@ namespace opensaml {
              * 
              * @return the entity's metadata or NULL if there is no metadata or no valid metadata
              */
-            virtual const EntityDescriptor* getEntityDescriptor(const char* id, bool requireValidMetadata=true) const=0;
+            virtual const EntityDescriptor* getEntityDescriptor(const char* id, bool requireValidMetadata=true) const;
+
+            /**
+             * Gets the metadata for an entity that issued a SAML artifact. If a valid entity is returned,
+             * the provider will be left in a locked state. The caller MUST unlock the
+             * provider when finished with the entity.
+             *  
+             * @param artifact              a SAML artifact to find the issuer of
+             * 
+             * @return the entity's metadata or NULL if there is no valid metadata
+             */
+            virtual const EntityDescriptor* getEntityDescriptor(const SAMLArtifact* artifact) const;
 
             /**
              * Gets the metadata for a given group of entities. If a valid group is returned,
@@ -143,7 +157,7 @@ namespace opensaml {
              * 
              * @return the group's metadata or NULL if there is no metadata or no valid metadata
              */
-            virtual const EntitiesDescriptor* getEntitiesDescriptor(const XMLCh* name, bool requireValidMetadata=true) const=0;
+            virtual const EntitiesDescriptor* getEntitiesDescriptor(const XMLCh* name, bool requireValidMetadata=true) const;
 
             /**
              * Gets the metadata for a given group of entities. If a valid group is returned,
@@ -155,7 +169,7 @@ namespace opensaml {
              * 
              * @return the group's metadata or NULL if there is no metadata or no valid metadata
              */
-            virtual const EntitiesDescriptor* getEntitiesDescriptor(const char* name, bool requireValidMetadata=true) const=0;
+            virtual const EntitiesDescriptor* getEntitiesDescriptor(const char* name, bool requireValidMetadata=true) const;
 
         protected:
             /**
@@ -164,9 +178,37 @@ namespace opensaml {
              * @param xmlObject the metadata to be filtered
              */
             void doFilters(xmltooling::XMLObject& xmlObject) const;
+
+            /**
+             * Loads an entity into the cache for faster lookup. This includes
+             * processing known reverse lookup strategies for artifacts.
+             * 
+             * @param site          entity definition
+             * @param validUntil    expiration time of the entity definition
+             */
+            virtual void index(EntityDescriptor* site, time_t validUntil);
+
+            /**
+             * Loads a group of entities into the cache for faster lookup.
+             * 
+             * @param group         group definition
+             * @param validUntil    expiration time of the group definition
+             */
+            virtual void index(EntitiesDescriptor* group, time_t validUntil);
+        
+            /**
+             * Clear the cache of known entities and groups.
+             */
+            virtual void clearIndex();
         
         private:
             std::vector<MetadataFilter*> m_filters;
+
+            typedef std::multimap<std::string,const EntityDescriptor*> sitemap_t;
+            typedef std::multimap<std::string,const EntitiesDescriptor*> groupmap_t;
+            sitemap_t m_sites;
+            sitemap_t m_sources;
+            groupmap_t m_groups;
         };
         
         /**
index e9d3dbf..51c2b83 100644 (file)
@@ -55,10 +55,6 @@ namespace opensaml {
 
             void init();
 
-            const EntityDescriptor* getEntityDescriptor(const XMLCh* id, bool requireValidMetadata=true) const;
-            const EntityDescriptor* getEntityDescriptor(const char* id, bool requireValidMetadata=true) const;
-            const EntitiesDescriptor* getEntitiesDescriptor(const XMLCh* name, bool requireValidMetadata=true) const;
-            const EntitiesDescriptor* getEntitiesDescriptor(const char* name, bool requireValidMetadata=true) const;
             const XMLObject* getMetadata() const {
                 return m_object;
             }
@@ -66,16 +62,7 @@ namespace opensaml {
         private:
             XMLObject* load() const;
             void index();
-            void index(EntityDescriptor* site, time_t validUntil=SAMLTIME_MAX);
-            void index(EntitiesDescriptor* group, time_t validUntil=SAMLTIME_MAX);
         
-            // index of loaded metadata
-            typedef multimap<string,const EntityDescriptor*> sitemap_t;
-            typedef multimap<string,const EntitiesDescriptor*> groupmap_t;
-            sitemap_t m_sites;
-            sitemap_t m_sources;
-            groupmap_t m_groups;
-
             const DOMElement* m_root; // survives only until init() method is done
             std::string m_source;
             time_t m_filestamp;
@@ -263,87 +250,11 @@ Lockable* FilesystemMetadataProvider::lock()
 
 void FilesystemMetadataProvider::index()
 {
-    m_sources.clear();
-    m_sites.clear();
-    m_groups.clear();
-    
     EntitiesDescriptor* group=dynamic_cast<EntitiesDescriptor*>(m_object);
     if (group) {
-        index(group);
+        MetadataProvider::index(group, SAMLTIME_MAX);
         return;
     }
     EntityDescriptor* site=dynamic_cast<EntityDescriptor*>(m_object);
-    index(site);
-}
-
-void FilesystemMetadataProvider::index(EntityDescriptor* site, time_t validUntil)
-{
-    if (validUntil < site->getValidUntilEpoch())
-        site->setValidUntil(validUntil);
-
-    auto_ptr_char id(site->getEntityID());
-    if (id.get()) {
-        m_sites.insert(make_pair(id.get(),site));
-    }
-}
-
-void FilesystemMetadataProvider::index(EntitiesDescriptor* group, time_t validUntil)
-{
-    if (validUntil < group->getValidUntilEpoch())
-        group->setValidUntil(validUntil);
-
-    auto_ptr_char name(group->getName());
-    if (name.get()) {
-        m_groups.insert(make_pair(name.get(),group));
-    }
-    
-    const vector<EntitiesDescriptor*>& groups=const_cast<const EntitiesDescriptor*>(group)->getEntitiesDescriptors();
-    for (vector<EntitiesDescriptor*>::const_iterator i=groups.begin(); i!=groups.end(); i++)
-        index(*i,group->getValidUntilEpoch());
-
-    const vector<EntityDescriptor*>& sites=const_cast<const EntitiesDescriptor*>(group)->getEntityDescriptors();
-    for (vector<EntityDescriptor*>::const_iterator j=sites.begin(); j!=sites.end(); j++)
-        index(*j,group->getValidUntilEpoch());
-}
-
-const EntitiesDescriptor* FilesystemMetadataProvider::getEntitiesDescriptor(const char* name, bool strict) const
-{
-    pair<groupmap_t::const_iterator,groupmap_t::const_iterator> range=m_groups.equal_range(name);
-
-    time_t now=time(NULL);
-    for (groupmap_t::const_iterator i=range.first; i!=range.second; i++)
-        if (now < i->second->getValidUntilEpoch())
-            return i->second;
-    
-    if (!strict && range.first!=range.second)
-        return range.first->second;
-        
-    return NULL;
-}
-
-const EntitiesDescriptor* FilesystemMetadataProvider::getEntitiesDescriptor(const XMLCh* name, bool strict) const
-{
-    auto_ptr_char temp(name);
-    return getEntitiesDescriptor(temp.get(),strict);
-}
-
-const EntityDescriptor* FilesystemMetadataProvider::getEntityDescriptor(const char* name, bool strict) const
-{
-    pair<sitemap_t::const_iterator,sitemap_t::const_iterator> range=m_sites.equal_range(name);
-
-    time_t now=time(NULL);
-    for (sitemap_t::const_iterator i=range.first; i!=range.second; i++)
-        if (now < i->second->getValidUntilEpoch())
-            return i->second;
-    
-    if (!strict && range.first!=range.second)
-        return range.first->second;
-        
-    return NULL;
-}
-
-const EntityDescriptor* FilesystemMetadataProvider::getEntityDescriptor(const XMLCh* name, bool strict) const
-{
-    auto_ptr_char temp(name);
-    return getEntityDescriptor(temp.get(),strict);
+    MetadataProvider::index(site, SAMLTIME_MAX);
 }
index b0bf178..2a7052e 100644 (file)
  */
 
 #include "internal.h"
+#include "SAMLArtifact.h"
 #include "saml2/metadata/MetadataProvider.h"
 
 #include <log4cpp/Category.hh>
 #include <xmltooling/util/NDC.h>
 
 using namespace opensaml::saml2md;
+using namespace opensaml;
 using namespace xmltooling;
 using namespace log4cpp;
 using namespace std;
 
+namespace opensaml {
+    namespace saml2md {
+        SAML_DLLLOCAL PluginManager<MetadataProvider,const DOMElement*>::Factory FilesystemMetadataProviderFactory; 
+        SAML_DLLLOCAL PluginManager<MetadataFilter,const DOMElement*>::Factory BlacklistMetadataFilterFactory; 
+        SAML_DLLLOCAL PluginManager<MetadataFilter,const DOMElement*>::Factory WhitelistMetadataFilterFactory; 
+    };
+};
+
+void SAML_API opensaml::saml2md::registerMetadataProviders()
+{
+    SAMLConfig& conf=SAMLConfig::getConfig();
+    conf.MetadataProviderManager.registerFactory(FILESYSTEM_METADATA_PROVIDER, FilesystemMetadataProviderFactory);
+    conf.MetadataProviderManager.registerFactory("edu.internet2.middleware.shibboleth.metadata.provider.XMLMetadata", FilesystemMetadataProviderFactory);
+    conf.MetadataProviderManager.registerFactory("edu.internet2.middleware.shibboleth.common.provider.XMLMetadata", FilesystemMetadataProviderFactory);
+}
+
+void SAML_API opensaml::saml2md::registerMetadataFilters()
+{
+    SAMLConfig::getConfig().MetadataFilterManager.registerFactory(BLACKLIST_METADATA_FILTER, BlacklistMetadataFilterFactory);
+    SAMLConfig::getConfig().MetadataFilterManager.registerFactory(WHITELIST_METADATA_FILTER, WhitelistMetadataFilterFactory);
+}
+
 static const XMLCh Blacklist[] =                    UNICODE_LITERAL_23(B,l,a,c,k,l,i,s,t,M,e,t,a,d,a,t,a,F,i,l,t,e,r);
 static const XMLCh Exclude[] =                      UNICODE_LITERAL_7(E,x,c,l,u,d,e);
 static const XMLCh Include[] =                      UNICODE_LITERAL_7(I,n,c,l,u,d,e);
@@ -93,24 +117,137 @@ void MetadataProvider::doFilters(XMLObject& xmlObject) const
     }
 }
 
-namespace opensaml {
-    namespace saml2md {
-        SAML_DLLLOCAL PluginManager<MetadataProvider,const DOMElement*>::Factory FilesystemMetadataProviderFactory; 
-        SAML_DLLLOCAL PluginManager<MetadataFilter,const DOMElement*>::Factory BlacklistMetadataFilterFactory; 
-        SAML_DLLLOCAL PluginManager<MetadataFilter,const DOMElement*>::Factory WhitelistMetadataFilterFactory; 
-    };
-};
+void MetadataProvider::index(EntityDescriptor* site, time_t validUntil)
+{
+    if (validUntil < site->getValidUntilEpoch())
+        site->setValidUntil(validUntil);
 
-void SAML_API opensaml::saml2md::registerMetadataProviders()
+    auto_ptr_char id(site->getEntityID());
+    if (id.get()) {
+        m_sites.insert(make_pair(id.get(),site));
+    }
+    
+    // Process each IdP role.
+    const vector<IDPSSODescriptor*>& roles=const_cast<const EntityDescriptor*>(site)->getIDPSSODescriptors();
+    for (vector<IDPSSODescriptor*>::const_iterator i=roles.begin(); i!=roles.end(); i++) {
+        // SAML 1.x?
+        if ((*i)->hasSupport(SAMLConstants::SAML10_PROTOCOL_ENUM) || (*i)->hasSupport(SAMLConstants::SAML11_PROTOCOL_ENUM)) {
+            // Check for SourceID extension element.
+            const Extensions* exts=(*i)->getExtensions();
+            if (exts) {
+                const list<XMLObject*>& children=exts->getXMLObjects();
+                for (list<XMLObject*>::const_iterator ext=children.begin(); ext!=children.end(); ext++) {
+                    SourceID* sid=dynamic_cast<SourceID*>(*ext);
+                    if (sid) {
+                        auto_ptr_char sourceid(sid->getID());
+                        if (sourceid.get()) {
+                            m_sources.insert(pair<string,const EntityDescriptor*>(sourceid.get(),site));
+                            break;
+                        }
+                    }
+                }
+            }
+            
+            // Hash the ID.
+            m_sources.insert(
+                pair<string,const EntityDescriptor*>(SAMLConfig::getConfig().hashSHA1(id.get(), true),site)
+                );
+                
+            // Load endpoints for type 0x0002 artifacts.
+            const vector<ArtifactResolutionService*>& locs=const_cast<const IDPSSODescriptor*>(*i)->getArtifactResolutionServices();
+            for (vector<ArtifactResolutionService*>::const_iterator loc=locs.begin(); loc!=locs.end(); loc++) {
+                auto_ptr_char location((*loc)->getLocation());
+                if (location.get())
+                    m_sources.insert(pair<string,const EntityDescriptor*>(location.get(),site));
+            }
+        }
+        
+        // SAML 2.0?
+        if ((*i)->hasSupport(SAMLConstants::SAML20P_NS)) {
+            // Hash the ID.
+            m_sources.insert(
+                pair<string,const EntityDescriptor*>(SAMLConfig::getConfig().hashSHA1(id.get(), true),site)
+                );
+        }
+    }
+}
+
+void MetadataProvider::index(EntitiesDescriptor* group, time_t validUntil)
 {
-    SAMLConfig& conf=SAMLConfig::getConfig();
-    conf.MetadataProviderManager.registerFactory(FILESYSTEM_METADATA_PROVIDER, FilesystemMetadataProviderFactory);
-    conf.MetadataProviderManager.registerFactory("edu.internet2.middleware.shibboleth.metadata.provider.XMLMetadata", FilesystemMetadataProviderFactory);
-    conf.MetadataProviderManager.registerFactory("edu.internet2.middleware.shibboleth.common.provider.XMLMetadata", FilesystemMetadataProviderFactory);
+    if (validUntil < group->getValidUntilEpoch())
+        group->setValidUntil(validUntil);
+
+    auto_ptr_char name(group->getName());
+    if (name.get()) {
+        m_groups.insert(make_pair(name.get(),group));
+    }
+    
+    const vector<EntitiesDescriptor*>& groups=const_cast<const EntitiesDescriptor*>(group)->getEntitiesDescriptors();
+    for (vector<EntitiesDescriptor*>::const_iterator i=groups.begin(); i!=groups.end(); i++)
+        index(*i,group->getValidUntilEpoch());
+
+    const vector<EntityDescriptor*>& sites=const_cast<const EntitiesDescriptor*>(group)->getEntityDescriptors();
+    for (vector<EntityDescriptor*>::const_iterator j=sites.begin(); j!=sites.end(); j++)
+        index(*j,group->getValidUntilEpoch());
 }
 
-void SAML_API opensaml::saml2md::registerMetadataFilters()
+void MetadataProvider::clearIndex()
 {
-    SAMLConfig::getConfig().MetadataFilterManager.registerFactory(BLACKLIST_METADATA_FILTER, BlacklistMetadataFilterFactory);
-    SAMLConfig::getConfig().MetadataFilterManager.registerFactory(WHITELIST_METADATA_FILTER, WhitelistMetadataFilterFactory);
+    m_sources.clear();
+    m_sites.clear();
+    m_groups.clear();
+}
+
+const EntitiesDescriptor* MetadataProvider::getEntitiesDescriptor(const char* name, bool strict) const
+{
+    pair<groupmap_t::const_iterator,groupmap_t::const_iterator> range=m_groups.equal_range(name);
+
+    time_t now=time(NULL);
+    for (groupmap_t::const_iterator i=range.first; i!=range.second; i++)
+        if (now < i->second->getValidUntilEpoch())
+            return i->second;
+    
+    if (!strict && range.first!=range.second)
+        return range.first->second;
+        
+    return NULL;
+}
+
+const EntitiesDescriptor* MetadataProvider::getEntitiesDescriptor(const XMLCh* name, bool strict) const
+{
+    auto_ptr_char temp(name);
+    return getEntitiesDescriptor(temp.get(),strict);
+}
+
+const EntityDescriptor* MetadataProvider::getEntityDescriptor(const char* name, bool strict) const
+{
+    pair<sitemap_t::const_iterator,sitemap_t::const_iterator> range=m_sites.equal_range(name);
+
+    time_t now=time(NULL);
+    for (sitemap_t::const_iterator i=range.first; i!=range.second; i++)
+        if (now < i->second->getValidUntilEpoch())
+            return i->second;
+    
+    if (!strict && range.first!=range.second)
+        return range.first->second;
+        
+    return NULL;
+}
+
+const EntityDescriptor* MetadataProvider::getEntityDescriptor(const XMLCh* name, bool strict) const
+{
+    auto_ptr_char temp(name);
+    return getEntityDescriptor(temp.get(),strict);
+}
+
+const EntityDescriptor* MetadataProvider::getEntityDescriptor(const SAMLArtifact* artifact) const
+{
+    pair<sitemap_t::const_iterator,sitemap_t::const_iterator> range=m_sources.equal_range(artifact->getSource());
+
+    time_t now=time(NULL);
+    for (sitemap_t::const_iterator i=range.first; i!=range.second; i++)
+        if (now < i->second->getValidUntilEpoch())
+            return i->second;
+
+    return NULL;
 }
index 8556105..45365d8 100644 (file)
@@ -34,7 +34,7 @@ public:
         auto_ptr<SAMLArtifactType0002> artifact(new SAMLArtifactType0002(providerIdStr));\r
         auto_ptr<SAMLArtifact> tempArtifact(SAMLArtifact::parse(artifact->encode().c_str()));\r
         \r
-        TS_ASSERT_SAME_DATA(artifact->getSource().c_str(),tempArtifact->getSource().c_str(),artifact->getSource().length());\r
+        TS_ASSERT_EQUALS(artifact->getSource(),tempArtifact->getSource());\r
         TS_ASSERT_EQUALS(artifact->getMessageHandle(),tempArtifact->getMessageHandle());\r
 \r
         TS_ASSERT_THROWS(auto_ptr<SAMLArtifact> bogus1(new SAMLArtifactType0002(providerIdStr, artifact->getMessageHandle() + artifact->getMessageHandle())), ArtifactException);\r
index a6da0a7..a9c1f83 100644 (file)
  */\r
 \r
 #include "internal.h"\r
+#include <saml/saml2/core/SAML2ArtifactType0004.h>\r
 #include <saml/saml2/metadata/MetadataProvider.h>\r
 \r
 using namespace opensaml::saml2md;\r
+using namespace opensaml::saml2p;\r
 \r
 class FilesystemMetadataProviderTest : public CxxTest::TestSuite, public SAMLObjectBaseTestCase {\r
     XMLCh* entityID;\r
@@ -66,6 +68,13 @@ public:
         TSM_ASSERT_EQUALS("Unexpected number of roles", 1, descriptor->getIDPSSODescriptors().size());\r
         TSM_ASSERT("Role lookup failed", descriptor->getIDPSSODescriptor(supportedProtocol)!=NULL);\r
         TSM_ASSERT("Role lookup failed", descriptor->getIDPSSODescriptor(supportedProtocol2)!=NULL);\r
+\r
+        auto_ptr<SAML2ArtifactType0004> artifact(\r
+            new SAML2ArtifactType0004(SAMLConfig::getConfig().hashSHA1("urn:mace:incommon:washington.edu"),1)\r
+            );\r
+        descriptor = metadataProvider->getEntityDescriptor(artifact.get());\r
+        TSM_ASSERT("Retrieved entity descriptor was null", descriptor!=NULL);\r
+        assertEquals("Entity's ID does not match requested ID", entityID, descriptor->getEntityID());\r
     }\r
 \r
     void testFilesystemWithBlacklists() {\r