2018-02-15 12:50:23 +03:00
|
|
|
<?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 GuzzleHttp\Client;
|
|
|
|
use GuzzleHttp\Exception\ClientException;
|
|
|
|
use GuzzleHttp\Exception\ConnectException;
|
|
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
use GuzzleHttp\HandlerStack;
|
|
|
|
use OCP\Files\StorageAuthException;
|
|
|
|
use OCP\Files\StorageNotAvailableException;
|
|
|
|
use OCP\ICache;
|
2018-03-16 17:20:16 +03:00
|
|
|
use OCP\ILogger;
|
2018-02-14 17:08:52 +03:00
|
|
|
use OpenStack\Common\Auth\Token;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OpenStack\Common\Error\BadResponseError;
|
|
|
|
use OpenStack\Common\Transport\Utils as TransportUtils;
|
2018-12-18 16:27:29 +03:00
|
|
|
use OpenStack\Identity\v2\Models\Catalog;
|
2018-02-14 17:08:52 +03:00
|
|
|
use OpenStack\Identity\v2\Service as IdentityV2Service;
|
|
|
|
use OpenStack\Identity\v3\Service as IdentityV3Service;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OpenStack\ObjectStore\v1\Models\Container;
|
2018-02-15 12:50:23 +03:00
|
|
|
use OpenStack\OpenStack;
|
|
|
|
use Psr\Http\Message\RequestInterface;
|
|
|
|
|
2018-02-12 17:08:27 +03:00
|
|
|
class SwiftFactory {
|
2018-02-15 12:50:23 +03:00
|
|
|
private $cache;
|
2018-02-12 17:08:27 +03:00
|
|
|
private $params;
|
|
|
|
/** @var Container|null */
|
|
|
|
private $container = null;
|
2018-03-16 17:20:16 +03:00
|
|
|
private $logger;
|
2018-02-15 12:50:23 +03:00
|
|
|
|
2018-12-18 16:27:29 +03:00
|
|
|
const DEFAULT_OPTIONS = [
|
|
|
|
'autocreate' => false,
|
|
|
|
'urlType' => 'publicURL',
|
|
|
|
'catalogName' => 'swift',
|
|
|
|
'catalogType' => 'object-store'
|
|
|
|
];
|
|
|
|
|
2018-03-16 17:20:16 +03:00
|
|
|
public function __construct(ICache $cache, array $params, ILogger $logger) {
|
2018-02-15 12:50:23 +03:00
|
|
|
$this->cache = $cache;
|
2018-02-12 17:08:27 +03:00
|
|
|
$this->params = $params;
|
2018-03-16 17:20:16 +03:00
|
|
|
$this->logger = $logger;
|
2018-02-15 12:50:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getCachedToken(string $cacheKey) {
|
|
|
|
$cachedTokenString = $this->cache->get($cacheKey . '/token');
|
|
|
|
if ($cachedTokenString) {
|
2018-04-26 21:47:47 +03:00
|
|
|
return json_decode($cachedTokenString, true);
|
2018-02-15 12:50:23 +03:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-18 16:27:29 +03:00
|
|
|
private function cacheToken(Token $token, string $serviceUrl, string $cacheKey) {
|
2018-03-12 22:53:38 +03:00
|
|
|
if ($token instanceof \OpenStack\Identity\v3\Models\Token) {
|
2018-12-18 16:27:29 +03:00
|
|
|
// for v3 the catalog is cached as part of the token, so no need to cache $serviceUrl separately
|
2018-03-12 22:53:38 +03:00
|
|
|
$value = json_encode($token->export());
|
|
|
|
} else {
|
2018-12-18 16:27:29 +03:00
|
|
|
/** @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
|
|
|
|
]
|
|
|
|
]);
|
2018-03-12 22:53:38 +03:00
|
|
|
}
|
|
|
|
$this->cache->set($cacheKey . '/token', $value);
|
2018-02-15 12:50:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return OpenStack
|
|
|
|
* @throws StorageAuthException
|
|
|
|
*/
|
2018-02-12 17:08:27 +03:00
|
|
|
private function getClient() {
|
|
|
|
if (isset($this->params['bucket'])) {
|
|
|
|
$this->params['container'] = $this->params['bucket'];
|
2018-02-15 12:50:23 +03:00
|
|
|
}
|
2018-02-12 17:08:27 +03:00
|
|
|
if (!isset($this->params['container'])) {
|
2018-03-12 21:59:33 +03:00
|
|
|
$this->params['container'] = 'nextcloud';
|
2018-02-15 12:50:23 +03:00
|
|
|
}
|
2018-02-14 17:08:52 +03:00
|
|
|
if (isset($this->params['user']) && is_array($this->params['user'])) {
|
2018-02-21 18:26:58 +03:00
|
|
|
$userName = $this->params['user']['name'];
|
2018-02-14 17:08:52 +03:00
|
|
|
} else {
|
|
|
|
if (!isset($this->params['username']) && isset($this->params['user'])) {
|
|
|
|
$this->params['username'] = $this->params['user'];
|
|
|
|
}
|
|
|
|
$userName = $this->params['username'];
|
2018-02-12 17:08:27 +03:00
|
|
|
}
|
|
|
|
if (!isset($this->params['tenantName']) && isset($this->params['tenant'])) {
|
|
|
|
$this->params['tenantName'] = $this->params['tenant'];
|
2018-02-15 12:50:23 +03:00
|
|
|
}
|
2018-12-18 16:27:29 +03:00
|
|
|
$this->params = array_merge(self::DEFAULT_OPTIONS, $this->params);
|
2018-02-15 12:50:23 +03:00
|
|
|
|
2018-03-12 21:59:33 +03:00
|
|
|
$cacheKey = $userName . '@' . $this->params['url'] . '/' . $this->params['container'];
|
2018-02-15 12:50:23 +03:00
|
|
|
$token = $this->getCachedToken($cacheKey);
|
2018-03-16 17:20:16 +03:00
|
|
|
$this->params['cachedToken'] = $token;
|
2018-02-14 17:08:52 +03:00
|
|
|
|
2018-02-15 12:50:23 +03:00
|
|
|
$httpClient = new Client([
|
2018-02-12 17:08:27 +03:00
|
|
|
'base_uri' => TransportUtils::normalizeUrl($this->params['url']),
|
2018-02-15 12:50:23 +03:00
|
|
|
'handler' => HandlerStack::create()
|
|
|
|
]);
|
|
|
|
|
2019-02-04 19:29:35 +03:00
|
|
|
if (isset($this->params['user']) && is_array($this->params['user']) && isset($this->params['user']['name'])) {
|
2018-04-23 20:32:41 +03:00
|
|
|
if (!isset($this->params['scope'])) {
|
|
|
|
throw new StorageAuthException('Scope has to be defined for V3 requests');
|
|
|
|
}
|
|
|
|
|
2018-02-14 17:08:52 +03:00
|
|
|
return $this->auth(IdentityV3Service::factory($httpClient), $cacheKey);
|
|
|
|
} else {
|
2018-12-18 16:27:29 +03:00
|
|
|
return $this->auth(SwiftV2CachingAuthService::factory($httpClient), $cacheKey);
|
2018-02-14 17:08:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IdentityV2Service|IdentityV3Service $authService
|
|
|
|
* @param string $cacheKey
|
|
|
|
* @return OpenStack
|
|
|
|
* @throws StorageAuthException
|
|
|
|
*/
|
|
|
|
private function auth($authService, string $cacheKey) {
|
2018-02-12 17:08:27 +03:00
|
|
|
$this->params['identityService'] = $authService;
|
|
|
|
$this->params['authUrl'] = $this->params['url'];
|
2018-02-15 12:50:23 +03:00
|
|
|
|
2018-03-16 17:20:16 +03:00
|
|
|
$cachedToken = $this->params['cachedToken'];
|
|
|
|
$hasValidCachedToken = false;
|
2018-12-18 16:27:29 +03:00
|
|
|
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;
|
|
|
|
}
|
2018-03-16 17:20:16 +03:00
|
|
|
} else {
|
2018-12-18 16:27:29 +03:00
|
|
|
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);
|
|
|
|
}
|
2018-03-16 17:20:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$hasValidCachedToken) {
|
2019-02-13 14:26:27 +03:00
|
|
|
unset($this->params['cachedToken']);
|
2018-02-15 12:50:23 +03:00
|
|
|
try {
|
2018-12-18 16:27:29 +03:00
|
|
|
list($token, $serviceUrl) = $authService->authenticate($this->params);
|
|
|
|
$this->cacheToken($token, $serviceUrl, $cacheKey);
|
2018-02-15 12:50:23 +03:00
|
|
|
} catch (ConnectException $e) {
|
|
|
|
throw new StorageAuthException('Failed to connect to keystone, verify the keystone url', $e);
|
|
|
|
} catch (ClientException $e) {
|
|
|
|
$statusCode = $e->getResponse()->getStatusCode();
|
|
|
|
if ($statusCode === 404) {
|
|
|
|
throw new StorageAuthException('Keystone not found, verify the keystone url', $e);
|
|
|
|
} else 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);
|
|
|
|
}
|
|
|
|
} catch (RequestException $e) {
|
|
|
|
throw new StorageAuthException('Connection reset while connecting to keystone, verify the keystone url', $e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-18 16:27:29 +03:00
|
|
|
|
|
|
|
$client = new OpenStack($this->params);
|
|
|
|
|
2018-02-15 12:50:23 +03:00
|
|
|
return $client;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \OpenStack\ObjectStore\v1\Models\Container
|
|
|
|
* @throws StorageAuthException
|
2018-02-12 17:08:27 +03:00
|
|
|
* @throws StorageNotAvailableException
|
2018-02-15 12:50:23 +03:00
|
|
|
*/
|
2018-02-12 17:08:27 +03:00
|
|
|
public function getContainer() {
|
|
|
|
if (is_null($this->container)) {
|
|
|
|
$this->container = $this->createContainer();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->container;
|
|
|
|
}
|
2018-02-15 12:50:23 +03:00
|
|
|
|
2018-02-12 17:08:27 +03:00
|
|
|
/**
|
|
|
|
* @return \OpenStack\ObjectStore\v1\Models\Container
|
|
|
|
* @throws StorageAuthException
|
|
|
|
* @throws StorageNotAvailableException
|
|
|
|
*/
|
|
|
|
private function createContainer() {
|
|
|
|
$client = $this->getClient();
|
2018-02-15 12:50:23 +03:00
|
|
|
$objectStoreService = $client->objectStoreV1();
|
|
|
|
|
2018-02-12 17:08:27 +03:00
|
|
|
$autoCreate = isset($this->params['autocreate']) && $this->params['autocreate'] === true;
|
2018-02-15 12:50:23 +03:00
|
|
|
try {
|
2018-02-12 17:08:27 +03:00
|
|
|
$container = $objectStoreService->getContainer($this->params['container']);
|
2018-02-15 12:50:23 +03:00
|
|
|
if ($autoCreate) {
|
|
|
|
$container->getMetadata();
|
|
|
|
}
|
|
|
|
return $container;
|
|
|
|
} catch (BadResponseError $ex) {
|
|
|
|
// if the container does not exist and autocreate is true try to create the container on the fly
|
|
|
|
if ($ex->getResponse()->getStatusCode() === 404 && $autoCreate) {
|
|
|
|
return $objectStoreService->createContainer([
|
2018-02-12 17:08:27 +03:00
|
|
|
'name' => $this->params['container']
|
2018-02-15 12:50:23 +03:00
|
|
|
]);
|
|
|
|
} else {
|
2018-04-23 12:34:37 +03:00
|
|
|
throw new StorageNotAvailableException('Invalid response while trying to get container info', StorageNotAvailableException::STATUS_ERROR, $ex);
|
2018-02-15 12:50:23 +03:00
|
|
|
}
|
|
|
|
} catch (ConnectException $e) {
|
|
|
|
/** @var RequestInterface $request */
|
|
|
|
$request = $e->getRequest();
|
|
|
|
$host = $request->getUri()->getHost() . ':' . $request->getUri()->getPort();
|
|
|
|
\OC::$server->getLogger()->error("Can't connect to object storage server at $host");
|
|
|
|
throw new StorageNotAvailableException("Can't connect to object storage server at $host", StorageNotAvailableException::STATUS_ERROR, $e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|