Skip to content

Commit d049839

Browse files
committed
Fix deleting a backup that is locked and failed; closes pterodactyl#3404
1 parent 725fc82 commit d049839

File tree

4 files changed

+141
-11
lines changed

4 files changed

+141
-11
lines changed

app/Http/Controllers/Api/Remote/Backups/BackupStatusController.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,11 @@
1212
use Pterodactyl\Exceptions\DisplayException;
1313
use Pterodactyl\Http\Controllers\Controller;
1414
use Pterodactyl\Extensions\Backups\BackupManager;
15-
use Pterodactyl\Repositories\Eloquent\BackupRepository;
1615
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
1716
use Pterodactyl\Http\Requests\Api\Remote\ReportBackupCompleteRequest;
1817

1918
class BackupStatusController extends Controller
2019
{
21-
/**
22-
* @var \Pterodactyl\Repositories\Eloquent\BackupRepository
23-
*/
24-
private $repository;
25-
2620
/**
2721
* @var \Pterodactyl\Extensions\Backups\BackupManager
2822
*/
@@ -31,9 +25,8 @@ class BackupStatusController extends Controller
3125
/**
3226
* BackupStatusController constructor.
3327
*/
34-
public function __construct(BackupRepository $repository, BackupManager $backupManager)
28+
public function __construct(BackupManager $backupManager)
3529
{
36-
$this->repository = $repository;
3730
$this->backupManager = $backupManager;
3831
}
3932

@@ -64,6 +57,10 @@ public function index(ReportBackupCompleteRequest $request, string $backup)
6457
$successful = $request->input('successful') ? true : false;
6558
$model->fill([
6659
'is_successful' => $successful,
60+
// Change the lock state to unlocked if this was a failed backup so that it can be
61+
// deleted easily. Also does not make sense to have a locked backup on the system
62+
// that is failed.
63+
'is_locked' => $successful ? $model->is_locked : false,
6764
'checksum' => $successful ? ($request->input('checksum_type') . ':' . $request->input('checksum')) : null,
6865
'bytes' => $successful ? $request->input('size') : 0,
6966
'completed_at' => CarbonImmutable::now(),

app/Services/Backups/DeleteBackupService.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,20 @@ public function __construct(
5050
}
5151

5252
/**
53-
* Deletes a backup from the system.
53+
* Deletes a backup from the system. If the backup is stored in S3 a request
54+
* will be made to delete that backup from the disk as well.
5455
*
5556
* @throws \Throwable
5657
*/
5758
public function handle(Backup $backup)
5859
{
59-
if ($backup->is_locked) {
60+
// If the backup is marked as failed it can still be deleted, even if locked
61+
// since the UI doesn't allow you to unlock a failed backup in the first place.
62+
//
63+
// I also don't really see any reason you'd have a locked, failed backup to keep
64+
// around. The logic that updates the backup to the failed state will also remove
65+
// the lock, so this condition should really never happen.
66+
if ($backup->is_locked && ($backup->completed_at && $backup->is_successful)) {
6067
throw new BackupLockedException();
6168
}
6269

database/Factories/BackupFactory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Database\Factories;
44

55
use Ramsey\Uuid\Uuid;
6+
use Carbon\CarbonImmutable;
67
use Pterodactyl\Models\Backup;
78
use Illuminate\Database\Eloquent\Factories\Factory;
89

@@ -22,9 +23,11 @@ public function definition(): array
2223
{
2324
return [
2425
'uuid' => Uuid::uuid4()->toString(),
25-
'is_successful' => true,
2626
'name' => $this->faker->sentence,
2727
'disk' => Backup::ADAPTER_WINGS,
28+
'is_successful' => true,
29+
'created_at' => CarbonImmutable::now(),
30+
'completed_at' => CarbonImmutable::now(),
2831
];
2932
}
3033
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
namespace Pterodactyl\Tests\Integration\Services\Backups;
4+
5+
use Mockery;
6+
use GuzzleHttp\Psr7\Request;
7+
use GuzzleHttp\Psr7\Response;
8+
use Pterodactyl\Models\Backup;
9+
use GuzzleHttp\Exception\ClientException;
10+
use Pterodactyl\Extensions\Backups\BackupManager;
11+
use Pterodactyl\Services\Backups\DeleteBackupService;
12+
use Pterodactyl\Tests\Integration\IntegrationTestCase;
13+
use Pterodactyl\Repositories\Wings\DaemonBackupRepository;
14+
use Pterodactyl\Exceptions\Service\Backup\BackupLockedException;
15+
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
16+
17+
class DeleteBackupServiceTest extends IntegrationTestCase
18+
{
19+
private $repository;
20+
21+
public function setUp(): void
22+
{
23+
parent::setUp();
24+
25+
$this->repository = Mockery::mock(DaemonBackupRepository::class);
26+
27+
$this->app->instance(DaemonBackupRepository::class, $this->repository);
28+
}
29+
30+
public function testLockedBackupCannotBeDeleted()
31+
{
32+
$server = $this->createServerModel();
33+
$backup = Backup::factory()->create([
34+
'server_id' => $server->id,
35+
'is_locked' => true,
36+
]);
37+
38+
$this->expectException(BackupLockedException::class);
39+
40+
$this->app->make(DeleteBackupService::class)->handle($backup);
41+
}
42+
43+
public function testFailedBackupThatIsLockedCanBeDeleted()
44+
{
45+
$server = $this->createServerModel();
46+
$backup = Backup::factory()->create([
47+
'server_id' => $server->id,
48+
'is_locked' => true,
49+
'is_successful' => false,
50+
]);
51+
52+
$this->repository->expects('setServer->delete')->with($backup)->andReturn(
53+
new Response()
54+
);
55+
56+
$this->app->make(DeleteBackupService::class)->handle($backup);
57+
58+
$backup->refresh();
59+
60+
$this->assertNotNull($backup->deleted_at);
61+
}
62+
63+
public function testExceptionThrownDueToMissingBackupIsIgnored()
64+
{
65+
$server = $this->createServerModel();
66+
$backup = Backup::factory()->create(['server_id' => $server->id]);
67+
68+
$this->repository->expects('setServer->delete')->with($backup)->andThrow(
69+
new DaemonConnectionException(
70+
new ClientException('', new Request('DELETE', '/'), new Response(404))
71+
)
72+
);
73+
74+
$this->app->make(DeleteBackupService::class)->handle($backup);
75+
76+
$backup->refresh();
77+
78+
$this->assertNotNull($backup->deleted_at);
79+
}
80+
81+
public function testExceptionIsThrownIfNot404()
82+
{
83+
$server = $this->createServerModel();
84+
$backup = Backup::factory()->create(['server_id' => $server->id]);
85+
86+
$this->repository->expects('setServer->delete')->with($backup)->andThrow(
87+
new DaemonConnectionException(
88+
new ClientException('', new Request('DELETE', '/'), new Response(500))
89+
)
90+
);
91+
92+
$this->expectException(DaemonConnectionException::class);
93+
94+
$this->app->make(DeleteBackupService::class)->handle($backup);
95+
96+
$backup->refresh();
97+
98+
$this->assertNull($backup->deleted_at);
99+
}
100+
101+
public function testS3ObjectCanBeDeleted()
102+
{
103+
$server = $this->createServerModel();
104+
$backup = Backup::factory()->create([
105+
'disk' => Backup::ADAPTER_AWS_S3,
106+
'server_id' => $server->id,
107+
]);
108+
109+
$manager = $this->mock(BackupManager::class);
110+
$manager->expects('getBucket')->andReturns('foobar');
111+
$manager->expects('adapter')->with(Backup::ADAPTER_AWS_S3)->andReturnSelf();
112+
$manager->expects('getClient->deleteObject')->with([
113+
'Bucket' => 'foobar',
114+
'Key' => sprintf('%s/%s.tar.gz', $server->uuid, $backup->uuid),
115+
]);
116+
117+
$this->app->make(DeleteBackupService::class)->handle($backup);
118+
119+
$backup->refresh();
120+
121+
$this->assertNotNull($backup->deleted_at);
122+
}
123+
}

0 commit comments

Comments
 (0)