diff options
author | Morris Jobke <hey@morrisjobke.de> | 2019-01-23 17:30:12 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-23 17:30:12 +0300 |
commit | e5af104517dcbc83dc64d67367fee2809647e56b (patch) | |
tree | ee01139b2fa99eeb1ce1ef8e83c964c1b4787004 /lib | |
parent | 238655538e1ca7831e5f988afbf2cba9121c0241 (diff) | |
parent | 4da5589fe2439ee4662c43a31fa20cc678fe461f (diff) |
Merge pull request #13741 from nextcloud/backport/13140/stable14
[stable14] Cache tokens when using swift's v2 authentication
Diffstat (limited to 'lib')
-rw-r--r-- | lib/composer/composer/autoload_classmap.php | 1 | ||||
-rw-r--r-- | lib/composer/composer/autoload_static.php | 1 | ||||
-rw-r--r-- | lib/private/Files/ObjectStore/SwiftFactory.php | 68 | ||||
-rw-r--r-- | lib/private/Files/ObjectStore/SwiftV2CachingAuthService.php | 35 |
4 files changed, 88 insertions, 17 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index bee556c9e47..c5f0fc5af1f 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -740,6 +740,7 @@ return array( 'OC\\Files\\ObjectStore\\StorageObjectStore' => $baseDir . '/lib/private/Files/ObjectStore/StorageObjectStore.php', 'OC\\Files\\ObjectStore\\Swift' => $baseDir . '/lib/private/Files/ObjectStore/Swift.php', 'OC\\Files\\ObjectStore\\SwiftFactory' => $baseDir . '/lib/private/Files/ObjectStore/SwiftFactory.php', + 'OC\\Files\\ObjectStore\\SwiftV2CachingAuthService' => $baseDir . '/lib/private/Files/ObjectStore/SwiftV2CachingAuthService.php', 'OC\\Files\\Search\\SearchBinaryOperator' => $baseDir . '/lib/private/Files/Search/SearchBinaryOperator.php', 'OC\\Files\\Search\\SearchComparison' => $baseDir . '/lib/private/Files/Search/SearchComparison.php', 'OC\\Files\\Search\\SearchOrder' => $baseDir . '/lib/private/Files/Search/SearchOrder.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 989265d31d1..4717797128f 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -770,6 +770,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\Files\\ObjectStore\\StorageObjectStore' => __DIR__ . '/../../..' . '/lib/private/Files/ObjectStore/StorageObjectStore.php', 'OC\\Files\\ObjectStore\\Swift' => __DIR__ . '/../../..' . '/lib/private/Files/ObjectStore/Swift.php', 'OC\\Files\\ObjectStore\\SwiftFactory' => __DIR__ . '/../../..' . '/lib/private/Files/ObjectStore/SwiftFactory.php', + 'OC\\Files\\ObjectStore\\SwiftV2CachingAuthService' => __DIR__ . '/../../..' . '/lib/private/Files/ObjectStore/SwiftV2CachingAuthService.php', 'OC\\Files\\Search\\SearchBinaryOperator' => __DIR__ . '/../../..' . '/lib/private/Files/Search/SearchBinaryOperator.php', 'OC\\Files\\Search\\SearchComparison' => __DIR__ . '/../../..' . '/lib/private/Files/Search/SearchComparison.php', 'OC\\Files\\Search\\SearchOrder' => __DIR__ . '/../../..' . '/lib/private/Files/Search/SearchOrder.php', diff --git a/lib/private/Files/ObjectStore/SwiftFactory.php b/lib/private/Files/ObjectStore/SwiftFactory.php index 3ff534b4e64..1034026327b 100644 --- a/lib/private/Files/ObjectStore/SwiftFactory.php +++ b/lib/private/Files/ObjectStore/SwiftFactory.php @@ -33,6 +33,7 @@ use OCP\ICache; use OCP\ILogger; use OpenStack\Common\Error\BadResponseError; use OpenStack\Common\Auth\Token; +use OpenStack\Identity\v2\Models\Catalog; use OpenStack\Identity\v2\Service as IdentityV2Service; use OpenStack\Identity\v3\Service as IdentityV3Service; use OpenStack\OpenStack; @@ -47,6 +48,13 @@ class SwiftFactory { private $container = null; private $logger; + const DEFAULT_OPTIONS = [ + 'autocreate' => false, + 'urlType' => 'publicURL', + 'catalogName' => 'swift', + 'catalogType' => 'object-store' + ]; + public function __construct(ICache $cache, array $params, ILogger $logger) { $this->cache = $cache; $this->params = $params; @@ -62,11 +70,21 @@ class SwiftFactory { } } - private function cacheToken(Token $token, string $cacheKey) { + private function cacheToken(Token $token, string $serviceUrl, string $cacheKey) { if ($token instanceof \OpenStack\Identity\v3\Models\Token) { + // for v3 the catalog is cached as part of the token, so no need to cache $serviceUrl separately $value = json_encode($token->export()); } else { - $value = json_encode($token); + /** @var \OpenStack\Identity\v2\Models\Token $token */ + $value = json_encode([ + 'serviceUrl' => $serviceUrl, + 'token' => [ + 'issued_at' => $token->issuedAt->format('c'), + 'expires' => $token->expires->format('c'), + 'id' => $token->id, + 'tenant' => $token->tenant + ] + ]); } $this->cache->set($cacheKey . '/token', $value); } @@ -82,10 +100,6 @@ class SwiftFactory { if (!isset($this->params['container'])) { $this->params['container'] = 'nextcloud'; } - if (!isset($this->params['autocreate'])) { - // should only be true for tests - $this->params['autocreate'] = false; - } if (isset($this->params['user']) && is_array($this->params['user'])) { $userName = $this->params['user']['name']; } else { @@ -97,6 +111,7 @@ class SwiftFactory { if (!isset($this->params['tenantName']) && isset($this->params['tenant'])) { $this->params['tenantName'] = $this->params['tenant']; } + $this->params = array_merge(self::DEFAULT_OPTIONS, $this->params); $cacheKey = $userName . '@' . $this->params['url'] . '/' . $this->params['container']; $token = $this->getCachedToken($cacheKey); @@ -114,7 +129,7 @@ class SwiftFactory { return $this->auth(IdentityV3Service::factory($httpClient), $cacheKey); } else { - return $this->auth(IdentityV2Service::factory($httpClient), $cacheKey); + return $this->auth(SwiftV2CachingAuthService::factory($httpClient), $cacheKey); } } @@ -127,25 +142,41 @@ class SwiftFactory { private function auth($authService, string $cacheKey) { $this->params['identityService'] = $authService; $this->params['authUrl'] = $this->params['url']; - $client = new OpenStack($this->params); $cachedToken = $this->params['cachedToken']; $hasValidCachedToken = false; - if (\is_array($cachedToken) && ($authService instanceof IdentityV3Service)) { - $token = $authService->generateTokenFromCache($cachedToken); - if (\is_null($token->catalog)) { - $this->logger->warning('Invalid cached token for swift, no catalog set: ' . json_encode($cachedToken)); - } else if ($token->hasExpired()) { - $this->logger->debug('Cached token for swift expired'); + if (\is_array($cachedToken)) { + if ($authService instanceof IdentityV3Service) { + $token = $authService->generateTokenFromCache($cachedToken); + if (\is_null($token->catalog)) { + $this->logger->warning('Invalid cached token for swift, no catalog set: ' . json_encode($cachedToken)); + } else if ($token->hasExpired()) { + $this->logger->debug('Cached token for swift expired'); + } else { + $hasValidCachedToken = true; + } } else { - $hasValidCachedToken = true; + try { + /** @var \OpenStack\Identity\v2\Models\Token $token */ + $token = $authService->model(\OpenStack\Identity\v2\Models\Token::class, $cachedToken['token']); + $now = new \DateTimeImmutable("now"); + if ($token->expires > $now) { + $hasValidCachedToken = true; + $this->params['v2cachedToken'] = $token; + $this->params['v2serviceUrl'] = $cachedToken['serviceUrl']; + } else { + $this->logger->debug('Cached token for swift expired'); + } + } catch (\Exception $e) { + $this->logger->logException($e); + } } } if (!$hasValidCachedToken) { try { - $token = $authService->generateToken($this->params); - $this->cacheToken($token, $cacheKey); + list($token, $serviceUrl) = $authService->authenticate($this->params); + $this->cacheToken($token, $serviceUrl, $cacheKey); } catch (ConnectException $e) { throw new StorageAuthException('Failed to connect to keystone, verify the keystone url', $e); } catch (ClientException $e) { @@ -164,6 +195,9 @@ class SwiftFactory { } } + + $client = new OpenStack($this->params); + return $client; } diff --git a/lib/private/Files/ObjectStore/SwiftV2CachingAuthService.php b/lib/private/Files/ObjectStore/SwiftV2CachingAuthService.php new file mode 100644 index 00000000000..38f1e54dac3 --- /dev/null +++ b/lib/private/Files/ObjectStore/SwiftV2CachingAuthService.php @@ -0,0 +1,35 @@ +<?php declare(strict_types=1); +/** + * @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\Files\ObjectStore; + + +use OpenStack\Identity\v2\Service; + +class SwiftV2CachingAuthService extends Service { + public function authenticate(array $options = []): array { + if (!empty($options['v2cachedToken'])) { + return [$options['v2cachedToken'], $options['v2serviceUrl']]; + } else { + return parent::authenticate($options); + } + } +} |