mirror of
https://github.com/pterodactyl/panel.git
synced 2026-11-29 10:55:43 -05:00
scope node configure flow
This commit is contained in:
@@ -6,6 +6,7 @@ use Illuminate\Http\Request;
|
||||
use Pterodactyl\Models\Node;
|
||||
use Pterodactyl\Models\ApiKey;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
||||
use Pterodactyl\Http\Controllers\Controller;
|
||||
use Illuminate\Contracts\Encryption\Encrypter;
|
||||
use Pterodactyl\Services\Api\KeyCreationService;
|
||||
@@ -23,7 +24,7 @@ class NodeAutoDeployController extends Controller
|
||||
|
||||
/**
|
||||
* Generates a new API key for the logged-in user with only permission to read
|
||||
* nodes, and returns that as the deployment key for a node.
|
||||
* node configurations, and returns that as the deployment key for a node.
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
*/
|
||||
@@ -31,18 +32,20 @@ class NodeAutoDeployController extends Controller
|
||||
{
|
||||
$key = ApiKey::query()
|
||||
->where('user_id', $request->user()->id)
|
||||
->where('node_id', $node->id)
|
||||
->where('key_type', ApiKey::TYPE_APPLICATION)
|
||||
->where('r_nodes', 1)
|
||||
->where('r_nodes', AdminAcl::READ_CONFIGURATION)
|
||||
->first();
|
||||
|
||||
// We couldn't find a key that exists for this user with only permission for
|
||||
// reading nodes. Go ahead and create it now.
|
||||
// reading node configurations. Go ahead and create it now.
|
||||
if (!$key) {
|
||||
$key = $this->keyCreationService->setKeyType(ApiKey::TYPE_APPLICATION)->handle([
|
||||
'user_id' => $request->user()->id,
|
||||
'node_id' => $node->id,
|
||||
'memo' => 'Automatically generated node deployment key.',
|
||||
'allowed_ips' => [],
|
||||
], ['r_nodes' => 1]);
|
||||
], ['r_nodes' => AdminAcl::READ_CONFIGURATION]);
|
||||
}
|
||||
|
||||
return new JsonResponse([
|
||||
|
||||
@@ -16,9 +16,13 @@ class StoreApplicationApiKeyRequest extends AdminFormRequest
|
||||
{
|
||||
$modelRules = ApiKey::getRules();
|
||||
|
||||
return collect(AdminAcl::getResourceList())->mapWithKeys(function ($resource) use ($modelRules) {
|
||||
$rules = collect(AdminAcl::getResourceList())->mapWithKeys(function ($resource) use ($modelRules) {
|
||||
return [AdminAcl::COLUMN_IDENTIFIER . $resource => $modelRules['r_' . $resource]];
|
||||
})->merge(['memo' => $modelRules['memo']])->toArray();
|
||||
|
||||
$rules['r_' . AdminAcl::RESOURCE_NODES] = ['integer', 'in:0,1,3'];
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
public function attributes(): array
|
||||
|
||||
@@ -2,9 +2,25 @@
|
||||
|
||||
namespace Pterodactyl\Http\Requests\Api\Application\Nodes;
|
||||
|
||||
use Pterodactyl\Models\Node;
|
||||
use Pterodactyl\Models\ApiKey;
|
||||
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
||||
|
||||
class GetNodeConfigurationRequest extends GetNodesRequest
|
||||
{
|
||||
protected int $permission = AdminAcl::WRITE;
|
||||
|
||||
public function authorize(): bool
|
||||
{
|
||||
$token = $this->user()->currentAccessToken();
|
||||
if ($token instanceof ApiKey && AdminAcl::check($token, $this->resource, AdminAcl::READ_CONFIGURATION)) {
|
||||
$node = $this->parameter('node', Node::class);
|
||||
|
||||
if ($token->node_id === $node->id) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return parent::authorize();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $user_id
|
||||
* @property int|null $node_id
|
||||
* @property int $key_type
|
||||
* @property string $identifier
|
||||
* @property string $token
|
||||
@@ -103,6 +104,7 @@ class ApiKey extends Model implements HasAbilities
|
||||
protected $casts = [
|
||||
'allowed_ips' => 'array',
|
||||
'user_id' => 'int',
|
||||
'node_id' => 'int',
|
||||
'last_used_at' => 'datetime',
|
||||
'expires_at' => 'datetime',
|
||||
self::CREATED_AT => 'datetime',
|
||||
@@ -147,6 +149,7 @@ class ApiKey extends Model implements HasAbilities
|
||||
'memo' => 'required|nullable|string|max:500',
|
||||
'allowed_ips' => 'nullable|array',
|
||||
'allowed_ips.*' => 'string',
|
||||
'node_id' => 'nullable|exists:nodes,id',
|
||||
'last_used_at' => 'nullable|date',
|
||||
'expires_at' => 'nullable|date',
|
||||
'r_' . AdminAcl::RESOURCE_USERS => 'integer|min:0|max:3',
|
||||
@@ -156,7 +159,7 @@ class ApiKey extends Model implements HasAbilities
|
||||
'r_' . AdminAcl::RESOURCE_EGGS => 'integer|min:0|max:3',
|
||||
'r_' . AdminAcl::RESOURCE_LOCATIONS => 'integer|min:0|max:3',
|
||||
'r_' . AdminAcl::RESOURCE_NESTS => 'integer|min:0|max:3',
|
||||
'r_' . AdminAcl::RESOURCE_NODES => 'integer|min:0|max:3',
|
||||
'r_' . AdminAcl::RESOURCE_NODES => 'integer|min:0|max:7',
|
||||
'r_' . AdminAcl::RESOURCE_SERVERS => 'integer|min:0|max:3',
|
||||
];
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ class AdminAcl
|
||||
public const NONE = 0;
|
||||
public const READ = 1;
|
||||
public const WRITE = 2;
|
||||
public const READ_CONFIGURATION = 4;
|
||||
|
||||
/**
|
||||
* Resources that are available on the API and can contain a permissions
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
return new class () extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('api_keys', function (Blueprint $table) {
|
||||
$table->unsignedInteger('node_id')->nullable()->after('user_id');
|
||||
$table->foreign('node_id')->references('id')->on('nodes')->onDelete('cascade');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('api_keys', function (Blueprint $table) {
|
||||
$table->dropForeign(['node_id']);
|
||||
$table->dropColumn('node_id');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -63,6 +63,7 @@ DROP TABLE IF EXISTS `api_keys`;
|
||||
CREATE TABLE `api_keys` (
|
||||
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
`user_id` int(10) unsigned NOT NULL,
|
||||
`node_id` int(10) unsigned DEFAULT NULL,
|
||||
`key_type` tinyint(3) unsigned NOT NULL DEFAULT 0,
|
||||
`identifier` char(16) DEFAULT NULL,
|
||||
`token` text NOT NULL,
|
||||
@@ -84,6 +85,8 @@ CREATE TABLE `api_keys` (
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `api_keys_identifier_unique` (`identifier`),
|
||||
KEY `api_keys_user_id_foreign` (`user_id`),
|
||||
KEY `api_keys_node_id_foreign` (`node_id`),
|
||||
CONSTRAINT `api_keys_node_id_foreign` FOREIGN KEY (`node_id`) REFERENCES `nodes` (`id`) ON DELETE CASCADE,
|
||||
CONSTRAINT `api_keys_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
@@ -876,3 +879,4 @@ INSERT INTO `migrations` VALUES (191,'2022_08_16_230204_add_installed_at_column_
|
||||
INSERT INTO `migrations` VALUES (192,'2022_12_12_213937_update_mail_settings_to_new_format',1);
|
||||
INSERT INTO `migrations` VALUES (193,'2023_01_24_210051_add_uuid_column_to_failed_jobs_table',1);
|
||||
INSERT INTO `migrations` VALUES (194,'2023_02_23_191004_add_expires_at_column_to_api_keys_table',1);
|
||||
INSERT INTO `migrations` VALUES (195,'2026_06_23_205500_add_node_id_to_api_keys_table',1);
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace Pterodactyl\Tests\Integration\Api\Application\Nodes;
|
||||
|
||||
use Pterodactyl\Models\Node;
|
||||
use Pterodactyl\Models\Location;
|
||||
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
||||
use Pterodactyl\Tests\Integration\Api\Application\ApplicationApiIntegrationTestCase;
|
||||
|
||||
class NodeConfigurationControllerTest extends ApplicationApiIntegrationTestCase
|
||||
{
|
||||
public function testConfigurationKeyCanGetNodeConfiguration(): void
|
||||
{
|
||||
$node = Node::factory()->for(Location::factory())->create();
|
||||
$this->createNewDefaultApiKey($this->getApiUser(), [
|
||||
'node_id' => $node->id,
|
||||
'r_nodes' => AdminAcl::READ_CONFIGURATION,
|
||||
]);
|
||||
|
||||
$response = $this->getJson('/api/application/nodes/' . $node->id . '/configuration');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('uuid', $node->uuid);
|
||||
$response->assertJsonPath('token_id', $node->daemon_token_id);
|
||||
$this->assertSame(decrypt($node->daemon_token), $response->json('token'));
|
||||
}
|
||||
|
||||
public function testConfigurationKeyCannotGetOtherNodeConfiguration(): void
|
||||
{
|
||||
$node = Node::factory()->for(Location::factory())->create();
|
||||
$otherNode = Node::factory()->for(Location::factory())->create();
|
||||
$this->createNewDefaultApiKey($this->getApiUser(), [
|
||||
'node_id' => $node->id,
|
||||
'r_nodes' => AdminAcl::READ_CONFIGURATION,
|
||||
]);
|
||||
|
||||
$this->assertAccessDeniedJson($this->getJson('/api/application/nodes/' . $otherNode->id . '/configuration'));
|
||||
}
|
||||
|
||||
public function testWriteKeyCanGetNodeConfiguration(): void
|
||||
{
|
||||
$this->createNewDefaultApiKey($this->getApiUser(), ['r_nodes' => AdminAcl::WRITE]);
|
||||
|
||||
$node = Node::factory()->for(Location::factory())->create();
|
||||
|
||||
$this->getJson('/api/application/nodes/' . $node->id . '/configuration')->assertOk();
|
||||
}
|
||||
|
||||
public function testReadOnlyNodeKeyCannotGetNodeConfiguration(): void
|
||||
{
|
||||
$this->createNewDefaultApiKey($this->getApiUser(), ['r_nodes' => AdminAcl::READ]);
|
||||
|
||||
$node = Node::factory()->for(Location::factory())->create();
|
||||
|
||||
$this->assertAccessDeniedJson($this->getJson('/api/application/nodes/' . $node->id . '/configuration'));
|
||||
}
|
||||
|
||||
public function testConfigurationKeyCannotUpdateNode(): void
|
||||
{
|
||||
$node = Node::factory()->for(Location::factory())->create();
|
||||
$this->createNewDefaultApiKey($this->getApiUser(), [
|
||||
'node_id' => $node->id,
|
||||
'r_nodes' => AdminAcl::READ_CONFIGURATION,
|
||||
]);
|
||||
|
||||
$this->assertAccessDeniedJson($this->patchJson(route('api.application.nodes.update', ['node' => $node])));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Pterodactyl\Tests\Integration\Http\Controllers\Admin;
|
||||
|
||||
use Pterodactyl\Models\Node;
|
||||
use Pterodactyl\Models\User;
|
||||
use Pterodactyl\Models\ApiKey;
|
||||
use Pterodactyl\Models\Location;
|
||||
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
||||
use Pterodactyl\Tests\Integration\Http\HttpTestCase;
|
||||
|
||||
class NodeAutoDeployControllerTest extends HttpTestCase
|
||||
{
|
||||
public function testGeneratedTokenIsScopedToNodeConfiguration(): void
|
||||
{
|
||||
$node = Node::factory()->for(Location::factory())->create();
|
||||
|
||||
$response = $this->actingAs(User::factory()->admin()->create())
|
||||
->postJson(route('admin.nodes.view.configuration.token', ['node' => $node]));
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('node', $node->id);
|
||||
|
||||
$key = ApiKey::query()
|
||||
->where('identifier', substr($response->json('token'), 0, ApiKey::IDENTIFIER_LENGTH))
|
||||
->firstOrFail();
|
||||
|
||||
$this->assertSame($node->id, $key->node_id);
|
||||
$this->assertSame(AdminAcl::READ_CONFIGURATION, $key->r_nodes);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user