2 * Copyright 2001-2009 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * DynamicMetadataProvider.cpp
20 * Simple implementation of a dynamic caching MetadataProvider.
24 #include "binding/SAMLArtifact.h"
25 #include "saml2/metadata/Metadata.h"
26 #include "saml2/metadata/DynamicMetadataProvider.h"
28 #include <xercesc/framework/Wrapper4InputSource.hpp>
29 #include <xercesc/util/XMLUniDefs.hpp>
30 #include <xmltooling/logging.h>
31 #include <xmltooling/util/XMLHelper.h>
32 #include <xmltooling/validation/ValidatorSuite.h>
34 using namespace opensaml::saml2md;
35 using namespace xmltooling::logging;
36 using namespace xmltooling;
40 # define min(a,b) (((a) < (b)) ? (a) : (b))
43 static const XMLCh maxCacheDuration[] = UNICODE_LITERAL_16(m,a,x,C,a,c,h,e,D,u,r,a,t,i,o,n);
44 static const XMLCh validate[] = UNICODE_LITERAL_8(v,a,l,i,d,a,t,e);
48 MetadataProvider* SAML_DLLLOCAL DynamicMetadataProviderFactory(const DOMElement* const & e)
50 return new DynamicMetadataProvider(e);
55 DynamicMetadataProvider::DynamicMetadataProvider(const DOMElement* e)
56 : AbstractMetadataProvider(e), m_maxCacheDuration(28800), m_lock(RWLock::create())
58 const XMLCh* flag=e ? e->getAttributeNS(NULL,validate) : NULL;
59 m_validate=(XMLString::equals(flag,xmlconstants::XML_TRUE) || XMLString::equals(flag,xmlconstants::XML_ONE));
60 flag = e ? e->getAttributeNS(NULL,maxCacheDuration) : NULL;
62 m_maxCacheDuration = XMLString::parseInt(flag);
63 if (m_maxCacheDuration == 0)
64 m_maxCacheDuration = 28800;
68 DynamicMetadataProvider::~DynamicMetadataProvider()
70 // Each entity in the map is unique (no multimap semantics), so this is safe.
71 clearDescriptorIndex(true);
75 pair<const EntityDescriptor*,const RoleDescriptor*> DynamicMetadataProvider::getEntityDescriptor(const Criteria& criteria) const
77 // Check cache while holding the read lock.
78 pair<const EntityDescriptor*,const RoleDescriptor*> entity = AbstractMetadataProvider::getEntityDescriptor(criteria);
79 if (entity.first) // even if the role isn't found, we're done
83 if (criteria.entityID_ascii)
84 name = criteria.entityID_ascii;
85 else if (criteria.entityID_unicode) {
86 auto_ptr_char temp(criteria.entityID_unicode);
89 else if (criteria.artifact) {
90 name = criteria.artifact->getSource();
95 Category& log = Category::getInstance(SAML_LOGCAT".MetadataProvider.Dynamic");
96 log.info("resolving metadata for (%s)", name.c_str());
100 auto_ptr<EntityDescriptor> entity2(resolve(criteria));
102 // Verify the entityID.
103 if (criteria.entityID_unicode && !XMLString::equals(criteria.entityID_unicode, entity2->getEntityID())) {
104 log.error("metadata instance did not match expected entityID");
108 auto_ptr_XMLCh temp2(name.c_str());
109 if (!XMLString::equals(temp2.get(), entity2->getEntityID())) {
110 log.error("metadata instance did not match expected entityID");
115 // Preprocess the metadata (even if we schema-validated).
117 SchemaValidators.validate(entity2.get());
119 catch (exception& ex) {
120 log.error("metadata intance failed manual validation checking: %s", ex.what());
121 throw MetadataException("Metadata instance failed manual validation checking.");
124 // Filter it, which may throw.
125 doFilters(*entity2.get());
127 time_t now = time(NULL);
129 if (entity2->getValidUntil() && entity2->getValidUntilEpoch() < now + 60)
130 throw MetadataException("Metadata was already invalid at the time of retrieval.");
132 log.info("caching resolved metadata for (%s)", name.c_str());
134 // Upgrade our lock so we can cache the new metadata.
141 // Make sure we clear out any existing copies, including stale metadata or if somebody snuck in.
142 time_t exp = m_maxCacheDuration;
143 if (entity2->getCacheDuration())
144 exp = min(m_maxCacheDuration, entity2->getCacheDurationEpoch());
146 index(entity2.release(), exp, true);
148 // Downgrade back to a read lock.
152 catch (exception& e) {
153 log.error("error while resolving entityID (%s): %s", name.c_str(), e.what());
158 return getEntityDescriptor(criteria);
161 EntityDescriptor* DynamicMetadataProvider::resolve(const Criteria& criteria) const
164 if (criteria.entityID_ascii) {
165 name = criteria.entityID_ascii;
167 else if (criteria.entityID_unicode) {
168 auto_ptr_char temp(criteria.entityID_unicode);
171 else if (criteria.artifact) {
172 throw MetadataException("Unable to resolve metadata dynamically from an artifact.");
176 DOMDocument* doc=NULL;
177 auto_ptr_XMLCh widenit(name.c_str());
178 URLInputSource src(widenit.get());
179 Wrapper4InputSource dsrc(&src,false);
181 doc=XMLToolingConfig::getConfig().getValidatingParser().parse(dsrc);
183 doc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
185 // Wrap the document for now.
186 XercesJanitor<DOMDocument> docjanitor(doc);
188 // Unmarshall objects, binding the document.
189 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
190 docjanitor.release();
192 // Make sure it's metadata.
193 EntityDescriptor* entity = dynamic_cast<EntityDescriptor*>(xmlObject.get());
195 throw MetadataException(
196 "Root of metadata instance not recognized: $1", params(1,xmlObject->getElementQName().toString().c_str())
202 catch (XMLException& e) {
203 auto_ptr_char msg(e.getMessage());
204 Category::getInstance(SAML_LOGCAT".MetadataProvider.Dynamic").error(
205 "Xerces error while resolving entityID (%s): %s", name.c_str(), msg.get()
207 throw MetadataException(msg.get());