Skip to content

Commit 8697185

Browse files
committed
Fix up database creation and handling code for servers; ref pterodactyl#2447
1 parent a4d7170 commit 8697185

File tree

10 files changed

+512
-90
lines changed

10 files changed

+512
-90
lines changed

app/Contracts/Repository/DatabaseRepositoryInterface.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,6 @@ public function getDatabasesForServer(int $server): Collection;
4242
*/
4343
public function getDatabasesForHost(int $host, int $count = 25): LengthAwarePaginator;
4444

45-
/**
46-
* Create a new database if it does not already exist on the host with
47-
* the provided details.
48-
*
49-
* @param array $data
50-
* @return \Pterodactyl\Models\Database
51-
*
52-
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
53-
* @throws \Pterodactyl\Exceptions\Repository\DuplicateDatabaseNameException
54-
*/
55-
public function createIfNotExists(array $data): Database;
56-
5745
/**
5846
* Create a new database on a given connection.
5947
*

app/Http/Controllers/Admin/ServersController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ public function saveStartup(Request $request, Server $server)
362362
public function newDatabase(StoreServerDatabaseRequest $request, Server $server)
363363
{
364364
$this->databaseManagementService->create($server, [
365-
'database' => $request->input('database'),
365+
'database' => DatabaseManagementService::generateUniqueDatabaseName($request->input('database'), $server->id),
366366
'remote' => $request->input('remote'),
367367
'database_host_id' => $request->input('database_host_id'),
368368
'max_connections' => $request->input('max_connections'),

app/Http/Controllers/Api/Application/Servers/DatabaseController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ public function resetPassword(ServerDatabaseWriteRequest $request, Server $serve
110110
*/
111111
public function store(StoreServerDatabaseRequest $request, Server $server): JsonResponse
112112
{
113-
$database = $this->databaseManagementService->create($server, $request->validated());
113+
$database = $this->databaseManagementService->create($server, array_merge($request->validated(), [
114+
'database' => $request->databaseName(),
115+
]));
114116

115117
return $this->fractal->item($database)
116118
->transformWith($this->getTransformer(ServerDatabaseTransformer::class))

app/Http/Requests/Api/Application/Servers/Databases/StoreServerDatabaseRequest.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace Pterodactyl\Http\Requests\Api\Application\Servers\Databases;
44

5+
use Webmozart\Assert\Assert;
6+
use Pterodactyl\Models\Server;
57
use Illuminate\Validation\Rule;
68
use Illuminate\Database\Query\Builder;
79
use Pterodactyl\Services\Acl\Api\AdminAcl;
10+
use Pterodactyl\Services\Databases\DatabaseManagementService;
811
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
912

1013
class StoreServerDatabaseRequest extends ApplicationApiRequest
@@ -26,14 +29,16 @@ class StoreServerDatabaseRequest extends ApplicationApiRequest
2629
*/
2730
public function rules(): array
2831
{
32+
$server = $this->route()->parameter('server');
33+
2934
return [
3035
'database' => [
3136
'required',
32-
'string',
37+
'alpha_dash',
3338
'min:1',
34-
'max:24',
35-
Rule::unique('databases')->where(function (Builder $query) {
36-
$query->where('database_host_id', $this->input('host') ?? 0);
39+
'max:48',
40+
Rule::unique('databases')->where(function (Builder $query) use ($server) {
41+
$query->where('server_id', $server->id)->where('database', $this->databaseName());
3742
}),
3843
],
3944
'remote' => 'required|string|regex:/^[0-9%.]{1,15}$/',
@@ -68,4 +73,18 @@ public function attributes()
6873
'database' => 'Database Name',
6974
];
7075
}
76+
77+
/**
78+
* Returns the database name in the expected format.
79+
*
80+
* @return string
81+
*/
82+
public function databaseName(): string
83+
{
84+
$server = $this->route()->parameter('server');
85+
86+
Assert::isInstanceOf($server, Server::class);
87+
88+
return DatabaseManagementService::generateUniqueDatabaseName($this->input('database'), $server->id);
89+
}
7190
}

app/Http/Requests/Api/Client/Servers/Databases/StoreDatabaseRequest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Database\Query\Builder;
1010
use Pterodactyl\Contracts\Http\ClientPermissionsRequest;
1111
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
12+
use Pterodactyl\Services\Databases\DatabaseManagementService;
1213

1314
class StoreDatabaseRequest extends ClientApiRequest implements ClientPermissionsRequest
1415
{
@@ -33,19 +34,23 @@ public function rules(): array
3334
'database' => [
3435
'required',
3536
'alpha_dash',
36-
'min:3',
37+
'min:1',
3738
'max:48',
3839
// Yes, I am aware that you could have the same database name across two unique hosts. However,
3940
// I don't really care about that for this validation. We just want to make sure it is unique to
4041
// the server itself. No need for complexity.
41-
Rule::unique('databases', 'database')->where(function (Builder $query) use ($server) {
42-
$query->where('server_id', $server->id);
42+
Rule::unique('databases')->where(function (Builder $query) use ($server) {
43+
$query->where('server_id', $server->id)
44+
->where('database', DatabaseManagementService::generateUniqueDatabaseName($this->input('database'), $server->id));
4345
}),
4446
],
4547
'remote' => 'required|string|regex:/^[0-9%.]{1,15}$/',
4648
];
4749
}
4850

51+
/**
52+
* @return array
53+
*/
4954
public function messages()
5055
{
5156
return [

app/Repositories/Eloquent/DatabaseRepository.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -93,31 +93,6 @@ public function getDatabasesForHost(int $host, int $count = 25): LengthAwarePagi
9393
->paginate($count, $this->getColumns());
9494
}
9595

96-
/**
97-
* Create a new database if it does not already exist on the host with
98-
* the provided details.
99-
*
100-
* @param array $data
101-
* @return \Pterodactyl\Models\Database
102-
*
103-
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
104-
* @throws \Pterodactyl\Exceptions\Repository\DuplicateDatabaseNameException
105-
*/
106-
public function createIfNotExists(array $data): Database
107-
{
108-
$count = $this->getBuilder()->where([
109-
['server_id', '=', array_get($data, 'server_id')],
110-
['database_host_id', '=', array_get($data, 'database_host_id')],
111-
['database', '=', array_get($data, 'database')],
112-
])->count();
113-
114-
if ($count > 0) {
115-
throw new DuplicateDatabaseNameException('A database with those details already exists for the specified server.');
116-
}
117-
118-
return $this->create($data);
119-
}
120-
12196
/**
12297
* Create a new database on a given connection.
12398
*

app/Services/Databases/DatabaseManagementService.php

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,29 @@
33
namespace Pterodactyl\Services\Databases;
44

55
use Exception;
6+
use InvalidArgumentException;
67
use Pterodactyl\Models\Server;
78
use Pterodactyl\Models\Database;
89
use Pterodactyl\Helpers\Utilities;
910
use Illuminate\Database\ConnectionInterface;
11+
use Symfony\Component\VarDumper\Cloner\Data;
1012
use Illuminate\Contracts\Encryption\Encrypter;
1113
use Pterodactyl\Extensions\DynamicDatabaseConnection;
12-
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
14+
use Pterodactyl\Repositories\Eloquent\DatabaseRepository;
15+
use Pterodactyl\Exceptions\Repository\DuplicateDatabaseNameException;
1316
use Pterodactyl\Exceptions\Service\Database\TooManyDatabasesException;
1417
use Pterodactyl\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException;
1518

1619
class DatabaseManagementService
1720
{
21+
/**
22+
* The regex used to validate that the database name passed through to the function is
23+
* in the expected format.
24+
*
25+
* @see \Pterodactyl\Services\Databases\DatabaseManagementService::generateUniqueDatabaseName()
26+
*/
27+
private const MATCH_NAME_REGEX = '/^(s[\d]+_)(.*)$/';
28+
1829
/**
1930
* @var \Illuminate\Database\ConnectionInterface
2031
*/
@@ -31,7 +42,7 @@ class DatabaseManagementService
3142
private $encrypter;
3243

3344
/**
34-
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
45+
* @var \Pterodactyl\Repositories\Eloquent\DatabaseRepository
3546
*/
3647
private $repository;
3748

@@ -50,13 +61,13 @@ class DatabaseManagementService
5061
*
5162
* @param \Illuminate\Database\ConnectionInterface $connection
5263
* @param \Pterodactyl\Extensions\DynamicDatabaseConnection $dynamic
53-
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $repository
64+
* @param \Pterodactyl\Repositories\Eloquent\DatabaseRepository $repository
5465
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
5566
*/
5667
public function __construct(
5768
ConnectionInterface $connection,
5869
DynamicDatabaseConnection $dynamic,
59-
DatabaseRepositoryInterface $repository,
70+
DatabaseRepository $repository,
6071
Encrypter $encrypter
6172
) {
6273
$this->connection = $connection;
@@ -65,6 +76,21 @@ public function __construct(
6576
$this->repository = $repository;
6677
}
6778

79+
/**
80+
* Generates a unique database name for the given server. This name should be passed through when
81+
* calling this handle function for this service, otherwise the database will be created with
82+
* whatever name is provided.
83+
*
84+
* @param string $name
85+
* @param int $serverId
86+
* @return string
87+
*/
88+
public static function generateUniqueDatabaseName(string $name, int $serverId): string
89+
{
90+
// Max of 48 characters, including the s123_ that we append to the front.
91+
return sprintf('s%d_%s', $serverId, substr($name, 0, 48 - strlen("s{$serverId}_")));
92+
}
93+
6894
/**
6995
* Set wether or not this class should validate that the server has enough slots
7096
* left before creating the new database.
@@ -104,12 +130,15 @@ public function create(Server $server, array $data)
104130
}
105131
}
106132

107-
// Max of 48 characters, including the s123_ that we append to the front.
108-
$truncatedName = substr($data['database'], 0, 48 - strlen("s{$server->id}_"));
133+
// Protect against developer mistakes...
134+
if (empty($data['database']) || ! preg_match(self::MATCH_NAME_REGEX, $data['database'])) {
135+
throw new InvalidArgumentException(
136+
'The database name passed to DatabaseManagementService::handle MUST be prefixed with "s{server_id}_".'
137+
);
138+
}
109139

110140
$data = array_merge($data, [
111141
'server_id' => $server->id,
112-
'database' => $truncatedName,
113142
'username' => sprintf('u%d_%s', $server->id, str_random(10)),
114143
'password' => $this->encrypter->encrypt(
115144
Utilities::randomStringWithSpecialCharacters(24)
@@ -120,7 +149,8 @@ public function create(Server $server, array $data)
120149

121150
try {
122151
return $this->connection->transaction(function () use ($data, &$database) {
123-
$database = $this->repository->createIfNotExists($data);
152+
$database = $this->createModel($data);
153+
124154
$this->dynamic->set('dynamic', $data['database_host_id']);
125155

126156
$this->repository->createDatabase($database->database);
@@ -139,7 +169,7 @@ public function create(Server $server, array $data)
139169
$this->repository->dropUser($database->username, $database->remote);
140170
$this->repository->flush();
141171
}
142-
} catch (Exception $exception) {
172+
} catch (Exception $deletionException) {
143173
// Do nothing here. We've already encountered an issue before this point so no
144174
// reason to prioritize this error over the initial one.
145175
}
@@ -166,4 +196,33 @@ public function delete(Database $database)
166196

167197
return $database->delete();
168198
}
199+
200+
/**
201+
* Create the database if there is not an identical match in the DB. While you can technically
202+
* have the same name across multiple hosts, for the sake of keeping this logic easy to understand
203+
* and avoiding user confusion we will ignore the specific host and just look across all hosts.
204+
*
205+
* @param array $data
206+
* @return \Pterodactyl\Models\Database
207+
*
208+
* @throws \Pterodactyl\Exceptions\Repository\DuplicateDatabaseNameException
209+
* @throws \Throwable
210+
*/
211+
protected function createModel(array $data): Database
212+
{
213+
$exists = Database::query()->where('server_id', $data['server_id'])
214+
->where('database', $data['database'])
215+
->exists();
216+
217+
if ($exists) {
218+
throw new DuplicateDatabaseNameException(
219+
'A database with that name already exists for this server.'
220+
);
221+
}
222+
223+
$database = (new Database)->forceFill($data);
224+
$database->saveOrFail();
225+
226+
return $database;
227+
}
169228
}

app/Services/Databases/DeployServerDatabaseService.php

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,27 @@
22

33
namespace Pterodactyl\Services\Databases;
44

5+
use Webmozart\Assert\Assert;
56
use Pterodactyl\Models\Server;
67
use Pterodactyl\Models\Database;
7-
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
8-
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
8+
use Pterodactyl\Models\DatabaseHost;
99
use Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException;
1010

1111
class DeployServerDatabaseService
1212
{
13-
/**
14-
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface
15-
*/
16-
private $databaseHostRepository;
17-
1813
/**
1914
* @var \Pterodactyl\Services\Databases\DatabaseManagementService
2015
*/
2116
private $managementService;
2217

23-
/**
24-
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
25-
*/
26-
private $repository;
27-
2818
/**
2919
* ServerDatabaseCreationService constructor.
3020
*
31-
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $repository
32-
* @param \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface $databaseHostRepository
3321
* @param \Pterodactyl\Services\Databases\DatabaseManagementService $managementService
3422
*/
35-
public function __construct(
36-
DatabaseRepositoryInterface $repository,
37-
DatabaseHostRepositoryInterface $databaseHostRepository,
38-
DatabaseManagementService $managementService
39-
) {
40-
$this->databaseHostRepository = $databaseHostRepository;
23+
public function __construct(DatabaseManagementService $managementService)
24+
{
4125
$this->managementService = $managementService;
42-
$this->repository = $repository;
4326
}
4427

4528
/**
@@ -53,28 +36,26 @@ public function __construct(
5336
*/
5437
public function handle(Server $server, array $data): Database
5538
{
56-
$allowRandom = config('pterodactyl.client_features.databases.allow_random');
57-
$hosts = $this->databaseHostRepository->setColumns(['id'])->findWhere([
58-
['node_id', '=', $server->node_id],
59-
]);
39+
Assert::notEmpty($data['database'] ?? null);
40+
Assert::notEmpty($data['remote'] ?? null);
6041

61-
if ($hosts->isEmpty() && ! $allowRandom) {
42+
$hosts = DatabaseHost::query()->get()->toBase();
43+
if ($hosts->isEmpty()) {
6244
throw new NoSuitableDatabaseHostException;
63-
}
45+
} else {
46+
$nodeHosts = $hosts->where('node_id', $server->node_id)->toBase();
6447

65-
if ($hosts->isEmpty()) {
66-
$hosts = $this->databaseHostRepository->setColumns(['id'])->all();
67-
if ($hosts->isEmpty()) {
48+
if ($nodeHosts->isEmpty() && ! config('pterodactyl.client_features.databases.allow_random')) {
6849
throw new NoSuitableDatabaseHostException;
6950
}
7051
}
7152

72-
$host = $hosts->random();
73-
7453
return $this->managementService->create($server, [
75-
'database_host_id' => $host->id,
76-
'database' => array_get($data, 'database'),
77-
'remote' => array_get($data, 'remote'),
54+
'database_host_id' => $nodeHosts->isEmpty()
55+
? $hosts->random()->id
56+
: $nodeHosts->random()->id,
57+
'database' => DatabaseManagementService::generateUniqueDatabaseName($data['database'], $server->id),
58+
'remote' => $data['remote'],
7859
]);
7960
}
8061
}

0 commit comments

Comments
 (0)