Don't fallback from uk to ru
[lhc/web/wiklou.git] / includes / filebackend / SwiftFileBackend.php
1 <?php
2 /**
3 * OpenStack Swift based file backend.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup FileBackend
22 * @author Russ Nelson
23 * @author Aaron Schulz
24 */
25
26 /**
27 * @brief Class for an OpenStack Swift (or Ceph RGW) based file backend.
28 *
29 * Status messages should avoid mentioning the Swift account name.
30 * Likewise, error suppression should be used to avoid path disclosure.
31 *
32 * @ingroup FileBackend
33 * @since 1.19
34 */
35 class SwiftFileBackend extends FileBackendStore {
36 /** @var MultiHttpClient */
37 protected $http;
38
39 /** @var int TTL in seconds */
40 protected $authTTL;
41
42 /** @var string Authentication base URL (without version) */
43 protected $swiftAuthUrl;
44
45 /** @var string Swift user (account:user) to authenticate as */
46 protected $swiftUser;
47
48 /** @var string Secret key for user */
49 protected $swiftKey;
50
51 /** @var string Shared secret value for making temp URLs */
52 protected $swiftTempUrlKey;
53
54 /** @var string S3 access key (RADOS Gateway) */
55 protected $rgwS3AccessKey;
56
57 /** @var string S3 authentication key (RADOS Gateway) */
58 protected $rgwS3SecretKey;
59
60 /** @var BagOStuff */
61 protected $srvCache;
62
63 /** @var ProcessCacheLRU Container stat cache */
64 protected $containerStatCache;
65
66 /** @var array */
67 protected $authCreds;
68
69 /** @var int UNIX timestamp */
70 protected $authSessionTimestamp = 0;
71
72 /** @var int UNIX timestamp */
73 protected $authErrorTimestamp = null;
74
75 /** @var bool Whether the server is an Ceph RGW */
76 protected $isRGW = false;
77
78 /**
79 * @see FileBackendStore::__construct()
80 * Additional $config params include:
81 * - swiftAuthUrl : Swift authentication server URL
82 * - swiftUser : Swift user used by MediaWiki (account:username)
83 * - swiftKey : Swift authentication key for the above user
84 * - swiftAuthTTL : Swift authentication TTL (seconds)
85 * - swiftTempUrlKey : Swift "X-Account-Meta-Temp-URL-Key" value on the account.
86 * Do not set this until it has been set in the backend.
87 * - shardViaHashLevels : Map of container names to sharding config with:
88 * - base : base of hash characters, 16 or 36
89 * - levels : the number of hash levels (and digits)
90 * - repeat : hash subdirectories are prefixed with all the
91 * parent hash directory names (e.g. "a/ab/abc")
92 * - cacheAuthInfo : Whether to cache authentication tokens in APC, XCache, ect.
93 * If those are not available, then the main cache will be used.
94 * This is probably insecure in shared hosting environments.
95 * - rgwS3AccessKey : Rados Gateway S3 "access key" value on the account.
96 * Do not set this until it has been set in the backend.
97 * This is used for generating expiring pre-authenticated URLs.
98 * Only use this when using rgw and to work around
99 * http://tracker.newdream.net/issues/3454.
100 * - rgwS3SecretKey : Rados Gateway S3 "secret key" value on the account.
101 * Do not set this until it has been set in the backend.
102 * This is used for generating expiring pre-authenticated URLs.
103 * Only use this when using rgw and to work around
104 * http://tracker.newdream.net/issues/3454.
105 */
106 public function __construct( array $config ) {
107 parent::__construct( $config );
108 // Required settings
109 $this->swiftAuthUrl = $config['swiftAuthUrl'];
110 $this->swiftUser = $config['swiftUser'];
111 $this->swiftKey = $config['swiftKey'];
112 // Optional settings
113 $this->authTTL = isset( $config['swiftAuthTTL'] )
114 ? $config['swiftAuthTTL']
115 : 15 * 60; // some sane number
116 $this->swiftTempUrlKey = isset( $config['swiftTempUrlKey'] )
117 ? $config['swiftTempUrlKey']
118 : '';
119 $this->shardViaHashLevels = isset( $config['shardViaHashLevels'] )
120 ? $config['shardViaHashLevels']
121 : '';
122 $this->rgwS3AccessKey = isset( $config['rgwS3AccessKey'] )
123 ? $config['rgwS3AccessKey']
124 : '';
125 $this->rgwS3SecretKey = isset( $config['rgwS3SecretKey'] )
126 ? $config['rgwS3SecretKey']
127 : '';
128 // HTTP helper client
129 $this->http = new MultiHttpClient( [] );
130 // Cache container information to mask latency
131 if ( isset( $config['wanCache'] ) && $config['wanCache'] instanceof WANObjectCache ) {
132 $this->memCache = $config['wanCache'];
133 }
134 // Process cache for container info
135 $this->containerStatCache = new ProcessCacheLRU( 300 );
136 // Cache auth token information to avoid RTTs
137 if ( !empty( $config['cacheAuthInfo'] ) ) {
138 if ( PHP_SAPI === 'cli' ) {
139 // Preferrably memcached
140 $this->srvCache = ObjectCache::getLocalClusterInstance();
141 } else {
142 // Look for APC, XCache, WinCache, ect...
143 $this->srvCache = ObjectCache::getLocalServerInstance( CACHE_NONE );
144 }
145 } else {
146 $this->srvCache = new EmptyBagOStuff();
147 }
148 }
149
150 public function getFeatures() {
151 return ( FileBackend::ATTR_UNICODE_PATHS |
152 FileBackend::ATTR_HEADERS | FileBackend::ATTR_METADATA );
153 }
154
155 protected function resolveContainerPath( $container, $relStoragePath ) {
156 if ( !mb_check_encoding( $relStoragePath, 'UTF-8' ) ) {
157 return null; // not UTF-8, makes it hard to use CF and the swift HTTP API
158 } elseif ( strlen( urlencode( $relStoragePath ) ) > 1024 ) {
159 return null; // too long for Swift
160 }
161
162 return $relStoragePath;
163 }
164
165 public function isPathUsableInternal( $storagePath ) {
166 list( $container, $rel ) = $this->resolveStoragePathReal( $storagePath );
167 if ( $rel === null ) {
168 return false; // invalid
169 }
170
171 return is_array( $this->getContainerStat( $container ) );
172 }
173
174 /**
175 * Sanitize and filter the custom headers from a $params array.
176 * Only allows certain "standard" Content- and X-Content- headers.
177 *
178 * @param array $params
179 * @return array Sanitized value of 'headers' field in $params
180 */
181 protected function sanitizeHdrs( array $params ) {
182 return isset( $params['headers'] )
183 ? $this->getCustomHeaders( $params['headers'] )
184 : [];
185
186 }
187
188 /**
189 * @param array $rawHeaders
190 * @return array Custom non-metadata HTTP headers
191 */
192 protected function getCustomHeaders( array $rawHeaders ) {
193 $headers = [];
194
195 // Normalize casing, and strip out illegal headers
196 foreach ( $rawHeaders as $name => $value ) {
197 $name = strtolower( $name );
198 if ( preg_match( '/^content-(type|length)$/', $name ) ) {
199 continue; // blacklisted
200 } elseif ( preg_match( '/^(x-)?content-/', $name ) ) {
201 $headers[$name] = $value; // allowed
202 } elseif ( preg_match( '/^content-(disposition)/', $name ) ) {
203 $headers[$name] = $value; // allowed
204 }
205 }
206 // By default, Swift has annoyingly low maximum header value limits
207 if ( isset( $headers['content-disposition'] ) ) {
208 $disposition = '';
209 // @note: assume FileBackend::makeContentDisposition() already used
210 foreach ( explode( ';', $headers['content-disposition'] ) as $part ) {
211 $part = trim( $part );
212 $new = ( $disposition === '' ) ? $part : "{$disposition};{$part}";
213 if ( strlen( $new ) <= 255 ) {
214 $disposition = $new;
215 } else {
216 break; // too long; sigh
217 }
218 }
219 $headers['content-disposition'] = $disposition;
220 }
221
222 return $headers;
223 }
224
225 /**
226 * @param array $rawHeaders
227 * @return array Custom metadata headers
228 */
229 protected function getMetadataHeaders( array $rawHeaders ) {
230 $headers = [];
231 foreach ( $rawHeaders as $name => $value ) {
232 $name = strtolower( $name );
233 if ( strpos( $name, 'x-object-meta-' ) === 0 ) {
234 $headers[$name] = $value;
235 }
236 }
237
238 return $headers;
239 }
240
241 /**
242 * @param array $rawHeaders
243 * @return array Custom metadata headers with prefix removed
244 */
245 protected function getMetadata( array $rawHeaders ) {
246 $metadata = [];
247 foreach ( $this->getMetadataHeaders( $rawHeaders ) as $name => $value ) {
248 $metadata[substr( $name, strlen( 'x-object-meta-' ) )] = $value;
249 }
250
251 return $metadata;
252 }
253
254 protected function doCreateInternal( array $params ) {
255 $status = Status::newGood();
256
257 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
258 if ( $dstRel === null ) {
259 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
260
261 return $status;
262 }
263
264 $sha1Hash = Wikimedia\base_convert( sha1( $params['content'] ), 16, 36, 31 );
265 $contentType = isset( $params['headers']['content-type'] )
266 ? $params['headers']['content-type']
267 : $this->getContentType( $params['dst'], $params['content'], null );
268
269 $reqs = [ [
270 'method' => 'PUT',
271 'url' => [ $dstCont, $dstRel ],
272 'headers' => [
273 'content-length' => strlen( $params['content'] ),
274 'etag' => md5( $params['content'] ),
275 'content-type' => $contentType,
276 'x-object-meta-sha1base36' => $sha1Hash
277 ] + $this->sanitizeHdrs( $params ),
278 'body' => $params['content']
279 ] ];
280
281 $method = __METHOD__;
282 $handler = function ( array $request, Status $status ) use ( $method, $params ) {
283 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
284 if ( $rcode === 201 ) {
285 // good
286 } elseif ( $rcode === 412 ) {
287 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
288 } else {
289 $this->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
290 }
291 };
292
293 $opHandle = new SwiftFileOpHandle( $this, $handler, $reqs );
294 if ( !empty( $params['async'] ) ) { // deferred
295 $status->value = $opHandle;
296 } else { // actually write the object in Swift
297 $status->merge( current( $this->doExecuteOpHandlesInternal( [ $opHandle ] ) ) );
298 }
299
300 return $status;
301 }
302
303 protected function doStoreInternal( array $params ) {
304 $status = Status::newGood();
305
306 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
307 if ( $dstRel === null ) {
308 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
309
310 return $status;
311 }
312
313 MediaWiki\suppressWarnings();
314 $sha1Hash = sha1_file( $params['src'] );
315 MediaWiki\restoreWarnings();
316 if ( $sha1Hash === false ) { // source doesn't exist?
317 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
318
319 return $status;
320 }
321 $sha1Hash = Wikimedia\base_convert( $sha1Hash, 16, 36, 31 );
322 $contentType = isset( $params['headers']['content-type'] )
323 ? $params['headers']['content-type']
324 : $this->getContentType( $params['dst'], null, $params['src'] );
325
326 $handle = fopen( $params['src'], 'rb' );
327 if ( $handle === false ) { // source doesn't exist?
328 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
329
330 return $status;
331 }
332
333 $reqs = [ [
334 'method' => 'PUT',
335 'url' => [ $dstCont, $dstRel ],
336 'headers' => [
337 'content-length' => filesize( $params['src'] ),
338 'etag' => md5_file( $params['src'] ),
339 'content-type' => $contentType,
340 'x-object-meta-sha1base36' => $sha1Hash
341 ] + $this->sanitizeHdrs( $params ),
342 'body' => $handle // resource
343 ] ];
344
345 $method = __METHOD__;
346 $handler = function ( array $request, Status $status ) use ( $method, $params ) {
347 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
348 if ( $rcode === 201 ) {
349 // good
350 } elseif ( $rcode === 412 ) {
351 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
352 } else {
353 $this->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
354 }
355 };
356
357 $opHandle = new SwiftFileOpHandle( $this, $handler, $reqs );
358 if ( !empty( $params['async'] ) ) { // deferred
359 $status->value = $opHandle;
360 } else { // actually write the object in Swift
361 $status->merge( current( $this->doExecuteOpHandlesInternal( [ $opHandle ] ) ) );
362 }
363
364 return $status;
365 }
366
367 protected function doCopyInternal( array $params ) {
368 $status = Status::newGood();
369
370 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
371 if ( $srcRel === null ) {
372 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
373
374 return $status;
375 }
376
377 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
378 if ( $dstRel === null ) {
379 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
380
381 return $status;
382 }
383
384 $reqs = [ [
385 'method' => 'PUT',
386 'url' => [ $dstCont, $dstRel ],
387 'headers' => [
388 'x-copy-from' => '/' . rawurlencode( $srcCont ) .
389 '/' . str_replace( "%2F", "/", rawurlencode( $srcRel ) )
390 ] + $this->sanitizeHdrs( $params ), // extra headers merged into object
391 ] ];
392
393 $method = __METHOD__;
394 $handler = function ( array $request, Status $status ) use ( $method, $params ) {
395 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
396 if ( $rcode === 201 ) {
397 // good
398 } elseif ( $rcode === 404 ) {
399 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
400 } else {
401 $this->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
402 }
403 };
404
405 $opHandle = new SwiftFileOpHandle( $this, $handler, $reqs );
406 if ( !empty( $params['async'] ) ) { // deferred
407 $status->value = $opHandle;
408 } else { // actually write the object in Swift
409 $status->merge( current( $this->doExecuteOpHandlesInternal( [ $opHandle ] ) ) );
410 }
411
412 return $status;
413 }
414
415 protected function doMoveInternal( array $params ) {
416 $status = Status::newGood();
417
418 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
419 if ( $srcRel === null ) {
420 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
421
422 return $status;
423 }
424
425 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
426 if ( $dstRel === null ) {
427 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
428
429 return $status;
430 }
431
432 $reqs = [
433 [
434 'method' => 'PUT',
435 'url' => [ $dstCont, $dstRel ],
436 'headers' => [
437 'x-copy-from' => '/' . rawurlencode( $srcCont ) .
438 '/' . str_replace( "%2F", "/", rawurlencode( $srcRel ) )
439 ] + $this->sanitizeHdrs( $params ) // extra headers merged into object
440 ]
441 ];
442 if ( "{$srcCont}/{$srcRel}" !== "{$dstCont}/{$dstRel}" ) {
443 $reqs[] = [
444 'method' => 'DELETE',
445 'url' => [ $srcCont, $srcRel ],
446 'headers' => []
447 ];
448 }
449
450 $method = __METHOD__;
451 $handler = function ( array $request, Status $status ) use ( $method, $params ) {
452 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
453 if ( $request['method'] === 'PUT' && $rcode === 201 ) {
454 // good
455 } elseif ( $request['method'] === 'DELETE' && $rcode === 204 ) {
456 // good
457 } elseif ( $rcode === 404 ) {
458 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
459 } else {
460 $this->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
461 }
462 };
463
464 $opHandle = new SwiftFileOpHandle( $this, $handler, $reqs );
465 if ( !empty( $params['async'] ) ) { // deferred
466 $status->value = $opHandle;
467 } else { // actually move the object in Swift
468 $status->merge( current( $this->doExecuteOpHandlesInternal( [ $opHandle ] ) ) );
469 }
470
471 return $status;
472 }
473
474 protected function doDeleteInternal( array $params ) {
475 $status = Status::newGood();
476
477 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
478 if ( $srcRel === null ) {
479 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
480
481 return $status;
482 }
483
484 $reqs = [ [
485 'method' => 'DELETE',
486 'url' => [ $srcCont, $srcRel ],
487 'headers' => []
488 ] ];
489
490 $method = __METHOD__;
491 $handler = function ( array $request, Status $status ) use ( $method, $params ) {
492 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
493 if ( $rcode === 204 ) {
494 // good
495 } elseif ( $rcode === 404 ) {
496 if ( empty( $params['ignoreMissingSource'] ) ) {
497 $status->fatal( 'backend-fail-delete', $params['src'] );
498 }
499 } else {
500 $this->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
501 }
502 };
503
504 $opHandle = new SwiftFileOpHandle( $this, $handler, $reqs );
505 if ( !empty( $params['async'] ) ) { // deferred
506 $status->value = $opHandle;
507 } else { // actually delete the object in Swift
508 $status->merge( current( $this->doExecuteOpHandlesInternal( [ $opHandle ] ) ) );
509 }
510
511 return $status;
512 }
513
514 protected function doDescribeInternal( array $params ) {
515 $status = Status::newGood();
516
517 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
518 if ( $srcRel === null ) {
519 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
520
521 return $status;
522 }
523
524 // Fetch the old object headers/metadata...this should be in stat cache by now
525 $stat = $this->getFileStat( [ 'src' => $params['src'], 'latest' => 1 ] );
526 if ( $stat && !isset( $stat['xattr'] ) ) { // older cache entry
527 $stat = $this->doGetFileStat( [ 'src' => $params['src'], 'latest' => 1 ] );
528 }
529 if ( !$stat ) {
530 $status->fatal( 'backend-fail-describe', $params['src'] );
531
532 return $status;
533 }
534
535 // POST clears prior headers, so we need to merge the changes in to the old ones
536 $metaHdrs = [];
537 foreach ( $stat['xattr']['metadata'] as $name => $value ) {
538 $metaHdrs["x-object-meta-$name"] = $value;
539 }
540 $customHdrs = $this->sanitizeHdrs( $params ) + $stat['xattr']['headers'];
541
542 $reqs = [ [
543 'method' => 'POST',
544 'url' => [ $srcCont, $srcRel ],
545 'headers' => $metaHdrs + $customHdrs
546 ] ];
547
548 $method = __METHOD__;
549 $handler = function ( array $request, Status $status ) use ( $method, $params ) {
550 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
551 if ( $rcode === 202 ) {
552 // good
553 } elseif ( $rcode === 404 ) {
554 $status->fatal( 'backend-fail-describe', $params['src'] );
555 } else {
556 $this->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
557 }
558 };
559
560 $opHandle = new SwiftFileOpHandle( $this, $handler, $reqs );
561 if ( !empty( $params['async'] ) ) { // deferred
562 $status->value = $opHandle;
563 } else { // actually change the object in Swift
564 $status->merge( current( $this->doExecuteOpHandlesInternal( [ $opHandle ] ) ) );
565 }
566
567 return $status;
568 }
569
570 protected function doPrepareInternal( $fullCont, $dir, array $params ) {
571 $status = Status::newGood();
572
573 // (a) Check if container already exists
574 $stat = $this->getContainerStat( $fullCont );
575 if ( is_array( $stat ) ) {
576 return $status; // already there
577 } elseif ( $stat === null ) {
578 $status->fatal( 'backend-fail-internal', $this->name );
579 wfDebugLog( 'SwiftBackend', __METHOD__ . ': cannot get container stat' );
580
581 return $status;
582 }
583
584 // (b) Create container as needed with proper ACLs
585 if ( $stat === false ) {
586 $params['op'] = 'prepare';
587 $status->merge( $this->createContainer( $fullCont, $params ) );
588 }
589
590 return $status;
591 }
592
593 protected function doSecureInternal( $fullCont, $dir, array $params ) {
594 $status = Status::newGood();
595 if ( empty( $params['noAccess'] ) ) {
596 return $status; // nothing to do
597 }
598
599 $stat = $this->getContainerStat( $fullCont );
600 if ( is_array( $stat ) ) {
601 // Make container private to end-users...
602 $status->merge( $this->setContainerAccess(
603 $fullCont,
604 [ $this->swiftUser ], // read
605 [ $this->swiftUser ] // write
606 ) );
607 } elseif ( $stat === false ) {
608 $status->fatal( 'backend-fail-usable', $params['dir'] );
609 } else {
610 $status->fatal( 'backend-fail-internal', $this->name );
611 wfDebugLog( 'SwiftBackend', __METHOD__ . ': cannot get container stat' );
612 }
613
614 return $status;
615 }
616
617 protected function doPublishInternal( $fullCont, $dir, array $params ) {
618 $status = Status::newGood();
619
620 $stat = $this->getContainerStat( $fullCont );
621 if ( is_array( $stat ) ) {
622 // Make container public to end-users...
623 $status->merge( $this->setContainerAccess(
624 $fullCont,
625 [ $this->swiftUser, '.r:*' ], // read
626 [ $this->swiftUser ] // write
627 ) );
628 } elseif ( $stat === false ) {
629 $status->fatal( 'backend-fail-usable', $params['dir'] );
630 } else {
631 $status->fatal( 'backend-fail-internal', $this->name );
632 wfDebugLog( 'SwiftBackend', __METHOD__ . ': cannot get container stat' );
633 }
634
635 return $status;
636 }
637
638 protected function doCleanInternal( $fullCont, $dir, array $params ) {
639 $status = Status::newGood();
640
641 // Only containers themselves can be removed, all else is virtual
642 if ( $dir != '' ) {
643 return $status; // nothing to do
644 }
645
646 // (a) Check the container
647 $stat = $this->getContainerStat( $fullCont, true );
648 if ( $stat === false ) {
649 return $status; // ok, nothing to do
650 } elseif ( !is_array( $stat ) ) {
651 $status->fatal( 'backend-fail-internal', $this->name );
652 wfDebugLog( 'SwiftBackend', __METHOD__ . ': cannot get container stat' );
653
654 return $status;
655 }
656
657 // (b) Delete the container if empty
658 if ( $stat['count'] == 0 ) {
659 $params['op'] = 'clean';
660 $status->merge( $this->deleteContainer( $fullCont, $params ) );
661 }
662
663 return $status;
664 }
665
666 protected function doGetFileStat( array $params ) {
667 $params = [ 'srcs' => [ $params['src'] ], 'concurrency' => 1 ] + $params;
668 unset( $params['src'] );
669 $stats = $this->doGetFileStatMulti( $params );
670
671 return reset( $stats );
672 }
673
674 /**
675 * Convert dates like "Tue, 03 Jan 2012 22:01:04 GMT"/"2013-05-11T07:37:27.678360Z".
676 * Dates might also come in like "2013-05-11T07:37:27.678360" from Swift listings,
677 * missing the timezone suffix (though Ceph RGW does not appear to have this bug).
678 *
679 * @param string $ts
680 * @param int $format Output format (TS_* constant)
681 * @return string
682 * @throws FileBackendError
683 */
684 protected function convertSwiftDate( $ts, $format = TS_MW ) {
685 try {
686 $timestamp = new MWTimestamp( $ts );
687
688 return $timestamp->getTimestamp( $format );
689 } catch ( Exception $e ) {
690 throw new FileBackendError( $e->getMessage() );
691 }
692 }
693
694 /**
695 * Fill in any missing object metadata and save it to Swift
696 *
697 * @param array $objHdrs Object response headers
698 * @param string $path Storage path to object
699 * @return array New headers
700 */
701 protected function addMissingMetadata( array $objHdrs, $path ) {
702 if ( isset( $objHdrs['x-object-meta-sha1base36'] ) ) {
703 return $objHdrs; // nothing to do
704 }
705
706 /** @noinspection PhpUnusedLocalVariableInspection */
707 $ps = Profiler::instance()->scopedProfileIn( __METHOD__ . "-{$this->name}" );
708 wfDebugLog( 'SwiftBackend', __METHOD__ . ": $path was not stored with SHA-1 metadata." );
709
710 $objHdrs['x-object-meta-sha1base36'] = false;
711
712 $auth = $this->getAuthentication();
713 if ( !$auth ) {
714 return $objHdrs; // failed
715 }
716
717 // Find prior custom HTTP headers
718 $postHeaders = $this->getCustomHeaders( $objHdrs );
719 // Find prior metadata headers
720 $postHeaders += $this->getMetadataHeaders( $objHdrs );
721
722 $status = Status::newGood();
723 /** @noinspection PhpUnusedLocalVariableInspection */
724 $scopeLockS = $this->getScopedFileLocks( [ $path ], LockManager::LOCK_UW, $status );
725 if ( $status->isOK() ) {
726 $tmpFile = $this->getLocalCopy( [ 'src' => $path, 'latest' => 1 ] );
727 if ( $tmpFile ) {
728 $hash = $tmpFile->getSha1Base36();
729 if ( $hash !== false ) {
730 $objHdrs['x-object-meta-sha1base36'] = $hash;
731 // Merge new SHA1 header into the old ones
732 $postHeaders['x-object-meta-sha1base36'] = $hash;
733 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $path );
734 list( $rcode ) = $this->http->run( [
735 'method' => 'POST',
736 'url' => $this->storageUrl( $auth, $srcCont, $srcRel ),
737 'headers' => $this->authTokenHeaders( $auth ) + $postHeaders
738 ] );
739 if ( $rcode >= 200 && $rcode <= 299 ) {
740 $this->deleteFileCache( $path );
741
742 return $objHdrs; // success
743 }
744 }
745 }
746 }
747
748 wfDebugLog( 'SwiftBackend', __METHOD__ . ": unable to set SHA-1 metadata for $path" );
749
750 return $objHdrs; // failed
751 }
752
753 protected function doGetFileContentsMulti( array $params ) {
754 $contents = [];
755
756 $auth = $this->getAuthentication();
757
758 $ep = array_diff_key( $params, [ 'srcs' => 1 ] ); // for error logging
759 // Blindly create tmp files and stream to them, catching any exception if the file does
760 // not exist. Doing stats here is useless and will loop infinitely in addMissingMetadata().
761 $reqs = []; // (path => op)
762
763 foreach ( $params['srcs'] as $path ) { // each path in this concurrent batch
764 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $path );
765 if ( $srcRel === null || !$auth ) {
766 $contents[$path] = false;
767 continue;
768 }
769 // Create a new temporary memory file...
770 $handle = fopen( 'php://temp', 'wb' );
771 if ( $handle ) {
772 $reqs[$path] = [
773 'method' => 'GET',
774 'url' => $this->storageUrl( $auth, $srcCont, $srcRel ),
775 'headers' => $this->authTokenHeaders( $auth )
776 + $this->headersFromParams( $params ),
777 'stream' => $handle,
778 ];
779 }
780 $contents[$path] = false;
781 }
782
783 $opts = [ 'maxConnsPerHost' => $params['concurrency'] ];
784 $reqs = $this->http->runMulti( $reqs, $opts );
785 foreach ( $reqs as $path => $op ) {
786 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $op['response'];
787 if ( $rcode >= 200 && $rcode <= 299 ) {
788 rewind( $op['stream'] ); // start from the beginning
789 $contents[$path] = stream_get_contents( $op['stream'] );
790 } elseif ( $rcode === 404 ) {
791 $contents[$path] = false;
792 } else {
793 $this->onError( null, __METHOD__,
794 [ 'src' => $path ] + $ep, $rerr, $rcode, $rdesc );
795 }
796 fclose( $op['stream'] ); // close open handle
797 }
798
799 return $contents;
800 }
801
802 protected function doDirectoryExists( $fullCont, $dir, array $params ) {
803 $prefix = ( $dir == '' ) ? null : "{$dir}/";
804 $status = $this->objectListing( $fullCont, 'names', 1, null, $prefix );
805 if ( $status->isOK() ) {
806 return ( count( $status->value ) ) > 0;
807 }
808
809 return null; // error
810 }
811
812 /**
813 * @see FileBackendStore::getDirectoryListInternal()
814 * @param string $fullCont
815 * @param string $dir
816 * @param array $params
817 * @return SwiftFileBackendDirList
818 */
819 public function getDirectoryListInternal( $fullCont, $dir, array $params ) {
820 return new SwiftFileBackendDirList( $this, $fullCont, $dir, $params );
821 }
822
823 /**
824 * @see FileBackendStore::getFileListInternal()
825 * @param string $fullCont
826 * @param string $dir
827 * @param array $params
828 * @return SwiftFileBackendFileList
829 */
830 public function getFileListInternal( $fullCont, $dir, array $params ) {
831 return new SwiftFileBackendFileList( $this, $fullCont, $dir, $params );
832 }
833
834 /**
835 * Do not call this function outside of SwiftFileBackendFileList
836 *
837 * @param string $fullCont Resolved container name
838 * @param string $dir Resolved storage directory with no trailing slash
839 * @param string|null $after Resolved container relative path to list items after
840 * @param int $limit Max number of items to list
841 * @param array $params Parameters for getDirectoryList()
842 * @return array List of container relative resolved paths of directories directly under $dir
843 * @throws FileBackendError
844 */
845 public function getDirListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
846 $dirs = [];
847 if ( $after === INF ) {
848 return $dirs; // nothing more
849 }
850
851 $ps = Profiler::instance()->scopedProfileIn( __METHOD__ . "-{$this->name}" );
852
853 $prefix = ( $dir == '' ) ? null : "{$dir}/";
854 // Non-recursive: only list dirs right under $dir
855 if ( !empty( $params['topOnly'] ) ) {
856 $status = $this->objectListing( $fullCont, 'names', $limit, $after, $prefix, '/' );
857 if ( !$status->isOK() ) {
858 throw new FileBackendError( "Iterator page I/O error: {$status->getMessage()}" );
859 }
860 $objects = $status->value;
861 foreach ( $objects as $object ) { // files and directories
862 if ( substr( $object, -1 ) === '/' ) {
863 $dirs[] = $object; // directories end in '/'
864 }
865 }
866 } else {
867 // Recursive: list all dirs under $dir and its subdirs
868 $getParentDir = function ( $path ) {
869 return ( strpos( $path, '/' ) !== false ) ? dirname( $path ) : false;
870 };
871
872 // Get directory from last item of prior page
873 $lastDir = $getParentDir( $after ); // must be first page
874 $status = $this->objectListing( $fullCont, 'names', $limit, $after, $prefix );
875
876 if ( !$status->isOK() ) {
877 throw new FileBackendError( "Iterator page I/O error: {$status->getMessage()}" );
878 }
879
880 $objects = $status->value;
881
882 foreach ( $objects as $object ) { // files
883 $objectDir = $getParentDir( $object ); // directory of object
884
885 if ( $objectDir !== false && $objectDir !== $dir ) {
886 // Swift stores paths in UTF-8, using binary sorting.
887 // See function "create_container_table" in common/db.py.
888 // If a directory is not "greater" than the last one,
889 // then it was already listed by the calling iterator.
890 if ( strcmp( $objectDir, $lastDir ) > 0 ) {
891 $pDir = $objectDir;
892 do { // add dir and all its parent dirs
893 $dirs[] = "{$pDir}/";
894 $pDir = $getParentDir( $pDir );
895 } while ( $pDir !== false // sanity
896 && strcmp( $pDir, $lastDir ) > 0 // not done already
897 && strlen( $pDir ) > strlen( $dir ) // within $dir
898 );
899 }
900 $lastDir = $objectDir;
901 }
902 }
903 }
904 // Page on the unfiltered directory listing (what is returned may be filtered)
905 if ( count( $objects ) < $limit ) {
906 $after = INF; // avoid a second RTT
907 } else {
908 $after = end( $objects ); // update last item
909 }
910
911 return $dirs;
912 }
913
914 /**
915 * Do not call this function outside of SwiftFileBackendFileList
916 *
917 * @param string $fullCont Resolved container name
918 * @param string $dir Resolved storage directory with no trailing slash
919 * @param string|null $after Resolved container relative path of file to list items after
920 * @param int $limit Max number of items to list
921 * @param array $params Parameters for getDirectoryList()
922 * @return array List of resolved container relative paths of files under $dir
923 * @throws FileBackendError
924 */
925 public function getFileListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
926 $files = []; // list of (path, stat array or null) entries
927 if ( $after === INF ) {
928 return $files; // nothing more
929 }
930
931 $ps = Profiler::instance()->scopedProfileIn( __METHOD__ . "-{$this->name}" );
932
933 $prefix = ( $dir == '' ) ? null : "{$dir}/";
934 // $objects will contain a list of unfiltered names or CF_Object items
935 // Non-recursive: only list files right under $dir
936 if ( !empty( $params['topOnly'] ) ) {
937 if ( !empty( $params['adviseStat'] ) ) {
938 $status = $this->objectListing( $fullCont, 'info', $limit, $after, $prefix, '/' );
939 } else {
940 $status = $this->objectListing( $fullCont, 'names', $limit, $after, $prefix, '/' );
941 }
942 } else {
943 // Recursive: list all files under $dir and its subdirs
944 if ( !empty( $params['adviseStat'] ) ) {
945 $status = $this->objectListing( $fullCont, 'info', $limit, $after, $prefix );
946 } else {
947 $status = $this->objectListing( $fullCont, 'names', $limit, $after, $prefix );
948 }
949 }
950
951 // Reformat this list into a list of (name, stat array or null) entries
952 if ( !$status->isOK() ) {
953 throw new FileBackendError( "Iterator page I/O error: {$status->getMessage()}" );
954 }
955
956 $objects = $status->value;
957 $files = $this->buildFileObjectListing( $params, $dir, $objects );
958
959 // Page on the unfiltered object listing (what is returned may be filtered)
960 if ( count( $objects ) < $limit ) {
961 $after = INF; // avoid a second RTT
962 } else {
963 $after = end( $objects ); // update last item
964 $after = is_object( $after ) ? $after->name : $after;
965 }
966
967 return $files;
968 }
969
970 /**
971 * Build a list of file objects, filtering out any directories
972 * and extracting any stat info if provided in $objects (for CF_Objects)
973 *
974 * @param array $params Parameters for getDirectoryList()
975 * @param string $dir Resolved container directory path
976 * @param array $objects List of CF_Object items or object names
977 * @return array List of (names,stat array or null) entries
978 */
979 private function buildFileObjectListing( array $params, $dir, array $objects ) {
980 $names = [];
981 foreach ( $objects as $object ) {
982 if ( is_object( $object ) ) {
983 if ( isset( $object->subdir ) || !isset( $object->name ) ) {
984 continue; // virtual directory entry; ignore
985 }
986 $stat = [
987 // Convert various random Swift dates to TS_MW
988 'mtime' => $this->convertSwiftDate( $object->last_modified, TS_MW ),
989 'size' => (int)$object->bytes,
990 'sha1' => null,
991 // Note: manifiest ETags are not an MD5 of the file
992 'md5' => ctype_xdigit( $object->hash ) ? $object->hash : null,
993 'latest' => false // eventually consistent
994 ];
995 $names[] = [ $object->name, $stat ];
996 } elseif ( substr( $object, -1 ) !== '/' ) {
997 // Omit directories, which end in '/' in listings
998 $names[] = [ $object, null ];
999 }
1000 }
1001
1002 return $names;
1003 }
1004
1005 /**
1006 * Do not call this function outside of SwiftFileBackendFileList
1007 *
1008 * @param string $path Storage path
1009 * @param array $val Stat value
1010 */
1011 public function loadListingStatInternal( $path, array $val ) {
1012 $this->cheapCache->set( $path, 'stat', $val );
1013 }
1014
1015 protected function doGetFileXAttributes( array $params ) {
1016 $stat = $this->getFileStat( $params );
1017 if ( $stat ) {
1018 if ( !isset( $stat['xattr'] ) ) {
1019 // Stat entries filled by file listings don't include metadata/headers
1020 $this->clearCache( [ $params['src'] ] );
1021 $stat = $this->getFileStat( $params );
1022 }
1023
1024 return $stat['xattr'];
1025 } else {
1026 return false;
1027 }
1028 }
1029
1030 protected function doGetFileSha1base36( array $params ) {
1031 $stat = $this->getFileStat( $params );
1032 if ( $stat ) {
1033 if ( !isset( $stat['sha1'] ) ) {
1034 // Stat entries filled by file listings don't include SHA1
1035 $this->clearCache( [ $params['src'] ] );
1036 $stat = $this->getFileStat( $params );
1037 }
1038
1039 return $stat['sha1'];
1040 } else {
1041 return false;
1042 }
1043 }
1044
1045 protected function doStreamFile( array $params ) {
1046 $status = Status::newGood();
1047
1048 $flags = !empty( $params['headless'] ) ? StreamFile::STREAM_HEADLESS : 0;
1049
1050 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1051 if ( $srcRel === null ) {
1052 StreamFile::send404Message( $params['src'], $flags );
1053 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
1054
1055 return $status;
1056 }
1057
1058 $auth = $this->getAuthentication();
1059 if ( !$auth || !is_array( $this->getContainerStat( $srcCont ) ) ) {
1060 StreamFile::send404Message( $params['src'], $flags );
1061 $status->fatal( 'backend-fail-stream', $params['src'] );
1062
1063 return $status;
1064 }
1065
1066 // If "headers" is set, we only want to send them if the file is there.
1067 // Do not bother checking if the file exists if headers are not set though.
1068 if ( $params['headers'] && !$this->fileExists( $params ) ) {
1069 StreamFile::send404Message( $params['src'], $flags );
1070 $status->fatal( 'backend-fail-stream', $params['src'] );
1071
1072 return $status;
1073 }
1074
1075 // Send the requested additional headers
1076 foreach ( $params['headers'] as $header ) {
1077 header( $header ); // aways send
1078 }
1079
1080 if ( empty( $params['allowOB'] ) ) {
1081 // Cancel output buffering and gzipping if set
1082 wfResetOutputBuffers();
1083 }
1084
1085 $handle = fopen( 'php://output', 'wb' );
1086 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->http->run( [
1087 'method' => 'GET',
1088 'url' => $this->storageUrl( $auth, $srcCont, $srcRel ),
1089 'headers' => $this->authTokenHeaders( $auth )
1090 + $this->headersFromParams( $params ) + $params['options'],
1091 'stream' => $handle,
1092 'flags' => [ 'relayResponseHeaders' => empty( $params['headless'] ) ]
1093 ] );
1094
1095 if ( $rcode >= 200 && $rcode <= 299 ) {
1096 // good
1097 } elseif ( $rcode === 404 ) {
1098 $status->fatal( 'backend-fail-stream', $params['src'] );
1099 // Per bug 41113, nasty things can happen if bad cache entries get
1100 // stuck in cache. It's also possible that this error can come up
1101 // with simple race conditions. Clear out the stat cache to be safe.
1102 $this->clearCache( [ $params['src'] ] );
1103 $this->deleteFileCache( $params['src'] );
1104 } else {
1105 $this->onError( $status, __METHOD__, $params, $rerr, $rcode, $rdesc );
1106 }
1107
1108 return $status;
1109 }
1110
1111 protected function doGetLocalCopyMulti( array $params ) {
1112 $tmpFiles = [];
1113
1114 $auth = $this->getAuthentication();
1115
1116 $ep = array_diff_key( $params, [ 'srcs' => 1 ] ); // for error logging
1117 // Blindly create tmp files and stream to them, catching any exception if the file does
1118 // not exist. Doing a stat here is useless causes infinite loops in addMissingMetadata().
1119 $reqs = []; // (path => op)
1120
1121 foreach ( $params['srcs'] as $path ) { // each path in this concurrent batch
1122 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $path );
1123 if ( $srcRel === null || !$auth ) {
1124 $tmpFiles[$path] = null;
1125 continue;
1126 }
1127 // Get source file extension
1128 $ext = FileBackend::extensionFromPath( $path );
1129 // Create a new temporary file...
1130 $tmpFile = TempFSFile::factory( 'localcopy_', $ext );
1131 if ( $tmpFile ) {
1132 $handle = fopen( $tmpFile->getPath(), 'wb' );
1133 if ( $handle ) {
1134 $reqs[$path] = [
1135 'method' => 'GET',
1136 'url' => $this->storageUrl( $auth, $srcCont, $srcRel ),
1137 'headers' => $this->authTokenHeaders( $auth )
1138 + $this->headersFromParams( $params ),
1139 'stream' => $handle,
1140 ];
1141 } else {
1142 $tmpFile = null;
1143 }
1144 }
1145 $tmpFiles[$path] = $tmpFile;
1146 }
1147
1148 $isLatest = ( $this->isRGW || !empty( $params['latest'] ) );
1149 $opts = [ 'maxConnsPerHost' => $params['concurrency'] ];
1150 $reqs = $this->http->runMulti( $reqs, $opts );
1151 foreach ( $reqs as $path => $op ) {
1152 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $op['response'];
1153 fclose( $op['stream'] ); // close open handle
1154 if ( $rcode >= 200 && $rcode <= 299 ) {
1155 $size = $tmpFiles[$path] ? $tmpFiles[$path]->getSize() : 0;
1156 // Double check that the disk is not full/broken
1157 if ( $size != $rhdrs['content-length'] ) {
1158 $tmpFiles[$path] = null;
1159 $rerr = "Got {$size}/{$rhdrs['content-length']} bytes";
1160 $this->onError( null, __METHOD__,
1161 [ 'src' => $path ] + $ep, $rerr, $rcode, $rdesc );
1162 }
1163 // Set the file stat process cache in passing
1164 $stat = $this->getStatFromHeaders( $rhdrs );
1165 $stat['latest'] = $isLatest;
1166 $this->cheapCache->set( $path, 'stat', $stat );
1167 } elseif ( $rcode === 404 ) {
1168 $tmpFiles[$path] = false;
1169 } else {
1170 $tmpFiles[$path] = null;
1171 $this->onError( null, __METHOD__,
1172 [ 'src' => $path ] + $ep, $rerr, $rcode, $rdesc );
1173 }
1174 }
1175
1176 return $tmpFiles;
1177 }
1178
1179 public function getFileHttpUrl( array $params ) {
1180 if ( $this->swiftTempUrlKey != '' ||
1181 ( $this->rgwS3AccessKey != '' && $this->rgwS3SecretKey != '' )
1182 ) {
1183 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1184 if ( $srcRel === null ) {
1185 return null; // invalid path
1186 }
1187
1188 $auth = $this->getAuthentication();
1189 if ( !$auth ) {
1190 return null;
1191 }
1192
1193 $ttl = isset( $params['ttl'] ) ? $params['ttl'] : 86400;
1194 $expires = time() + $ttl;
1195
1196 if ( $this->swiftTempUrlKey != '' ) {
1197 $url = $this->storageUrl( $auth, $srcCont, $srcRel );
1198 // Swift wants the signature based on the unencoded object name
1199 $contPath = parse_url( $this->storageUrl( $auth, $srcCont ), PHP_URL_PATH );
1200 $signature = hash_hmac( 'sha1',
1201 "GET\n{$expires}\n{$contPath}/{$srcRel}",
1202 $this->swiftTempUrlKey
1203 );
1204
1205 return "{$url}?temp_url_sig={$signature}&temp_url_expires={$expires}";
1206 } else { // give S3 API URL for rgw
1207 // Path for signature starts with the bucket
1208 $spath = '/' . rawurlencode( $srcCont ) . '/' .
1209 str_replace( '%2F', '/', rawurlencode( $srcRel ) );
1210 // Calculate the hash
1211 $signature = base64_encode( hash_hmac(
1212 'sha1',
1213 "GET\n\n\n{$expires}\n{$spath}",
1214 $this->rgwS3SecretKey,
1215 true // raw
1216 ) );
1217 // See http://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html.
1218 // Note: adding a newline for empty CanonicalizedAmzHeaders does not work.
1219 return wfAppendQuery(
1220 str_replace( '/swift/v1', '', // S3 API is the rgw default
1221 $this->storageUrl( $auth ) . $spath ),
1222 [
1223 'Signature' => $signature,
1224 'Expires' => $expires,
1225 'AWSAccessKeyId' => $this->rgwS3AccessKey ]
1226 );
1227 }
1228 }
1229
1230 return null;
1231 }
1232
1233 protected function directoriesAreVirtual() {
1234 return true;
1235 }
1236
1237 /**
1238 * Get headers to send to Swift when reading a file based
1239 * on a FileBackend params array, e.g. that of getLocalCopy().
1240 * $params is currently only checked for a 'latest' flag.
1241 *
1242 * @param array $params
1243 * @return array
1244 */
1245 protected function headersFromParams( array $params ) {
1246 $hdrs = [];
1247 if ( !empty( $params['latest'] ) ) {
1248 $hdrs['x-newest'] = 'true';
1249 }
1250
1251 return $hdrs;
1252 }
1253
1254 /**
1255 * @param FileBackendStoreOpHandle[] $fileOpHandles
1256 *
1257 * @return Status[]
1258 */
1259 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
1260 $statuses = [];
1261
1262 $auth = $this->getAuthentication();
1263 if ( !$auth ) {
1264 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
1265 $statuses[$index] = Status::newFatal( 'backend-fail-connect', $this->name );
1266 }
1267
1268 return $statuses;
1269 }
1270
1271 // Split the HTTP requests into stages that can be done concurrently
1272 $httpReqsByStage = []; // map of (stage => index => HTTP request)
1273 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
1274 $reqs = $fileOpHandle->httpOp;
1275 // Convert the 'url' parameter to an actual URL using $auth
1276 foreach ( $reqs as $stage => &$req ) {
1277 list( $container, $relPath ) = $req['url'];
1278 $req['url'] = $this->storageUrl( $auth, $container, $relPath );
1279 $req['headers'] = isset( $req['headers'] ) ? $req['headers'] : [];
1280 $req['headers'] = $this->authTokenHeaders( $auth ) + $req['headers'];
1281 $httpReqsByStage[$stage][$index] = $req;
1282 }
1283 $statuses[$index] = Status::newGood();
1284 }
1285
1286 // Run all requests for the first stage, then the next, and so on
1287 $reqCount = count( $httpReqsByStage );
1288 for ( $stage = 0; $stage < $reqCount; ++$stage ) {
1289 $httpReqs = $this->http->runMulti( $httpReqsByStage[$stage] );
1290 foreach ( $httpReqs as $index => $httpReq ) {
1291 // Run the callback for each request of this operation
1292 $callback = $fileOpHandles[$index]->callback;
1293 call_user_func_array( $callback, [ $httpReq, $statuses[$index] ] );
1294 // On failure, abort all remaining requests for this operation
1295 // (e.g. abort the DELETE request if the COPY request fails for a move)
1296 if ( !$statuses[$index]->isOK() ) {
1297 $stages = count( $fileOpHandles[$index]->httpOp );
1298 for ( $s = ( $stage + 1 ); $s < $stages; ++$s ) {
1299 unset( $httpReqsByStage[$s][$index] );
1300 }
1301 }
1302 }
1303 }
1304
1305 return $statuses;
1306 }
1307
1308 /**
1309 * Set read/write permissions for a Swift container.
1310 *
1311 * @see http://swift.openstack.org/misc.html#acls
1312 *
1313 * In general, we don't allow listings to end-users. It's not useful, isn't well-defined
1314 * (lists are truncated to 10000 item with no way to page), and is just a performance risk.
1315 *
1316 * @param string $container Resolved Swift container
1317 * @param array $readGrps List of the possible criteria for a request to have
1318 * access to read a container. Each item is one of the following formats:
1319 * - account:user : Grants access if the request is by the given user
1320 * - ".r:<regex>" : Grants access if the request is from a referrer host that
1321 * matches the expression and the request is not for a listing.
1322 * Setting this to '*' effectively makes a container public.
1323 * -".rlistings:<regex>" : Grants access if the request is from a referrer host that
1324 * matches the expression and the request is for a listing.
1325 * @param array $writeGrps A list of the possible criteria for a request to have
1326 * access to write to a container. Each item is of the following format:
1327 * - account:user : Grants access if the request is by the given user
1328 * @return Status
1329 */
1330 protected function setContainerAccess( $container, array $readGrps, array $writeGrps ) {
1331 $status = Status::newGood();
1332 $auth = $this->getAuthentication();
1333
1334 if ( !$auth ) {
1335 $status->fatal( 'backend-fail-connect', $this->name );
1336
1337 return $status;
1338 }
1339
1340 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->http->run( [
1341 'method' => 'POST',
1342 'url' => $this->storageUrl( $auth, $container ),
1343 'headers' => $this->authTokenHeaders( $auth ) + [
1344 'x-container-read' => implode( ',', $readGrps ),
1345 'x-container-write' => implode( ',', $writeGrps )
1346 ]
1347 ] );
1348
1349 if ( $rcode != 204 && $rcode !== 202 ) {
1350 $status->fatal( 'backend-fail-internal', $this->name );
1351 wfDebugLog( 'SwiftBackend', __METHOD__ . ': unexpected rcode value (' . $rcode . ')' );
1352 }
1353
1354 return $status;
1355 }
1356
1357 /**
1358 * Get a Swift container stat array, possibly from process cache.
1359 * Use $reCache if the file count or byte count is needed.
1360 *
1361 * @param string $container Container name
1362 * @param bool $bypassCache Bypass all caches and load from Swift
1363 * @return array|bool|null False on 404, null on failure
1364 */
1365 protected function getContainerStat( $container, $bypassCache = false ) {
1366 $ps = Profiler::instance()->scopedProfileIn( __METHOD__ . "-{$this->name}" );
1367
1368 if ( $bypassCache ) { // purge cache
1369 $this->containerStatCache->clear( $container );
1370 } elseif ( !$this->containerStatCache->has( $container, 'stat' ) ) {
1371 $this->primeContainerCache( [ $container ] ); // check persistent cache
1372 }
1373 if ( !$this->containerStatCache->has( $container, 'stat' ) ) {
1374 $auth = $this->getAuthentication();
1375 if ( !$auth ) {
1376 return null;
1377 }
1378
1379 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->http->run( [
1380 'method' => 'HEAD',
1381 'url' => $this->storageUrl( $auth, $container ),
1382 'headers' => $this->authTokenHeaders( $auth )
1383 ] );
1384
1385 if ( $rcode === 204 ) {
1386 $stat = [
1387 'count' => $rhdrs['x-container-object-count'],
1388 'bytes' => $rhdrs['x-container-bytes-used']
1389 ];
1390 if ( $bypassCache ) {
1391 return $stat;
1392 } else {
1393 $this->containerStatCache->set( $container, 'stat', $stat ); // cache it
1394 $this->setContainerCache( $container, $stat ); // update persistent cache
1395 }
1396 } elseif ( $rcode === 404 ) {
1397 return false;
1398 } else {
1399 $this->onError( null, __METHOD__,
1400 [ 'cont' => $container ], $rerr, $rcode, $rdesc );
1401
1402 return null;
1403 }
1404 }
1405
1406 return $this->containerStatCache->get( $container, 'stat' );
1407 }
1408
1409 /**
1410 * Create a Swift container
1411 *
1412 * @param string $container Container name
1413 * @param array $params
1414 * @return Status
1415 */
1416 protected function createContainer( $container, array $params ) {
1417 $status = Status::newGood();
1418
1419 $auth = $this->getAuthentication();
1420 if ( !$auth ) {
1421 $status->fatal( 'backend-fail-connect', $this->name );
1422
1423 return $status;
1424 }
1425
1426 // @see SwiftFileBackend::setContainerAccess()
1427 if ( empty( $params['noAccess'] ) ) {
1428 $readGrps = [ '.r:*', $this->swiftUser ]; // public
1429 } else {
1430 $readGrps = [ $this->swiftUser ]; // private
1431 }
1432 $writeGrps = [ $this->swiftUser ]; // sanity
1433
1434 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->http->run( [
1435 'method' => 'PUT',
1436 'url' => $this->storageUrl( $auth, $container ),
1437 'headers' => $this->authTokenHeaders( $auth ) + [
1438 'x-container-read' => implode( ',', $readGrps ),
1439 'x-container-write' => implode( ',', $writeGrps )
1440 ]
1441 ] );
1442
1443 if ( $rcode === 201 ) { // new
1444 // good
1445 } elseif ( $rcode === 202 ) { // already there
1446 // this shouldn't really happen, but is OK
1447 } else {
1448 $this->onError( $status, __METHOD__, $params, $rerr, $rcode, $rdesc );
1449 }
1450
1451 return $status;
1452 }
1453
1454 /**
1455 * Delete a Swift container
1456 *
1457 * @param string $container Container name
1458 * @param array $params
1459 * @return Status
1460 */
1461 protected function deleteContainer( $container, array $params ) {
1462 $status = Status::newGood();
1463
1464 $auth = $this->getAuthentication();
1465 if ( !$auth ) {
1466 $status->fatal( 'backend-fail-connect', $this->name );
1467
1468 return $status;
1469 }
1470
1471 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->http->run( [
1472 'method' => 'DELETE',
1473 'url' => $this->storageUrl( $auth, $container ),
1474 'headers' => $this->authTokenHeaders( $auth )
1475 ] );
1476
1477 if ( $rcode >= 200 && $rcode <= 299 ) { // deleted
1478 $this->containerStatCache->clear( $container ); // purge
1479 } elseif ( $rcode === 404 ) { // not there
1480 // this shouldn't really happen, but is OK
1481 } elseif ( $rcode === 409 ) { // not empty
1482 $this->onError( $status, __METHOD__, $params, $rerr, $rcode, $rdesc ); // race?
1483 } else {
1484 $this->onError( $status, __METHOD__, $params, $rerr, $rcode, $rdesc );
1485 }
1486
1487 return $status;
1488 }
1489
1490 /**
1491 * Get a list of objects under a container.
1492 * Either just the names or a list of stdClass objects with details can be returned.
1493 *
1494 * @param string $fullCont
1495 * @param string $type ('info' for a list of object detail maps, 'names' for names only)
1496 * @param int $limit
1497 * @param string|null $after
1498 * @param string|null $prefix
1499 * @param string|null $delim
1500 * @return Status With the list as value
1501 */
1502 private function objectListing(
1503 $fullCont, $type, $limit, $after = null, $prefix = null, $delim = null
1504 ) {
1505 $status = Status::newGood();
1506
1507 $auth = $this->getAuthentication();
1508 if ( !$auth ) {
1509 $status->fatal( 'backend-fail-connect', $this->name );
1510
1511 return $status;
1512 }
1513
1514 $query = [ 'limit' => $limit ];
1515 if ( $type === 'info' ) {
1516 $query['format'] = 'json';
1517 }
1518 if ( $after !== null ) {
1519 $query['marker'] = $after;
1520 }
1521 if ( $prefix !== null ) {
1522 $query['prefix'] = $prefix;
1523 }
1524 if ( $delim !== null ) {
1525 $query['delimiter'] = $delim;
1526 }
1527
1528 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->http->run( [
1529 'method' => 'GET',
1530 'url' => $this->storageUrl( $auth, $fullCont ),
1531 'query' => $query,
1532 'headers' => $this->authTokenHeaders( $auth )
1533 ] );
1534
1535 $params = [ 'cont' => $fullCont, 'prefix' => $prefix, 'delim' => $delim ];
1536 if ( $rcode === 200 ) { // good
1537 if ( $type === 'info' ) {
1538 $status->value = FormatJson::decode( trim( $rbody ) );
1539 } else {
1540 $status->value = explode( "\n", trim( $rbody ) );
1541 }
1542 } elseif ( $rcode === 204 ) {
1543 $status->value = []; // empty container
1544 } elseif ( $rcode === 404 ) {
1545 $status->value = []; // no container
1546 } else {
1547 $this->onError( $status, __METHOD__, $params, $rerr, $rcode, $rdesc );
1548 }
1549
1550 return $status;
1551 }
1552
1553 protected function doPrimeContainerCache( array $containerInfo ) {
1554 foreach ( $containerInfo as $container => $info ) {
1555 $this->containerStatCache->set( $container, 'stat', $info );
1556 }
1557 }
1558
1559 protected function doGetFileStatMulti( array $params ) {
1560 $stats = [];
1561
1562 $auth = $this->getAuthentication();
1563
1564 $reqs = [];
1565 foreach ( $params['srcs'] as $path ) {
1566 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $path );
1567 if ( $srcRel === null ) {
1568 $stats[$path] = false;
1569 continue; // invalid storage path
1570 } elseif ( !$auth ) {
1571 $stats[$path] = null;
1572 continue;
1573 }
1574
1575 // (a) Check the container
1576 $cstat = $this->getContainerStat( $srcCont );
1577 if ( $cstat === false ) {
1578 $stats[$path] = false;
1579 continue; // ok, nothing to do
1580 } elseif ( !is_array( $cstat ) ) {
1581 $stats[$path] = null;
1582 continue;
1583 }
1584
1585 $reqs[$path] = [
1586 'method' => 'HEAD',
1587 'url' => $this->storageUrl( $auth, $srcCont, $srcRel ),
1588 'headers' => $this->authTokenHeaders( $auth ) + $this->headersFromParams( $params )
1589 ];
1590 }
1591
1592 $opts = [ 'maxConnsPerHost' => $params['concurrency'] ];
1593 $reqs = $this->http->runMulti( $reqs, $opts );
1594
1595 foreach ( $params['srcs'] as $path ) {
1596 if ( array_key_exists( $path, $stats ) ) {
1597 continue; // some sort of failure above
1598 }
1599 // (b) Check the file
1600 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $reqs[$path]['response'];
1601 if ( $rcode === 200 || $rcode === 204 ) {
1602 // Update the object if it is missing some headers
1603 $rhdrs = $this->addMissingMetadata( $rhdrs, $path );
1604 // Load the stat array from the headers
1605 $stat = $this->getStatFromHeaders( $rhdrs );
1606 if ( $this->isRGW ) {
1607 $stat['latest'] = true; // strong consistency
1608 }
1609 } elseif ( $rcode === 404 ) {
1610 $stat = false;
1611 } else {
1612 $stat = null;
1613 $this->onError( null, __METHOD__, $params, $rerr, $rcode, $rdesc );
1614 }
1615 $stats[$path] = $stat;
1616 }
1617
1618 return $stats;
1619 }
1620
1621 /**
1622 * @param array $rhdrs
1623 * @return array
1624 */
1625 protected function getStatFromHeaders( array $rhdrs ) {
1626 // Fetch all of the custom metadata headers
1627 $metadata = $this->getMetadata( $rhdrs );
1628 // Fetch all of the custom raw HTTP headers
1629 $headers = $this->sanitizeHdrs( [ 'headers' => $rhdrs ] );
1630
1631 return [
1632 // Convert various random Swift dates to TS_MW
1633 'mtime' => $this->convertSwiftDate( $rhdrs['last-modified'], TS_MW ),
1634 // Empty objects actually return no content-length header in Ceph
1635 'size' => isset( $rhdrs['content-length'] ) ? (int)$rhdrs['content-length'] : 0,
1636 'sha1' => isset( $metadata['sha1base36'] ) ? $metadata['sha1base36'] : null,
1637 // Note: manifiest ETags are not an MD5 of the file
1638 'md5' => ctype_xdigit( $rhdrs['etag'] ) ? $rhdrs['etag'] : null,
1639 'xattr' => [ 'metadata' => $metadata, 'headers' => $headers ]
1640 ];
1641 }
1642
1643 /**
1644 * @return array|null Credential map
1645 */
1646 protected function getAuthentication() {
1647 if ( $this->authErrorTimestamp !== null ) {
1648 if ( ( time() - $this->authErrorTimestamp ) < 60 ) {
1649 return null; // failed last attempt; don't bother
1650 } else { // actually retry this time
1651 $this->authErrorTimestamp = null;
1652 }
1653 }
1654 // Session keys expire after a while, so we renew them periodically
1655 $reAuth = ( ( time() - $this->authSessionTimestamp ) > $this->authTTL );
1656 // Authenticate with proxy and get a session key...
1657 if ( !$this->authCreds || $reAuth ) {
1658 $this->authSessionTimestamp = 0;
1659 $cacheKey = $this->getCredsCacheKey( $this->swiftUser );
1660 $creds = $this->srvCache->get( $cacheKey ); // credentials
1661 // Try to use the credential cache
1662 if ( isset( $creds['auth_token'] ) && isset( $creds['storage_url'] ) ) {
1663 $this->authCreds = $creds;
1664 // Skew the timestamp for worst case to avoid using stale credentials
1665 $this->authSessionTimestamp = time() - ceil( $this->authTTL / 2 );
1666 } else { // cache miss
1667 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->http->run( [
1668 'method' => 'GET',
1669 'url' => "{$this->swiftAuthUrl}/v1.0",
1670 'headers' => [
1671 'x-auth-user' => $this->swiftUser,
1672 'x-auth-key' => $this->swiftKey
1673 ]
1674 ] );
1675
1676 if ( $rcode >= 200 && $rcode <= 299 ) { // OK
1677 $this->authCreds = [
1678 'auth_token' => $rhdrs['x-auth-token'],
1679 'storage_url' => $rhdrs['x-storage-url']
1680 ];
1681 $this->srvCache->set( $cacheKey, $this->authCreds, ceil( $this->authTTL / 2 ) );
1682 $this->authSessionTimestamp = time();
1683 } elseif ( $rcode === 401 ) {
1684 $this->onError( null, __METHOD__, [], "Authentication failed.", $rcode );
1685 $this->authErrorTimestamp = time();
1686
1687 return null;
1688 } else {
1689 $this->onError( null, __METHOD__, [], "HTTP return code: $rcode", $rcode );
1690 $this->authErrorTimestamp = time();
1691
1692 return null;
1693 }
1694 }
1695 // Ceph RGW does not use <account> in URLs (OpenStack Swift uses "/v1/<account>")
1696 if ( substr( $this->authCreds['storage_url'], -3 ) === '/v1' ) {
1697 $this->isRGW = true; // take advantage of strong consistency in Ceph
1698 }
1699 }
1700
1701 return $this->authCreds;
1702 }
1703
1704 /**
1705 * @param array $creds From getAuthentication()
1706 * @param string $container
1707 * @param string $object
1708 * @return array
1709 */
1710 protected function storageUrl( array $creds, $container = null, $object = null ) {
1711 $parts = [ $creds['storage_url'] ];
1712 if ( strlen( $container ) ) {
1713 $parts[] = rawurlencode( $container );
1714 }
1715 if ( strlen( $object ) ) {
1716 $parts[] = str_replace( "%2F", "/", rawurlencode( $object ) );
1717 }
1718
1719 return implode( '/', $parts );
1720 }
1721
1722 /**
1723 * @param array $creds From getAuthentication()
1724 * @return array
1725 */
1726 protected function authTokenHeaders( array $creds ) {
1727 return [ 'x-auth-token' => $creds['auth_token'] ];
1728 }
1729
1730 /**
1731 * Get the cache key for a container
1732 *
1733 * @param string $username
1734 * @return string
1735 */
1736 private function getCredsCacheKey( $username ) {
1737 return 'swiftcredentials:' . md5( $username . ':' . $this->swiftAuthUrl );
1738 }
1739
1740 /**
1741 * Log an unexpected exception for this backend.
1742 * This also sets the Status object to have a fatal error.
1743 *
1744 * @param Status|null $status
1745 * @param string $func
1746 * @param array $params
1747 * @param string $err Error string
1748 * @param int $code HTTP status
1749 * @param string $desc HTTP status description
1750 */
1751 public function onError( $status, $func, array $params, $err = '', $code = 0, $desc = '' ) {
1752 if ( $status instanceof Status ) {
1753 $status->fatal( 'backend-fail-internal', $this->name );
1754 }
1755 if ( $code == 401 ) { // possibly a stale token
1756 $this->srvCache->delete( $this->getCredsCacheKey( $this->swiftUser ) );
1757 }
1758 wfDebugLog( 'SwiftBackend',
1759 "HTTP $code ($desc) in '{$func}' (given '" . FormatJson::encode( $params ) . "')" .
1760 ( $err ? ": $err" : "" )
1761 );
1762 }
1763 }
1764
1765 /**
1766 * @see FileBackendStoreOpHandle
1767 */
1768 class SwiftFileOpHandle extends FileBackendStoreOpHandle {
1769 /** @var array List of Requests for MultiHttpClient */
1770 public $httpOp;
1771 /** @var Closure */
1772 public $callback;
1773
1774 /**
1775 * @param SwiftFileBackend $backend
1776 * @param Closure $callback Function that takes (HTTP request array, status)
1777 * @param array $httpOp MultiHttpClient op
1778 */
1779 public function __construct( SwiftFileBackend $backend, Closure $callback, array $httpOp ) {
1780 $this->backend = $backend;
1781 $this->callback = $callback;
1782 $this->httpOp = $httpOp;
1783 }
1784 }
1785
1786 /**
1787 * SwiftFileBackend helper class to page through listings.
1788 * Swift also has a listing limit of 10,000 objects for sanity.
1789 * Do not use this class from places outside SwiftFileBackend.
1790 *
1791 * @ingroup FileBackend
1792 */
1793 abstract class SwiftFileBackendList implements Iterator {
1794 /** @var array List of path or (path,stat array) entries */
1795 protected $bufferIter = [];
1796
1797 /** @var string List items *after* this path */
1798 protected $bufferAfter = null;
1799
1800 /** @var int */
1801 protected $pos = 0;
1802
1803 /** @var array */
1804 protected $params = [];
1805
1806 /** @var SwiftFileBackend */
1807 protected $backend;
1808
1809 /** @var string Container name */
1810 protected $container;
1811
1812 /** @var string Storage directory */
1813 protected $dir;
1814
1815 /** @var int */
1816 protected $suffixStart;
1817
1818 const PAGE_SIZE = 9000; // file listing buffer size
1819
1820 /**
1821 * @param SwiftFileBackend $backend
1822 * @param string $fullCont Resolved container name
1823 * @param string $dir Resolved directory relative to container
1824 * @param array $params
1825 */
1826 public function __construct( SwiftFileBackend $backend, $fullCont, $dir, array $params ) {
1827 $this->backend = $backend;
1828 $this->container = $fullCont;
1829 $this->dir = $dir;
1830 if ( substr( $this->dir, -1 ) === '/' ) {
1831 $this->dir = substr( $this->dir, 0, -1 ); // remove trailing slash
1832 }
1833 if ( $this->dir == '' ) { // whole container
1834 $this->suffixStart = 0;
1835 } else { // dir within container
1836 $this->suffixStart = strlen( $this->dir ) + 1; // size of "path/to/dir/"
1837 }
1838 $this->params = $params;
1839 }
1840
1841 /**
1842 * @see Iterator::key()
1843 * @return int
1844 */
1845 public function key() {
1846 return $this->pos;
1847 }
1848
1849 /**
1850 * @see Iterator::next()
1851 */
1852 public function next() {
1853 // Advance to the next file in the page
1854 next( $this->bufferIter );
1855 ++$this->pos;
1856 // Check if there are no files left in this page and
1857 // advance to the next page if this page was not empty.
1858 if ( !$this->valid() && count( $this->bufferIter ) ) {
1859 $this->bufferIter = $this->pageFromList(
1860 $this->container, $this->dir, $this->bufferAfter, self::PAGE_SIZE, $this->params
1861 ); // updates $this->bufferAfter
1862 }
1863 }
1864
1865 /**
1866 * @see Iterator::rewind()
1867 */
1868 public function rewind() {
1869 $this->pos = 0;
1870 $this->bufferAfter = null;
1871 $this->bufferIter = $this->pageFromList(
1872 $this->container, $this->dir, $this->bufferAfter, self::PAGE_SIZE, $this->params
1873 ); // updates $this->bufferAfter
1874 }
1875
1876 /**
1877 * @see Iterator::valid()
1878 * @return bool
1879 */
1880 public function valid() {
1881 if ( $this->bufferIter === null ) {
1882 return false; // some failure?
1883 } else {
1884 return ( current( $this->bufferIter ) !== false ); // no paths can have this value
1885 }
1886 }
1887
1888 /**
1889 * Get the given list portion (page)
1890 *
1891 * @param string $container Resolved container name
1892 * @param string $dir Resolved path relative to container
1893 * @param string $after
1894 * @param int $limit
1895 * @param array $params
1896 * @return Traversable|array
1897 */
1898 abstract protected function pageFromList( $container, $dir, &$after, $limit, array $params );
1899 }
1900
1901 /**
1902 * Iterator for listing directories
1903 */
1904 class SwiftFileBackendDirList extends SwiftFileBackendList {
1905 /**
1906 * @see Iterator::current()
1907 * @return string|bool String (relative path) or false
1908 */
1909 public function current() {
1910 return substr( current( $this->bufferIter ), $this->suffixStart, -1 );
1911 }
1912
1913 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1914 return $this->backend->getDirListPageInternal( $container, $dir, $after, $limit, $params );
1915 }
1916 }
1917
1918 /**
1919 * Iterator for listing regular files
1920 */
1921 class SwiftFileBackendFileList extends SwiftFileBackendList {
1922 /**
1923 * @see Iterator::current()
1924 * @return string|bool String (relative path) or false
1925 */
1926 public function current() {
1927 list( $path, $stat ) = current( $this->bufferIter );
1928 $relPath = substr( $path, $this->suffixStart );
1929 if ( is_array( $stat ) ) {
1930 $storageDir = rtrim( $this->params['dir'], '/' );
1931 $this->backend->loadListingStatInternal( "$storageDir/$relPath", $stat );
1932 }
1933
1934 return $relPath;
1935 }
1936
1937 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1938 return $this->backend->getFileListPageInternal( $container, $dir, $after, $limit, $params );
1939 }
1940 }