2 * Copyright 2001-2010 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 * XMLMetadataProvider.cpp
20 * Supplies metadata from an XML file
24 #include "binding/SAMLArtifact.h"
25 #include "saml2/metadata/Metadata.h"
26 #include "saml2/metadata/MetadataFilter.h"
27 #include "saml2/metadata/AbstractMetadataProvider.h"
28 #include "saml2/metadata/DiscoverableMetadataProvider.h"
31 #include <xmltooling/XMLToolingConfig.h>
32 #include <xmltooling/io/HTTPResponse.h>
33 #include <xmltooling/util/NDC.h>
34 #include <xmltooling/util/PathResolver.h>
35 #include <xmltooling/util/ReloadableXMLFile.h>
36 #include <xmltooling/util/Threads.h>
37 #include <xmltooling/validation/ValidatorSuite.h>
39 using namespace opensaml::saml2md;
40 using namespace xmltooling::logging;
41 using namespace xmltooling;
44 #if defined (_MSC_VER)
45 #pragma warning( push )
46 #pragma warning( disable : 4250 )
52 class SAML_DLLLOCAL XMLMetadataProvider
53 : public AbstractMetadataProvider, public DiscoverableMetadataProvider, public ReloadableXMLFile
56 XMLMetadataProvider(const DOMElement* e);
58 virtual ~XMLMetadataProvider() {
74 const XMLObject* getMetadata() const {
79 pair<bool,DOMElement*> load(bool backup);
80 pair<bool,DOMElement*> background_load();
83 using AbstractMetadataProvider::index;
84 void index(time_t& validUntil);
85 time_t computeNextRefresh();
89 double m_refreshDelayFactor;
90 unsigned int m_backoffFactor;
91 time_t m_minRefreshDelay,m_maxRefreshDelay,m_lastValidUntil;
94 MetadataProvider* SAML_DLLLOCAL XMLMetadataProviderFactory(const DOMElement* const & e)
96 return new XMLMetadataProvider(e);
99 static const XMLCh discoveryFeed[] = UNICODE_LITERAL_13(d,i,s,c,o,v,e,r,y,F,e,e,d);
100 static const XMLCh minRefreshDelay[] = UNICODE_LITERAL_15(m,i,n,R,e,f,r,e,s,h,D,e,l,a,y);
101 static const XMLCh refreshDelayFactor[] = UNICODE_LITERAL_18(r,e,f,r,e,s,h,D,e,l,a,y,F,a,c,t,o,r);
105 #if defined (_MSC_VER)
106 #pragma warning( pop )
109 XMLMetadataProvider::XMLMetadataProvider(const DOMElement* e)
110 : AbstractMetadataProvider(e), ReloadableXMLFile(e, Category::getInstance(SAML_LOGCAT".MetadataProvider.XML"), false),
111 m_object(nullptr), m_discoveryFeed(XMLHelper::getAttrBool(e, true, discoveryFeed)),
112 m_refreshDelayFactor(0.75), m_backoffFactor(1),
113 m_minRefreshDelay(XMLHelper::getAttrInt(e, 600, minRefreshDelay)),
114 m_maxRefreshDelay(m_reloadInterval), m_lastValidUntil(SAMLTIME_MAX)
116 if (!m_local && m_maxRefreshDelay) {
117 const XMLCh* setting = e->getAttributeNS(nullptr, refreshDelayFactor);
118 if (setting && *setting) {
119 auto_ptr_char delay(setting);
120 m_refreshDelayFactor = atof(delay.get());
121 if (m_refreshDelayFactor <= 0.0 || m_refreshDelayFactor >= 1.0) {
122 m_log.error("invalid refreshDelayFactor setting, using default");
123 m_refreshDelayFactor = 0.75;
127 if (m_minRefreshDelay > m_maxRefreshDelay) {
128 m_log.error("minRefreshDelay setting exceeds maxRefreshDelay/refreshInterval setting, lowering to match it");
129 m_minRefreshDelay = m_maxRefreshDelay;
134 pair<bool,DOMElement*> XMLMetadataProvider::load(bool backup)
136 // Lower the refresh rate in case of an error.
137 m_reloadInterval = m_minRefreshDelay;
139 // Call the base class to load/parse the appropriate XML resource.
140 pair<bool,DOMElement*> raw = ReloadableXMLFile::load(backup);
142 // If we own it, wrap it for now.
143 XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : nullptr);
145 // Unmarshall objects, binding the document.
146 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(raw.second, true));
147 docjanitor.release();
149 if (!dynamic_cast<const EntitiesDescriptor*>(xmlObject.get()) && !dynamic_cast<const EntityDescriptor*>(xmlObject.get()))
150 throw MetadataException(
151 "Root of metadata instance not recognized: $1", params(1,xmlObject->getElementQName().toString().c_str())
154 // Preprocess the metadata (even if we schema-validated).
156 SchemaValidators.validate(xmlObject.get());
158 catch (exception& ex) {
159 m_log.error("metadata intance failed manual validation checking: %s", ex.what());
160 throw MetadataException("Metadata instance failed manual validation checking.");
163 // This is the best place to take a backup, since it's superficially "correct" metadata.
165 if (!backup && !m_backing.empty()) {
166 // We compute a random filename extension to the "real" location.
167 SAMLConfig::getConfig().generateRandomBytes(backupKey, 2);
168 backupKey = m_backing + '.' + SAMLArtifact::toHex(backupKey);
169 m_log.debug("backing up remote metadata resource to (%s)", backupKey.c_str());
171 ofstream backer(backupKey.c_str());
172 backer << *(raw.second->getOwnerDocument());
174 catch (exception& ex) {
175 m_log.crit("exception while backing up metadata: %s", ex.what());
181 doFilters(*xmlObject.get());
184 if (!backupKey.empty())
185 remove(backupKey.c_str());
189 if (!backupKey.empty()) {
190 m_log.debug("committing backup file to permanent location (%s)", m_backing.c_str());
191 Locker locker(getBackupLock());
192 remove(m_backing.c_str());
193 if (rename(backupKey.c_str(), m_backing.c_str()) != 0)
194 m_log.crit("unable to rename metadata backup file");
198 xmlObject->releaseThisAndChildrenDOM();
199 xmlObject->setDocument(nullptr);
201 // Swap it in after acquiring write lock if necessary.
204 SharedLock locker(m_lock, false);
205 bool changed = m_object!=nullptr;
207 m_object = xmlObject.release();
208 m_lastValidUntil = SAMLTIME_MAX;
209 index(m_lastValidUntil);
215 // Tracking cacheUntil through the tree is TBD, but
216 // validUntil is the tightest interval amongst the children.
218 // If a remote resource, adjust the reload interval.
219 if (!backup && !m_local) {
221 m_reloadInterval = computeNextRefresh();
222 m_log.info("adjusted reload interval to %d seconds", m_reloadInterval);
226 return make_pair(false,(DOMElement*)nullptr);
229 pair<bool,DOMElement*> XMLMetadataProvider::background_load()
235 if (ex == HTTPResponse::XMLTOOLING_HTTP_STATUS_NOTMODIFIED) {
236 // Unchanged document, so re-establish previous refresh interval.
237 m_reloadInterval = computeNextRefresh();
238 m_log.info("remote resource (%s) unchanged, adjusted reload interval to %u seconds", m_source.c_str(), m_reloadInterval);
241 // Any other status code, just treat as an error.
242 m_reloadInterval = m_minRefreshDelay * m_backoffFactor++;
243 if (m_reloadInterval > m_maxRefreshDelay)
244 m_reloadInterval = m_maxRefreshDelay;
245 m_log.warn("adjusted reload interval to %u seconds", m_reloadInterval);
247 if (!m_loaded && !m_backing.empty())
253 m_reloadInterval = m_minRefreshDelay * m_backoffFactor++;
254 if (m_reloadInterval > m_maxRefreshDelay)
255 m_reloadInterval = m_maxRefreshDelay;
256 m_log.warn("adjusted reload interval to %u seconds", m_reloadInterval);
257 if (!m_loaded && !m_backing.empty())
264 time_t XMLMetadataProvider::computeNextRefresh()
266 time_t now = time(nullptr);
268 // If some or all of the metadata is already expired, reload after the minimum.
269 if (m_lastValidUntil < now) {
270 return m_minRefreshDelay;
273 // Compute the smaller of the validUntil / cacheDuration constraints.
274 time_t ret = m_lastValidUntil - now;
275 const CacheableSAMLObject* cacheable = dynamic_cast<const CacheableSAMLObject*>(m_object);
276 if (cacheable && cacheable->getCacheDuration())
277 ret = min(ret, cacheable->getCacheDurationEpoch());
279 // Adjust for the delay factor.
280 ret *= m_refreshDelayFactor;
282 // Bound by max and min.
283 if (ret > m_maxRefreshDelay)
284 return m_maxRefreshDelay;
285 else if (ret < m_minRefreshDelay)
286 return m_minRefreshDelay;
292 void XMLMetadataProvider::index(time_t& validUntil)
294 clearDescriptorIndex();
295 EntitiesDescriptor* group=dynamic_cast<EntitiesDescriptor*>(m_object);
297 indexGroup(group, validUntil);
300 indexEntity(dynamic_cast<EntityDescriptor*>(m_object), validUntil);