Merge pull request #8857 from nextcloud/swift-verify-cached-token

verify cached swift token
This commit is contained in:
Morris Jobke 2018-03-19 10:10:04 +01:00 committed by GitHub
commit 63bc633d89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 13 deletions

View File

@ -194,7 +194,11 @@ class Swift extends \OC\Files\Storage\Common {
$this->params = $params; $this->params = $params;
// FIXME: private class... // FIXME: private class...
$this->objectCache = new \OC\Cache\CappedMemoryCache(); $this->objectCache = new \OC\Cache\CappedMemoryCache();
$this->connectionFactory = new SwiftFactory(\OC::$server->getMemCacheFactory()->createDistributed('swift/'), $this->params); $this->connectionFactory = new SwiftFactory(
\OC::$server->getMemCacheFactory()->createDistributed('swift/'),
$this->params,
\OC::$server->getLogger()
);
$this->objectStore = new \OC\Files\ObjectStore\Swift($this->params, $this->connectionFactory); $this->objectStore = new \OC\Files\ObjectStore\Swift($this->params, $this->connectionFactory);
$this->bucket = $params['bucket']; $this->bucket = $params['bucket'];
} }

View File

@ -36,16 +36,15 @@ class Swift implements IObjectStore {
*/ */
private $params; private $params;
/**
* @var \OpenStack\ObjectStore\v1\Models\Container|null
*/
private $container = null;
/** @var SwiftFactory */ /** @var SwiftFactory */
private $swiftFactory; private $swiftFactory;
public function __construct($params, SwiftFactory $connectionFactory = null) { public function __construct($params, SwiftFactory $connectionFactory = null) {
$this->swiftFactory = $connectionFactory ?: new SwiftFactory(\OC::$server->getMemCacheFactory()->createDistributed('swift::'), $params); $this->swiftFactory = $connectionFactory ?: new SwiftFactory(
\OC::$server->getMemCacheFactory()->createDistributed('swift::'),
$params,
\OC::$server->getLogger()
);
$this->params = $params; $this->params = $params;
} }

View File

@ -30,6 +30,7 @@ use GuzzleHttp\HandlerStack;
use OCP\Files\StorageAuthException; use OCP\Files\StorageAuthException;
use OCP\Files\StorageNotAvailableException; use OCP\Files\StorageNotAvailableException;
use OCP\ICache; use OCP\ICache;
use OCP\ILogger;
use OpenStack\Common\Error\BadResponseError; use OpenStack\Common\Error\BadResponseError;
use OpenStack\Common\Auth\Token; use OpenStack\Common\Auth\Token;
use OpenStack\Identity\v2\Service as IdentityV2Service; use OpenStack\Identity\v2\Service as IdentityV2Service;
@ -44,10 +45,12 @@ class SwiftFactory {
private $params; private $params;
/** @var Container|null */ /** @var Container|null */
private $container = null; private $container = null;
private $logger;
public function __construct(ICache $cache, array $params) { public function __construct(ICache $cache, array $params, ILogger $logger) {
$this->cache = $cache; $this->cache = $cache;
$this->params = $params; $this->params = $params;
$this->logger = $logger;
} }
private function getCachedToken(string $cacheKey) { private function getCachedToken(string $cacheKey) {
@ -97,10 +100,7 @@ class SwiftFactory {
$cacheKey = $userName . '@' . $this->params['url'] . '/' . $this->params['container']; $cacheKey = $userName . '@' . $this->params['url'] . '/' . $this->params['container'];
$token = $this->getCachedToken($cacheKey); $token = $this->getCachedToken($cacheKey);
$hasToken = is_array($token) && (new \DateTimeImmutable($token['expires_at'])) > (new \DateTimeImmutable('now')); $this->params['cachedToken'] = $token;
if ($hasToken) {
$this->params['cachedToken'] = $token;
}
$httpClient = new Client([ $httpClient = new Client([
'base_uri' => TransportUtils::normalizeUrl($this->params['url']), 'base_uri' => TransportUtils::normalizeUrl($this->params['url']),
@ -125,7 +125,20 @@ class SwiftFactory {
$this->params['authUrl'] = $this->params['url']; $this->params['authUrl'] = $this->params['url'];
$client = new OpenStack($this->params); $client = new OpenStack($this->params);
if (!isset($this->params['cachedToken'])) { $cachedToken = $this->params['cachedToken'];
$hasValidCachedToken = false;
if (is_array($cachedToken)) {
$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;
}
}
if (!$hasValidCachedToken) {
try { try {
$token = $authService->generateToken($this->params); $token = $authService->generateToken($this->params);
$this->cacheToken($token, $cacheKey); $this->cacheToken($token, $cacheKey);