Completed
Push — master ( 2fab6b...63bc63 )
by Morris
18:24
created

SwiftFactory   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 170
rs 8.6
c 0
b 0
f 0
wmc 37
lcom 1
cbo 6

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getCachedToken() 0 8 2
A cacheToken() 0 8 2
C getClient() 0 38 12
C auth() 0 42 11
A getContainer() 0 7 2
C createContainer() 0 28 7
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2018 Robin Appelman <[email protected]>
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
namespace OC\Files\ObjectStore;
24
25
use GuzzleHttp\Client;
26
use GuzzleHttp\Exception\ClientException;
27
use GuzzleHttp\Exception\ConnectException;
28
use GuzzleHttp\Exception\RequestException;
29
use GuzzleHttp\HandlerStack;
30
use OCP\Files\StorageAuthException;
31
use OCP\Files\StorageNotAvailableException;
32
use OCP\ICache;
33
use OCP\ILogger;
34
use OpenStack\Common\Error\BadResponseError;
35
use OpenStack\Common\Auth\Token;
36
use OpenStack\Identity\v2\Service as IdentityV2Service;
37
use OpenStack\Identity\v3\Service as IdentityV3Service;
38
use OpenStack\OpenStack;
39
use OpenStack\Common\Transport\Utils as TransportUtils;
40
use Psr\Http\Message\RequestInterface;
41
use OpenStack\ObjectStore\v1\Models\Container;
42
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);
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) {
0 ignored issues
show
Bug introduced by
The class OpenStack\Identity\v3\Models\Token does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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
			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
		$cachedToken = $this->params['cachedToken'];
129
		$hasValidCachedToken = false;
130
		if (is_array($cachedToken)) {
131
			$token = $authService->generateTokenFromCache($cachedToken);
132
			if (is_null($token->catalog)) {
133
				$this->logger->warning('Invalid cached token for swift, no catalog set: ' . json_encode($cachedToken));
134
			} else if ($token->hasExpired()) {
135
				$this->logger->debug('Cached token for swift expired');
136
			} else {
137
				$hasValidCachedToken = true;
138
			}
139
		}
140
141
		if (!$hasValidCachedToken) {
142
			try {
143
				$token = $authService->generateToken($this->params);
144
				$this->cacheToken($token, $cacheKey);
145
			} catch (ConnectException $e) {
0 ignored issues
show
Bug introduced by
The class GuzzleHttp\Exception\ConnectException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
146
				throw new StorageAuthException('Failed to connect to keystone, verify the keystone url', $e);
147
			} catch (ClientException $e) {
0 ignored issues
show
Bug introduced by
The class GuzzleHttp\Exception\ClientException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
148
				$statusCode = $e->getResponse()->getStatusCode();
149
				if ($statusCode === 404) {
150
					throw new StorageAuthException('Keystone not found, verify the keystone url', $e);
151
				} else if ($statusCode === 412) {
152
					throw new StorageAuthException('Precondition failed, verify the keystone url', $e);
153
				} else if ($statusCode === 401) {
154
					throw new StorageAuthException('Authentication failed, verify the username, password and possibly tenant', $e);
155
				} else {
156
					throw new StorageAuthException('Unknown error', $e);
157
				}
158
			} catch (RequestException $e) {
0 ignored issues
show
Bug introduced by
The class GuzzleHttp\Exception\RequestException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
159
				throw new StorageAuthException('Connection reset while connecting to keystone, verify the keystone url', $e);
160
			}
161
		}
162
163
		return $client;
164
	}
165
166
	/**
167
	 * @return \OpenStack\ObjectStore\v1\Models\Container
168
	 * @throws StorageAuthException
169
	 * @throws StorageNotAvailableException
170
	 */
171
	public function getContainer() {
172
		if (is_null($this->container)) {
173
			$this->container = $this->createContainer();
174
		}
175
176
		return $this->container;
177
	}
178
179
	/**
180
	 * @return \OpenStack\ObjectStore\v1\Models\Container
181
	 * @throws StorageAuthException
182
	 * @throws StorageNotAvailableException
183
	 */
184
	private function createContainer() {
185
		$client = $this->getClient();
186
		$objectStoreService = $client->objectStoreV1();
187
188
		$autoCreate = isset($this->params['autocreate']) && $this->params['autocreate'] === true;
189
		try {
190
			$container = $objectStoreService->getContainer($this->params['container']);
191
			if ($autoCreate) {
192
				$container->getMetadata();
193
			}
194
			return $container;
195
		} catch (BadResponseError $ex) {
0 ignored issues
show
Bug introduced by
The class OpenStack\Common\Error\BadResponseError does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
196
			// if the container does not exist and autocreate is true try to create the container on the fly
197
			if ($ex->getResponse()->getStatusCode() === 404 && $autoCreate) {
198
				return $objectStoreService->createContainer([
199
					'name' => $this->params['container']
200
				]);
201
			} else {
202
				throw new StorageNotAvailableException('Invalid response while trying to get container info', StorageNotAvailableException::STATUS_ERROR, $e);
203
			}
204
		} catch (ConnectException $e) {
0 ignored issues
show
Bug introduced by
The class GuzzleHttp\Exception\ConnectException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
205
			/** @var RequestInterface $request */
206
			$request = $e->getRequest();
207
			$host = $request->getUri()->getHost() . ':' . $request->getUri()->getPort();
208
			\OC::$server->getLogger()->error("Can't connect to object storage server at $host");
209
			throw new StorageNotAvailableException("Can't connect to object storage server at $host", StorageNotAvailableException::STATUS_ERROR, $e);
210
		}
211
	}
212
}
213