1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
/**
* Licensed to the University Corporation for Advanced Internet
* Development, Inc. (UCAID) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* UCAID licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
/**
* XMLMetadataProvider.cpp
*
* Supplies metadata from an XML file
*/
#include "internal.h"
#include "binding/SAMLArtifact.h"
#include "saml2/metadata/Metadata.h"
#include "saml2/metadata/MetadataFilter.h"
#include "saml2/metadata/AbstractMetadataProvider.h"
#include "saml2/metadata/DiscoverableMetadataProvider.h"
#include <fstream>
#include <xmltooling/XMLToolingConfig.h>
#include <xmltooling/io/HTTPResponse.h>
#include <xmltooling/util/NDC.h>
#include <xmltooling/util/PathResolver.h>
#include <xmltooling/util/ReloadableXMLFile.h>
#include <xmltooling/util/Threads.h>
#include <xmltooling/validation/ValidatorSuite.h>
#if defined(OPENSAML_LOG4SHIB)
# include <log4shib/NDC.hh>
#elif defined(OPENSAML_LOG4CPP)
# include <log4cpp/NDC.hh>
#endif
using namespace opensaml::saml2md;
using namespace xmltooling::logging;
using namespace xmltooling;
using namespace boost;
using namespace std;
#if defined (_MSC_VER)
#pragma warning( push )
#pragma warning( disable : 4250 )
#endif
namespace opensaml {
namespace saml2md {
class SAML_DLLLOCAL XMLMetadataProvider
: public AbstractMetadataProvider, public DiscoverableMetadataProvider, public ReloadableXMLFile
{
public:
XMLMetadataProvider(const DOMElement* e, bool deprecationSupport=true);
virtual ~XMLMetadataProvider() {
shutdown();
}
void init();
const char* getId() const {
return m_id.c_str();
}
void outputStatus(ostream& os) const;
const XMLObject* getMetadata() const {
return m_object.get();
}
protected:
pair<bool,DOMElement*> load(bool backup, string backingFile);
pair<bool,DOMElement*> background_load();
private:
void index(time_t& validUntil);
time_t computeNextRefresh();
scoped_ptr<XMLObject> m_object;
bool m_discoveryFeed,m_dropDOM;
double m_refreshDelayFactor;
unsigned int m_backoffFactor;
time_t m_minRefreshDelay,m_maxRefreshDelay,m_lastValidUntil;
};
MetadataProvider* SAML_DLLLOCAL XMLMetadataProviderFactory(const DOMElement* const & e, bool deprecationSupport)
{
return new XMLMetadataProvider(e, deprecationSupport);
}
static const XMLCh discoveryFeed[] = UNICODE_LITERAL_13(d,i,s,c,o,v,e,r,y,F,e,e,d);
static const XMLCh dropDOM[] = UNICODE_LITERAL_7(d,r,o,p,D,O,M);
static const XMLCh minRefreshDelay[] = UNICODE_LITERAL_15(m,i,n,R,e,f,r,e,s,h,D,e,l,a,y);
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);
};
};
#if defined (_MSC_VER)
#pragma warning( pop )
#endif
XMLMetadataProvider::XMLMetadataProvider(const DOMElement* e, bool deprecationSupport)
: MetadataProvider(e, deprecationSupport), AbstractMetadataProvider(e, deprecationSupport), DiscoverableMetadataProvider(e, deprecationSupport),
ReloadableXMLFile(e, Category::getInstance(SAML_LOGCAT ".MetadataProvider.XML"), false, deprecationSupport),
m_discoveryFeed(XMLHelper::getAttrBool(e, true, discoveryFeed)),
m_dropDOM(XMLHelper::getAttrBool(e, true, dropDOM)),
m_refreshDelayFactor(0.75), m_backoffFactor(1),
m_minRefreshDelay(XMLHelper::getAttrInt(e, 600, minRefreshDelay)),
m_maxRefreshDelay(m_reloadInterval), m_lastValidUntil(SAMLTIME_MAX)
{
if (!m_local && m_maxRefreshDelay) {
const XMLCh* setting = e->getAttributeNS(nullptr, refreshDelayFactor);
if (setting && *setting) {
auto_ptr_char delay(setting);
m_refreshDelayFactor = atof(delay.get());
if (m_refreshDelayFactor <= 0.0 || m_refreshDelayFactor >= 1.0) {
m_log.error("invalid refreshDelayFactor setting, using default");
m_refreshDelayFactor = 0.75;
}
}
if (m_minRefreshDelay > m_maxRefreshDelay) {
m_log.warn("minRefreshDelay setting exceeds maxRefreshDelay/reloadInterval setting, lowering to match it");
m_minRefreshDelay = m_maxRefreshDelay;
}
}
}
void XMLMetadataProvider::init()
{
try {
if (!m_id.empty()) {
string threadid("[");
threadid += m_id + ']';
logging::NDC::push(threadid);
}
background_load();
startup();
}
catch (...) {
startup();
if (!m_id.empty()) {
logging::NDC::pop();
}
throw;
}
if (!m_id.empty()) {
logging::NDC::pop();
}
}
pair<bool,DOMElement*> XMLMetadataProvider::load(bool backup, string backingFile)
{
if (!backup) {
// Lower the refresh rate in case of an error.
m_reloadInterval = m_minRefreshDelay;
}
string backupKey = "";
if (!backup && !backingFile.empty()) {
// We compute a random filename extension to the "real" location.
SAMLConfig::getConfig().generateRandomBytes(backupKey, 2);
backupKey = backingFile + '.' + SAMLArtifact::toHex(backupKey);
m_log.debug("remote metadata resource will be backed up to (%s)", backupKey.c_str());
}
// Call the base class to load/parse the appropriate XML resource.
pair<bool,DOMElement*> raw = ReloadableXMLFile::load(backup, backupKey);
// If we own it, wrap it for now.
XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : nullptr);
if (!raw.second)
throw MetadataException("XML document was empty");
// Unmarshall objects, binding the document.
scoped_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(raw.second, raw.first));
docjanitor.release();
if (!dynamic_cast<const EntitiesDescriptor*>(xmlObject.get()) && !dynamic_cast<const EntityDescriptor*>(xmlObject.get())) {
if (!backupKey.empty())
remove(backupKey.c_str());
throw MetadataException(
"Root of metadata instance not recognized: $1", params(1,xmlObject->getElementQName().toString().c_str())
);
}
// Preprocess the metadata (even if we schema-validated).
try {
SchemaValidators.validate(xmlObject.get());
}
catch (const std::exception& ex) {
m_log.error("metadata instance failed manual validation checking: %s", ex.what());
if (!backupKey.empty())
remove(backupKey.c_str());
throw MetadataException("Metadata instance failed manual validation checking.");
}
const TimeBoundSAMLObject* validityCheck = dynamic_cast<TimeBoundSAMLObject*>(xmlObject.get());
if (!validityCheck || !validityCheck->isValid()) {
m_log.error("metadata instance was invalid at time of acquisition");
if (!backupKey.empty())
remove(backupKey.c_str());
throw MetadataException("Metadata instance was invalid at time of acquisition.");
}
try {
BatchLoadMetadataFilterContext ctx(backup);
doFilters(&ctx , *xmlObject);
}
catch (const std::exception&) {
if (!backupKey.empty())
remove(backupKey.c_str());
throw;
}
if (!backupKey.empty()) {
m_log.debug("committing backup file to permanent location (%s)", backingFile.c_str());
Locker locker(getBackupLock());
remove(backingFile.c_str());
if (rename(backupKey.c_str(), backingFile.c_str()) != 0)
m_log.crit("unable to rename metadata backup file");
preserveCacheTag();
}
if (m_dropDOM) {
xmlObject->releaseThisAndChildrenDOM();
xmlObject->setDocument(nullptr);
}
// Swap it in after acquiring write lock if necessary.
if (m_lock)
m_lock->wrlock();
SharedLock locker(m_lock, false);
bool changed = m_object!=nullptr;
m_object.swap(xmlObject);
m_lastValidUntil = SAMLTIME_MAX;
index(m_lastValidUntil);
if (m_discoveryFeed)
generateFeed();
if (changed)
emitChangeEvent();
m_lastUpdate = time(nullptr);
// Tracking cacheUntil through the tree is TBD, but
// validUntil is the tightest interval amongst the children.
// If a remote resource that's monitored, adjust the reload interval.
if (!backup && !m_local && m_lock) {
m_backoffFactor = 1;
m_reloadInterval = computeNextRefresh();
m_log.info("adjusted reload interval to %d seconds", m_reloadInterval);
}
m_loaded = true;
return make_pair(false,(DOMElement*)nullptr);
}
pair<bool,DOMElement*> XMLMetadataProvider::background_load()
{
try {
return load(false, m_backing);
}
catch (long ex) {
if (ex == HTTPResponse::XMLTOOLING_HTTP_STATUS_NOTMODIFIED) {
// Unchanged document, so re-establish previous refresh interval.
m_reloadInterval = computeNextRefresh();
m_log.info("remote resource (%s) unchanged, adjusted reload interval to %u seconds", m_source.c_str(), m_reloadInterval);
}
else {
// Any other status code, just treat as an error.
m_reloadInterval = m_minRefreshDelay * m_backoffFactor++;
if (m_reloadInterval > m_maxRefreshDelay)
m_reloadInterval = m_maxRefreshDelay;
m_log.warn("adjusted reload interval to %u seconds", m_reloadInterval);
}
if (!m_loaded && !m_backing.empty())
return load(true, "");
throw;
}
catch (const std::exception& ex) {
if (!m_local) {
m_reloadInterval = m_minRefreshDelay * m_backoffFactor++;
if (m_reloadInterval > m_maxRefreshDelay)
m_reloadInterval = m_maxRefreshDelay;
m_log.warn("adjusted reload interval to %u seconds", m_reloadInterval);
if (!m_loaded && !m_backing.empty()) {
m_log.warn("trying backup file, exception loading remote resource: %s", ex.what());
return load(true, "");
}
}
throw;
}
}
time_t XMLMetadataProvider::computeNextRefresh()
{
time_t now = time(nullptr);
// If some or all of the metadata is already expired, reload after the minimum.
if (m_lastValidUntil < now) {
return m_minRefreshDelay;
}
else {
// Compute the smaller of the validUntil / cacheDuration constraints.
time_t ret = m_lastValidUntil - now;
const CacheableSAMLObject* cacheable = dynamic_cast<const CacheableSAMLObject*>(m_object.get());
if (cacheable && cacheable->getCacheDuration())
ret = min(ret, cacheable->getCacheDurationEpoch());
// Adjust for the delay factor.
ret *= m_refreshDelayFactor;
// Bound by max and min.
if (ret > m_maxRefreshDelay)
return m_maxRefreshDelay;
else if (ret < m_minRefreshDelay)
return m_minRefreshDelay;
return ret;
}
}
void XMLMetadataProvider::index(time_t& validUntil)
{
clearDescriptorIndex();
EntitiesDescriptor* group = dynamic_cast<EntitiesDescriptor*>(m_object.get());
if (group) {
indexGroup(group, validUntil);
return;
}
indexEntity(dynamic_cast<EntityDescriptor*>(m_object.get()), validUntil);
}
void XMLMetadataProvider::outputStatus(ostream& os) const
{
os << "<MetadataProvider";
if (getId() && *getId()) {
os << " id='"; XMLHelper::encode(os, getId()); os << "'";
}
if (!m_source.empty()) {
os << " source='"; XMLHelper::encode(os, m_source.c_str()); os << "'";
}
if (m_lastUpdate > 0) {
XMLDateTime ts(m_lastUpdate, false);
ts.parseDateTime();
auto_ptr_char timestamp(ts.getFormattedString());
os << " lastUpdate='" << timestamp.get() << "'";
}
if (!m_local && m_reloadInterval > 0) {
os << " reloadInterval='" << m_reloadInterval << "'";
}
os << "/>";
}
|