From 435c615ff1a2559aed8c79db90634018d2a6191e Mon Sep 17 00:00:00 2001 From: MartinOscar <40749467+rmartinoscar@users.noreply.github.com> Date: Thu, 1 May 2025 15:49:35 +0200 Subject: [PATCH] Add `throwIf` to `daemonRepository` (#1301) --- .../FileResource/Pages/EditFiles.php | 9 ++++++++ .../Api/Client/Servers/PowerController.php | 3 +++ .../Daemon/DaemonConfigurationRepository.php | 9 +------- app/Repositories/Daemon/DaemonRepository.php | 22 ++++++++++++++++++- 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/app/Filament/Server/Resources/FileResource/Pages/EditFiles.php b/app/Filament/Server/Resources/FileResource/Pages/EditFiles.php index 8795bee82..00091665d 100644 --- a/app/Filament/Server/Resources/FileResource/Pages/EditFiles.php +++ b/app/Filament/Server/Resources/FileResource/Pages/EditFiles.php @@ -179,6 +179,15 @@ class EditFiles extends Page ->info() ->closable() ->send(); + + try { + $this->getDaemonFileRepository()->getDirectory('/'); + } catch (ConnectionException) { + AlertBanner::make('node_connection_error') + ->title('Could not connect to the node!') + ->danger() + ->send(); + } } } diff --git a/app/Http/Controllers/Api/Client/Servers/PowerController.php b/app/Http/Controllers/Api/Client/Servers/PowerController.php index b1843d192..dcf78e0a7 100644 --- a/app/Http/Controllers/Api/Client/Servers/PowerController.php +++ b/app/Http/Controllers/Api/Client/Servers/PowerController.php @@ -9,6 +9,7 @@ use App\Repositories\Daemon\DaemonPowerRepository; use App\Http\Controllers\Api\Client\ClientApiController; use App\Http\Requests\Api\Client\Servers\SendPowerRequest; use Dedoc\Scramble\Attributes\Group; +use Illuminate\Http\Client\ConnectionException; #[Group('Server', weight: 2)] class PowerController extends ClientApiController @@ -25,6 +26,8 @@ class PowerController extends ClientApiController * Send power action * * Send a power action to a server. + * + * @throws ConnectionException */ public function index(SendPowerRequest $request, Server $server): Response { diff --git a/app/Repositories/Daemon/DaemonConfigurationRepository.php b/app/Repositories/Daemon/DaemonConfigurationRepository.php index 48d2b35c4..916e7d459 100644 --- a/app/Repositories/Daemon/DaemonConfigurationRepository.php +++ b/app/Repositories/Daemon/DaemonConfigurationRepository.php @@ -21,14 +21,7 @@ class DaemonConfigurationRepository extends DaemonRepository ->connectTimeout(3) ->get('/api/system') ->throwIf(function ($result) { - $header = $result->header('User-Agent'); - if ( - filled($header) && - preg_match('/^Pelican Wings\/v(?:\d+\.\d+\.\d+|develop) \(id:(\w*)\)$/', $header, $matches) && - array_get($matches, 1, '') !== $this->node->daemon_token_id - ) { - throw new ConnectionException($result->effectiveUri()->__toString() . ' does not match node token_id !'); - } + $this->enforceValidNodeToken($result); if (!$result->collect()->has(['architecture', 'cpu_count', 'kernel_version', 'os', 'version'])) { throw new ConnectionException($result->effectiveUri()->__toString() . ' is not Pelican Wings !'); } diff --git a/app/Repositories/Daemon/DaemonRepository.php b/app/Repositories/Daemon/DaemonRepository.php index ae7440a50..6b3b553d6 100644 --- a/app/Repositories/Daemon/DaemonRepository.php +++ b/app/Repositories/Daemon/DaemonRepository.php @@ -7,6 +7,8 @@ use Illuminate\Http\Client\PendingRequest; use Illuminate\Support\Facades\Http; use Webmozart\Assert\Assert; use App\Models\Server; +use Illuminate\Http\Client\ConnectionException; +use Illuminate\Http\Client\Response; abstract class DaemonRepository { @@ -47,6 +49,24 @@ abstract class DaemonRepository { Assert::isInstanceOf($this->node, Node::class); - return Http::daemon($this->node, $headers); + return Http::daemon($this->node, $headers)->throwIf(fn ($condition) => $this->enforceValidNodeToken($condition)); + } + + protected function enforceValidNodeToken(Response|bool $condition): bool + { + if (is_bool($condition)) { + return $condition; + } + + $header = $condition->header('User-Agent'); + if ( + empty($header) || + preg_match('/^Pelican Wings\/v(?:\d+\.\d+\.\d+|develop) \(id:(\w*)\)$/', $header, $matches) && + array_get($matches, 1, '') !== $this->node->daemon_token_id + ) { + throw new ConnectionException($condition->effectiveUri()->__toString() . ' does not match node token_id !'); + } + + return true; } }