Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Integration/Api/Client/SSHKeyControllerTest.php
7459 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Api\Client;
4
5
use phpseclib3\Crypt\EC;
6
use Pterodactyl\Models\User;
7
use Pterodactyl\Models\UserSSHKey;
8
9
class SSHKeyControllerTest extends ClientApiIntegrationTestCase
10
{
11
/**
12
* Test that only the SSH keys for the authenticated user are returned.
13
*/
14
public function testSSHKeysAreReturned()
15
{
16
$user = User::factory()->create();
17
$user2 = User::factory()->create();
18
19
$key = UserSSHKey::factory()->for($user)->create();
20
UserSSHKey::factory()->for($user2)->rsa()->create();
21
22
$this->actingAs($user);
23
$response = $this->getJson('/api/client/account/ssh-keys')
24
->assertOk()
25
->assertJsonPath('object', 'list')
26
->assertJsonPath('data.0.object', UserSSHKey::RESOURCE_NAME);
27
28
$this->assertJsonTransformedWith($response->json('data.0.attributes'), $key);
29
}
30
31
/**
32
* Test that a user's SSH key can be deleted, and that passing the fingerprint
33
* of another user's SSH key won't delete that key.
34
*/
35
public function testSSHKeyCanBeDeleted()
36
{
37
$user = User::factory()->create();
38
$user2 = User::factory()->create();
39
40
$key = UserSSHKey::factory()->for($user)->create();
41
$key2 = UserSSHKey::factory()->for($user2)->create();
42
43
$endpoint = '/api/client/account/ssh-keys/remove';
44
45
$this->actingAs($user);
46
$this->postJson($endpoint)
47
->assertUnprocessable()
48
->assertJsonPath('errors.0.meta', ['source_field' => 'fingerprint', 'rule' => 'required']);
49
50
$this->postJson($endpoint, ['fingerprint' => $key->fingerprint])->assertNoContent();
51
52
$this->assertSoftDeleted($key);
53
$this->assertNotSoftDeleted($key2);
54
55
$this->postJson($endpoint, ['fingerprint' => $key->fingerprint])->assertNoContent();
56
$this->postJson($endpoint, ['fingerprint' => $key2->fingerprint])->assertNoContent();
57
58
$this->assertNotSoftDeleted($key2);
59
}
60
61
public function testDSAKeyIsRejected()
62
{
63
$user = User::factory()->create();
64
$key = UserSSHKey::factory()->dsa()->make();
65
66
$this->actingAs($user)->postJson('/api/client/account/ssh-keys', [
67
'name' => 'Name',
68
'public_key' => $key->public_key,
69
])
70
->assertUnprocessable()
71
->assertJsonPath('errors.0.detail', 'DSA keys are not supported.');
72
73
$this->assertEquals(0, $user->sshKeys()->count());
74
}
75
76
public function testWeakRSAKeyIsRejected()
77
{
78
$user = User::factory()->create();
79
$key = UserSSHKey::factory()->rsa(true)->make();
80
81
$this->actingAs($user)->postJson('/api/client/account/ssh-keys', [
82
'name' => 'Name',
83
'public_key' => $key->public_key,
84
])
85
->assertUnprocessable()
86
->assertJsonPath('errors.0.detail', 'RSA keys must be at least 2048 bytes in length.');
87
88
$this->assertEquals(0, $user->sshKeys()->count());
89
}
90
91
public function testInvalidOrPrivateKeyIsRejected()
92
{
93
$user = User::factory()->create();
94
95
$this->actingAs($user)->postJson('/api/client/account/ssh-keys', [
96
'name' => 'Name',
97
'public_key' => 'invalid',
98
])
99
->assertUnprocessable()
100
->assertJsonPath('errors.0.detail', 'The public key provided is not valid.');
101
102
$this->assertEquals(0, $user->sshKeys()->count());
103
104
$key = EC::createKey('Ed25519');
105
$this->actingAs($user)->postJson('/api/client/account/ssh-keys', [
106
'name' => 'Name',
107
'public_key' => $key->toString('PKCS8'),
108
])
109
->assertUnprocessable()
110
->assertJsonPath('errors.0.detail', 'The public key provided is not valid.');
111
}
112
113
public function testPublicKeyCanBeStored()
114
{
115
$user = User::factory()->create();
116
$key = UserSSHKey::factory()->make();
117
118
$this->actingAs($user)->postJson('/api/client/account/ssh-keys', [
119
'name' => 'Name',
120
'public_key' => $key->public_key,
121
])
122
->assertOk()
123
->assertJsonPath('object', UserSSHKey::RESOURCE_NAME)
124
->assertJsonPath('attributes.public_key', $key->public_key);
125
126
$this->assertCount(1, $user->sshKeys);
127
$this->assertEquals($key->public_key, $user->sshKeys[0]->public_key);
128
}
129
130
public function testPublicKeyThatAlreadyExistsCannotBeAddedASecondTime()
131
{
132
$user = User::factory()->create();
133
$key = UserSSHKey::factory()->for($user)->create();
134
135
$this->actingAs($user)->postJson('/api/client/account/ssh-keys', [
136
'name' => 'Name',
137
'public_key' => $key->public_key,
138
])
139
->assertUnprocessable()
140
->assertJsonPath('errors.0.detail', 'The public key provided already exists on your account.');
141
142
$this->assertEquals(1, $user->sshKeys()->count());
143
}
144
}
145
146