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