Files
panel/tests/Integration/Api/Remote/SftpAuthenticationControllerTest.php

233 lines
7.4 KiB
PHP
Raw Normal View History

<?php
2024-03-12 22:39:16 -04:00
namespace App\Tests\Integration\Api\Remote;
2024-04-18 03:50:20 -04:00
use App\Enums\ServerState;
use App\Enums\SubuserPermission;
2024-03-12 22:39:16 -04:00
use App\Models\Node;
Admin Roles (#502) * add spatie/permissions * add policies * add role resource * add root admin role handling * replace some "root_admin" with function * add model specific permissions * make permission selection nicer * fix user creation * fix tests * add back subuser checks in server policy * add custom model for role * assign new users to role if root_admin is set * add api for roles * fix phpstan * add permissions for settings page * remove "restore" and "forceDelete" permissions * add user count to list * prevent deletion if role has users * update user list * fix server policy * remove old `root_admin` column * small refactor * fix tests * forgot can checks here * forgot use * disable editing own roles & disable assigning root admin * don't allow to rename root admin role * remove php bombing exception handler * fix role assignment when creating a user * fix disableOptionWhen * fix missing `root_admin` attribute on react frontend * add permission check for bulk delete * rename viewAny to viewList * improve canAccessPanel check * fix admin not displaying for non-root admins * make sure non root admins can't edit root admins * fix import * fix settings page permission check * fix server permissions for non-subusers * fix settings page permission check v2 * small cleanup * cleanup config file * move consts from resouce into enum & model * Update database/migrations/2024_08_01_114538_remove_root_admin_column.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * fix config * fix phpstan * fix phpstan 2.0 --------- Co-authored-by: Lance Pioch <lancepioch@gmail.com>
2024-09-21 12:27:41 +02:00
use App\Models\Role;
2025-09-24 13:34:19 +02:00
use App\Models\Server;
use App\Models\User;
2024-03-12 22:39:16 -04:00
use App\Models\UserSSHKey;
use App\Tests\Integration\IntegrationTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
class SftpAuthenticationControllerTest extends IntegrationTestCase
{
protected User $user;
protected Server $server;
/**
* Sets up the tests.
*/
2024-03-19 21:09:16 -04:00
protected function setUp(): void
{
parent::setUp();
[$user, $server] = $this->generateTestAccount();
$user->update(['password' => password_hash('foobar', PASSWORD_DEFAULT)]);
$this->user = $user;
$this->server = $server;
$this->setAuthorization();
}
/**
* Test that a public key is validated correctly.
*/
public function test_public_key_is_validated_correctly(): void
{
$key = UserSSHKey::factory()->for($this->user)->create();
$this->postJson('/api/remote/sftp/auth', [])
->assertUnprocessable()
->assertJsonPath('errors.0.meta.source_field', 'username')
->assertJsonPath('errors.0.meta.rule', 'required')
->assertJsonPath('errors.1.meta.source_field', 'password')
->assertJsonPath('errors.1.meta.rule', 'required');
$data = [
'type' => 'public_key',
'username' => $this->getUsername(),
'password' => $key->public_key,
];
$this->postJson('/api/remote/sftp/auth', $data)
->assertOk()
->assertJsonPath('server', $this->server->uuid)
->assertJsonPath('permissions', ['*']);
$key->delete();
$this->postJson('/api/remote/sftp/auth', $data)->assertForbidden();
$this->postJson('/api/remote/sftp/auth', array_merge($data, ['type' => null]))->assertForbidden();
}
/**
* Test that an account password is validated correctly.
*/
public function test_password_is_validated_correctly(): void
{
$this->postJson('/api/remote/sftp/auth', [
'username' => $this->getUsername(),
'password' => '',
])
->assertUnprocessable()
->assertJsonPath('errors.0.meta.source_field', 'password')
->assertJsonPath('errors.0.meta.rule', 'required');
$this->postJson('/api/remote/sftp/auth', [
'username' => $this->getUsername(),
'password' => 'wrong password',
])
->assertForbidden();
$this->user->update(['password' => password_hash('foobar', PASSWORD_DEFAULT)]);
$this->postJson('/api/remote/sftp/auth', [
'username' => $this->getUsername(),
'password' => 'foobar',
])
->assertOk();
}
/**
* Test that providing an invalid key and/or invalid username triggers the throttle on
* the endpoint.
*/
#[DataProvider('authorizationTypeDataProvider')]
public function test_user_is_throttled_if_invalid_credentials_are_provided(): void
{
2024-03-12 22:47:32 -04:00
for ($i = 0; $i <= 10; $i++) {
$this->postJson('/api/remote/sftp/auth', [
'type' => 'public_key',
'username' => $i % 2 === 0 ? $this->user->username : $this->getUsername(),
'password' => 'invalid key',
])
->assertStatus($i === 10 ? 429 : 403);
}
}
/**
* Test that a request is rejected if the credentials are valid but the username indicates
* a server on a different node.
*/
#[DataProvider('authorizationTypeDataProvider')]
public function test_request_is_rejected_if_server_belongs_to_different_node(string $type): void
{
$node2 = $this->createServerModel()->node;
$this->setAuthorization($node2);
$password = $type === 'public_key'
? UserSSHKey::factory()->for($this->user)->create()->public_key
: 'foobar';
$this->postJson('/api/remote/sftp/auth', [
'type' => 'public_key',
'username' => $this->getUsername(),
'password' => $password,
])
->assertForbidden();
}
public function test_request_is_denied_if_user_lacks_sftp_permission(): void
{
[$user, $server] = $this->generateTestAccount([SubuserPermission::FileRead]);
$user->update(['password' => password_hash('foobar', PASSWORD_DEFAULT)]);
$this->setAuthorization($server->node);
$this->postJson('/api/remote/sftp/auth', [
'username' => $user->username . '.' . $server->uuid_short,
'password' => 'foobar',
])
->assertForbidden()
->assertJsonPath('errors.0.detail', 'You do not have permission to access SFTP for this server.');
}
#[DataProvider('serverStateDataProvider')]
public function test_invalid_server_state_returns_conflict_error(string $status): void
{
$this->server->update(['status' => $status]);
$this->postJson('/api/remote/sftp/auth', ['username' => $this->getUsername(), 'password' => 'foobar'])
->assertStatus(409);
}
/**
* Test that permissions are returned for the user account correctly.
*/
public function test_user_permissions_are_returned_correctly(): void
{
[$user, $server] = $this->generateTestAccount([SubuserPermission::FileRead, SubuserPermission::FileSftp]);
$user->update(['password' => password_hash('foobar', PASSWORD_DEFAULT)]);
$this->setAuthorization($server->node);
$data = [
'username' => $user->username . '.' . $server->uuid_short,
'password' => 'foobar',
];
$this->postJson('/api/remote/sftp/auth', $data)
->assertOk()
->assertJsonPath('permissions', [SubuserPermission::FileRead->value, SubuserPermission::FileSftp->value]);
Admin Roles (#502) * add spatie/permissions * add policies * add role resource * add root admin role handling * replace some "root_admin" with function * add model specific permissions * make permission selection nicer * fix user creation * fix tests * add back subuser checks in server policy * add custom model for role * assign new users to role if root_admin is set * add api for roles * fix phpstan * add permissions for settings page * remove "restore" and "forceDelete" permissions * add user count to list * prevent deletion if role has users * update user list * fix server policy * remove old `root_admin` column * small refactor * fix tests * forgot can checks here * forgot use * disable editing own roles & disable assigning root admin * don't allow to rename root admin role * remove php bombing exception handler * fix role assignment when creating a user * fix disableOptionWhen * fix missing `root_admin` attribute on react frontend * add permission check for bulk delete * rename viewAny to viewList * improve canAccessPanel check * fix admin not displaying for non-root admins * make sure non root admins can't edit root admins * fix import * fix settings page permission check * fix server permissions for non-subusers * fix settings page permission check v2 * small cleanup * cleanup config file * move consts from resouce into enum & model * Update database/migrations/2024_08_01_114538_remove_root_admin_column.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * fix config * fix phpstan * fix phpstan 2.0 --------- Co-authored-by: Lance Pioch <lancepioch@gmail.com>
2024-09-21 12:27:41 +02:00
$user->syncRoles(Role::getRootAdmin());
$this->postJson('/api/remote/sftp/auth', $data)
->assertOk()
->assertJsonPath('permissions.0', '*');
$this->setAuthorization();
$data['username'] = $user->username . '.' . $this->server->uuid_short;
$this->post('/api/remote/sftp/auth', $data)
->assertOk()
->assertJsonPath('permissions.0', '*');
Admin Roles (#502) * add spatie/permissions * add policies * add role resource * add root admin role handling * replace some "root_admin" with function * add model specific permissions * make permission selection nicer * fix user creation * fix tests * add back subuser checks in server policy * add custom model for role * assign new users to role if root_admin is set * add api for roles * fix phpstan * add permissions for settings page * remove "restore" and "forceDelete" permissions * add user count to list * prevent deletion if role has users * update user list * fix server policy * remove old `root_admin` column * small refactor * fix tests * forgot can checks here * forgot use * disable editing own roles & disable assigning root admin * don't allow to rename root admin role * remove php bombing exception handler * fix role assignment when creating a user * fix disableOptionWhen * fix missing `root_admin` attribute on react frontend * add permission check for bulk delete * rename viewAny to viewList * improve canAccessPanel check * fix admin not displaying for non-root admins * make sure non root admins can't edit root admins * fix import * fix settings page permission check * fix server permissions for non-subusers * fix settings page permission check v2 * small cleanup * cleanup config file * move consts from resouce into enum & model * Update database/migrations/2024_08_01_114538_remove_root_admin_column.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * fix config * fix phpstan * fix phpstan 2.0 --------- Co-authored-by: Lance Pioch <lancepioch@gmail.com>
2024-09-21 12:27:41 +02:00
$user->syncRoles();
$this->post('/api/remote/sftp/auth', $data)->assertForbidden();
}
2023-02-23 12:30:16 -07:00
public static function authorizationTypeDataProvider(): array
{
return [
'password auth' => ['password'],
'public key auth' => ['public_key'],
];
}
2023-02-23 12:30:16 -07:00
public static function serverStateDataProvider(): array
{
return [
2024-04-18 03:50:20 -04:00
'installing' => [ServerState::Installing->value],
'suspended' => [ServerState::Suspended->value],
'restoring a backup' => [ServerState::RestoringBackup->value],
];
}
/**
* Returns the username for connecting to SFTP.
*/
protected function getUsername(bool $long = false): string
{
return $this->user->username . '.' . ($long ? $this->server->uuid : $this->server->uuid_short);
}
/**
* Sets the authorization header for the rest of the test.
*/
protected function setAuthorization(?Node $node = null): void
{
$node = $node ?? $this->server->node;
$this->withHeader('Authorization', 'Bearer ' . $node->daemon_token_id . '.' . $node->daemon_token);
}
}