Passed
Push — master ( 3fa243...333665 )
by Morris
17:53 queued 13s
created
lib/private/Files/ObjectStore/S3ConnectionTrait.php 1 patch
Indentation   +159 added lines, -159 removed lines patch added patch discarded remove patch
@@ -42,163 +42,163 @@
 block discarded – undo
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
-			'use_arn_region' => false,
125
-		];
126
-		if (isset($this->params['proxy'])) {
127
-			$options['request.options'] = ['proxy' => $this->params['proxy']];
128
-		}
129
-		if (isset($this->params['legacy_auth']) && $this->params['legacy_auth']) {
130
-			$options['signature_version'] = 'v2';
131
-		}
132
-		$this->connection = new S3Client($options);
133
-
134
-		if (!$this->connection::isBucketDnsCompatible($this->bucket)) {
135
-			$logger = \OC::$server->getLogger();
136
-			$logger->debug('Bucket "' . $this->bucket . '" This bucket name is not dns compatible, it may contain invalid characters.',
137
-					 ['app' => 'objectstore']);
138
-		}
139
-
140
-		if ($this->params['verify_bucket_exists'] && !$this->connection->doesBucketExist($this->bucket)) {
141
-			$logger = \OC::$server->getLogger();
142
-			try {
143
-				$logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']);
144
-				if (!$this->connection::isBucketDnsCompatible($this->bucket)) {
145
-					throw new \Exception("The bucket will not be created because the name is not dns compatible, please correct it: " . $this->bucket);
146
-				}
147
-				$this->connection->createBucket(['Bucket' => $this->bucket]);
148
-				$this->testTimeout();
149
-			} catch (S3Exception $e) {
150
-				$logger->logException($e, [
151
-					'message' => 'Invalid remote storage.',
152
-					'level' => ILogger::DEBUG,
153
-					'app' => 'objectstore',
154
-				]);
155
-				throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage());
156
-			}
157
-		}
158
-
159
-		// google cloud's s3 compatibility doesn't like the EncodingType parameter
160
-		if (strpos($base_url, 'storage.googleapis.com')) {
161
-			$this->connection->getHandlerList()->remove('s3.auto_encode');
162
-		}
163
-
164
-		return $this->connection;
165
-	}
166
-
167
-	/**
168
-	 * when running the tests wait to let the buckets catch up
169
-	 */
170
-	private function testTimeout() {
171
-		if ($this->test) {
172
-			sleep($this->timeout);
173
-		}
174
-	}
175
-
176
-	public static function legacySignatureProvider($version, $service, $region) {
177
-		switch ($version) {
178
-			case 'v2':
179
-			case 's3':
180
-				return new S3Signature();
181
-			default:
182
-				return null;
183
-		}
184
-	}
185
-
186
-	/**
187
-	 * This function creates a credential provider based on user parameter file
188
-	 */
189
-	protected function paramCredentialProvider() : callable {
190
-		return function () {
191
-			$key = empty($this->params['key']) ? null : $this->params['key'];
192
-			$secret = empty($this->params['secret']) ? null : $this->params['secret'];
193
-
194
-			if ($key && $secret) {
195
-				return Promise\promise_for(
196
-					new Credentials($key, $secret)
197
-				);
198
-			}
199
-
200
-			$msg = 'Could not find parameters set for credentials in config file.';
201
-			return new RejectedPromise(new CredentialsException($msg));
202
-		};
203
-	}
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
+            'use_arn_region' => false,
125
+        ];
126
+        if (isset($this->params['proxy'])) {
127
+            $options['request.options'] = ['proxy' => $this->params['proxy']];
128
+        }
129
+        if (isset($this->params['legacy_auth']) && $this->params['legacy_auth']) {
130
+            $options['signature_version'] = 'v2';
131
+        }
132
+        $this->connection = new S3Client($options);
133
+
134
+        if (!$this->connection::isBucketDnsCompatible($this->bucket)) {
135
+            $logger = \OC::$server->getLogger();
136
+            $logger->debug('Bucket "' . $this->bucket . '" This bucket name is not dns compatible, it may contain invalid characters.',
137
+                        ['app' => 'objectstore']);
138
+        }
139
+
140
+        if ($this->params['verify_bucket_exists'] && !$this->connection->doesBucketExist($this->bucket)) {
141
+            $logger = \OC::$server->getLogger();
142
+            try {
143
+                $logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']);
144
+                if (!$this->connection::isBucketDnsCompatible($this->bucket)) {
145
+                    throw new \Exception("The bucket will not be created because the name is not dns compatible, please correct it: " . $this->bucket);
146
+                }
147
+                $this->connection->createBucket(['Bucket' => $this->bucket]);
148
+                $this->testTimeout();
149
+            } catch (S3Exception $e) {
150
+                $logger->logException($e, [
151
+                    'message' => 'Invalid remote storage.',
152
+                    'level' => ILogger::DEBUG,
153
+                    'app' => 'objectstore',
154
+                ]);
155
+                throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage());
156
+            }
157
+        }
158
+
159
+        // google cloud's s3 compatibility doesn't like the EncodingType parameter
160
+        if (strpos($base_url, 'storage.googleapis.com')) {
161
+            $this->connection->getHandlerList()->remove('s3.auto_encode');
162
+        }
163
+
164
+        return $this->connection;
165
+    }
166
+
167
+    /**
168
+     * when running the tests wait to let the buckets catch up
169
+     */
170
+    private function testTimeout() {
171
+        if ($this->test) {
172
+            sleep($this->timeout);
173
+        }
174
+    }
175
+
176
+    public static function legacySignatureProvider($version, $service, $region) {
177
+        switch ($version) {
178
+            case 'v2':
179
+            case 's3':
180
+                return new S3Signature();
181
+            default:
182
+                return null;
183
+        }
184
+    }
185
+
186
+    /**
187
+     * This function creates a credential provider based on user parameter file
188
+     */
189
+    protected function paramCredentialProvider() : callable {
190
+        return function () {
191
+            $key = empty($this->params['key']) ? null : $this->params['key'];
192
+            $secret = empty($this->params['secret']) ? null : $this->params['secret'];
193
+
194
+            if ($key && $secret) {
195
+                return Promise\promise_for(
196
+                    new Credentials($key, $secret)
197
+                );
198
+            }
199
+
200
+            $msg = 'Could not find parameters set for credentials in config file.';
201
+            return new RejectedPromise(new CredentialsException($msg));
202
+        };
203
+    }
204 204
 }
Please login to merge, or discard this patch.