Add throwIf to daemonRepository (#1301)

This commit is contained in:
MartinOscar 2025-05-01 15:49:35 +02:00 committed by GitHub
parent 3effd98013
commit 435c615ff1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 34 additions and 9 deletions

View File

@ -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();
}
}
}

View File

@ -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
{

View File

@ -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 !');
}

View File

@ -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;
}
}