MediaWiki master
SwiftFileBackend.php
Go to the documentation of this file.
1<?php
26use Psr\Log\LoggerInterface;
27use Wikimedia\AtEase\AtEase;
28use Wikimedia\RequestTimeout\TimeoutException;
29
40 private const DEFAULT_HTTP_OPTIONS = [ 'httpVersion' => 'v1.1' ];
41 private const AUTH_FAILURE_ERROR = 'Could not connect due to prior authentication failure';
42
44 protected $http;
46 protected $authTTL;
48 protected $swiftAuthUrl;
52 protected $swiftUser;
54 protected $swiftKey;
58 protected $rgwS3AccessKey;
60 protected $rgwS3SecretKey;
62 protected $readUsers;
64 protected $writeUsers;
69
71 protected $srvCache;
72
75
77 protected $authCreds;
79 protected $authErrorTimestamp = null;
80
82 protected $isRGW = false;
83
122 public function __construct( array $config ) {
123 parent::__construct( $config );
124 // Required settings
125 $this->swiftAuthUrl = $config['swiftAuthUrl'];
126 $this->swiftUser = $config['swiftUser'];
127 $this->swiftKey = $config['swiftKey'];
128 // Optional settings
129 $this->authTTL = $config['swiftAuthTTL'] ?? 15 * 60; // some sensible number
130 $this->swiftTempUrlKey = $config['swiftTempUrlKey'] ?? '';
131 $this->swiftStorageUrl = $config['swiftStorageUrl'] ?? null;
132 $this->shardViaHashLevels = $config['shardViaHashLevels'] ?? '';
133 $this->rgwS3AccessKey = $config['rgwS3AccessKey'] ?? '';
134 $this->rgwS3SecretKey = $config['rgwS3SecretKey'] ?? '';
135
136 // HTTP helper client
137 $httpOptions = [];
138 foreach ( [ 'connTimeout', 'reqTimeout' ] as $optionName ) {
139 if ( isset( $config[$optionName] ) ) {
140 $httpOptions[$optionName] = $config[$optionName];
141 }
142 }
143 $this->http = new MultiHttpClient( $httpOptions );
144 $this->http->setLogger( $this->logger );
145
146 // Cache container information to mask latency
147 if ( isset( $config['wanCache'] ) && $config['wanCache'] instanceof WANObjectCache ) {
148 $this->memCache = $config['wanCache'];
149 }
150 // Process cache for container info
151 $this->containerStatCache = new MapCacheLRU( 300 );
152 // Cache auth token information to avoid RTTs
153 if ( !empty( $config['cacheAuthInfo'] ) && isset( $config['srvCache'] ) ) {
154 $this->srvCache = $config['srvCache'];
155 } else {
156 $this->srvCache = new EmptyBagOStuff();
157 }
158 $this->readUsers = $config['readUsers'] ?? [];
159 $this->writeUsers = $config['writeUsers'] ?? [];
160 $this->secureReadUsers = $config['secureReadUsers'] ?? [];
161 $this->secureWriteUsers = $config['secureWriteUsers'] ?? [];
162 // Per https://docs.openstack.org/swift/latest/overview_large_objects.html
163 // we need to split objects if they are larger than 5 GB. Support for
164 // splitting objects has not yet been implemented by this class
165 // so limit max file size to 5GiB.
166 $this->maxFileSize = 5 * 1024 * 1024 * 1024;
167 }
168
169 public function setLogger( LoggerInterface $logger ) {
170 parent::setLogger( $logger );
171 $this->http->setLogger( $logger );
172 }
173
174 public function getFeatures() {
175 return (
176 self::ATTR_UNICODE_PATHS |
177 self::ATTR_HEADERS |
178 self::ATTR_METADATA
179 );
180 }
181
182 protected function resolveContainerPath( $container, $relStoragePath ) {
183 if ( !mb_check_encoding( $relStoragePath, 'UTF-8' ) ) {
184 return null; // not UTF-8, makes it hard to use CF and the swift HTTP API
185 } elseif ( strlen( rawurlencode( $relStoragePath ) ) > 1024 ) {
186 return null; // too long for Swift
187 }
188
189 return $relStoragePath;
190 }
191
192 public function isPathUsableInternal( $storagePath ) {
193 [ $container, $rel ] = $this->resolveStoragePathReal( $storagePath );
194 if ( $rel === null ) {
195 return false; // invalid
196 }
197
198 return is_array( $this->getContainerStat( $container ) );
199 }
200
210 protected function extractMutableContentHeaders( array $headers ) {
211 $contentHeaders = [];
212 // Normalize casing, and strip out illegal headers
213 foreach ( $headers as $name => $value ) {
214 $name = strtolower( $name );
215 if ( $name === 'x-delete-at' && is_numeric( $value ) ) {
216 // Expects a Unix Epoch date
217 $contentHeaders[$name] = $value;
218 } elseif ( $name === 'x-delete-after' && is_numeric( $value ) ) {
219 // Expects number of minutes time to live.
220 $contentHeaders[$name] = $value;
221 } elseif ( preg_match( '/^(x-)?content-(?!length$)/', $name ) ) {
222 // Only allow content-* and x-content-* headers (but not content-length)
223 $contentHeaders[$name] = $value;
224 } elseif ( $name === 'content-type' && strlen( $value ) ) {
225 // This header can be set to a value but not unset
226 $contentHeaders[$name] = $value;
227 }
228 }
229 // By default, Swift has annoyingly low maximum header value limits
230 if ( isset( $contentHeaders['content-disposition'] ) ) {
231 $maxLength = 255;
232 // @note: assume FileBackend::makeContentDisposition() already used
233 $offset = $maxLength - strlen( $contentHeaders['content-disposition'] );
234 if ( $offset < 0 ) {
235 $pos = strrpos( $contentHeaders['content-disposition'], ';', $offset );
236 $contentHeaders['content-disposition'] = $pos === false
237 ? ''
238 : trim( substr( $contentHeaders['content-disposition'], 0, $pos ) );
239 }
240 }
241
242 return $contentHeaders;
243 }
244
250 protected function extractMetadataHeaders( array $headers ) {
251 $metadataHeaders = [];
252 foreach ( $headers as $name => $value ) {
253 $name = strtolower( $name );
254 if ( strpos( $name, 'x-object-meta-' ) === 0 ) {
255 $metadataHeaders[$name] = $value;
256 }
257 }
258
259 return $metadataHeaders;
260 }
261
267 protected function getMetadataFromHeaders( array $headers ) {
268 $prefixLen = strlen( 'x-object-meta-' );
269
270 $metadata = [];
271 foreach ( $this->extractMetadataHeaders( $headers ) as $name => $value ) {
272 $metadata[substr( $name, $prefixLen )] = $value;
273 }
274
275 return $metadata;
276 }
277
278 protected function doCreateInternal( array $params ) {
279 $status = $this->newStatus();
280
281 [ $dstCont, $dstRel ] = $this->resolveStoragePathReal( $params['dst'] );
282 if ( $dstRel === null ) {
283 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
284
285 return $status;
286 }
287
288 // Headers that are not strictly a function of the file content
289 $mutableHeaders = $this->extractMutableContentHeaders( $params['headers'] ?? [] );
290 // Make sure that the "content-type" header is set to something sensible
291 $mutableHeaders['content-type']
292 ??= $this->getContentType( $params['dst'], $params['content'], null );
293
294 $reqs = [ [
295 'method' => 'PUT',
296 'container' => $dstCont,
297 'relPath' => $dstRel,
298 'headers' => array_merge(
299 $mutableHeaders,
300 [
301 'etag' => md5( $params['content'] ),
302 'content-length' => strlen( $params['content'] ),
303 'x-object-meta-sha1base36' =>
304 Wikimedia\base_convert( sha1( $params['content'] ), 16, 36, 31 )
305 ]
306 ),
307 'body' => $params['content']
308 ] ];
309
310 $method = __METHOD__;
311 $handler = function ( array $request, StatusValue $status ) use ( $method, $params ) {
312 [ $rcode, $rdesc, , $rbody, $rerr ] = $request['response'];
313 if ( $rcode === 201 || $rcode === 202 ) {
314 // good
315 } elseif ( $rcode === 412 ) {
316 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
317 } else {
318 $this->onError( $status, $method, $params, $rerr, $rcode, $rdesc, $rbody );
319 }
320
321 return SwiftFileOpHandle::CONTINUE_IF_OK;
322 };
323
324 $opHandle = new SwiftFileOpHandle( $this, $handler, $reqs );
325 if ( !empty( $params['async'] ) ) { // deferred
326 $status->value = $opHandle;
327 } else { // actually write the object in Swift
328 $status->merge( current( $this->executeOpHandlesInternal( [ $opHandle ] ) ) );
329 }
330
331 return $status;
332 }
333
334 protected function doStoreInternal( array $params ) {
335 $status = $this->newStatus();
336
337 [ $dstCont, $dstRel ] = $this->resolveStoragePathReal( $params['dst'] );
338 if ( $dstRel === null ) {
339 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
340
341 return $status;
342 }
343
344 // Open a handle to the source file so that it can be streamed. The size and hash
345 // will be computed using the handle. In the off chance that the source file changes
346 // during this operation, the PUT will fail due to an ETag mismatch and be aborted.
347 AtEase::suppressWarnings();
348 $srcHandle = fopen( $params['src'], 'rb' );
349 AtEase::restoreWarnings();
350 if ( $srcHandle === false ) { // source doesn't exist?
351 $status->fatal( 'backend-fail-notexists', $params['src'] );
352
353 return $status;
354 }
355
356 // Compute the MD5 and SHA-1 hashes in one pass
357 $srcSize = fstat( $srcHandle )['size'];
358 $md5Context = hash_init( 'md5' );
359 $sha1Context = hash_init( 'sha1' );
360 $hashDigestSize = 0;
361 while ( !feof( $srcHandle ) ) {
362 $buffer = (string)fread( $srcHandle, 131_072 ); // 128 KiB
363 hash_update( $md5Context, $buffer );
364 hash_update( $sha1Context, $buffer );
365 $hashDigestSize += strlen( $buffer );
366 }
367 // Reset the handle back to the beginning so that it can be streamed
368 rewind( $srcHandle );
369
370 if ( $hashDigestSize !== $srcSize ) {
371 $status->fatal( 'backend-fail-hash', $params['src'] );
372
373 return $status;
374 }
375
376 // Headers that are not strictly a function of the file content
377 $mutableHeaders = $this->extractMutableContentHeaders( $params['headers'] ?? [] );
378 // Make sure that the "content-type" header is set to something sensible
379 $mutableHeaders['content-type']
380 ??= $this->getContentType( $params['dst'], null, $params['src'] );
381
382 $reqs = [ [
383 'method' => 'PUT',
384 'container' => $dstCont,
385 'relPath' => $dstRel,
386 'headers' => array_merge(
387 $mutableHeaders,
388 [
389 'content-length' => $srcSize,
390 'etag' => hash_final( $md5Context ),
391 'x-object-meta-sha1base36' =>
392 Wikimedia\base_convert( hash_final( $sha1Context ), 16, 36, 31 )
393 ]
394 ),
395 'body' => $srcHandle // resource
396 ] ];
397
398 $method = __METHOD__;
399 $handler = function ( array $request, StatusValue $status ) use ( $method, $params ) {
400 [ $rcode, $rdesc, , $rbody, $rerr ] = $request['response'];
401 if ( $rcode === 201 || $rcode === 202 ) {
402 // good
403 } elseif ( $rcode === 412 ) {
404 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
405 } else {
406 $this->onError( $status, $method, $params, $rerr, $rcode, $rdesc, $rbody );
407 }
408
409 return SwiftFileOpHandle::CONTINUE_IF_OK;
410 };
411
412 $opHandle = new SwiftFileOpHandle( $this, $handler, $reqs );
413 $opHandle->resourcesToClose[] = $srcHandle;
414
415 if ( !empty( $params['async'] ) ) { // deferred
416 $status->value = $opHandle;
417 } else { // actually write the object in Swift
418 $status->merge( current( $this->executeOpHandlesInternal( [ $opHandle ] ) ) );
419 }
420
421 return $status;
422 }
423
424 protected function doCopyInternal( array $params ) {
425 $status = $this->newStatus();
426
427 [ $srcCont, $srcRel ] = $this->resolveStoragePathReal( $params['src'] );
428 if ( $srcRel === null ) {
429 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
430
431 return $status;
432 }
433
434 [ $dstCont, $dstRel ] = $this->resolveStoragePathReal( $params['dst'] );
435 if ( $dstRel === null ) {
436 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
437
438 return $status;
439 }
440
441 $reqs = [ [
442 'method' => 'PUT',
443 'container' => $dstCont,
444 'relPath' => $dstRel,
445 'headers' => array_merge(
446 $this->extractMutableContentHeaders( $params['headers'] ?? [] ),
447 [
448 'x-copy-from' => '/' . rawurlencode( $srcCont ) . '/' .
449 str_replace( "%2F", "/", rawurlencode( $srcRel ) )
450 ]
451 )
452 ] ];
453
454 $method = __METHOD__;
455 $handler = function ( array $request, StatusValue $status ) use ( $method, $params ) {
456 [ $rcode, $rdesc, , $rbody, $rerr ] = $request['response'];
457 if ( $rcode === 201 ) {
458 // good
459 } elseif ( $rcode === 404 ) {
460 if ( empty( $params['ignoreMissingSource'] ) ) {
461 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
462 }
463 } else {
464 $this->onError( $status, $method, $params, $rerr, $rcode, $rdesc, $rbody );
465 }
466
467 return SwiftFileOpHandle::CONTINUE_IF_OK;
468 };
469
470 $opHandle = new SwiftFileOpHandle( $this, $handler, $reqs );
471 if ( !empty( $params['async'] ) ) { // deferred
472 $status->value = $opHandle;
473 } else { // actually write the object in Swift
474 $status->merge( current( $this->executeOpHandlesInternal( [ $opHandle ] ) ) );
475 }
476
477 return $status;
478 }
479
480 protected function doMoveInternal( array $params ) {
481 $status = $this->newStatus();
482
483 [ $srcCont, $srcRel ] = $this->resolveStoragePathReal( $params['src'] );
484 if ( $srcRel === null ) {
485 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
486
487 return $status;
488 }
489
490 [ $dstCont, $dstRel ] = $this->resolveStoragePathReal( $params['dst'] );
491 if ( $dstRel === null ) {
492 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
493
494 return $status;
495 }
496
497 $reqs = [ [
498 'method' => 'PUT',
499 'container' => $dstCont,
500 'relPath' => $dstRel,
501 'headers' => array_merge(
502 $this->extractMutableContentHeaders( $params['headers'] ?? [] ),
503 [
504 'x-copy-from' => '/' . rawurlencode( $srcCont ) . '/' .
505 str_replace( "%2F", "/", rawurlencode( $srcRel ) )
506 ]
507 )
508 ] ];
509 if ( "{$srcCont}/{$srcRel}" !== "{$dstCont}/{$dstRel}" ) {
510 $reqs[] = [
511 'method' => 'DELETE',
512 'container' => $srcCont,
513 'relPath' => $srcRel,
514 'headers' => []
515 ];
516 }
517
518 $method = __METHOD__;
519 $handler = function ( array $request, StatusValue $status ) use ( $method, $params ) {
520 [ $rcode, $rdesc, , $rbody, $rerr ] = $request['response'];
521 if ( $request['method'] === 'PUT' && $rcode === 201 ) {
522 // good
523 } elseif ( $request['method'] === 'DELETE' && $rcode === 204 ) {
524 // good
525 } elseif ( $rcode === 404 ) {
526 if ( empty( $params['ignoreMissingSource'] ) ) {
527 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
528 } else {
529 // Leave Status as OK but skip the DELETE request
530 return SwiftFileOpHandle::CONTINUE_NO;
531 }
532 } else {
533 $this->onError( $status, $method, $params, $rerr, $rcode, $rdesc, $rbody );
534 }
535
536 return SwiftFileOpHandle::CONTINUE_IF_OK;
537 };
538
539 $opHandle = new SwiftFileOpHandle( $this, $handler, $reqs );
540 if ( !empty( $params['async'] ) ) { // deferred
541 $status->value = $opHandle;
542 } else { // actually move the object in Swift
543 $status->merge( current( $this->executeOpHandlesInternal( [ $opHandle ] ) ) );
544 }
545
546 return $status;
547 }
548
549 protected function doDeleteInternal( array $params ) {
550 $status = $this->newStatus();
551
552 [ $srcCont, $srcRel ] = $this->resolveStoragePathReal( $params['src'] );
553 if ( $srcRel === null ) {
554 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
555
556 return $status;
557 }
558
559 $reqs = [ [
560 'method' => 'DELETE',
561 'container' => $srcCont,
562 'relPath' => $srcRel,
563 'headers' => []
564 ] ];
565
566 $method = __METHOD__;
567 $handler = function ( array $request, StatusValue $status ) use ( $method, $params ) {
568 [ $rcode, $rdesc, , $rbody, $rerr ] = $request['response'];
569 if ( $rcode === 204 ) {
570 // good
571 } elseif ( $rcode === 404 ) {
572 if ( empty( $params['ignoreMissingSource'] ) ) {
573 $status->fatal( 'backend-fail-delete', $params['src'] );
574 }
575 } else {
576 $this->onError( $status, $method, $params, $rerr, $rcode, $rdesc, $rbody );
577 }
578
579 return SwiftFileOpHandle::CONTINUE_IF_OK;
580 };
581
582 $opHandle = new SwiftFileOpHandle( $this, $handler, $reqs );
583 if ( !empty( $params['async'] ) ) { // deferred
584 $status->value = $opHandle;
585 } else { // actually delete the object in Swift
586 $status->merge( current( $this->executeOpHandlesInternal( [ $opHandle ] ) ) );
587 }
588
589 return $status;
590 }
591
592 protected function doDescribeInternal( array $params ) {
593 $status = $this->newStatus();
594
595 [ $srcCont, $srcRel ] = $this->resolveStoragePathReal( $params['src'] );
596 if ( $srcRel === null ) {
597 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
598
599 return $status;
600 }
601
602 // Fetch the old object headers/metadata...this should be in stat cache by now
603 $stat = $this->getFileStat( [ 'src' => $params['src'], 'latest' => 1 ] );
604 if ( $stat && !isset( $stat['xattr'] ) ) { // older cache entry
605 $stat = $this->doGetFileStat( [ 'src' => $params['src'], 'latest' => 1 ] );
606 }
607 if ( !$stat ) {
608 $status->fatal( 'backend-fail-describe', $params['src'] );
609
610 return $status;
611 }
612
613 // Swift object POST clears any prior headers, so merge the new and old headers here.
614 // Also, during, POST, libcurl adds "Content-Type: application/x-www-form-urlencoded"
615 // if "Content-Type" is not set, which would clobber the header value for the object.
616 $oldMetadataHeaders = [];
617 foreach ( $stat['xattr']['metadata'] as $name => $value ) {
618 $oldMetadataHeaders["x-object-meta-$name"] = $value;
619 }
620 $newContentHeaders = $this->extractMutableContentHeaders( $params['headers'] ?? [] );
621 $oldContentHeaders = $stat['xattr']['headers'];
622
623 $reqs = [ [
624 'method' => 'POST',
625 'container' => $srcCont,
626 'relPath' => $srcRel,
627 'headers' => $oldMetadataHeaders + $newContentHeaders + $oldContentHeaders
628 ] ];
629
630 $method = __METHOD__;
631 $handler = function ( array $request, StatusValue $status ) use ( $method, $params ) {
632 [ $rcode, $rdesc, , $rbody, $rerr ] = $request['response'];
633 if ( $rcode === 202 ) {
634 // good
635 } elseif ( $rcode === 404 ) {
636 $status->fatal( 'backend-fail-describe', $params['src'] );
637 } else {
638 $this->onError( $status, $method, $params, $rerr, $rcode, $rdesc, $rbody );
639 }
640 };
641
642 $opHandle = new SwiftFileOpHandle( $this, $handler, $reqs );
643 if ( !empty( $params['async'] ) ) { // deferred
644 $status->value = $opHandle;
645 } else { // actually change the object in Swift
646 $status->merge( current( $this->executeOpHandlesInternal( [ $opHandle ] ) ) );
647 }
648
649 return $status;
650 }
651
655 protected function doPrepareInternal( $fullCont, $dir, array $params ) {
656 $status = $this->newStatus();
657
658 // (a) Check if container already exists
659 $stat = $this->getContainerStat( $fullCont );
660 if ( is_array( $stat ) ) {
661 return $status; // already there
662 } elseif ( $stat === self::RES_ERROR ) {
663 $status->fatal( 'backend-fail-internal', $this->name );
664 $this->logger->error( __METHOD__ . ': cannot get container stat' );
665 } else {
666 // (b) Create container as needed with proper ACLs
667 $params['op'] = 'prepare';
668 $status->merge( $this->createContainer( $fullCont, $params ) );
669 }
670
671 return $status;
672 }
673
674 protected function doSecureInternal( $fullCont, $dir, array $params ) {
675 $status = $this->newStatus();
676 if ( empty( $params['noAccess'] ) ) {
677 return $status; // nothing to do
678 }
679
680 $stat = $this->getContainerStat( $fullCont );
681 if ( is_array( $stat ) ) {
682 $readUsers = array_merge( $this->secureReadUsers, [ $this->swiftUser ] );
683 $writeUsers = array_merge( $this->secureWriteUsers, [ $this->swiftUser ] );
684 // Make container private to end-users...
685 $status->merge( $this->setContainerAccess(
686 $fullCont,
689 ) );
690 } elseif ( $stat === self::RES_ABSENT ) {
691 $status->fatal( 'backend-fail-usable', $params['dir'] );
692 } else {
693 $status->fatal( 'backend-fail-internal', $this->name );
694 $this->logger->error( __METHOD__ . ': cannot get container stat' );
695 }
696
697 return $status;
698 }
699
700 protected function doPublishInternal( $fullCont, $dir, array $params ) {
701 $status = $this->newStatus();
702 if ( empty( $params['access'] ) ) {
703 return $status; // nothing to do
704 }
705
706 $stat = $this->getContainerStat( $fullCont );
707 if ( is_array( $stat ) ) {
708 $readUsers = array_merge( $this->readUsers, [ $this->swiftUser, '.r:*' ] );
709 if ( !empty( $params['listing'] ) ) {
710 array_push( $readUsers, '.rlistings' );
711 }
712 $writeUsers = array_merge( $this->writeUsers, [ $this->swiftUser ] );
713
714 // Make container public to end-users...
715 $status->merge( $this->setContainerAccess(
716 $fullCont,
719 ) );
720 } elseif ( $stat === self::RES_ABSENT ) {
721 $status->fatal( 'backend-fail-usable', $params['dir'] );
722 } else {
723 $status->fatal( 'backend-fail-internal', $this->name );
724 $this->logger->error( __METHOD__ . ': cannot get container stat' );
725 }
726
727 return $status;
728 }
729
730 protected function doCleanInternal( $fullCont, $dir, array $params ) {
731 $status = $this->newStatus();
732
733 // Only containers themselves can be removed, all else is virtual
734 if ( $dir != '' ) {
735 return $status; // nothing to do
736 }
737
738 // (a) Check the container
739 $stat = $this->getContainerStat( $fullCont, true );
740 if ( $stat === self::RES_ABSENT ) {
741 return $status; // ok, nothing to do
742 } elseif ( $stat === self::RES_ERROR ) {
743 $status->fatal( 'backend-fail-internal', $this->name );
744 $this->logger->error( __METHOD__ . ': cannot get container stat' );
745 } elseif ( is_array( $stat ) && $stat['count'] == 0 ) {
746 // (b) Delete the container if empty
747 $params['op'] = 'clean';
748 $status->merge( $this->deleteContainer( $fullCont, $params ) );
749 }
750
751 return $status;
752 }
753
754 protected function doGetFileStat( array $params ) {
755 $params = [ 'srcs' => [ $params['src'] ], 'concurrency' => 1 ] + $params;
756 unset( $params['src'] );
757 $stats = $this->doGetFileStatMulti( $params );
758
759 return reset( $stats );
760 }
761
772 protected function convertSwiftDate( $ts, $format = TS_MW ) {
773 try {
774 $timestamp = new MWTimestamp( $ts );
775
776 return $timestamp->getTimestamp( $format );
777 } catch ( TimeoutException $e ) {
778 throw $e;
779 } catch ( Exception $e ) {
780 throw new FileBackendError( $e->getMessage() );
781 }
782 }
783
791 protected function addMissingHashMetadata( array $objHdrs, $path ) {
792 if ( isset( $objHdrs['x-object-meta-sha1base36'] ) ) {
793 return $objHdrs; // nothing to do
794 }
795
797 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
798 $this->logger->error( __METHOD__ . ": {path} was not stored with SHA-1 metadata.",
799 [ 'path' => $path ] );
800
801 $objHdrs['x-object-meta-sha1base36'] = false;
802
803 // Find prior custom HTTP headers
804 $postHeaders = $this->extractMutableContentHeaders( $objHdrs );
805 // Find prior metadata headers
806 $postHeaders += $this->extractMetadataHeaders( $objHdrs );
807
808 $status = $this->newStatus();
810 $scopeLockS = $this->getScopedFileLocks( [ $path ], LockManager::LOCK_UW, $status );
811 if ( $status->isOK() ) {
812 $tmpFile = $this->getLocalCopy( [ 'src' => $path, 'latest' => 1 ] );
813 if ( $tmpFile ) {
814 $hash = $tmpFile->getSha1Base36();
815 if ( $hash !== false ) {
816 $objHdrs['x-object-meta-sha1base36'] = $hash;
817 // Merge new SHA1 header into the old ones
818 $postHeaders['x-object-meta-sha1base36'] = $hash;
819 [ $srcCont, $srcRel ] = $this->resolveStoragePathReal( $path );
820 [ $rcode ] = $this->requestWithAuth( [
821 'method' => 'POST',
822 'container' => $srcCont,
823 'relPath' => $srcRel,
824 'headers' => $postHeaders
825 ] );
826 if ( $rcode >= 200 && $rcode <= 299 ) {
827 $this->deleteFileCache( $path );
828
829 return $objHdrs; // success
830 }
831 }
832 }
833 }
834
835 $this->logger->error( __METHOD__ . ': unable to set SHA-1 metadata for {path}',
836 [ 'path' => $path ] );
837
838 return $objHdrs; // failed
839 }
840
841 protected function doGetFileContentsMulti( array $params ) {
842 $ep = array_diff_key( $params, [ 'srcs' => 1 ] ); // for error logging
843 // Blindly create tmp files and stream to them, catching any exception
844 // if the file does not exist. Do not waste time doing file stats here.
845 $reqs = []; // (path => op)
846
847 // Initial dummy values to preserve path order
848 $contents = array_fill_keys( $params['srcs'], self::RES_ERROR );
849 foreach ( $params['srcs'] as $path ) { // each path in this concurrent batch
850 [ $srcCont, $srcRel ] = $this->resolveStoragePathReal( $path );
851 if ( $srcRel === null ) {
852 continue; // invalid storage path
853 }
854 // Create a new temporary memory file...
855 $handle = fopen( 'php://temp', 'wb' );
856 if ( $handle ) {
857 $reqs[$path] = [
858 'method' => 'GET',
859 'container' => $srcCont,
860 'relPath' => $srcRel,
861 'headers' => $this->headersFromParams( $params ),
862 'stream' => $handle,
863 ];
864 }
865 }
866
867 $reqs = $this->requestMultiWithAuth(
868 $reqs,
869 [ 'maxConnsPerHost' => $params['concurrency'] ]
870 );
871 foreach ( $reqs as $path => $op ) {
872 [ $rcode, $rdesc, $rhdrs, $rbody, $rerr ] = $op['response'];
873 if ( $rcode >= 200 && $rcode <= 299 ) {
874 rewind( $op['stream'] ); // start from the beginning
875 $content = (string)stream_get_contents( $op['stream'] );
876 $size = strlen( $content );
877 // Make sure that stream finished
878 if ( $size === (int)$rhdrs['content-length'] ) {
879 $contents[$path] = $content;
880 } else {
881 $contents[$path] = self::RES_ERROR;
882 $rerr = "Got {$size}/{$rhdrs['content-length']} bytes";
883 $this->onError( null, __METHOD__,
884 [ 'src' => $path ] + $ep, $rerr, $rcode, $rdesc );
885 }
886 } elseif ( $rcode === 404 ) {
887 $contents[$path] = self::RES_ABSENT;
888 } else {
889 $contents[$path] = self::RES_ERROR;
890 $this->onError( null, __METHOD__,
891 [ 'src' => $path ] + $ep, $rerr, $rcode, $rdesc, $rbody );
892 }
893 fclose( $op['stream'] ); // close open handle
894 }
895
896 return $contents;
897 }
898
899 protected function doDirectoryExists( $fullCont, $dir, array $params ) {
900 $prefix = ( $dir == '' ) ? null : "{$dir}/";
901 $status = $this->objectListing( $fullCont, 'names', 1, null, $prefix );
902 if ( $status->isOK() ) {
903 return ( count( $status->value ) ) > 0;
904 }
905
906 return self::RES_ERROR;
907 }
908
916 public function getDirectoryListInternal( $fullCont, $dir, array $params ) {
917 return new SwiftFileBackendDirList( $this, $fullCont, $dir, $params );
918 }
919
927 public function getFileListInternal( $fullCont, $dir, array $params ) {
928 return new SwiftFileBackendFileList( $this, $fullCont, $dir, $params );
929 }
930
942 public function getDirListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
943 $dirs = [];
944 if ( $after === INF ) {
945 return $dirs; // nothing more
946 }
947
949 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
950
951 $prefix = ( $dir == '' ) ? null : "{$dir}/";
952 // Non-recursive: only list dirs right under $dir
953 if ( !empty( $params['topOnly'] ) ) {
954 $status = $this->objectListing( $fullCont, 'names', $limit, $after, $prefix, '/' );
955 if ( !$status->isOK() ) {
956 throw new FileBackendError( "Iterator page I/O error." );
957 }
958 $objects = $status->value;
959 // @phan-suppress-next-line PhanTypeSuspiciousNonTraversableForeach
960 foreach ( $objects as $object ) { // files and directories
961 if ( substr( $object, -1 ) === '/' ) {
962 $dirs[] = $object; // directories end in '/'
963 }
964 }
965 } else {
966 // Recursive: list all dirs under $dir and its subdirs
967 $getParentDir = static function ( $path ) {
968 return ( $path !== null && strpos( $path, '/' ) !== false ) ? dirname( $path ) : false;
969 };
970
971 // Get directory from last item of prior page
972 $lastDir = $getParentDir( $after ); // must be first page
973 $status = $this->objectListing( $fullCont, 'names', $limit, $after, $prefix );
974
975 if ( !$status->isOK() ) {
976 throw new FileBackendError( "Iterator page I/O error." );
977 }
978
979 $objects = $status->value;
980
981 // @phan-suppress-next-line PhanTypeSuspiciousNonTraversableForeach
982 foreach ( $objects as $object ) { // files
983 $objectDir = $getParentDir( $object ); // directory of object
984
985 if ( $objectDir !== false && $objectDir !== $dir ) {
986 // Swift stores paths in UTF-8, using binary sorting.
987 // See function "create_container_table" in common/db.py.
988 // If a directory is not "greater" than the last one,
989 // then it was already listed by the calling iterator.
990 if ( strcmp( $objectDir, $lastDir ) > 0 ) {
991 $pDir = $objectDir;
992 do { // add dir and all its parent dirs
993 $dirs[] = "{$pDir}/";
994 $pDir = $getParentDir( $pDir );
995 } while ( $pDir !== false
996 && strcmp( $pDir, $lastDir ) > 0 // not done already
997 && strlen( $pDir ) > strlen( $dir ) // within $dir
998 );
999 }
1000 $lastDir = $objectDir;
1001 }
1002 }
1003 }
1004 // Page on the unfiltered directory listing (what is returned may be filtered)
1005 if ( count( $objects ) < $limit ) {
1006 $after = INF; // avoid a second RTT
1007 } else {
1008 $after = end( $objects ); // update last item
1009 }
1010
1011 return $dirs;
1012 }
1013
1025 public function getFileListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
1026 $files = []; // list of (path, stat map or null) entries
1027 if ( $after === INF ) {
1028 return $files; // nothing more
1029 }
1030
1032 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
1033
1034 $prefix = ( $dir == '' ) ? null : "{$dir}/";
1035 // $objects will contain a list of unfiltered names or stdClass items
1036 // Non-recursive: only list files right under $dir
1037 if ( !empty( $params['topOnly'] ) ) {
1038 if ( !empty( $params['adviseStat'] ) ) {
1039 $status = $this->objectListing( $fullCont, 'info', $limit, $after, $prefix, '/' );
1040 } else {
1041 $status = $this->objectListing( $fullCont, 'names', $limit, $after, $prefix, '/' );
1042 }
1043 } else {
1044 // Recursive: list all files under $dir and its subdirs
1045 if ( !empty( $params['adviseStat'] ) ) {
1046 $status = $this->objectListing( $fullCont, 'info', $limit, $after, $prefix );
1047 } else {
1048 $status = $this->objectListing( $fullCont, 'names', $limit, $after, $prefix );
1049 }
1050 }
1051
1052 // Reformat this list into a list of (name, stat map or null) entries
1053 if ( !$status->isOK() ) {
1054 throw new FileBackendError( "Iterator page I/O error." );
1055 }
1056
1057 $objects = $status->value;
1058 $files = $this->buildFileObjectListing( $objects );
1059
1060 // Page on the unfiltered object listing (what is returned may be filtered)
1061 if ( count( $objects ) < $limit ) {
1062 $after = INF; // avoid a second RTT
1063 } else {
1064 $after = end( $objects ); // update last item
1065 $after = is_object( $after ) ? $after->name : $after;
1066 }
1067
1068 return $files;
1069 }
1070
1078 private function buildFileObjectListing( array $objects ) {
1079 $names = [];
1080 foreach ( $objects as $object ) {
1081 if ( is_object( $object ) ) {
1082 if ( isset( $object->subdir ) || !isset( $object->name ) ) {
1083 continue; // virtual directory entry; ignore
1084 }
1085 $stat = [
1086 // Convert various random Swift dates to TS_MW
1087 'mtime' => $this->convertSwiftDate( $object->last_modified, TS_MW ),
1088 'size' => (int)$object->bytes,
1089 'sha1' => null,
1090 // Note: manifest ETags are not an MD5 of the file
1091 'md5' => ctype_xdigit( $object->hash ) ? $object->hash : null,
1092 'latest' => false // eventually consistent
1093 ];
1094 $names[] = [ $object->name, $stat ];
1095 } elseif ( substr( $object, -1 ) !== '/' ) {
1096 // Omit directories, which end in '/' in listings
1097 $names[] = [ $object, null ];
1098 }
1099 }
1100
1101 return $names;
1102 }
1103
1110 public function loadListingStatInternal( $path, array $val ) {
1111 $this->cheapCache->setField( $path, 'stat', $val );
1112 }
1113
1114 protected function doGetFileXAttributes( array $params ) {
1115 $stat = $this->getFileStat( $params );
1116 // Stat entries filled by file listings don't include metadata/headers
1117 if ( is_array( $stat ) && !isset( $stat['xattr'] ) ) {
1118 $this->clearCache( [ $params['src'] ] );
1119 $stat = $this->getFileStat( $params );
1120 }
1121
1122 if ( is_array( $stat ) ) {
1123 return $stat['xattr'];
1124 }
1125
1126 return $stat === self::RES_ERROR ? self::RES_ERROR : self::RES_ABSENT;
1127 }
1128
1129 protected function doGetFileSha1base36( array $params ) {
1130 // Avoid using stat entries from file listings, which never include the SHA-1 hash.
1131 // Also, recompute the hash if it's not part of the metadata headers for some reason.
1132 $params['requireSHA1'] = true;
1133
1134 $stat = $this->getFileStat( $params );
1135 if ( is_array( $stat ) ) {
1136 return $stat['sha1'];
1137 }
1138
1139 return $stat === self::RES_ERROR ? self::RES_ERROR : self::RES_ABSENT;
1140 }
1141
1142 protected function doStreamFile( array $params ) {
1143 $status = $this->newStatus();
1144
1145 $flags = !empty( $params['headless'] ) ? HTTPFileStreamer::STREAM_HEADLESS : 0;
1146
1147 [ $srcCont, $srcRel ] = $this->resolveStoragePathReal( $params['src'] );
1148 if ( $srcRel === null ) {
1149 HTTPFileStreamer::send404Message( $params['src'], $flags );
1150 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
1151
1152 return $status;
1153 }
1154
1155 if ( !is_array( $this->getContainerStat( $srcCont ) ) ) {
1156 HTTPFileStreamer::send404Message( $params['src'], $flags );
1157 $status->fatal( 'backend-fail-stream', $params['src'] );
1158
1159 return $status;
1160 }
1161
1162 // If "headers" is set, we only want to send them if the file is there.
1163 // Do not bother checking if the file exists if headers are not set though.
1164 if ( $params['headers'] && !$this->fileExists( $params ) ) {
1165 HTTPFileStreamer::send404Message( $params['src'], $flags );
1166 $status->fatal( 'backend-fail-stream', $params['src'] );
1167
1168 return $status;
1169 }
1170
1171 // Send the requested additional headers
1172 if ( empty( $params['headless'] ) ) {
1173 foreach ( $params['headers'] as $header ) {
1174 header( $header );
1175 }
1176 }
1177
1178 if ( empty( $params['allowOB'] ) ) {
1179 // Cancel output buffering and gzipping if set
1180 ( $this->obResetFunc )();
1181 }
1182
1183 $handle = fopen( 'php://output', 'wb' );
1184 [ $rcode, $rdesc, , $rbody, $rerr ] = $this->requestWithAuth( [
1185 'method' => 'GET',
1186 'container' => $srcCont,
1187 'relPath' => $srcRel,
1188 'headers' => $this->headersFromParams( $params ) + $params['options'],
1189 'stream' => $handle,
1190 'flags' => [ 'relayResponseHeaders' => empty( $params['headless'] ) ]
1191 ] );
1192
1193 if ( $rcode >= 200 && $rcode <= 299 ) {
1194 // good
1195 } elseif ( $rcode === 404 ) {
1196 $status->fatal( 'backend-fail-stream', $params['src'] );
1197 // Per T43113, nasty things can happen if bad cache entries get
1198 // stuck in cache. It's also possible that this error can come up
1199 // with simple race conditions. Clear out the stat cache to be safe.
1200 $this->clearCache( [ $params['src'] ] );
1201 $this->deleteFileCache( $params['src'] );
1202 } else {
1203 $this->onError( $status, __METHOD__, $params, $rerr, $rcode, $rdesc, $rbody );
1204 }
1205
1206 return $status;
1207 }
1208
1209 protected function doGetLocalCopyMulti( array $params ) {
1210 $ep = array_diff_key( $params, [ 'srcs' => 1 ] ); // for error logging
1211 // Blindly create tmp files and stream to them, catching any exception
1212 // if the file does not exist. Do not waste time doing file stats here.
1213 $reqs = []; // (path => op)
1214
1215 // Initial dummy values to preserve path order
1216 $tmpFiles = array_fill_keys( $params['srcs'], self::RES_ERROR );
1217 foreach ( $params['srcs'] as $path ) { // each path in this concurrent batch
1218 [ $srcCont, $srcRel ] = $this->resolveStoragePathReal( $path );
1219 if ( $srcRel === null ) {
1220 continue; // invalid storage path
1221 }
1222 // Get source file extension
1224 // Create a new temporary file...
1225 $tmpFile = $this->tmpFileFactory->newTempFSFile( 'localcopy_', $ext );
1226 $handle = $tmpFile ? fopen( $tmpFile->getPath(), 'wb' ) : false;
1227 if ( $handle ) {
1228 $reqs[$path] = [
1229 'method' => 'GET',
1230 'container' => $srcCont,
1231 'relPath' => $srcRel,
1232 'headers' => $this->headersFromParams( $params ),
1233 'stream' => $handle,
1234 ];
1235 $tmpFiles[$path] = $tmpFile;
1236 }
1237 }
1238
1239 // Ceph RADOS Gateway is in use (strong consistency) or X-Newest will be used
1240 $latest = ( $this->isRGW || !empty( $params['latest'] ) );
1241
1242 $reqs = $this->requestMultiWithAuth(
1243 $reqs,
1244 [ 'maxConnsPerHost' => $params['concurrency'] ]
1245 );
1246 foreach ( $reqs as $path => $op ) {
1247 [ $rcode, $rdesc, $rhdrs, $rbody, $rerr ] = $op['response'];
1248 fclose( $op['stream'] ); // close open handle
1249 if ( $rcode >= 200 && $rcode <= 299 ) {
1251 $tmpFile = $tmpFiles[$path];
1252 // Make sure that the stream finished and fully wrote to disk
1253 $size = $tmpFile->getSize();
1254 if ( $size !== (int)$rhdrs['content-length'] ) {
1255 $tmpFiles[$path] = self::RES_ERROR;
1256 $rerr = "Got {$size}/{$rhdrs['content-length']} bytes";
1257 $this->onError( null, __METHOD__,
1258 [ 'src' => $path ] + $ep, $rerr, $rcode, $rdesc );
1259 }
1260 // Set the file stat process cache in passing
1261 $stat = $this->getStatFromHeaders( $rhdrs );
1262 $stat['latest'] = $latest;
1263 $this->cheapCache->setField( $path, 'stat', $stat );
1264 } elseif ( $rcode === 404 ) {
1265 $tmpFiles[$path] = self::RES_ABSENT;
1266 $this->cheapCache->setField(
1267 $path,
1268 'stat',
1269 $latest ? self::ABSENT_LATEST : self::ABSENT_NORMAL
1270 );
1271 } else {
1272 $tmpFiles[$path] = self::RES_ERROR;
1273 $this->onError( null, __METHOD__,
1274 [ 'src' => $path ] + $ep, $rerr, $rcode, $rdesc, $rbody );
1275 }
1276 }
1277
1278 return $tmpFiles;
1279 }
1280
1281 public function getFileHttpUrl( array $params ) {
1282 if ( $this->swiftTempUrlKey != '' ||
1283 ( $this->rgwS3AccessKey != '' && $this->rgwS3SecretKey != '' )
1284 ) {
1285 [ $srcCont, $srcRel ] = $this->resolveStoragePathReal( $params['src'] );
1286 if ( $srcRel === null ) {
1287 return self::TEMPURL_ERROR; // invalid path
1288 }
1289
1290 $auth = $this->getAuthentication();
1291 if ( !$auth ) {
1292 return self::TEMPURL_ERROR;
1293 }
1294
1295 $ttl = $params['ttl'] ?? 86400;
1296 $expires = time() + $ttl;
1297
1298 if ( $this->swiftTempUrlKey != '' ) {
1299 $url = $this->storageUrl( $auth, $srcCont, $srcRel );
1300 // Swift wants the signature based on the unencoded object name
1301 $contPath = parse_url( $this->storageUrl( $auth, $srcCont ), PHP_URL_PATH );
1302 $signature = hash_hmac( 'sha1',
1303 "GET\n{$expires}\n{$contPath}/{$srcRel}",
1304 $this->swiftTempUrlKey
1305 );
1306
1307 return "{$url}?temp_url_sig={$signature}&temp_url_expires={$expires}";
1308 } else { // give S3 API URL for rgw
1309 // Path for signature starts with the bucket
1310 $spath = '/' . rawurlencode( $srcCont ) . '/' .
1311 str_replace( '%2F', '/', rawurlencode( $srcRel ) );
1312 // Calculate the hash
1313 $signature = base64_encode( hash_hmac(
1314 'sha1',
1315 "GET\n\n\n{$expires}\n{$spath}",
1316 $this->rgwS3SecretKey,
1317 true // raw
1318 ) );
1319 // See https://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html.
1320 // Note: adding a newline for empty CanonicalizedAmzHeaders does not work.
1321 // Note: S3 API is the rgw default; remove the /swift/ URL bit.
1322 return str_replace( '/swift/v1', '', $this->storageUrl( $auth ) . $spath ) .
1323 '?' .
1324 http_build_query( [
1325 'Signature' => $signature,
1326 'Expires' => $expires,
1327 'AWSAccessKeyId' => $this->rgwS3AccessKey
1328 ] );
1329 }
1330 }
1331
1332 return self::TEMPURL_ERROR;
1333 }
1334
1335 protected function directoriesAreVirtual() {
1336 return true;
1337 }
1338
1347 protected function headersFromParams( array $params ) {
1348 $hdrs = [];
1349 if ( !empty( $params['latest'] ) ) {
1350 $hdrs['x-newest'] = 'true';
1351 }
1352
1353 return $hdrs;
1354 }
1355
1356 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
1358 '@phan-var SwiftFileOpHandle[] $fileOpHandles';
1359
1361 $statuses = [];
1362
1363 // Split the HTTP requests into stages that can be done concurrently
1364 $httpReqsByStage = []; // map of (stage => index => HTTP request)
1365 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
1366 $reqs = $fileOpHandle->httpOp;
1367 foreach ( $reqs as $stage => $req ) {
1368 $httpReqsByStage[$stage][$index] = $req;
1369 }
1370 $statuses[$index] = $this->newStatus();
1371 }
1372
1373 // Run all requests for the first stage, then the next, and so on
1374 $reqCount = count( $httpReqsByStage );
1375 for ( $stage = 0; $stage < $reqCount; ++$stage ) {
1376 $httpReqs = $this->requestMultiWithAuth( $httpReqsByStage[$stage] );
1377 foreach ( $httpReqs as $index => $httpReq ) {
1379 $fileOpHandle = $fileOpHandles[$index];
1380 // Run the callback for each request of this operation
1381 $status = $statuses[$index];
1382 ( $fileOpHandle->callback )( $httpReq, $status );
1383 // On failure, abort all remaining requests for this operation. This is used
1384 // in "move" operations to abort the DELETE request if the PUT request fails.
1385 if (
1386 !$status->isOK() ||
1387 $fileOpHandle->state === $fileOpHandle::CONTINUE_NO
1388 ) {
1389 $stages = count( $fileOpHandle->httpOp );
1390 for ( $s = ( $stage + 1 ); $s < $stages; ++$s ) {
1391 unset( $httpReqsByStage[$s][$index] );
1392 }
1393 }
1394 }
1395 }
1396
1397 return $statuses;
1398 }
1399
1422 protected function setContainerAccess( $container, array $readUsers, array $writeUsers ) {
1423 $status = $this->newStatus();
1424
1425 [ $rcode, , , , ] = $this->requestWithAuth( [
1426 'method' => 'POST',
1427 'container' => $container,
1428 'headers' => [
1429 'x-container-read' => implode( ',', $readUsers ),
1430 'x-container-write' => implode( ',', $writeUsers )
1431 ]
1432 ] );
1433
1434 if ( $rcode != 204 && $rcode !== 202 ) {
1435 $status->fatal( 'backend-fail-internal', $this->name );
1436 $this->logger->error( __METHOD__ . ': unexpected rcode value ({rcode})',
1437 [ 'rcode' => $rcode ] );
1438 }
1439
1440 return $status;
1441 }
1442
1451 protected function getContainerStat( $container, $bypassCache = false ) {
1453 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
1454
1455 if ( $bypassCache ) { // purge cache
1456 $this->containerStatCache->clear( $container );
1457 } elseif ( !$this->containerStatCache->hasField( $container, 'stat' ) ) {
1458 $this->primeContainerCache( [ $container ] ); // check persistent cache
1459 }
1460 if ( !$this->containerStatCache->hasField( $container, 'stat' ) ) {
1461 [ $rcode, $rdesc, $rhdrs, $rbody, $rerr ] = $this->requestWithAuth( [
1462 'method' => 'HEAD',
1463 'container' => $container
1464 ] );
1465
1466 if ( $rcode === 204 ) {
1467 $stat = [
1468 'count' => $rhdrs['x-container-object-count'],
1469 'bytes' => $rhdrs['x-container-bytes-used']
1470 ];
1471 if ( $bypassCache ) {
1472 return $stat;
1473 } else {
1474 $this->containerStatCache->setField( $container, 'stat', $stat ); // cache it
1475 $this->setContainerCache( $container, $stat ); // update persistent cache
1476 }
1477 } elseif ( $rcode === 404 ) {
1478 return self::RES_ABSENT;
1479 } else {
1480 $this->onError( null, __METHOD__,
1481 [ 'cont' => $container ], $rerr, $rcode, $rdesc, $rbody );
1482
1483 return self::RES_ERROR;
1484 }
1485 }
1486
1487 return $this->containerStatCache->getField( $container, 'stat' );
1488 }
1489
1497 protected function createContainer( $container, array $params ) {
1498 $status = $this->newStatus();
1499
1500 // @see SwiftFileBackend::setContainerAccess()
1501 if ( empty( $params['noAccess'] ) ) {
1502 // public
1503 $readUsers = array_merge( $this->readUsers, [ '.r:*', $this->swiftUser ] );
1504 if ( empty( $params['noListing'] ) ) {
1505 array_push( $readUsers, '.rlistings' );
1506 }
1507 $writeUsers = array_merge( $this->writeUsers, [ $this->swiftUser ] );
1508 } else {
1509 // private
1510 $readUsers = array_merge( $this->secureReadUsers, [ $this->swiftUser ] );
1511 $writeUsers = array_merge( $this->secureWriteUsers, [ $this->swiftUser ] );
1512 }
1513
1514 [ $rcode, $rdesc, , $rbody, $rerr ] = $this->requestWithAuth( [
1515 'method' => 'PUT',
1516 'container' => $container,
1517 'headers' => [
1518 'x-container-read' => implode( ',', $readUsers ),
1519 'x-container-write' => implode( ',', $writeUsers )
1520 ]
1521 ] );
1522
1523 if ( $rcode === 201 ) { // new
1524 // good
1525 } elseif ( $rcode === 202 ) { // already there
1526 // this shouldn't really happen, but is OK
1527 } else {
1528 $this->onError( $status, __METHOD__, $params, $rerr, $rcode, $rdesc, $rbody );
1529 }
1530
1531 return $status;
1532 }
1533
1541 protected function deleteContainer( $container, array $params ) {
1542 $status = $this->newStatus();
1543
1544 [ $rcode, $rdesc, , $rbody, $rerr ] = $this->requestWithAuth( [
1545 'method' => 'DELETE',
1546 'container' => $container
1547 ] );
1548
1549 if ( $rcode >= 200 && $rcode <= 299 ) { // deleted
1550 $this->containerStatCache->clear( $container ); // purge
1551 } elseif ( $rcode === 404 ) { // not there
1552 // this shouldn't really happen, but is OK
1553 } elseif ( $rcode === 409 ) { // not empty
1554 $this->onError( $status, __METHOD__, $params, $rerr, $rcode, $rdesc ); // race?
1555 } else {
1556 $this->onError( $status, __METHOD__, $params, $rerr, $rcode, $rdesc, $rbody );
1557 }
1558
1559 return $status;
1560 }
1561
1574 private function objectListing(
1575 $fullCont, $type, $limit, $after = null, $prefix = null, $delim = null
1576 ) {
1577 $status = $this->newStatus();
1578
1579 $query = [ 'limit' => $limit ];
1580 if ( $type === 'info' ) {
1581 $query['format'] = 'json';
1582 }
1583 if ( $after !== null ) {
1584 $query['marker'] = $after;
1585 }
1586 if ( $prefix !== null ) {
1587 $query['prefix'] = $prefix;
1588 }
1589 if ( $delim !== null ) {
1590 $query['delimiter'] = $delim;
1591 }
1592
1593 [ $rcode, $rdesc, , $rbody, $rerr ] = $this->requestWithAuth( [
1594 'method' => 'GET',
1595 'container' => $fullCont,
1596 'query' => $query,
1597 ] );
1598
1599 $params = [ 'cont' => $fullCont, 'prefix' => $prefix, 'delim' => $delim ];
1600 if ( $rcode === 200 ) { // good
1601 if ( $type === 'info' ) {
1602 $status->value = FormatJson::decode( trim( $rbody ) );
1603 } else {
1604 $status->value = explode( "\n", trim( $rbody ) );
1605 }
1606 } elseif ( $rcode === 204 ) {
1607 $status->value = []; // empty container
1608 } elseif ( $rcode === 404 ) {
1609 $status->value = []; // no container
1610 } else {
1611 $this->onError( $status, __METHOD__, $params, $rerr, $rcode, $rdesc, $rbody );
1612 }
1613
1614 return $status;
1615 }
1616
1617 protected function doPrimeContainerCache( array $containerInfo ) {
1618 foreach ( $containerInfo as $container => $info ) {
1619 $this->containerStatCache->setField( $container, 'stat', $info );
1620 }
1621 }
1622
1623 protected function doGetFileStatMulti( array $params ) {
1624 $stats = [];
1625
1626 $reqs = []; // (path => op)
1627 // (a) Check the containers of the paths...
1628 foreach ( $params['srcs'] as $path ) {
1629 [ $srcCont, $srcRel ] = $this->resolveStoragePathReal( $path );
1630 if ( $srcRel === null ) {
1631 // invalid storage path
1632 $stats[$path] = self::RES_ERROR;
1633 continue;
1634 }
1635
1636 $cstat = $this->getContainerStat( $srcCont );
1637 if ( $cstat === self::RES_ABSENT ) {
1638 $stats[$path] = self::RES_ABSENT;
1639 continue; // ok, nothing to do
1640 } elseif ( $cstat === self::RES_ERROR ) {
1641 $stats[$path] = self::RES_ERROR;
1642 continue;
1643 }
1644
1645 $reqs[$path] = [
1646 'method' => 'HEAD',
1647 'container' => $srcCont,
1648 'relPath' => $srcRel,
1649 'headers' => $this->headersFromParams( $params )
1650 ];
1651 }
1652
1653 // (b) Check the files themselves...
1654 $reqs = $this->requestMultiWithAuth(
1655 $reqs,
1656 [ 'maxConnsPerHost' => $params['concurrency'] ]
1657 );
1658 foreach ( $reqs as $path => $op ) {
1659 [ $rcode, $rdesc, $rhdrs, $rbody, $rerr ] = $op['response'];
1660 if ( $rcode === 200 || $rcode === 204 ) {
1661 // Update the object if it is missing some headers
1662 if ( !empty( $params['requireSHA1'] ) ) {
1663 $rhdrs = $this->addMissingHashMetadata( $rhdrs, $path );
1664 }
1665 // Load the stat map from the headers
1666 $stat = $this->getStatFromHeaders( $rhdrs );
1667 if ( $this->isRGW ) {
1668 $stat['latest'] = true; // strong consistency
1669 }
1670 } elseif ( $rcode === 404 ) {
1671 $stat = self::RES_ABSENT;
1672 } else {
1673 $stat = self::RES_ERROR;
1674 $this->onError( null, __METHOD__, $params, $rerr, $rcode, $rdesc, $rbody );
1675 }
1676 $stats[$path] = $stat;
1677 }
1678
1679 return $stats;
1680 }
1681
1686 protected function getStatFromHeaders( array $rhdrs ) {
1687 // Fetch all of the custom metadata headers
1688 $metadata = $this->getMetadataFromHeaders( $rhdrs );
1689 // Fetch all of the custom raw HTTP headers
1690 $headers = $this->extractMutableContentHeaders( $rhdrs );
1691
1692 return [
1693 // Convert various random Swift dates to TS_MW
1694 'mtime' => $this->convertSwiftDate( $rhdrs['last-modified'], TS_MW ),
1695 // Empty objects actually return no content-length header in Ceph
1696 'size' => isset( $rhdrs['content-length'] ) ? (int)$rhdrs['content-length'] : 0,
1697 'sha1' => $metadata['sha1base36'] ?? null,
1698 // Note: manifest ETags are not an MD5 of the file
1699 'md5' => ctype_xdigit( $rhdrs['etag'] ) ? $rhdrs['etag'] : null,
1700 'xattr' => [ 'metadata' => $metadata, 'headers' => $headers ]
1701 ];
1702 }
1703
1709 protected function getAuthentication() {
1710 if ( $this->authErrorTimestamp !== null ) {
1711 $interval = time() - $this->authErrorTimestamp;
1712 if ( $interval < 60 ) {
1713 $this->logger->debug(
1714 'rejecting request since auth failure occurred {interval} seconds ago',
1715 [ 'interval' => $interval ]
1716 );
1717 return null;
1718 } else { // actually retry this time
1719 $this->authErrorTimestamp = null;
1720 }
1721 }
1722 // Authenticate with proxy and get a session key...
1723 if ( !$this->authCreds ) {
1724 $cacheKey = $this->getCredsCacheKey( $this->swiftUser );
1725 $creds = $this->srvCache->get( $cacheKey ); // credentials
1726 // Try to use the credential cache
1727 if ( isset( $creds['auth_token'] )
1728 && isset( $creds['storage_url'] )
1729 && isset( $creds['expiry_time'] )
1730 && $creds['expiry_time'] > time()
1731 ) {
1732 $this->setAuthCreds( $creds );
1733 } else { // cache miss
1734 $this->refreshAuthentication();
1735 }
1736 }
1737
1738 return $this->authCreds;
1739 }
1740
1746 private function setAuthCreds( ?array $creds ) {
1747 $this->logger->debug( 'Using auth token with expiry_time={expiry_time}',
1748 [
1749 'expiry_time' => isset( $creds['expiry_time'] )
1750 ? gmdate( 'c', $creds['expiry_time'] ) : 'null'
1751 ]
1752 );
1753 $this->authCreds = $creds;
1754 // Ceph RGW does not use <account> in URLs (OpenStack Swift uses "/v1/<account>")
1755 if ( $creds && str_ends_with( $creds['storage_url'], '/v1' ) ) {
1756 $this->isRGW = true; // take advantage of strong consistency in Ceph
1757 }
1758 }
1759
1765 private function refreshAuthentication() {
1766 [ $rcode, , $rhdrs, $rbody, ] = $this->http->run( [
1767 'method' => 'GET',
1768 'url' => "{$this->swiftAuthUrl}/v1.0",
1769 'headers' => [
1770 'x-auth-user' => $this->swiftUser,
1771 'x-auth-key' => $this->swiftKey
1772 ]
1773 ], self::DEFAULT_HTTP_OPTIONS );
1774
1775 if ( $rcode >= 200 && $rcode <= 299 ) { // OK
1776 if ( isset( $rhdrs['x-auth-token-expires'] ) ) {
1777 $ttl = intval( $rhdrs['x-auth-token-expires'] );
1778 } else {
1779 $ttl = $this->authTTL;
1780 }
1781 $expiryTime = time() + $ttl;
1782 $creds = [
1783 'auth_token' => $rhdrs['x-auth-token'],
1784 'storage_url' => $this->swiftStorageUrl ?? $rhdrs['x-storage-url'],
1785 'expiry_time' => $expiryTime,
1786 ];
1787 $this->srvCache->set( $this->getCredsCacheKey( $this->swiftUser ), $creds, $expiryTime );
1788 } elseif ( $rcode === 401 ) {
1789 $this->onError( null, __METHOD__, [], "Authentication failed.", $rcode );
1790 $this->authErrorTimestamp = time();
1791 $creds = null;
1792 } else {
1793 $this->onError( null, __METHOD__, [], "HTTP return code: $rcode", $rcode, $rbody );
1794 $this->authErrorTimestamp = time();
1795 $creds = null;
1796 }
1797 $this->setAuthCreds( $creds );
1798 return $creds;
1799 }
1800
1807 protected function storageUrl( array $creds, $container = null, $object = null ) {
1808 $parts = [ $creds['storage_url'] ];
1809 if ( strlen( $container ?? '' ) ) {
1810 $parts[] = rawurlencode( $container );
1811 }
1812 if ( strlen( $object ?? '' ) ) {
1813 $parts[] = str_replace( "%2F", "/", rawurlencode( $object ) );
1814 }
1815
1816 return implode( '/', $parts );
1817 }
1818
1823 protected function authTokenHeaders( array $creds ) {
1824 return [ 'x-auth-token' => $creds['auth_token'] ];
1825 }
1826
1833 private function getCredsCacheKey( $username ) {
1834 return 'swiftcredentials:' . md5( $username . ':' . $this->swiftAuthUrl );
1835 }
1836
1851 private function requestWithAuth( array $req, array $options = [] ) {
1852 return $this->requestMultiWithAuth( [ $req ], $options )[0]['response'];
1853 }
1854
1864 private function requestMultiWithAuth( array $reqs, $options = [] ) {
1865 $remainingTries = 2;
1866 $auth = $this->getAuthentication();
1867 while ( true ) {
1868 if ( !$auth ) {
1869 foreach ( $reqs as &$req ) {
1870 if ( !isset( $req['response'] ) ) {
1871 $req['response'] = $this->getAuthFailureResponse();
1872 }
1873 }
1874 break;
1875 }
1876 foreach ( $reqs as &$req ) {
1877 '@phan-var array $req'; // Not array[]
1878 if ( isset( $req['response'] ) ) {
1879 // Request was attempted before
1880 // Retry only if it gave a 401 response code
1881 if ( $req['response']['code'] !== 401 ) {
1882 continue;
1883 }
1884 }
1885 $req['headers'] = $this->authTokenHeaders( $auth ) + ( $req['headers'] ?? [] );
1886 $req['url'] = $this->storageUrl( $auth, $req['container'], $req['relPath'] ?? null );
1887 }
1888 unset( $req );
1889 $reqs = $this->http->runMulti( $reqs, $options + self::DEFAULT_HTTP_OPTIONS );
1890 if ( --$remainingTries > 0 ) {
1891 // Retry if any request failed with 401 "not authorized"
1892 foreach ( $reqs as $req ) {
1893 if ( $req['response']['code'] === 401 ) {
1894 $auth = $this->refreshAuthentication();
1895 continue 2;
1896 }
1897 }
1898 }
1899 break;
1900 }
1901 return $reqs;
1902 }
1903
1912 private function getAuthFailureResponse() {
1913 return [
1914 'code' => 0,
1915 0 => 0,
1916 'reason' => '',
1917 1 => '',
1918 'headers' => [],
1919 2 => [],
1920 'body' => '',
1921 3 => '',
1922 'error' => self::AUTH_FAILURE_ERROR,
1923 4 => self::AUTH_FAILURE_ERROR
1924 ];
1925 }
1926
1934 private function isAuthFailureResponse( $code, $error ) {
1935 return $code === 0 && $error === self::AUTH_FAILURE_ERROR;
1936 }
1937
1950 public function onError( $status, $func, array $params, $err = '', $code = 0, $desc = '', $body = '' ) {
1951 if ( $this->isAuthFailureResponse( $code, $err ) ) {
1952 if ( $status instanceof StatusValue ) {
1953 $status->fatal( 'backend-fail-connect', $this->name );
1954 }
1955 // Already logged
1956 return;
1957 }
1958 if ( $status instanceof StatusValue ) {
1959 $status->fatal( 'backend-fail-internal', $this->name );
1960 }
1961 $msg = "HTTP {code} ({desc}) in '{func}' (given '{req_params}')";
1962 $msgParams = [
1963 'code' => $code,
1964 'desc' => $desc,
1965 'func' => $func,
1966 'req_params' => FormatJson::encode( $params ),
1967 ];
1968 if ( $err ) {
1969 $msg .= ': {err}';
1970 $msgParams['err'] = $err;
1971 }
1972 if ( $code == 502 ) {
1973 $msg .= ' ({truncatedBody})';
1974 $msgParams['truncatedBody'] = substr( strip_tags( $body ), 0, 100 );
1975 }
1976 $this->logger->error( $msg, $msgParams );
1977 }
1978}
array $params
The job parameters.
Class representing a cache/ephemeral data store.
Definition BagOStuff.php:85
A BagOStuff object with no objects in it.
File backend exception for checked exceptions (e.g.
Base class for all backends using particular storage medium.
setContainerCache( $container, array $val)
Set the cached info for a container.
executeOpHandlesInternal(array $fileOpHandles)
Execute a list of FileBackendStoreOpHandle handles in parallel.
getFileStat(array $params)
Get quick information about a file at a storage path in the backend.
resolveStoragePathReal( $storagePath)
Like resolveStoragePath() except null values are returned if the container is sharded and the shard c...
clearCache(array $paths=null)
Invalidate any in-process file stat and property cache.
primeContainerCache(array $items)
Do a batch lookup from cache for container stats for all containers used in a list of container names...
deleteFileCache( $path)
Delete the cached stat info for a file path.
getContentType( $storagePath, $content, $fsPath)
Get the content type to use in HEAD/GET requests for a file.
fileExists(array $params)
Check if a file exists at a storage path in the backend.
getLocalCopy(array $params)
Get a local copy on disk of the file at a storage path in the backend.
string $name
Unique backend name.
callable $obResetFunc
LoggerInterface $logger
static extensionFromPath( $path, $case='lowercase')
Get the final extension from a storage or FS path.
getScopedFileLocks(array $paths, $type, StatusValue $status, $timeout=0)
Lock the files at the given storage paths in the backend.
scopedProfileSection( $section)
newStatus(... $args)
Yields the result of the status wrapper callback on either:
static decode( $value, $assoc=false)
Decodes a JSON string.
Store key-value entries in a size-limited in-memory LRU cache.
Library for creating and parsing MW-style timestamps.
Class to handle multiple HTTP requests.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Iterator for listing directories.
Iterator for listing regular files.
Class for an OpenStack Swift (or Ceph RGW) based file backend.
string $swiftUser
Swift user (account:user) to authenticate as.
string $swiftAuthUrl
Authentication base URL (without version)
string $swiftTempUrlKey
Shared secret value for making temp URLs.
MapCacheLRU $containerStatCache
Container stat cache.
isPathUsableInternal( $storagePath)
Check if a file can be created or changed at a given storage path in the backend.
getDirListPageInternal( $fullCont, $dir, &$after, $limit, array $params)
Do not call this function outside of SwiftFileBackendFileList.
doPublishInternal( $fullCont, $dir, array $params)
doCreateInternal(array $params)
doGetFileStatMulti(array $params)
Get file stat information (concurrently if possible) for several files.
doGetFileSha1base36(array $params)
int null $authErrorTimestamp
UNIX timestamp.
array $writeUsers
Additional users (account:user) with write permissions on public containers.
__construct(array $config)
MultiHttpClient $http
doGetFileXAttributes(array $params)
authTokenHeaders(array $creds)
getStatFromHeaders(array $rhdrs)
string $swiftStorageUrl
Override of storage base URL.
createContainer( $container, array $params)
Create a Swift container.
doCopyInternal(array $params)
getDirectoryListInternal( $fullCont, $dir, array $params)
string $rgwS3AccessKey
S3 access key (RADOS Gateway)
setContainerAccess( $container, array $readUsers, array $writeUsers)
Set read/write permissions for a Swift container.
getFileHttpUrl(array $params)
array $secureWriteUsers
Additional users (account:user) with write permissions on private containers.
extractMetadataHeaders(array $headers)
int $authTTL
TTL in seconds.
headersFromParams(array $params)
Get headers to send to Swift when reading a file based on a FileBackend params array,...
bool $isRGW
Whether the server is an Ceph RGW.
doStoreInternal(array $params)
onError( $status, $func, array $params, $err='', $code=0, $desc='', $body='')
Log an unexpected exception for this backend.
loadListingStatInternal( $path, array $val)
Do not call this function outside of SwiftFileBackendFileList.
doPrepareInternal( $fullCont, $dir, array $params)
FileBackendStore::doPrepare() to override StatusValue Good status without value for success,...
setLogger(LoggerInterface $logger)
doSecureInternal( $fullCont, $dir, array $params)
getFileListInternal( $fullCont, $dir, array $params)
getMetadataFromHeaders(array $headers)
doMoveInternal(array $params)
addMissingHashMetadata(array $objHdrs, $path)
Fill in any missing object metadata and save it to Swift.
getFeatures()
Get the a bitfield of extra features supported by the backend medium.
deleteContainer( $container, array $params)
Delete a Swift container.
doGetFileStat(array $params)
getAuthentication()
Get the cached auth token.
doGetLocalCopyMulti(array $params)
string $rgwS3SecretKey
S3 authentication key (RADOS Gateway)
doGetFileContentsMulti(array $params)
storageUrl(array $creds, $container=null, $object=null)
convertSwiftDate( $ts, $format=TS_MW)
Convert dates like "Tue, 03 Jan 2012 22:01:04 GMT"/"2013-05-11T07:37:27.678360Z".
doStreamFile(array $params)
doPrimeContainerCache(array $containerInfo)
Fill the backend-specific process cache given an array of resolved container names and their correspo...
resolveContainerPath( $container, $relStoragePath)
Resolve a relative storage path, checking if it's allowed by the backend.
array $readUsers
Additional users (account:user) with read permissions on public containers.
array $secureReadUsers
Additional users (account:user) with read permissions on private containers.
doCleanInternal( $fullCont, $dir, array $params)
getFileListPageInternal( $fullCont, $dir, &$after, $limit, array $params)
Do not call this function outside of SwiftFileBackendFileList.
string $swiftKey
Secret key for user.
doDirectoryExists( $fullCont, $dir, array $params)
directoriesAreVirtual()
Is this a key/value store where directories are just virtual? Virtual directories exists in so much a...
doExecuteOpHandlesInternal(array $fileOpHandles)
doDeleteInternal(array $params)
doDescribeInternal(array $params)
extractMutableContentHeaders(array $headers)
Filter/normalize a header map to only include mutable "content-"/"x-content-" headers.
getContainerStat( $container, $bypassCache=false)
Get a Swift container stat map, possibly from process cache.
Multi-datacenter aware caching interface.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
$header