Skip to content
Snippets Groups Projects
Unverified Commit 347129b7 authored by Thijs Kinkhorst's avatar Thijs Kinkhorst Committed by GitHub
Browse files

Merge pull request #725 from bajnokk/bug/mdx-break

Make MDQ more robust on errors (fixes #723)
parents 71a90aa9 124f93d2
No related branches found
No related tags found
No related merge requests found
...@@ -262,7 +262,8 @@ class MDQ extends \SimpleSAML_Metadata_MetaDataStorageSource ...@@ -262,7 +262,8 @@ class MDQ extends \SimpleSAML_Metadata_MetaDataStorageSource
* *
* @return array An associative array with metadata for the given entity, or NULL if we are unable to * @return array An associative array with metadata for the given entity, or NULL if we are unable to
* locate the entity. * locate the entity.
* @throws \Exception If an error occurs while downloading metadata, validating the signature or writing to cache. * @throws \Exception If an error occurs while validating the signature or the metadata is in an
* incorrect set.
*/ */
public function getMetaData($index, $set) public function getMetaData($index, $set)
{ {
...@@ -272,7 +273,13 @@ class MDQ extends \SimpleSAML_Metadata_MetaDataStorageSource ...@@ -272,7 +273,13 @@ class MDQ extends \SimpleSAML_Metadata_MetaDataStorageSource
Logger::info(__CLASS__.': loading metadata entity ['.$index.'] from ['.$set.']'); Logger::info(__CLASS__.': loading metadata entity ['.$index.'] from ['.$set.']');
// read from cache if possible // read from cache if possible
$data = $this->getFromCache($set, $index); try {
$data = $this->getFromCache($set, $index);
} catch (\Exception $e) {
Logger::error($e->getMessage());
// proceed with fetching metadata even if the cache is broken
$data = null;
}
if ($data !== null && array_key_exists('expires', $data) && $data['expires'] < time()) { if ($data !== null && array_key_exists('expires', $data) && $data['expires'] < time()) {
// metadata has expired // metadata has expired
...@@ -292,14 +299,15 @@ class MDQ extends \SimpleSAML_Metadata_MetaDataStorageSource ...@@ -292,14 +299,15 @@ class MDQ extends \SimpleSAML_Metadata_MetaDataStorageSource
try { try {
$xmldata = HTTP::fetch($mdq_url); $xmldata = HTTP::fetch($mdq_url);
} catch (\Exception $e) { } catch (\Exception $e) {
Logger::warning('Fetching metadata for '.$index.': '.$e->getMessage()); // Avoid propagating the exception, make sure we can handle the error later
$xmldata = false;
} }
if (empty($xmldata)) { if (empty($xmldata)) {
$error = error_get_last(); $error = error_get_last();
throw new \Exception( Logger::info('Unable to fetch metadata for "'.$index.'" from '.$mdq_url.': '.
'Error downloading metadata for "'.$index.'" from "'.$mdq_url.'": '.$error['message'] (is_array($error) ? $error['message'] : 'no error available'));
); return null;
} }
/** @var string $xmldata */ /** @var string $xmldata */
...@@ -317,7 +325,12 @@ class MDQ extends \SimpleSAML_Metadata_MetaDataStorageSource ...@@ -317,7 +325,12 @@ class MDQ extends \SimpleSAML_Metadata_MetaDataStorageSource
throw new \Exception(__CLASS__.': no metadata for set "'.$set.'" available from "'.$index.'".'); throw new \Exception(__CLASS__.': no metadata for set "'.$set.'" available from "'.$index.'".');
} }
$this->writeToCache($set, $index, $data); try {
$this->writeToCache($set, $index, $data);
} catch (\Exception $e) {
// Proceed without writing to cache
Logger::error('Error writing MDQ result to cache: '.$e->getMessage());
}
return $data; return $data;
} }
......
...@@ -444,7 +444,7 @@ class HTTP ...@@ -444,7 +444,7 @@ class HTTP
} }
$context = stream_context_create($context); $context = stream_context_create($context);
$data = file_get_contents($url, false, $context); $data = @file_get_contents($url, false, $context);
if ($data === false) { if ($data === false) {
$error = error_get_last(); $error = error_get_last();
throw new \SimpleSAML_Error_Exception('Error fetching '.var_export($url, true).':'. throw new \SimpleSAML_Error_Exception('Error fetching '.var_export($url, true).':'.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment