Files

135 lines
4.8 KiB
PHP
Raw Permalink Normal View History

2020-10-31 17:44:20 -06:00
<?php
namespace Pterodactyl\Http\Controllers\Api\Remote\Backups;
use Carbon\CarbonImmutable;
use Illuminate\Http\Request;
2020-11-01 10:11:45 -07:00
use Pterodactyl\Models\Backup;
2020-10-31 17:44:20 -06:00
use Illuminate\Http\JsonResponse;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Extensions\Backups\BackupManager;
2022-10-14 10:59:20 -06:00
use Pterodactyl\Extensions\Filesystem\S3Filesystem;
use Pterodactyl\Exceptions\Http\HttpForbiddenException;
2020-12-27 11:34:55 -08:00
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
2020-11-01 10:11:45 -07:00
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
2020-10-31 17:44:20 -06:00
class BackupRemoteUploadController extends Controller
{
public const DEFAULT_MAX_PART_SIZE = 5 * 1024 * 1024 * 1024;
2020-10-31 17:44:20 -06:00
/**
* BackupRemoteUploadController constructor.
*/
2022-10-14 10:59:20 -06:00
public function __construct(private BackupManager $backupManager)
2020-10-31 17:44:20 -06:00
{
}
/**
2020-11-01 10:11:45 -07:00
* Returns the required presigned urls to upload a backup to S3 cloud storage.
2020-10-31 17:44:20 -06:00
*
* @throws \Exception
* @throws \Throwable
2020-11-01 10:11:45 -07:00
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
2020-10-31 17:44:20 -06:00
*/
2022-10-14 10:59:20 -06:00
public function __invoke(Request $request, string $backup): JsonResponse
2020-10-31 17:44:20 -06:00
{
// Get the node associated with the request.
/** @var \Pterodactyl\Models\Node $node */
$node = $request->attributes->get('node');
2020-11-01 10:11:45 -07:00
// Get the size query parameter.
2020-12-16 14:15:07 -07:00
$size = (int) $request->query('size');
if (empty($size)) {
throw new BadRequestHttpException('A non-empty "size" query parameter must be provided.');
2020-10-31 17:44:20 -06:00
}
$model = Backup::query()->where('uuid', $backup)->firstOrFail();
2020-10-31 17:44:20 -06:00
// Check that the backup is "owned" by the node making the request. This avoids other nodes
// from messing with backups that they don't own.
$server = $model->server;
if ($server->node_id !== $node->id) {
throw new HttpForbiddenException('Requesting node does not have permission to access this server.');
}
2020-10-31 17:44:20 -06:00
// Prevent backups that have already been completed from trying to
// be uploaded again.
if (!is_null($model->completed_at)) {
2020-12-27 11:34:55 -08:00
throw new ConflictHttpException('This backup is already in a completed state.');
2020-10-31 17:44:20 -06:00
}
// Ensure we are using the S3 adapter.
$adapter = $this->backupManager->adapter();
2022-10-14 10:59:20 -06:00
if (!$adapter instanceof S3Filesystem) {
throw new BadRequestHttpException('The configured backup adapter is not an S3 compatible adapter.');
2020-10-31 17:44:20 -06:00
}
2020-11-01 10:11:45 -07:00
// The path where backup will be uploaded to
$path = sprintf('%s/%s.tar.gz', $model->server->uuid, $model->uuid);
2020-10-31 17:44:20 -06:00
2020-11-01 10:11:45 -07:00
// Get the S3 client
2020-10-31 17:44:20 -06:00
$client = $adapter->getClient();
2020-12-06 15:19:54 -07:00
$expires = CarbonImmutable::now()->addMinutes(config('backups.presigned_url_lifespan', 60));
2020-10-31 17:44:20 -06:00
2020-11-01 10:11:45 -07:00
// Params for generating the presigned urls
$params = [
2020-10-31 17:44:20 -06:00
'Bucket' => $adapter->getBucket(),
'Key' => $path,
'ContentType' => 'application/x-gzip',
2020-11-01 10:11:45 -07:00
];
$storageClass = config('backups.disks.s3.storage_class');
if (!is_null($storageClass)) {
$params['StorageClass'] = $storageClass;
}
2020-11-01 10:11:45 -07:00
// Execute the CreateMultipartUpload request
$result = $client->execute($client->getCommand('CreateMultipartUpload', $params));
// Get the UploadId from the CreateMultipartUpload request, this is needed to create
// the other presigned urls.
$params['UploadId'] = $result->get('UploadId');
2020-10-31 17:44:20 -06:00
// Retrieve configured part size
$maxPartSize = $this->getConfiguredMaxPartSize();
2020-11-01 10:11:45 -07:00
// Create as many UploadPart presigned urls as needed
2020-10-31 17:44:20 -06:00
$parts = [];
for ($i = 0; $i < ($size / $maxPartSize); ++$i) {
$parts[] = $client->createPresignedRequest(
2021-01-23 12:33:34 -08:00
$client->getCommand('UploadPart', array_merge($params, ['PartNumber' => $i + 1])),
$expires
)->getUri()->__toString();
2020-10-31 17:44:20 -06:00
}
// Set the upload_id on the backup in the database.
$model->update(['upload_id' => $params['UploadId']]);
return new JsonResponse([
2020-11-01 10:11:45 -07:00
'parts' => $parts,
'part_size' => $maxPartSize,
2020-11-01 10:11:45 -07:00
]);
2020-10-31 17:44:20 -06:00
}
/**
2022-10-14 10:59:20 -06:00
* Get the configured maximum size of a single part in the multipart upload.
*
* The function tries to retrieve a configured value from the configuration.
* If no value is specified, a fallback value will be used.
*
* Note if the received config cannot be converted to int (0), is zero or is negative,
* the fallback value will be used too.
*
* The fallback value is {@see BackupRemoteUploadController::DEFAULT_MAX_PART_SIZE}.
*/
2022-10-14 10:59:20 -06:00
private function getConfiguredMaxPartSize(): int
{
$maxPartSize = (int) config('backups.max_part_size', self::DEFAULT_MAX_PART_SIZE);
if ($maxPartSize <= 0) {
$maxPartSize = self::DEFAULT_MAX_PART_SIZE;
}
return $maxPartSize;
}
2020-10-31 17:44:20 -06:00
}