Fix logic to update a daemon's configuration

This commit is contained in:
Dane Everitt 2020-04-11 16:33:15 -07:00
parent 60f6e86b8b
commit 1327bbbbe5
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
2 changed files with 34 additions and 51 deletions

View file

@ -2,6 +2,7 @@
namespace Pterodactyl\Repositories\Wings; namespace Pterodactyl\Repositories\Wings;
use Pterodactyl\Models\Node;
use GuzzleHttp\Exception\TransferException; use GuzzleHttp\Exception\TransferException;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
@ -25,17 +26,19 @@ class DaemonConfigurationRepository extends DaemonRepository
} }
/** /**
* Updates the configuration information for a daemon. * Updates the configuration information for a daemon. Updates the information for
* this instance using a passed-in model. This allows us to change plenty of information
* in the model, and still use the old, pre-update model to actually make the HTTP request.
* *
* @param array $attributes * @param \Pterodactyl\Models\Node $node
* @return \Psr\Http\Message\ResponseInterface * @return \Psr\Http\Message\ResponseInterface
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/ */
public function update(array $attributes = []) public function update(?Node $node)
{ {
try { try {
return $this->getHttpClient()->post( return $this->getHttpClient()->post(
'/api/update', array_merge($this->node->getConfiguration(), $attributes) '/api/update', ['json' => $node->getConfiguration()]
); );
} catch (TransferException $exception) { } catch (TransferException $exception) {
throw new DaemonConnectionException($exception); throw new DaemonConnectionException($exception);

View file

@ -5,11 +5,10 @@ namespace Pterodactyl\Services\Nodes;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Pterodactyl\Models\Node; use Pterodactyl\Models\Node;
use GuzzleHttp\Exception\ConnectException; use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Repositories\Eloquent\NodeRepository;
use Pterodactyl\Repositories\Daemon\ConfigurationRepository; use Pterodactyl\Repositories\Daemon\ConfigurationRepository;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Repositories\Wings\DaemonConfigurationRepository; use Pterodactyl\Repositories\Wings\DaemonConfigurationRepository;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Exceptions\Service\Node\ConfigurationNotPersistedException; use Pterodactyl\Exceptions\Service\Node\ConfigurationNotPersistedException;
@ -21,11 +20,6 @@ class NodeUpdateService
*/ */
private $connection; private $connection;
/**
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
*/
private $repository;
/** /**
* @var \Pterodactyl\Repositories\Wings\DaemonConfigurationRepository * @var \Pterodactyl\Repositories\Wings\DaemonConfigurationRepository
*/ */
@ -36,24 +30,29 @@ class NodeUpdateService
*/ */
private $encrypter; private $encrypter;
/**
* @var \Pterodactyl\Repositories\Eloquent\NodeRepository
*/
private $repository;
/** /**
* UpdateService constructor. * UpdateService constructor.
* *
* @param \Illuminate\Database\ConnectionInterface $connection * @param \Illuminate\Database\ConnectionInterface $connection
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
* @param \Pterodactyl\Repositories\Wings\DaemonConfigurationRepository $configurationRepository * @param \Pterodactyl\Repositories\Wings\DaemonConfigurationRepository $configurationRepository
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $repository * @param \Pterodactyl\Repositories\Eloquent\NodeRepository $repository
*/ */
public function __construct( public function __construct(
ConnectionInterface $connection, ConnectionInterface $connection,
Encrypter $encrypter, Encrypter $encrypter,
DaemonConfigurationRepository $configurationRepository, DaemonConfigurationRepository $configurationRepository,
NodeRepositoryInterface $repository NodeRepository $repository
) { ) {
$this->connection = $connection; $this->connection = $connection;
$this->repository = $repository;
$this->configurationRepository = $configurationRepository; $this->configurationRepository = $configurationRepository;
$this->encrypter = $encrypter; $this->encrypter = $encrypter;
$this->repository = $repository;
} }
/** /**
@ -64,55 +63,36 @@ class NodeUpdateService
* @param bool $resetToken * @param bool $resetToken
* *
* @return \Pterodactyl\Models\Node * @return \Pterodactyl\Models\Node
* * @throws \Throwable
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
* @throws \Pterodactyl\Exceptions\Service\Node\ConfigurationNotPersistedException
*/ */
public function handle(Node $node, array $data, bool $resetToken = false) public function handle(Node $node, array $data, bool $resetToken = false)
{ {
if ($resetToken) { if ($resetToken) {
$data['daemon_token'] = Str::random(Node::DAEMON_TOKEN_LENGTH); $data['daemon_token'] = $this->encrypter->encrypt(Str::random(Node::DAEMON_TOKEN_LENGTH));
$data['daemon_token_id'] = $this->encrypter->encrypt( $data['daemon_token_id'] = Str::random(Node::DAEMON_TOKEN_ID_LENGTH);
Str::random(Node::DAEMON_TOKEN_ID_LENGTH)
);
} }
$this->connection->beginTransaction(); [$updated, $exception] = $this->connection->transaction(function () use ($data, $node) {
/** @var \Pterodactyl\Models\Node $updated */
$updated = $this->repository->withFreshModel()->update($node->id, $data, true, true);
/** @var \Pterodactyl\Models\Node $updatedModel */ try {
$updatedModel = $this->repository->update($node->id, $data); $this->configurationRepository->setNode($node)->update($updated);
} catch (DaemonConnectionException $exception) {
if (! is_null($exception->getPrevious()) && $exception->getPrevious() instanceof ConnectException) {
return [$updated, true];
}
try { throw $exception;
if ($resetToken) {
// We need to clone the new model and set it's authentication token to be the
// old one so we can connect. Then we will pass the new token through as an
// override on the call.
$cloned = $updatedModel->replicate(['daemon_token']);
$cloned->setAttribute('daemon_token', $node->getAttribute('daemon_token'));
$this->configurationRepository->setNode($cloned)->update([
'daemon_token_id' => $updatedModel->daemon_token_id,
'daemon_token' => $updatedModel->getDecryptedKey(),
]);
} else {
$this->configurationRepository->setNode($updatedModel)->update();
} }
$this->connection->commit(); return [$updated, false];
} catch (RequestException $exception) { });
// Failed to connect to the Daemon. Let's go ahead and save the configuration
// and let the user know they'll need to manually update.
if ($exception instanceof ConnectException) {
$this->connection->commit();
throw new ConfigurationNotPersistedException(trans('exceptions.node.daemon_off_config_updated')); if ($exception) {
} throw new ConfigurationNotPersistedException(trans('exceptions.node.daemon_off_config_updated'));
throw new DaemonConnectionException($exception);
} }
return $updatedModel; return $updated;
} }
} }