From de83bda7778e3b581bf4ffd5ecc1ba0dfcf70f8e Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 28 Mar 2017 13:05:45 +0200 Subject: [PATCH 1/3] update rackspace/opencloud to 1.16.0 Signed-off-by: Robin Appelman --- 3rdparty | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rdparty b/3rdparty index 540cad1fa7020..18adb82bab030 160000 --- a/3rdparty +++ b/3rdparty @@ -1 +1 @@ -Subproject commit 540cad1fa70205ca38dd344ea09d412bd986a1e2 +Subproject commit 18adb82bab030a0af41e66e421a90d123a21a8f2 From 205d5586e84b5ea2b71d582e7bd731ed02583088 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 28 Mar 2017 13:06:09 +0200 Subject: [PATCH 2/3] cache swift tokens in memcache Signed-off-by: Robin Appelman --- lib/private/Files/ObjectStore/Swift.php | 46 +++++++++++++++++++------ 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/lib/private/Files/ObjectStore/Swift.php b/lib/private/Files/ObjectStore/Swift.php index 43ce32af94127..cfb5a654e6852 100644 --- a/lib/private/Files/ObjectStore/Swift.php +++ b/lib/private/Files/ObjectStore/Swift.php @@ -31,6 +31,7 @@ use OpenCloud\Common\Exceptions\EndpointError; use OpenCloud\Common\Service\Catalog; use OpenCloud\Common\Service\CatalogItem; +use OpenCloud\Identity\Resource\Token; use OpenCloud\ObjectStore\Service; use OpenCloud\OpenStack; use OpenCloud\Rackspace; @@ -57,6 +58,8 @@ class Swift implements IObjectStore { */ private $container; + private $memcache; + public function __construct($params) { if (isset($params['bucket'])) { $params['container'] = $params['bucket']; @@ -71,9 +74,15 @@ public function __construct($params) { if (isset($params['apiKey'])) { $this->client = new Rackspace($params['url'], $params); + $cacheKey = $this->params['username'] . '@' . $this->params['url'] . '/' . $this->params['bucket']; } else { $this->client = new OpenStack($params['url'], $params); + $cacheKey = $this->params['username'] . '@' . $this->params['url'] . '/' . $this->params['bucket']; } + + $cacheFactory = \OC::$server->getMemCacheFactory(); + $this->memcache = $cacheFactory->create('swift::' . $cacheKey); + $this->params = $params; } @@ -82,19 +91,36 @@ protected function init() { return; } - try { - $this->client->authenticate(); - } catch (ClientErrorResponseException $e) { - $statusCode = $e->getResponse()->getStatusCode(); - if ($statusCode == 412) { - throw new StorageAuthException('Precondition failed, verify the keystone url', $e); - } else if ($statusCode === 401) { - throw new StorageAuthException('Authentication failed, verify the username, password and possibly tenant', $e); - } else { - throw new StorageAuthException('Unknown error', $e); + $cachedTokenString = $this->memcache->get('token'); + if ($cachedTokenString) { + $cachedToken = unserialize($cachedTokenString); + try { + $this->client->importCredentials($cachedToken); + } catch (\Exception $e) { + $this->client->setTokenObject(new Token()); } } + /** @var Token $token */ + $token = $this->client->getTokenObject(); + + if (!$token || $token->hasExpired()) { + try { + $this->client->authenticate(); + $this->memcache->set('token', serialize($this->client->exportCredentials())); + } catch (ClientErrorResponseException $e) { + $statusCode = $e->getResponse()->getStatusCode(); + if ($statusCode == 412) { + throw new StorageAuthException('Precondition failed, verify the keystone url', $e); + } else if ($statusCode === 401) { + throw new StorageAuthException('Authentication failed, verify the username, password and possibly tenant', $e); + } else { + throw new StorageAuthException('Unknown error', $e); + } + } + } + + /** @var Catalog $catalog */ $catalog = $this->client->getCatalog(); From 6991b79d40cf3f92e57fd7a7a9ea49e2404b090d Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 28 Mar 2017 21:31:50 +0200 Subject: [PATCH 3/3] serialize the token to json instead of using php's serialize Signed-off-by: Robin Appelman --- lib/private/Files/ObjectStore/Swift.php | 46 +++++++++++++++++++------ 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/lib/private/Files/ObjectStore/Swift.php b/lib/private/Files/ObjectStore/Swift.php index cfb5a654e6852..38fb96e3af659 100644 --- a/lib/private/Files/ObjectStore/Swift.php +++ b/lib/private/Files/ObjectStore/Swift.php @@ -91,15 +91,7 @@ protected function init() { return; } - $cachedTokenString = $this->memcache->get('token'); - if ($cachedTokenString) { - $cachedToken = unserialize($cachedTokenString); - try { - $this->client->importCredentials($cachedToken); - } catch (\Exception $e) { - $this->client->setTokenObject(new Token()); - } - } + $this->importToken(); /** @var Token $token */ $token = $this->client->getTokenObject(); @@ -107,7 +99,7 @@ protected function init() { if (!$token || $token->hasExpired()) { try { $this->client->authenticate(); - $this->memcache->set('token', serialize($this->client->exportCredentials())); + $this->exportToken(); } catch (ClientErrorResponseException $e) { $statusCode = $e->getResponse()->getStatusCode(); if ($statusCode == 412) { @@ -163,6 +155,40 @@ protected function init() { } } + private function exportToken() { + $export = $this->client->exportCredentials(); + $export['catalog'] = array_map(function (CatalogItem $item) { + return [ + 'name' => $item->getName(), + 'endpoints' => $item->getEndpoints(), + 'type' => $item->getType() + ]; + }, $export['catalog']->getItems()); + $this->memcache->set('token', json_encode($export)); + } + + private function importToken() { + $cachedTokenString = $this->memcache->get('token'); + if ($cachedTokenString) { + $cachedToken = json_decode($cachedTokenString, true); + $cachedToken['catalog'] = array_map(function (array $item) { + $itemClass = new \stdClass(); + $itemClass->name = $item['name']; + $itemClass->endpoints = array_map(function (array $endpoint) { + return (object) $endpoint; + }, $item['endpoints']); + $itemClass->type = $item['type']; + + return $itemClass; + }, $cachedToken['catalog']); + try { + $this->client->importCredentials($cachedToken); + } catch (\Exception $e) { + $this->client->setTokenObject(new Token()); + } + } + } + /** * @param Catalog $catalog * @param $name