2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * XMLMetadataProvider.cpp
24 * Supplies metadata from an XML file
28 #include "binding/SAMLArtifact.h"
29 #include "saml2/metadata/Metadata.h"
30 #include "saml2/metadata/MetadataFilter.h"
31 #include "saml2/metadata/AbstractMetadataProvider.h"
32 #include "saml2/metadata/DiscoverableMetadataProvider.h"
35 #include <boost/scoped_ptr.hpp>
36 #include <xmltooling/XMLToolingConfig.h>
37 #include <xmltooling/io/HTTPResponse.h>
38 #include <xmltooling/util/DateTime.h>
39 #include <xmltooling/util/NDC.h>
40 #include <xmltooling/util/PathResolver.h>
41 #include <xmltooling/util/ReloadableXMLFile.h>
42 #include <xmltooling/util/Threads.h>
43 #include <xmltooling/validation/ValidatorSuite.h>
45 #if defined(OPENSAML_LOG4SHIB)
46 # include <log4shib/NDC.hh>
47 #elif defined(OPENSAML_LOG4CPP)
48 # include <log4cpp/NDC.hh>
51 using namespace opensaml::saml2md;
52 using namespace xmltooling::logging;
53 using namespace xmltooling;
54 using namespace boost;
57 #if defined (_MSC_VER)
58 #pragma warning( push )
59 #pragma warning( disable : 4250 )
65 class SAML_DLLLOCAL XMLMetadataProvider
66 : public AbstractMetadataProvider, public DiscoverableMetadataProvider, public ReloadableXMLFile
69 XMLMetadataProvider(const DOMElement* e);
71 virtual ~XMLMetadataProvider() {
79 threadid += m_id + ']';
80 logging::NDC::push(threadid);
98 const char* getId() const {
102 void outputStatus(ostream& os) const {
103 os << "<MetadataProvider";
105 if (getId() && *getId()) {
106 os << " id='" << getId() << "'";
109 if (!m_source.empty()) {
110 os << " source='" << m_source << "'";
113 if (m_lastUpdate > 0) {
114 DateTime ts(m_lastUpdate);
116 auto_ptr_char timestamp(ts.getFormattedString());
117 os << " lastUpdate='" << timestamp.get() << "'";
120 if (!m_local && m_reloadInterval > 0) {
121 os << " reloadInterval='" << m_reloadInterval << "'";
127 const XMLObject* getMetadata() const {
128 return m_object.get();
132 pair<bool,DOMElement*> load(bool backup);
133 pair<bool,DOMElement*> background_load();
136 using AbstractMetadataProvider::index;
137 void index(time_t& validUntil);
138 time_t computeNextRefresh();
140 scoped_ptr<XMLObject> m_object;
141 bool m_discoveryFeed;
142 double m_refreshDelayFactor;
143 unsigned int m_backoffFactor;
144 time_t m_minRefreshDelay,m_maxRefreshDelay,m_lastValidUntil;
147 MetadataProvider* SAML_DLLLOCAL XMLMetadataProviderFactory(const DOMElement* const & e)
149 return new XMLMetadataProvider(e);
152 static const XMLCh discoveryFeed[] = UNICODE_LITERAL_13(d,i,s,c,o,v,e,r,y,F,e,e,d);
153 static const XMLCh minRefreshDelay[] = UNICODE_LITERAL_15(m,i,n,R,e,f,r,e,s,h,D,e,l,a,y);
154 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);
158 #if defined (_MSC_VER)
159 #pragma warning( pop )
162 XMLMetadataProvider::XMLMetadataProvider(const DOMElement* e)
163 : MetadataProvider(e), AbstractMetadataProvider(e), DiscoverableMetadataProvider(e),
164 ReloadableXMLFile(e, Category::getInstance(SAML_LOGCAT".MetadataProvider.XML"), false),
165 m_discoveryFeed(XMLHelper::getAttrBool(e, true, discoveryFeed)),
166 m_refreshDelayFactor(0.75), m_backoffFactor(1),
167 m_minRefreshDelay(XMLHelper::getAttrInt(e, 600, minRefreshDelay)),
168 m_maxRefreshDelay(m_reloadInterval), m_lastValidUntil(SAMLTIME_MAX)
170 if (!m_local && m_maxRefreshDelay) {
171 const XMLCh* setting = e->getAttributeNS(nullptr, refreshDelayFactor);
172 if (setting && *setting) {
173 auto_ptr_char delay(setting);
174 m_refreshDelayFactor = atof(delay.get());
175 if (m_refreshDelayFactor <= 0.0 || m_refreshDelayFactor >= 1.0) {
176 m_log.error("invalid refreshDelayFactor setting, using default");
177 m_refreshDelayFactor = 0.75;
181 if (m_minRefreshDelay > m_maxRefreshDelay) {
182 m_log.warn("minRefreshDelay setting exceeds maxRefreshDelay/reloadInterval setting, lowering to match it");
183 m_minRefreshDelay = m_maxRefreshDelay;
188 pair<bool,DOMElement*> XMLMetadataProvider::load(bool backup)
191 // Lower the refresh rate in case of an error.
192 m_reloadInterval = m_minRefreshDelay;
195 // Call the base class to load/parse the appropriate XML resource.
196 pair<bool,DOMElement*> raw = ReloadableXMLFile::load(backup);
198 // If we own it, wrap it for now.
199 XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : nullptr);
201 // Unmarshall objects, binding the document.
202 scoped_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(raw.second, true));
203 docjanitor.release();
205 if (!dynamic_cast<const EntitiesDescriptor*>(xmlObject.get()) && !dynamic_cast<const EntityDescriptor*>(xmlObject.get()))
206 throw MetadataException(
207 "Root of metadata instance not recognized: $1", params(1,xmlObject->getElementQName().toString().c_str())
210 // Preprocess the metadata (even if we schema-validated).
212 SchemaValidators.validate(xmlObject.get());
214 catch (exception& ex) {
215 m_log.error("metadata intance failed manual validation checking: %s", ex.what());
216 throw MetadataException("Metadata instance failed manual validation checking.");
219 // This is the best place to take a backup, since it's superficially "correct" metadata.
221 if (!backup && !m_backing.empty()) {
222 // We compute a random filename extension to the "real" location.
223 SAMLConfig::getConfig().generateRandomBytes(backupKey, 2);
224 backupKey = m_backing + '.' + SAMLArtifact::toHex(backupKey);
225 m_log.debug("backing up remote metadata resource to (%s)", backupKey.c_str());
227 ofstream backer(backupKey.c_str());
228 backer << *(raw.second->getOwnerDocument());
230 catch (exception& ex) {
231 m_log.crit("exception while backing up metadata: %s", ex.what());
237 doFilters(*xmlObject);
240 if (!backupKey.empty())
241 remove(backupKey.c_str());
245 if (!backupKey.empty()) {
246 m_log.debug("committing backup file to permanent location (%s)", m_backing.c_str());
247 Locker locker(getBackupLock());
248 remove(m_backing.c_str());
249 if (rename(backupKey.c_str(), m_backing.c_str()) != 0)
250 m_log.crit("unable to rename metadata backup file");
254 xmlObject->releaseThisAndChildrenDOM();
255 xmlObject->setDocument(nullptr);
257 // Swap it in after acquiring write lock if necessary.
260 SharedLock locker(m_lock, false);
261 bool changed = m_object!=nullptr;
262 m_object.swap(xmlObject);
263 m_lastValidUntil = SAMLTIME_MAX;
264 index(m_lastValidUntil);
269 m_lastUpdate = time(nullptr);
271 // Tracking cacheUntil through the tree is TBD, but
272 // validUntil is the tightest interval amongst the children.
274 // If a remote resource that's monitored, adjust the reload interval.
275 if (!backup && !m_local && m_lock) {
277 m_reloadInterval = computeNextRefresh();
278 m_log.info("adjusted reload interval to %d seconds", m_reloadInterval);
282 return make_pair(false,(DOMElement*)nullptr);
285 pair<bool,DOMElement*> XMLMetadataProvider::background_load()
291 if (ex == HTTPResponse::XMLTOOLING_HTTP_STATUS_NOTMODIFIED) {
292 // Unchanged document, so re-establish previous refresh interval.
293 m_reloadInterval = computeNextRefresh();
294 m_log.info("remote resource (%s) unchanged, adjusted reload interval to %u seconds", m_source.c_str(), m_reloadInterval);
297 // Any other status code, just treat as an error.
298 m_reloadInterval = m_minRefreshDelay * m_backoffFactor++;
299 if (m_reloadInterval > m_maxRefreshDelay)
300 m_reloadInterval = m_maxRefreshDelay;
301 m_log.warn("adjusted reload interval to %u seconds", m_reloadInterval);
303 if (!m_loaded && !m_backing.empty())
309 m_reloadInterval = m_minRefreshDelay * m_backoffFactor++;
310 if (m_reloadInterval > m_maxRefreshDelay)
311 m_reloadInterval = m_maxRefreshDelay;
312 m_log.warn("adjusted reload interval to %u seconds", m_reloadInterval);
313 if (!m_loaded && !m_backing.empty())
320 time_t XMLMetadataProvider::computeNextRefresh()
322 time_t now = time(nullptr);
324 // If some or all of the metadata is already expired, reload after the minimum.
325 if (m_lastValidUntil < now) {
326 return m_minRefreshDelay;
329 // Compute the smaller of the validUntil / cacheDuration constraints.
330 time_t ret = m_lastValidUntil - now;
331 const CacheableSAMLObject* cacheable = dynamic_cast<const CacheableSAMLObject*>(m_object.get());
332 if (cacheable && cacheable->getCacheDuration())
333 ret = min(ret, cacheable->getCacheDurationEpoch());
335 // Adjust for the delay factor.
336 ret *= m_refreshDelayFactor;
338 // Bound by max and min.
339 if (ret > m_maxRefreshDelay)
340 return m_maxRefreshDelay;
341 else if (ret < m_minRefreshDelay)
342 return m_minRefreshDelay;
348 void XMLMetadataProvider::index(time_t& validUntil)
350 clearDescriptorIndex();
351 EntitiesDescriptor* group = dynamic_cast<EntitiesDescriptor*>(m_object.get());
353 indexGroup(group, validUntil);
356 indexEntity(dynamic_cast<EntityDescriptor*>(m_object.get()), validUntil);