@@ -42,162 +42,162 @@ |
||
| 42 | 42 | use OCP\ILogger; |
| 43 | 43 | |
| 44 | 44 | trait S3ConnectionTrait { |
| 45 | - /** @var array */ |
|
| 46 | - protected $params; |
|
| 47 | - |
|
| 48 | - /** @var S3Client */ |
|
| 49 | - protected $connection; |
|
| 50 | - |
|
| 51 | - /** @var string */ |
|
| 52 | - protected $id; |
|
| 53 | - |
|
| 54 | - /** @var string */ |
|
| 55 | - protected $bucket; |
|
| 56 | - |
|
| 57 | - /** @var int */ |
|
| 58 | - protected $timeout; |
|
| 59 | - |
|
| 60 | - /** @var int */ |
|
| 61 | - protected $uploadPartSize; |
|
| 62 | - |
|
| 63 | - protected $test; |
|
| 64 | - |
|
| 65 | - protected function parseParams($params) { |
|
| 66 | - if (empty($params['bucket'])) { |
|
| 67 | - throw new \Exception("Bucket has to be configured."); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $this->id = 'amazon::' . $params['bucket']; |
|
| 71 | - |
|
| 72 | - $this->test = isset($params['test']); |
|
| 73 | - $this->bucket = $params['bucket']; |
|
| 74 | - $this->timeout = !isset($params['timeout']) ? 15 : $params['timeout']; |
|
| 75 | - $this->uploadPartSize = !isset($params['uploadPartSize']) ? 524288000 : $params['uploadPartSize']; |
|
| 76 | - $params['region'] = empty($params['region']) ? 'eu-west-1' : $params['region']; |
|
| 77 | - $params['hostname'] = empty($params['hostname']) ? 's3.' . $params['region'] . '.amazonaws.com' : $params['hostname']; |
|
| 78 | - if (!isset($params['port']) || $params['port'] === '') { |
|
| 79 | - $params['port'] = (isset($params['use_ssl']) && $params['use_ssl'] === false) ? 80 : 443; |
|
| 80 | - } |
|
| 81 | - $params['verify_bucket_exists'] = empty($params['verify_bucket_exists']) ? true : $params['verify_bucket_exists']; |
|
| 82 | - $this->params = $params; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - public function getBucket() { |
|
| 86 | - return $this->bucket; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Returns the connection |
|
| 91 | - * |
|
| 92 | - * @return S3Client connected client |
|
| 93 | - * @throws \Exception if connection could not be made |
|
| 94 | - */ |
|
| 95 | - public function getConnection() { |
|
| 96 | - if (!is_null($this->connection)) { |
|
| 97 | - return $this->connection; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $scheme = (isset($this->params['use_ssl']) && $this->params['use_ssl'] === false) ? 'http' : 'https'; |
|
| 101 | - $base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/'; |
|
| 102 | - |
|
| 103 | - // Adding explicit credential provider to the beginning chain. |
|
| 104 | - // Including environment variables and IAM instance profiles. |
|
| 105 | - $provider = CredentialProvider::memoize( |
|
| 106 | - CredentialProvider::chain( |
|
| 107 | - $this->paramCredentialProvider(), |
|
| 108 | - CredentialProvider::env(), |
|
| 109 | - CredentialProvider::assumeRoleWithWebIdentityCredentialProvider(), |
|
| 110 | - !empty(getenv(EcsCredentialProvider::ENV_URI)) |
|
| 111 | - ? CredentialProvider::ecsCredentials() |
|
| 112 | - : CredentialProvider::instanceProfile() |
|
| 113 | - ) |
|
| 114 | - ); |
|
| 115 | - |
|
| 116 | - $options = [ |
|
| 117 | - 'version' => isset($this->params['version']) ? $this->params['version'] : 'latest', |
|
| 118 | - 'credentials' => $provider, |
|
| 119 | - 'endpoint' => $base_url, |
|
| 120 | - 'region' => $this->params['region'], |
|
| 121 | - 'use_path_style_endpoint' => isset($this->params['use_path_style']) ? $this->params['use_path_style'] : false, |
|
| 122 | - 'signature_provider' => \Aws\or_chain([self::class, 'legacySignatureProvider'], ClientResolver::_default_signature_provider()), |
|
| 123 | - 'csm' => false, |
|
| 124 | - ]; |
|
| 125 | - if (isset($this->params['proxy'])) { |
|
| 126 | - $options['request.options'] = ['proxy' => $this->params['proxy']]; |
|
| 127 | - } |
|
| 128 | - if (isset($this->params['legacy_auth']) && $this->params['legacy_auth']) { |
|
| 129 | - $options['signature_version'] = 'v2'; |
|
| 130 | - } |
|
| 131 | - $this->connection = new S3Client($options); |
|
| 132 | - |
|
| 133 | - if (!$this->connection::isBucketDnsCompatible($this->bucket)) { |
|
| 134 | - $logger = \OC::$server->getLogger(); |
|
| 135 | - $logger->debug('Bucket "' . $this->bucket . '" This bucket name is not dns compatible, it may contain invalid characters.', |
|
| 136 | - ['app' => 'objectstore']); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - if ($this->params['verify_bucket_exists'] && !$this->connection->doesBucketExist($this->bucket)) { |
|
| 140 | - $logger = \OC::$server->getLogger(); |
|
| 141 | - try { |
|
| 142 | - $logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']); |
|
| 143 | - if (!$this->connection::isBucketDnsCompatible($this->bucket)) { |
|
| 144 | - throw new \Exception("The bucket will not be created because the name is not dns compatible, please correct it: " . $this->bucket); |
|
| 145 | - } |
|
| 146 | - $this->connection->createBucket(['Bucket' => $this->bucket]); |
|
| 147 | - $this->testTimeout(); |
|
| 148 | - } catch (S3Exception $e) { |
|
| 149 | - $logger->logException($e, [ |
|
| 150 | - 'message' => 'Invalid remote storage.', |
|
| 151 | - 'level' => ILogger::DEBUG, |
|
| 152 | - 'app' => 'objectstore', |
|
| 153 | - ]); |
|
| 154 | - throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage()); |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - // google cloud's s3 compatibility doesn't like the EncodingType parameter |
|
| 159 | - if (strpos($base_url, 'storage.googleapis.com')) { |
|
| 160 | - $this->connection->getHandlerList()->remove('s3.auto_encode'); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - return $this->connection; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * when running the tests wait to let the buckets catch up |
|
| 168 | - */ |
|
| 169 | - private function testTimeout() { |
|
| 170 | - if ($this->test) { |
|
| 171 | - sleep($this->timeout); |
|
| 172 | - } |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - public static function legacySignatureProvider($version, $service, $region) { |
|
| 176 | - switch ($version) { |
|
| 177 | - case 'v2': |
|
| 178 | - case 's3': |
|
| 179 | - return new S3Signature(); |
|
| 180 | - default: |
|
| 181 | - return null; |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * This function creates a credential provider based on user parameter file |
|
| 187 | - */ |
|
| 188 | - protected function paramCredentialProvider() : callable { |
|
| 189 | - return function () { |
|
| 190 | - $key = empty($this->params['key']) ? null : $this->params['key']; |
|
| 191 | - $secret = empty($this->params['secret']) ? null : $this->params['secret']; |
|
| 192 | - |
|
| 193 | - if ($key && $secret) { |
|
| 194 | - return Promise\promise_for( |
|
| 195 | - new Credentials($key, $secret) |
|
| 196 | - ); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - $msg = 'Could not find parameters set for credentials in config file.'; |
|
| 200 | - return new RejectedPromise(new CredentialsException($msg)); |
|
| 201 | - }; |
|
| 202 | - } |
|
| 45 | + /** @var array */ |
|
| 46 | + protected $params; |
|
| 47 | + |
|
| 48 | + /** @var S3Client */ |
|
| 49 | + protected $connection; |
|
| 50 | + |
|
| 51 | + /** @var string */ |
|
| 52 | + protected $id; |
|
| 53 | + |
|
| 54 | + /** @var string */ |
|
| 55 | + protected $bucket; |
|
| 56 | + |
|
| 57 | + /** @var int */ |
|
| 58 | + protected $timeout; |
|
| 59 | + |
|
| 60 | + /** @var int */ |
|
| 61 | + protected $uploadPartSize; |
|
| 62 | + |
|
| 63 | + protected $test; |
|
| 64 | + |
|
| 65 | + protected function parseParams($params) { |
|
| 66 | + if (empty($params['bucket'])) { |
|
| 67 | + throw new \Exception("Bucket has to be configured."); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $this->id = 'amazon::' . $params['bucket']; |
|
| 71 | + |
|
| 72 | + $this->test = isset($params['test']); |
|
| 73 | + $this->bucket = $params['bucket']; |
|
| 74 | + $this->timeout = !isset($params['timeout']) ? 15 : $params['timeout']; |
|
| 75 | + $this->uploadPartSize = !isset($params['uploadPartSize']) ? 524288000 : $params['uploadPartSize']; |
|
| 76 | + $params['region'] = empty($params['region']) ? 'eu-west-1' : $params['region']; |
|
| 77 | + $params['hostname'] = empty($params['hostname']) ? 's3.' . $params['region'] . '.amazonaws.com' : $params['hostname']; |
|
| 78 | + if (!isset($params['port']) || $params['port'] === '') { |
|
| 79 | + $params['port'] = (isset($params['use_ssl']) && $params['use_ssl'] === false) ? 80 : 443; |
|
| 80 | + } |
|
| 81 | + $params['verify_bucket_exists'] = empty($params['verify_bucket_exists']) ? true : $params['verify_bucket_exists']; |
|
| 82 | + $this->params = $params; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + public function getBucket() { |
|
| 86 | + return $this->bucket; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Returns the connection |
|
| 91 | + * |
|
| 92 | + * @return S3Client connected client |
|
| 93 | + * @throws \Exception if connection could not be made |
|
| 94 | + */ |
|
| 95 | + public function getConnection() { |
|
| 96 | + if (!is_null($this->connection)) { |
|
| 97 | + return $this->connection; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $scheme = (isset($this->params['use_ssl']) && $this->params['use_ssl'] === false) ? 'http' : 'https'; |
|
| 101 | + $base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/'; |
|
| 102 | + |
|
| 103 | + // Adding explicit credential provider to the beginning chain. |
|
| 104 | + // Including environment variables and IAM instance profiles. |
|
| 105 | + $provider = CredentialProvider::memoize( |
|
| 106 | + CredentialProvider::chain( |
|
| 107 | + $this->paramCredentialProvider(), |
|
| 108 | + CredentialProvider::env(), |
|
| 109 | + CredentialProvider::assumeRoleWithWebIdentityCredentialProvider(), |
|
| 110 | + !empty(getenv(EcsCredentialProvider::ENV_URI)) |
|
| 111 | + ? CredentialProvider::ecsCredentials() |
|
| 112 | + : CredentialProvider::instanceProfile() |
|
| 113 | + ) |
|
| 114 | + ); |
|
| 115 | + |
|
| 116 | + $options = [ |
|
| 117 | + 'version' => isset($this->params['version']) ? $this->params['version'] : 'latest', |
|
| 118 | + 'credentials' => $provider, |
|
| 119 | + 'endpoint' => $base_url, |
|
| 120 | + 'region' => $this->params['region'], |
|
| 121 | + 'use_path_style_endpoint' => isset($this->params['use_path_style']) ? $this->params['use_path_style'] : false, |
|
| 122 | + 'signature_provider' => \Aws\or_chain([self::class, 'legacySignatureProvider'], ClientResolver::_default_signature_provider()), |
|
| 123 | + 'csm' => false, |
|
| 124 | + ]; |
|
| 125 | + if (isset($this->params['proxy'])) { |
|
| 126 | + $options['request.options'] = ['proxy' => $this->params['proxy']]; |
|
| 127 | + } |
|
| 128 | + if (isset($this->params['legacy_auth']) && $this->params['legacy_auth']) { |
|
| 129 | + $options['signature_version'] = 'v2'; |
|
| 130 | + } |
|
| 131 | + $this->connection = new S3Client($options); |
|
| 132 | + |
|
| 133 | + if (!$this->connection::isBucketDnsCompatible($this->bucket)) { |
|
| 134 | + $logger = \OC::$server->getLogger(); |
|
| 135 | + $logger->debug('Bucket "' . $this->bucket . '" This bucket name is not dns compatible, it may contain invalid characters.', |
|
| 136 | + ['app' => 'objectstore']); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + if ($this->params['verify_bucket_exists'] && !$this->connection->doesBucketExist($this->bucket)) { |
|
| 140 | + $logger = \OC::$server->getLogger(); |
|
| 141 | + try { |
|
| 142 | + $logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']); |
|
| 143 | + if (!$this->connection::isBucketDnsCompatible($this->bucket)) { |
|
| 144 | + throw new \Exception("The bucket will not be created because the name is not dns compatible, please correct it: " . $this->bucket); |
|
| 145 | + } |
|
| 146 | + $this->connection->createBucket(['Bucket' => $this->bucket]); |
|
| 147 | + $this->testTimeout(); |
|
| 148 | + } catch (S3Exception $e) { |
|
| 149 | + $logger->logException($e, [ |
|
| 150 | + 'message' => 'Invalid remote storage.', |
|
| 151 | + 'level' => ILogger::DEBUG, |
|
| 152 | + 'app' => 'objectstore', |
|
| 153 | + ]); |
|
| 154 | + throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage()); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + // google cloud's s3 compatibility doesn't like the EncodingType parameter |
|
| 159 | + if (strpos($base_url, 'storage.googleapis.com')) { |
|
| 160 | + $this->connection->getHandlerList()->remove('s3.auto_encode'); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + return $this->connection; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * when running the tests wait to let the buckets catch up |
|
| 168 | + */ |
|
| 169 | + private function testTimeout() { |
|
| 170 | + if ($this->test) { |
|
| 171 | + sleep($this->timeout); |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + public static function legacySignatureProvider($version, $service, $region) { |
|
| 176 | + switch ($version) { |
|
| 177 | + case 'v2': |
|
| 178 | + case 's3': |
|
| 179 | + return new S3Signature(); |
|
| 180 | + default: |
|
| 181 | + return null; |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * This function creates a credential provider based on user parameter file |
|
| 187 | + */ |
|
| 188 | + protected function paramCredentialProvider() : callable { |
|
| 189 | + return function () { |
|
| 190 | + $key = empty($this->params['key']) ? null : $this->params['key']; |
|
| 191 | + $secret = empty($this->params['secret']) ? null : $this->params['secret']; |
|
| 192 | + |
|
| 193 | + if ($key && $secret) { |
|
| 194 | + return Promise\promise_for( |
|
| 195 | + new Credentials($key, $secret) |
|
| 196 | + ); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + $msg = 'Could not find parameters set for credentials in config file.'; |
|
| 200 | + return new RejectedPromise(new CredentialsException($msg)); |
|
| 201 | + }; |
|
| 202 | + } |
|
| 203 | 203 | } |