Implement Panel changes to support internal SFTP subsystem on Daemon (#703)
This commit is contained in:
parent
57db949a9c
commit
058e490ec4
23 changed files with 484 additions and 247 deletions
|
@ -18,7 +18,6 @@ use Illuminate\Database\ConnectionInterface;
|
|||
use Pterodactyl\Exceptions\PterodactylException;
|
||||
use Pterodactyl\Services\Servers\ServerCreationService;
|
||||
use Pterodactyl\Services\Servers\VariableValidatorService;
|
||||
use Pterodactyl\Services\Servers\UsernameGenerationService;
|
||||
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
||||
|
@ -109,11 +108,6 @@ class ServerCreationServiceTest extends TestCase
|
|||
*/
|
||||
protected $userRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Servers\UsernameGenerationService|\Mockery\Mock
|
||||
*/
|
||||
protected $usernameService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Servers\VariableValidatorService|\Mockery\Mock
|
||||
*/
|
||||
|
@ -135,7 +129,6 @@ class ServerCreationServiceTest extends TestCase
|
|||
$this->repository = m::mock(ServerRepositoryInterface::class);
|
||||
$this->serverVariableRepository = m::mock(ServerVariableRepositoryInterface::class);
|
||||
$this->userRepository = m::mock(UserRepositoryInterface::class);
|
||||
$this->usernameService = m::mock(UsernameGenerationService::class);
|
||||
$this->validatorService = m::mock(VariableValidatorService::class);
|
||||
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Servers', 'str_random')
|
||||
|
@ -150,7 +143,6 @@ class ServerCreationServiceTest extends TestCase
|
|||
$this->repository,
|
||||
$this->serverVariableRepository,
|
||||
$this->userRepository,
|
||||
$this->usernameService,
|
||||
$this->validatorService
|
||||
);
|
||||
}
|
||||
|
@ -165,8 +157,6 @@ class ServerCreationServiceTest extends TestCase
|
|||
->shouldReceive('validate')->with($this->data['egg_id'])->once()->andReturnSelf();
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->usernameService->shouldReceive('generate')->with($this->data['name'], 'random_string')
|
||||
->once()->andReturn('user_name');
|
||||
|
||||
$this->repository->shouldReceive('create')->with(m::subset([
|
||||
'uuid' => $this->getKnownUuid(),
|
||||
|
@ -211,7 +201,6 @@ class ServerCreationServiceTest extends TestCase
|
|||
{
|
||||
$this->validatorService->shouldReceive('isAdmin->setFields->validate->getResults')->once()->andReturn([]);
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->usernameService->shouldReceive('generate')->once()->andReturn('user_name');
|
||||
$this->repository->shouldReceive('create')->once()->andReturn((object) [
|
||||
'node_id' => 1,
|
||||
'id' => 1,
|
||||
|
|
|
@ -1,109 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* This software is licensed under the terms of the MIT license.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
namespace Tests\Unit\Services\Servers;
|
||||
|
||||
use Tests\TestCase;
|
||||
use phpmock\phpunit\PHPMock;
|
||||
use Pterodactyl\Services\Servers\UsernameGenerationService;
|
||||
|
||||
class UsernameGenerationServiceTest extends TestCase
|
||||
{
|
||||
use PHPMock;
|
||||
|
||||
/**
|
||||
* @var UsernameGenerationService
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->service = new UsernameGenerationService();
|
||||
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Servers', 'str_random')
|
||||
->expects($this->any())->willReturnCallback(function ($count) {
|
||||
return str_pad('', $count, '0');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a valid username is returned and is the correct length.
|
||||
*/
|
||||
public function testShouldReturnAValidUsernameWithASelfGeneratedIdentifier()
|
||||
{
|
||||
$response = $this->service->generate('testname');
|
||||
|
||||
$this->assertEquals('testna_00000000', $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a name and identifier provided returns the expected username.
|
||||
*/
|
||||
public function testShouldReturnAValidUsernameWithAnIdentifierProvided()
|
||||
{
|
||||
$response = $this->service->generate('testname', 'identifier');
|
||||
|
||||
$this->assertEquals('testna_identifi', $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the identifier is extended to 8 characters if it is shorter.
|
||||
*/
|
||||
public function testShouldExtendIdentifierToBe8CharactersIfItIsShorter()
|
||||
{
|
||||
$response = $this->service->generate('testname', 'xyz');
|
||||
|
||||
$this->assertEquals('testna_xyz00000', $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that special characters are removed from the username.
|
||||
*/
|
||||
public function testShouldStripSpecialCharactersFromName()
|
||||
{
|
||||
$response = $this->service->generate('te!st_n$ame', 'identifier');
|
||||
|
||||
$this->assertEquals('testna_identifi', $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an empty name is replaced with 6 random characters.
|
||||
*/
|
||||
public function testEmptyNamesShouldBeReplacedWithRandomCharacters()
|
||||
{
|
||||
$response = $this->service->generate('');
|
||||
|
||||
$this->assertEquals('000000_00000000', $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a name consisting entirely of special characters is handled.
|
||||
*/
|
||||
public function testNameOfOnlySpecialCharactersIsHandledProperly()
|
||||
{
|
||||
$response = $this->service->generate('$%#*#(@#(#*$&#(#!#@');
|
||||
|
||||
$this->assertEquals('000000_00000000', $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that passing a name shorter than 6 characters returns the entire name.
|
||||
*/
|
||||
public function testNameShorterThan6CharactersShouldBeRenderedEntirely()
|
||||
{
|
||||
$response = $this->service->generate('test', 'identifier');
|
||||
|
||||
$this->assertEquals('test_identifi', $response);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,181 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Sftp;
|
||||
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use Pterodactyl\Models\User;
|
||||
use Pterodactyl\Models\Server;
|
||||
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
||||
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
|
||||
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
||||
use Pterodactyl\Services\Sftp\AuthenticateUsingPasswordService;
|
||||
|
||||
class AuthenticateUsingPasswordServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService|\Mockery\Mock
|
||||
*/
|
||||
private $keyProviderService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
private $userRepository;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->keyProviderService = m::mock(DaemonKeyProviderService::class);
|
||||
$this->repository = m::mock(ServerRepositoryInterface::class);
|
||||
$this->userRepository = m::mock(UserRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an account can be authenticated.
|
||||
*/
|
||||
public function testNonAdminAccountIsAuthenticated()
|
||||
{
|
||||
$user = factory(User::class)->make(['root_admin' => 0]);
|
||||
$server = factory(Server::class)->make(['node_id' => 1, 'owner_id' => $user->id]);
|
||||
|
||||
$this->userRepository->shouldReceive('withColumns')->with(['id', 'root_admin', 'password'])->once()->andReturnSelf();
|
||||
$this->userRepository->shouldReceive('findFirstWhere')->with([['username', '=', $user->username]])->once()->andReturn($user);
|
||||
|
||||
$this->repository->shouldReceive('withColumns')->with(['id', 'node_id', 'owner_id', 'uuid'])->once()->andReturnSelf();
|
||||
$this->repository->shouldReceive('getByUuid')->with($server->uuidShort)->once()->andReturn($server);
|
||||
|
||||
$this->keyProviderService->shouldReceive('handle')->with($server->id, $user->id)->once()->andReturn('server_token');
|
||||
|
||||
$response = $this->getService()->handle($user->username, 'password', 1, $server->uuidShort);
|
||||
$this->assertNotEmpty($response);
|
||||
$this->assertArrayHasKey('server', $response);
|
||||
$this->assertArrayHasKey('token', $response);
|
||||
$this->assertSame($server->uuid, $response['server']);
|
||||
$this->assertSame('server_token', $response['token']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an administrative user can access servers that they are not
|
||||
* set as the owner of.
|
||||
*/
|
||||
public function testAdminAccountIsAuthenticated()
|
||||
{
|
||||
$user = factory(User::class)->make(['root_admin' => 1]);
|
||||
$server = factory(Server::class)->make(['node_id' => 1, 'owner_id' => $user->id + 1]);
|
||||
|
||||
$this->userRepository->shouldReceive('withColumns')->with(['id', 'root_admin', 'password'])->once()->andReturnSelf();
|
||||
$this->userRepository->shouldReceive('findFirstWhere')->with([['username', '=', $user->username]])->once()->andReturn($user);
|
||||
|
||||
$this->repository->shouldReceive('withColumns')->with(['id', 'node_id', 'owner_id', 'uuid'])->once()->andReturnSelf();
|
||||
$this->repository->shouldReceive('getByUuid')->with($server->uuidShort)->once()->andReturn($server);
|
||||
|
||||
$this->keyProviderService->shouldReceive('handle')->with($server->id, $user->id)->once()->andReturn('server_token');
|
||||
|
||||
$response = $this->getService()->handle($user->username, 'password', 1, $server->uuidShort);
|
||||
$this->assertNotEmpty($response);
|
||||
$this->assertArrayHasKey('server', $response);
|
||||
$this->assertArrayHasKey('token', $response);
|
||||
$this->assertSame($server->uuid, $response['server']);
|
||||
$this->assertSame('server_token', $response['token']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test exception gets thrown if no server is passed into the function.
|
||||
*
|
||||
* @expectedException \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function testExceptionIsThrownIfNoServerIsProvided()
|
||||
{
|
||||
$this->getService()->handle('username', 'password', 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception is thrown if the user account exists but the wrong
|
||||
* credentials are passed.
|
||||
*
|
||||
* @expectedException \Illuminate\Auth\AuthenticationException
|
||||
*/
|
||||
public function testExceptionIsThrownIfUserDetailsAreIncorrect()
|
||||
{
|
||||
$user = factory(User::class)->make();
|
||||
|
||||
$this->userRepository->shouldReceive('withColumns')->with(['id', 'root_admin', 'password'])->once()->andReturnSelf();
|
||||
$this->userRepository->shouldReceive('findFirstWhere')->with([['username', '=', $user->username]])->once()->andReturn($user);
|
||||
|
||||
$this->getService()->handle($user->username, 'wrongpassword', 1, '1234');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception is thrown if no user account is found.
|
||||
*
|
||||
* @expectedException \Illuminate\Auth\AuthenticationException
|
||||
*/
|
||||
public function testExceptionIsThrownIfNoUserAccountIsFound()
|
||||
{
|
||||
$this->userRepository->shouldReceive('withColumns')->with(['id', 'root_admin', 'password'])->once()->andReturnSelf();
|
||||
$this->userRepository->shouldReceive('findFirstWhere')->with([['username', '=', 'something']])->once()->andThrow(new RecordNotFoundException);
|
||||
|
||||
$this->getService()->handle('something', 'password', 1, '1234');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception is thrown if the user is not the owner of the server
|
||||
* and is not an administrator.
|
||||
*
|
||||
* @expectedException \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function testExceptionIsThrownIfUserDoesNotOwnServer()
|
||||
{
|
||||
$user = factory(User::class)->make(['root_admin' => 0]);
|
||||
$server = factory(Server::class)->make(['node_id' => 1, 'owner_id' => $user->id + 1]);
|
||||
|
||||
$this->userRepository->shouldReceive('withColumns')->with(['id', 'root_admin', 'password'])->once()->andReturnSelf();
|
||||
$this->userRepository->shouldReceive('findFirstWhere')->with([['username', '=', $user->username]])->once()->andReturn($user);
|
||||
|
||||
$this->repository->shouldReceive('withColumns')->with(['id', 'node_id', 'owner_id', 'uuid'])->once()->andReturnSelf();
|
||||
$this->repository->shouldReceive('getByUuid')->with($server->uuidShort)->once()->andReturn($server);
|
||||
|
||||
$this->getService()->handle($user->username, 'password', 1, $server->uuidShort);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception is thrown if the requested server does not belong to
|
||||
* the node that the request is made from.
|
||||
*
|
||||
* @expectedException \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function testExceptionIsThrownIfServerDoesNotExistOnCurrentNode()
|
||||
{
|
||||
$user = factory(User::class)->make(['root_admin' => 0]);
|
||||
$server = factory(Server::class)->make(['node_id' => 2, 'owner_id' => $user->id]);
|
||||
|
||||
$this->userRepository->shouldReceive('withColumns')->with(['id', 'root_admin', 'password'])->once()->andReturnSelf();
|
||||
$this->userRepository->shouldReceive('findFirstWhere')->with([['username', '=', $user->username]])->once()->andReturn($user);
|
||||
|
||||
$this->repository->shouldReceive('withColumns')->with(['id', 'node_id', 'owner_id', 'uuid'])->once()->andReturnSelf();
|
||||
$this->repository->shouldReceive('getByUuid')->with($server->uuidShort)->once()->andReturn($server);
|
||||
|
||||
$this->getService()->handle($user->username, 'password', 1, $server->uuidShort);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an instance of the service with mocked dependencies.
|
||||
*
|
||||
* @return \Pterodactyl\Services\Sftp\AuthenticateUsingPasswordService
|
||||
*/
|
||||
private function getService(): AuthenticateUsingPasswordService
|
||||
{
|
||||
return new AuthenticateUsingPasswordService($this->keyProviderService, $this->repository, $this->userRepository);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue