Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/Filament/Admin/Resources/Servers/Pages/EditServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Enums\TablerIcon;
use App\Filament\Admin\Resources\Servers\ServerResource;
use App\Filament\Components\Actions\DeleteServerIcon;
use App\Filament\Components\Actions\ExportServerConfigAction;
use App\Filament\Components\Actions\PreviewStartupAction;
use App\Filament\Components\Forms\Fields\MonacoEditor;
use App\Filament\Components\Forms\Fields\StartupVariable;
Expand Down Expand Up @@ -1004,6 +1005,16 @@ protected function getDefaultTabs(): array
->hiddenLabel()
->hint(new HtmlString(trans('admin/server.transfer_help'))),
]),
Grid::make()
->columnSpan(3)
->schema([
Actions::make([
ExportServerConfigAction::make(),
])->fullWidth(),
ToggleButtons::make('export_help')
->hiddenLabel()
->hint('Export server configuration to a YAML file that can be imported on another panel'),
]),
Grid::make()
->columnSpan(3)
->schema([
Expand Down
2 changes: 2 additions & 0 deletions app/Filament/Admin/Resources/Servers/Pages/ListServers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Enums\TablerIcon;
use App\Filament\Admin\Resources\Servers\ServerResource;
use App\Filament\Components\Actions\ImportServerConfigAction;
use App\Filament\Server\Pages\Console;
use App\Models\Server;
use App\Traits\Filament\CanCustomizeHeaderActions;
Expand Down Expand Up @@ -111,6 +112,7 @@ public function table(Table $table): Table
protected function getDefaultHeaderActions(): array
{
return [
ImportServerConfigAction::make(),
CreateAction::make()
->iconButton()->iconSize(IconSize::ExtraLarge)
->icon(TablerIcon::FilePlus),
Expand Down
62 changes: 62 additions & 0 deletions app/Filament/Components/Actions/ExportServerConfigAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace App\Filament\Components\Actions;

use App\Models\Server;
use App\Services\Servers\Sharing\ServerConfigExporterService;
use Filament\Actions\Action;
use Filament\Forms\Components\Toggle;
use Filament\Support\Enums\Alignment;
use Filament\Support\Enums\IconSize;

class ExportServerConfigAction extends Action
{
public static function getDefaultName(): ?string
{
return 'export_config';
}

protected function setUp(): void
{
parent::setUp();

$this->label(trans('filament-actions::export.modal.actions.export.label'));

$this->iconSize(IconSize::ExtraLarge);

$this->tooltip(trans('admin/server.import_export.export_tooltip'));

$this->authorize(fn () => user()?->can('view server'));

$this->modalHeading(fn (Server $server) => trans('admin/server.import_export.export_heading', ['name' => $server->name]));

$this->modalDescription(trans('admin/server.import_export.export_description'));

$this->modalFooterActionsAlignment(Alignment::Center);

$this->schema([
Toggle::make('include_description')
->label(trans('admin/server.import_export.include_description'))
->helperText(trans('admin/server.import_export.include_description_help'))
->default(true),
Toggle::make('include_allocations')
->label(trans('admin/server.import_export.include_allocations'))
->helperText(trans('admin/server.import_export.include_allocations_help'))
->default(true),
Toggle::make('include_variable_values')
->label(trans('admin/server.import_export.include_variables'))
->helperText(trans('admin/server.import_export.include_variables_help'))
->default(true),
]);

$this->action(fn (ServerConfigExporterService $service, Server $server, array $data) => response()->streamDownload(
function () use ($service, $server, $data) {
echo $service->handle($server, $data);
},
'server-' . str($server->name)->kebab()->lower()->trim() . '.yaml',
[
'Content-Type' => 'application/x-yaml',
]
));
}
}
93 changes: 93 additions & 0 deletions app/Filament/Components/Actions/ImportServerConfigAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace App\Filament\Components\Actions;

use App\Exceptions\Service\InvalidFileUploadException;
use App\Services\Servers\Sharing\ServerConfigCreatorService;
use Filament\Actions\Action;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Select;
use Filament\Notifications\Notification;
use Filament\Support\Enums\IconSize;
use Illuminate\Http\UploadedFile;

class ImportServerConfigAction extends Action
{
public static function getDefaultName(): ?string
{
return 'import_config';
}

protected function setUp(): void
{
parent::setUp();

$this->label(trans('filament-actions::import.modal.actions.import.label'));

$this->iconButton();

$this->icon('tabler-file-import');

$this->iconSize(IconSize::ExtraLarge);

$this->tooltip(trans('admin/server.import_export.import_tooltip'));

$this->authorize(fn () => user()?->can('create server'));

$this->modalHeading(trans('admin/server.import_export.import_heading'));

$this->modalDescription(trans('admin/server.import_export.import_description'));

$this->schema([
FileUpload::make('file')
->label(trans('admin/server.import_export.config_file'))
->hint(trans('admin/server.import_export.config_file_hint'))
->acceptedFileTypes(['application/x-yaml', 'text/yaml', 'text/x-yaml', '.yaml', '.yml'])
->preserveFilenames()
->previewable(false)
->storeFiles(false)
->required()
->maxSize(1024), // 1MB max
Select::make('node_id')
->label(trans('admin/server.import_export.node_select'))
->hint(trans('admin/server.import_export.node_select_hint'))
->options(fn () => user()?->accessibleNodes()->pluck('name', 'id') ?? [])
->searchable()
->required()
->visible(fn () => (user()?->accessibleNodes()->count() ?? 0) > 1)
->default(fn () => user()?->accessibleNodes()->first()?->id),
]);

$this->action(function (ServerConfigCreatorService $createService, array $data): void {
/** @var UploadedFile $file */
$file = $data['file'];
$nodeId = $data['node_id'] ?? null;

try {
$server = $createService->fromFile($file, $nodeId);

Notification::make()
->title(trans('admin/server.notifications.import_created'))
->body(trans('admin/server.notifications.import_created_body', ['name' => $server->name]))
->success()
->send();

redirect()->route('filament.admin.resources.servers.edit', ['record' => $server]);
} catch (InvalidFileUploadException $exception) {
Notification::make()
->title(trans('admin/server.notifications.import_failed'))
->body($exception->getMessage())
->danger()
->send();
} catch (\Exception $exception) {
Notification::make()
->title(trans('admin/server.notifications.import_failed'))
->body(trans('admin/server.notifications.import_failed_body', ['error' => $exception->getMessage()]))
->danger()
->send();

report($exception);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace App\Http\Controllers\Api\Application\Servers;

use App\Exceptions\Service\InvalidFileUploadException;
use App\Http\Controllers\Api\Application\ApplicationApiController;
use App\Http\Requests\Api\Application\Servers\GetServerRequest;
use App\Models\Server;
use App\Services\Servers\Sharing\ServerConfigCreatorService;
use App\Services\Servers\Sharing\ServerConfigExporterService;
use App\Transformers\Api\Application\ServerTransformer;
use Dedoc\Scramble\Attributes\Group;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

#[Group('Server Config', weight: 1)]
class ServerConfigController extends ApplicationApiController
{
public function __construct(
private ServerConfigExporterService $exporterService,
private ServerConfigCreatorService $creatorService
) {
parent::__construct();
}

/**
* Export server configuration
*
* Export a server's configuration to YAML format. Returns the configuration as a
* downloadable YAML file containing settings, limits, allocations, and variable values.
*/
public function export(GetServerRequest $request, Server $server): Response
{
$options = [
'include_description' => $request->boolean('include_description', true),
'include_allocations' => $request->boolean('include_allocations', true),
'include_variable_values' => $request->boolean('include_variable_values', true),
];

$yaml = $this->exporterService->handle($server, $options);

$filename = 'server-config-' . str($server->name)->kebab()->lower()->trim() . '.yaml';

return response($yaml, 200, [
'Content-Type' => 'application/x-yaml',
'Content-Disposition' => 'attachment; filename="' . $filename . '"',
]);
}

/**
* Create server from configuration
*
* Create a new server from a YAML configuration file. The configuration must
* include a valid egg UUID that exists in the system. Optionally specify a
* node_id to create the server on a specific node.
*
* @throws InvalidFileUploadException
*/
public function create(Request $request): JsonResponse
{
$request->validate([
'file' => 'required|file|mimes:yaml,yml|max:1024',
'node_id' => 'sometimes|integer|exists:nodes,id',
]);
Comment on lines 60 to 65
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Missing authorization check for server creation.

The create method uses a generic Request instead of a custom FormRequest with proper authorization. Unlike the export method which uses GetServerRequest (with AdminAcl::READ), the create endpoint lacks permission verification.

Additionally, the node_id validation only checks existence but doesn't verify the API user has access to that node. While ServerConfigCreatorService uses user()?->accessibleNodes(), this relies on the web session's user() helper which may not work correctly for API token-based requests.

Suggested fix

Create a dedicated request class with proper authorization:

// app/Http/Requests/Api/Application/Servers/CreateServerFromConfigRequest.php
class CreateServerFromConfigRequest extends ApplicationApiRequest
{
    protected ?string $resource = Server::RESOURCE_NAME;
    protected int $permission = AdminAcl::WRITE;

    public function rules(): array
    {
        return [
            'file' => 'required|file|max:1024',
            'node_id' => 'sometimes|integer|exists:nodes,id',
        ];
    }
}

Then use it in the controller:

-    public function create(Request $request): JsonResponse
+    public function create(CreateServerFromConfigRequest $request): JsonResponse
     {
-        $request->validate([
-            'file' => 'required|file|mimes:yaml,yml|max:1024',
-            'node_id' => 'sometimes|integer|exists:nodes,id',
-        ]);
🤖 Prompt for AI Agents
In `@app/Http/Controllers/Api/Application/Servers/ServerConfigController.php`
around lines 60 - 65, The create method in ServerConfigController lacks
authorization and relies on Request and user()?->accessibleNodes(), which can
fail for API token auth; replace the generic Request with a new FormRequest
(e.g., CreateServerFromConfigRequest extending ApplicationApiRequest) that sets
protected ?string $resource = Server::RESOURCE_NAME and protected int
$permission = AdminAcl::WRITE and implements the same rules for 'file' and
'node_id'; update the controller create(Request $request) signature to
create(CreateServerFromConfigRequest $request) and ensure
ServerConfigCreatorService is fed the authorized user or their accessible node
IDs from the validated request (not via user() helper) so node access is
enforced for API token requests.


$file = $request->file('file');
$nodeId = $request->input('node_id');

$server = $this->creatorService->fromFile($file, $nodeId);

return $this->fractal->item($server)
->transformWith($this->getTransformer(ServerTransformer::class))
->respond(201);
}
}
Loading