From 36837df0a6c3402c9194fa6b8b20a3d48a196347 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 3 Mar 2018 22:20:53 -0600 Subject: [PATCH] Use beginning of UUID for server uuidShort --- CHANGELOG.md | 1 + .../Repository/ServerRepositoryInterface.php | 9 ++++++++ .../Eloquent/ServerRepository.php | 12 ++++++++++ .../Servers/ServerCreationService.php | 22 ++++++++++++++++-- .../Servers/ServerCreationServiceTest.php | 23 +++++++++++++++++-- 5 files changed, 63 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc9321981..6d9126443 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines. * Databases are now properly paginated when viewing a database host. * No more loading daemon keys for every server model being loaded, some of us value our databases. * Changed behavior of the subuser middleware to add a daemon access key if one is missing from the database for some reason. +* Server short-codes are now based on the UUID as they were in previous versions of Pterodactyl. ## v0.7.4-h1 (Derelict Dermodactylus) ### Fixed diff --git a/app/Contracts/Repository/ServerRepositoryInterface.php b/app/Contracts/Repository/ServerRepositoryInterface.php index 983cf7e6e..dc677fba0 100644 --- a/app/Contracts/Repository/ServerRepositoryInterface.php +++ b/app/Contracts/Repository/ServerRepositoryInterface.php @@ -136,4 +136,13 @@ interface ServerRepositoryInterface extends RepositoryInterface, SearchableInter * @return int */ public function getServersForPowerActionCount(array $servers = [], array $nodes = []): int; + + /** + * Check if a given UUID and UUID-Short string are unique to a server. + * + * @param string $uuid + * @param string $short + * @return bool + */ + public function isUniqueUuidCombo(string $uuid, string $short): bool; } diff --git a/app/Repositories/Eloquent/ServerRepository.php b/app/Repositories/Eloquent/ServerRepository.php index ec244dd0b..2fc5f878b 100644 --- a/app/Repositories/Eloquent/ServerRepository.php +++ b/app/Repositories/Eloquent/ServerRepository.php @@ -303,6 +303,18 @@ class ServerRepository extends EloquentRepository implements ServerRepositoryInt return $this->getServersForPowerAction($servers, $nodes, true); } + /** + * Check if a given UUID and UUID-Short string are unique to a server. + * + * @param string $uuid + * @param string $short + * @return bool + */ + public function isUniqueUuidCombo(string $uuid, string $short): bool + { + return ! $this->getBuilder()->where('uuid', '=', $uuid)->orWhere('uuidShort', '=', $short)->exists(); + } + /** * Return an array of server IDs that a given user can access based * on owner and subuser permissions. diff --git a/app/Services/Servers/ServerCreationService.php b/app/Services/Servers/ServerCreationService.php index 36144241f..4b4c4ef89 100644 --- a/app/Services/Servers/ServerCreationService.php +++ b/app/Services/Servers/ServerCreationService.php @@ -210,10 +210,12 @@ class ServerCreationService */ private function createModel(array $data): Server { + $uuid = $this->generateUniqueUuidCombo(); + return $this->repository->create([ 'external_id' => array_get($data, 'external_id'), - 'uuid' => Uuid::uuid4()->toString(), - 'uuidShort' => str_random(8), + 'uuid' => $uuid, + 'uuidShort' => substr($uuid, 0, 8), 'node_id' => array_get($data, 'node_id'), 'name' => array_get($data, 'name'), 'description' => array_get($data, 'description') ?? '', @@ -289,4 +291,20 @@ class ServerCreationService return $allocation->node_id; } + + /** + * Create a unique UUID and UUID-Short combo for a server. + * + * @return string + */ + private function generateUniqueUuidCombo(): string + { + $uuid = Uuid::uuid4()->toString(); + + if (! $this->repository->isUniqueUuidCombo($uuid, substr($uuid, 0, 8))) { + return $this->generateUniqueUuidCombo(); + } + + return $uuid; + } } diff --git a/tests/Unit/Services/Servers/ServerCreationServiceTest.php b/tests/Unit/Services/Servers/ServerCreationServiceTest.php index 309508dbf..92fbec127 100644 --- a/tests/Unit/Services/Servers/ServerCreationServiceTest.php +++ b/tests/Unit/Services/Servers/ServerCreationServiceTest.php @@ -116,8 +116,14 @@ class ServerCreationServiceTest extends TestCase ]); $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); + $this->repository->shouldReceive('isUniqueUuidCombo') + ->once() + ->with($this->getKnownUuid(), substr($this->getKnownUuid(), 0, 8)) + ->andReturn(true); + $this->repository->shouldReceive('create')->with(m::subset([ 'uuid' => $this->getKnownUuid(), + 'uuidShort' => substr($this->getKnownUuid(), 0, 8), 'node_id' => $model->node_id, 'allocation_id' => $model->allocation_id, 'owner_id' => $model->owner_id, @@ -164,8 +170,14 @@ class ServerCreationServiceTest extends TestCase $this->eggRepository->shouldReceive('setColumns->find')->once()->with($model->egg_id)->andReturn($eggModel); $this->validatorService->shouldReceive('setUserLevel->handle')->once()->andReturn(collect([])); - $this->repository->shouldReceive('create')->once()->with(m::subset([ + $this->repository->shouldReceive('isUniqueUuidCombo') + ->once() + ->with($this->getKnownUuid(), substr($this->getKnownUuid(), 0, 8)) + ->andReturn(true); + + $this->repository->shouldReceive('create')->with(m::subset([ 'uuid' => $this->getKnownUuid(), + 'uuidShort' => substr($this->getKnownUuid(), 0, 8), 'node_id' => $model->node_id, 'allocation_id' => $model->allocation_id, 'nest_id' => $model->nest_id, @@ -211,8 +223,14 @@ class ServerCreationServiceTest extends TestCase $this->allocationSelectionService->shouldReceive('handle')->once()->withNoArgs()->andReturn($allocationModel); $this->validatorService->shouldReceive('setUserLevel->handle')->once()->andReturn(collect([])); - $this->repository->shouldReceive('create')->once()->with(m::subset([ + $this->repository->shouldReceive('isUniqueUuidCombo') + ->once() + ->with($this->getKnownUuid(), substr($this->getKnownUuid(), 0, 8)) + ->andReturn(true); + + $this->repository->shouldReceive('create')->with(m::subset([ 'uuid' => $this->getKnownUuid(), + 'uuidShort' => substr($this->getKnownUuid(), 0, 8), 'node_id' => $model->node_id, 'allocation_id' => $model->allocation_id, 'nest_id' => $model->nest_id, @@ -244,6 +262,7 @@ class ServerCreationServiceTest extends TestCase ]); $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); + $this->repository->shouldReceive('isUniqueUuidCombo')->once()->andReturn(true); $this->repository->shouldReceive('create')->once()->andReturn($model); $this->allocationRepository->shouldReceive('assignAllocationsToServer')->once()->andReturn(1); $this->validatorService->shouldReceive('setUserLevel')->once()->andReturnSelf();