@@ -41,176 +41,176 @@ |
||
| 41 | 41 | use OpenStack\ObjectStore\v1\Models\Container; |
| 42 | 42 | |
| 43 | 43 | class SwiftFactory { |
| 44 | - private $cache; |
|
| 45 | - private $params; |
|
| 46 | - /** @var Container|null */ |
|
| 47 | - private $container = null; |
|
| 48 | - private $logger; |
|
| 49 | - |
|
| 50 | - public function __construct(ICache $cache, array $params, ILogger $logger) { |
|
| 51 | - $this->cache = $cache; |
|
| 52 | - $this->params = $params; |
|
| 53 | - $this->logger = $logger; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - private function getCachedToken(string $cacheKey) { |
|
| 57 | - $cachedTokenString = $this->cache->get($cacheKey . '/token'); |
|
| 58 | - if ($cachedTokenString) { |
|
| 59 | - return json_decode($cachedTokenString, true); |
|
| 60 | - } else { |
|
| 61 | - return null; |
|
| 62 | - } |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - private function cacheToken(Token $token, string $cacheKey) { |
|
| 66 | - if ($token instanceof \OpenStack\Identity\v3\Models\Token) { |
|
| 67 | - $value = json_encode($token->export()); |
|
| 68 | - } else { |
|
| 69 | - $value = json_encode($token); |
|
| 70 | - } |
|
| 71 | - $this->cache->set($cacheKey . '/token', $value); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @return OpenStack |
|
| 76 | - * @throws StorageAuthException |
|
| 77 | - */ |
|
| 78 | - private function getClient() { |
|
| 79 | - if (isset($this->params['bucket'])) { |
|
| 80 | - $this->params['container'] = $this->params['bucket']; |
|
| 81 | - } |
|
| 82 | - if (!isset($this->params['container'])) { |
|
| 83 | - $this->params['container'] = 'nextcloud'; |
|
| 84 | - } |
|
| 85 | - if (!isset($this->params['autocreate'])) { |
|
| 86 | - // should only be true for tests |
|
| 87 | - $this->params['autocreate'] = false; |
|
| 88 | - } |
|
| 89 | - if (isset($this->params['user']) && is_array($this->params['user'])) { |
|
| 90 | - $userName = $this->params['user']['name']; |
|
| 91 | - } else { |
|
| 92 | - if (!isset($this->params['username']) && isset($this->params['user'])) { |
|
| 93 | - $this->params['username'] = $this->params['user']; |
|
| 94 | - } |
|
| 95 | - $userName = $this->params['username']; |
|
| 96 | - } |
|
| 97 | - if (!isset($this->params['tenantName']) && isset($this->params['tenant'])) { |
|
| 98 | - $this->params['tenantName'] = $this->params['tenant']; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - $cacheKey = $userName . '@' . $this->params['url'] . '/' . $this->params['container']; |
|
| 102 | - $token = $this->getCachedToken($cacheKey); |
|
| 103 | - $this->params['cachedToken'] = $token; |
|
| 104 | - |
|
| 105 | - $httpClient = new Client([ |
|
| 106 | - 'base_uri' => TransportUtils::normalizeUrl($this->params['url']), |
|
| 107 | - 'handler' => HandlerStack::create() |
|
| 108 | - ]); |
|
| 109 | - |
|
| 110 | - if (isset($this->params['user']) && isset($this->params['user']['name'])) { |
|
| 111 | - if (!isset($this->params['scope'])) { |
|
| 112 | - throw new StorageAuthException('Scope has to be defined for V3 requests'); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - return $this->auth(IdentityV3Service::factory($httpClient), $cacheKey); |
|
| 116 | - } else { |
|
| 117 | - return $this->auth(IdentityV2Service::factory($httpClient), $cacheKey); |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * @param IdentityV2Service|IdentityV3Service $authService |
|
| 123 | - * @param string $cacheKey |
|
| 124 | - * @return OpenStack |
|
| 125 | - * @throws StorageAuthException |
|
| 126 | - */ |
|
| 127 | - private function auth($authService, string $cacheKey) { |
|
| 128 | - $this->params['identityService'] = $authService; |
|
| 129 | - $this->params['authUrl'] = $this->params['url']; |
|
| 130 | - $client = new OpenStack($this->params); |
|
| 131 | - |
|
| 132 | - $cachedToken = $this->params['cachedToken']; |
|
| 133 | - $hasValidCachedToken = false; |
|
| 134 | - if (\is_array($cachedToken) && ($authService instanceof IdentityV3Service)) { |
|
| 135 | - $token = $authService->generateTokenFromCache($cachedToken); |
|
| 136 | - if (\is_null($token->catalog)) { |
|
| 137 | - $this->logger->warning('Invalid cached token for swift, no catalog set: ' . json_encode($cachedToken)); |
|
| 138 | - } else if ($token->hasExpired()) { |
|
| 139 | - $this->logger->debug('Cached token for swift expired'); |
|
| 140 | - } else { |
|
| 141 | - $hasValidCachedToken = true; |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - if (!$hasValidCachedToken) { |
|
| 146 | - try { |
|
| 147 | - $token = $authService->generateToken($this->params); |
|
| 148 | - $this->cacheToken($token, $cacheKey); |
|
| 149 | - } catch (ConnectException $e) { |
|
| 150 | - throw new StorageAuthException('Failed to connect to keystone, verify the keystone url', $e); |
|
| 151 | - } catch (ClientException $e) { |
|
| 152 | - $statusCode = $e->getResponse()->getStatusCode(); |
|
| 153 | - if ($statusCode === 404) { |
|
| 154 | - throw new StorageAuthException('Keystone not found, verify the keystone url', $e); |
|
| 155 | - } else if ($statusCode === 412) { |
|
| 156 | - throw new StorageAuthException('Precondition failed, verify the keystone url', $e); |
|
| 157 | - } else if ($statusCode === 401) { |
|
| 158 | - throw new StorageAuthException('Authentication failed, verify the username, password and possibly tenant', $e); |
|
| 159 | - } else { |
|
| 160 | - throw new StorageAuthException('Unknown error', $e); |
|
| 161 | - } |
|
| 162 | - } catch (RequestException $e) { |
|
| 163 | - throw new StorageAuthException('Connection reset while connecting to keystone, verify the keystone url', $e); |
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - return $client; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * @return \OpenStack\ObjectStore\v1\Models\Container |
|
| 172 | - * @throws StorageAuthException |
|
| 173 | - * @throws StorageNotAvailableException |
|
| 174 | - */ |
|
| 175 | - public function getContainer() { |
|
| 176 | - if (is_null($this->container)) { |
|
| 177 | - $this->container = $this->createContainer(); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - return $this->container; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * @return \OpenStack\ObjectStore\v1\Models\Container |
|
| 185 | - * @throws StorageAuthException |
|
| 186 | - * @throws StorageNotAvailableException |
|
| 187 | - */ |
|
| 188 | - private function createContainer() { |
|
| 189 | - $client = $this->getClient(); |
|
| 190 | - $objectStoreService = $client->objectStoreV1(); |
|
| 191 | - |
|
| 192 | - $autoCreate = isset($this->params['autocreate']) && $this->params['autocreate'] === true; |
|
| 193 | - try { |
|
| 194 | - $container = $objectStoreService->getContainer($this->params['container']); |
|
| 195 | - if ($autoCreate) { |
|
| 196 | - $container->getMetadata(); |
|
| 197 | - } |
|
| 198 | - return $container; |
|
| 199 | - } catch (BadResponseError $ex) { |
|
| 200 | - // if the container does not exist and autocreate is true try to create the container on the fly |
|
| 201 | - if ($ex->getResponse()->getStatusCode() === 404 && $autoCreate) { |
|
| 202 | - return $objectStoreService->createContainer([ |
|
| 203 | - 'name' => $this->params['container'] |
|
| 204 | - ]); |
|
| 205 | - } else { |
|
| 206 | - throw new StorageNotAvailableException('Invalid response while trying to get container info', StorageNotAvailableException::STATUS_ERROR, $ex); |
|
| 207 | - } |
|
| 208 | - } catch (ConnectException $e) { |
|
| 209 | - /** @var RequestInterface $request */ |
|
| 210 | - $request = $e->getRequest(); |
|
| 211 | - $host = $request->getUri()->getHost() . ':' . $request->getUri()->getPort(); |
|
| 212 | - \OC::$server->getLogger()->error("Can't connect to object storage server at $host"); |
|
| 213 | - throw new StorageNotAvailableException("Can't connect to object storage server at $host", StorageNotAvailableException::STATUS_ERROR, $e); |
|
| 214 | - } |
|
| 215 | - } |
|
| 44 | + private $cache; |
|
| 45 | + private $params; |
|
| 46 | + /** @var Container|null */ |
|
| 47 | + private $container = null; |
|
| 48 | + private $logger; |
|
| 49 | + |
|
| 50 | + public function __construct(ICache $cache, array $params, ILogger $logger) { |
|
| 51 | + $this->cache = $cache; |
|
| 52 | + $this->params = $params; |
|
| 53 | + $this->logger = $logger; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + private function getCachedToken(string $cacheKey) { |
|
| 57 | + $cachedTokenString = $this->cache->get($cacheKey . '/token'); |
|
| 58 | + if ($cachedTokenString) { |
|
| 59 | + return json_decode($cachedTokenString, true); |
|
| 60 | + } else { |
|
| 61 | + return null; |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + private function cacheToken(Token $token, string $cacheKey) { |
|
| 66 | + if ($token instanceof \OpenStack\Identity\v3\Models\Token) { |
|
| 67 | + $value = json_encode($token->export()); |
|
| 68 | + } else { |
|
| 69 | + $value = json_encode($token); |
|
| 70 | + } |
|
| 71 | + $this->cache->set($cacheKey . '/token', $value); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @return OpenStack |
|
| 76 | + * @throws StorageAuthException |
|
| 77 | + */ |
|
| 78 | + private function getClient() { |
|
| 79 | + if (isset($this->params['bucket'])) { |
|
| 80 | + $this->params['container'] = $this->params['bucket']; |
|
| 81 | + } |
|
| 82 | + if (!isset($this->params['container'])) { |
|
| 83 | + $this->params['container'] = 'nextcloud'; |
|
| 84 | + } |
|
| 85 | + if (!isset($this->params['autocreate'])) { |
|
| 86 | + // should only be true for tests |
|
| 87 | + $this->params['autocreate'] = false; |
|
| 88 | + } |
|
| 89 | + if (isset($this->params['user']) && is_array($this->params['user'])) { |
|
| 90 | + $userName = $this->params['user']['name']; |
|
| 91 | + } else { |
|
| 92 | + if (!isset($this->params['username']) && isset($this->params['user'])) { |
|
| 93 | + $this->params['username'] = $this->params['user']; |
|
| 94 | + } |
|
| 95 | + $userName = $this->params['username']; |
|
| 96 | + } |
|
| 97 | + if (!isset($this->params['tenantName']) && isset($this->params['tenant'])) { |
|
| 98 | + $this->params['tenantName'] = $this->params['tenant']; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + $cacheKey = $userName . '@' . $this->params['url'] . '/' . $this->params['container']; |
|
| 102 | + $token = $this->getCachedToken($cacheKey); |
|
| 103 | + $this->params['cachedToken'] = $token; |
|
| 104 | + |
|
| 105 | + $httpClient = new Client([ |
|
| 106 | + 'base_uri' => TransportUtils::normalizeUrl($this->params['url']), |
|
| 107 | + 'handler' => HandlerStack::create() |
|
| 108 | + ]); |
|
| 109 | + |
|
| 110 | + if (isset($this->params['user']) && isset($this->params['user']['name'])) { |
|
| 111 | + if (!isset($this->params['scope'])) { |
|
| 112 | + throw new StorageAuthException('Scope has to be defined for V3 requests'); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + return $this->auth(IdentityV3Service::factory($httpClient), $cacheKey); |
|
| 116 | + } else { |
|
| 117 | + return $this->auth(IdentityV2Service::factory($httpClient), $cacheKey); |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * @param IdentityV2Service|IdentityV3Service $authService |
|
| 123 | + * @param string $cacheKey |
|
| 124 | + * @return OpenStack |
|
| 125 | + * @throws StorageAuthException |
|
| 126 | + */ |
|
| 127 | + private function auth($authService, string $cacheKey) { |
|
| 128 | + $this->params['identityService'] = $authService; |
|
| 129 | + $this->params['authUrl'] = $this->params['url']; |
|
| 130 | + $client = new OpenStack($this->params); |
|
| 131 | + |
|
| 132 | + $cachedToken = $this->params['cachedToken']; |
|
| 133 | + $hasValidCachedToken = false; |
|
| 134 | + if (\is_array($cachedToken) && ($authService instanceof IdentityV3Service)) { |
|
| 135 | + $token = $authService->generateTokenFromCache($cachedToken); |
|
| 136 | + if (\is_null($token->catalog)) { |
|
| 137 | + $this->logger->warning('Invalid cached token for swift, no catalog set: ' . json_encode($cachedToken)); |
|
| 138 | + } else if ($token->hasExpired()) { |
|
| 139 | + $this->logger->debug('Cached token for swift expired'); |
|
| 140 | + } else { |
|
| 141 | + $hasValidCachedToken = true; |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + if (!$hasValidCachedToken) { |
|
| 146 | + try { |
|
| 147 | + $token = $authService->generateToken($this->params); |
|
| 148 | + $this->cacheToken($token, $cacheKey); |
|
| 149 | + } catch (ConnectException $e) { |
|
| 150 | + throw new StorageAuthException('Failed to connect to keystone, verify the keystone url', $e); |
|
| 151 | + } catch (ClientException $e) { |
|
| 152 | + $statusCode = $e->getResponse()->getStatusCode(); |
|
| 153 | + if ($statusCode === 404) { |
|
| 154 | + throw new StorageAuthException('Keystone not found, verify the keystone url', $e); |
|
| 155 | + } else if ($statusCode === 412) { |
|
| 156 | + throw new StorageAuthException('Precondition failed, verify the keystone url', $e); |
|
| 157 | + } else if ($statusCode === 401) { |
|
| 158 | + throw new StorageAuthException('Authentication failed, verify the username, password and possibly tenant', $e); |
|
| 159 | + } else { |
|
| 160 | + throw new StorageAuthException('Unknown error', $e); |
|
| 161 | + } |
|
| 162 | + } catch (RequestException $e) { |
|
| 163 | + throw new StorageAuthException('Connection reset while connecting to keystone, verify the keystone url', $e); |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + return $client; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * @return \OpenStack\ObjectStore\v1\Models\Container |
|
| 172 | + * @throws StorageAuthException |
|
| 173 | + * @throws StorageNotAvailableException |
|
| 174 | + */ |
|
| 175 | + public function getContainer() { |
|
| 176 | + if (is_null($this->container)) { |
|
| 177 | + $this->container = $this->createContainer(); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + return $this->container; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * @return \OpenStack\ObjectStore\v1\Models\Container |
|
| 185 | + * @throws StorageAuthException |
|
| 186 | + * @throws StorageNotAvailableException |
|
| 187 | + */ |
|
| 188 | + private function createContainer() { |
|
| 189 | + $client = $this->getClient(); |
|
| 190 | + $objectStoreService = $client->objectStoreV1(); |
|
| 191 | + |
|
| 192 | + $autoCreate = isset($this->params['autocreate']) && $this->params['autocreate'] === true; |
|
| 193 | + try { |
|
| 194 | + $container = $objectStoreService->getContainer($this->params['container']); |
|
| 195 | + if ($autoCreate) { |
|
| 196 | + $container->getMetadata(); |
|
| 197 | + } |
|
| 198 | + return $container; |
|
| 199 | + } catch (BadResponseError $ex) { |
|
| 200 | + // if the container does not exist and autocreate is true try to create the container on the fly |
|
| 201 | + if ($ex->getResponse()->getStatusCode() === 404 && $autoCreate) { |
|
| 202 | + return $objectStoreService->createContainer([ |
|
| 203 | + 'name' => $this->params['container'] |
|
| 204 | + ]); |
|
| 205 | + } else { |
|
| 206 | + throw new StorageNotAvailableException('Invalid response while trying to get container info', StorageNotAvailableException::STATUS_ERROR, $ex); |
|
| 207 | + } |
|
| 208 | + } catch (ConnectException $e) { |
|
| 209 | + /** @var RequestInterface $request */ |
|
| 210 | + $request = $e->getRequest(); |
|
| 211 | + $host = $request->getUri()->getHost() . ':' . $request->getUri()->getPort(); |
|
| 212 | + \OC::$server->getLogger()->error("Can't connect to object storage server at $host"); |
|
| 213 | + throw new StorageNotAvailableException("Can't connect to object storage server at $host", StorageNotAvailableException::STATUS_ERROR, $e); |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | 216 | } |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | private function getCachedToken(string $cacheKey) { |
| 57 | - $cachedTokenString = $this->cache->get($cacheKey . '/token'); |
|
| 57 | + $cachedTokenString = $this->cache->get($cacheKey.'/token'); |
|
| 58 | 58 | if ($cachedTokenString) { |
| 59 | 59 | return json_decode($cachedTokenString, true); |
| 60 | 60 | } else { |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | } else { |
| 69 | 69 | $value = json_encode($token); |
| 70 | 70 | } |
| 71 | - $this->cache->set($cacheKey . '/token', $value); |
|
| 71 | + $this->cache->set($cacheKey.'/token', $value); |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | /** |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | $this->params['tenantName'] = $this->params['tenant']; |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | - $cacheKey = $userName . '@' . $this->params['url'] . '/' . $this->params['container']; |
|
| 101 | + $cacheKey = $userName.'@'.$this->params['url'].'/'.$this->params['container']; |
|
| 102 | 102 | $token = $this->getCachedToken($cacheKey); |
| 103 | 103 | $this->params['cachedToken'] = $token; |
| 104 | 104 | |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | if (\is_array($cachedToken) && ($authService instanceof IdentityV3Service)) { |
| 135 | 135 | $token = $authService->generateTokenFromCache($cachedToken); |
| 136 | 136 | if (\is_null($token->catalog)) { |
| 137 | - $this->logger->warning('Invalid cached token for swift, no catalog set: ' . json_encode($cachedToken)); |
|
| 137 | + $this->logger->warning('Invalid cached token for swift, no catalog set: '.json_encode($cachedToken)); |
|
| 138 | 138 | } else if ($token->hasExpired()) { |
| 139 | 139 | $this->logger->debug('Cached token for swift expired'); |
| 140 | 140 | } else { |
@@ -208,7 +208,7 @@ discard block |
||
| 208 | 208 | } catch (ConnectException $e) { |
| 209 | 209 | /** @var RequestInterface $request */ |
| 210 | 210 | $request = $e->getRequest(); |
| 211 | - $host = $request->getUri()->getHost() . ':' . $request->getUri()->getPort(); |
|
| 211 | + $host = $request->getUri()->getHost().':'.$request->getUri()->getPort(); |
|
| 212 | 212 | \OC::$server->getLogger()->error("Can't connect to object storage server at $host"); |
| 213 | 213 | throw new StorageNotAvailableException("Can't connect to object storage server at $host", StorageNotAvailableException::STATUS_ERROR, $e); |
| 214 | 214 | } |