Merge branch 'master' into develop

This commit is contained in:
Dane Everitt 2019-08-04 13:49:26 -07:00
commit 81143e231a
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
41 changed files with 303 additions and 190 deletions

View file

@ -2,8 +2,9 @@
namespace Pterodactyl\Services\Databases;
use Webmozart\Assert\Assert;
use Exception;
use Pterodactyl\Models\Database;
use Illuminate\Support\Facades\Log;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Extensions\DynamicDatabaseConnection;
@ -55,35 +56,39 @@ class DatabasePasswordService
* Updates a password for a given database.
*
* @param \Pterodactyl\Models\Database|int $database
* @param string $password
* @return bool
* @return string
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Throwable
*/
public function handle($database, string $password): bool
public function handle(Database $database): string
{
if (! $database instanceof Database) {
Assert::integerish($database);
$password = str_random(24);
// Given a random string of characters, randomly loop through the characters and replace some
// with special characters to avoid issues with MySQL password requirements on some servers.
try {
for ($i = 0; $i < random_int(2, 6); $i++) {
$character = ['!', '@', '=', '.', '+', '^'][random_int(0, 5)];
$database = $this->repository->find($database);
$password = substr_replace($password, $character, random_int(0, 23), 1);
}
} catch (Exception $exception) {
// Just log the error and hope for the best at this point.
Log::error($exception);
}
$this->dynamic->set('dynamic', $database->database_host_id);
$this->connection->beginTransaction();
$this->connection->transaction(function () use ($database, $password) {
$this->dynamic->set('dynamic', $database->database_host_id);
$updated = $this->repository->withoutFreshModel()->update($database->id, [
'password' => $this->encrypter->encrypt($password),
]);
$this->repository->withoutFreshModel()->update($database->id, [
'password' => $this->encrypter->encrypt($password),
]);
$this->repository->dropUser($database->username, $database->remote);
$this->repository->createUser($database->username, $database->remote, $password);
$this->repository->assignUserToDatabase($database->database, $database->username, $database->remote);
$this->repository->flush();
$this->repository->dropUser($database->username, $database->remote);
$this->repository->createUser($database->username, $database->remote, $password);
$this->repository->assignUserToDatabase($database->database, $database->username, $database->remote);
$this->repository->flush();
});
unset($password);
$this->connection->commit();
return $updated;
return $password;
}
}

View file

@ -65,28 +65,26 @@ class HostCreationService
* @param array $data
* @return \Pterodactyl\Models\DatabaseHost
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Throwable
*/
public function handle(array $data): DatabaseHost
{
$this->connection->beginTransaction();
return $this->connection->transaction(function () use ($data) {
$host = $this->repository->create([
'password' => $this->encrypter->encrypt(array_get($data, 'password')),
'name' => array_get($data, 'name'),
'host' => array_get($data, 'host'),
'port' => array_get($data, 'port'),
'username' => array_get($data, 'username'),
'max_databases' => null,
'node_id' => array_get($data, 'node_id'),
]);
$host = $this->repository->create([
'password' => $this->encrypter->encrypt(array_get($data, 'password')),
'name' => array_get($data, 'name'),
'host' => array_get($data, 'host'),
'port' => array_get($data, 'port'),
'username' => array_get($data, 'username'),
'max_databases' => null,
'node_id' => array_get($data, 'node_id'),
]);
// Confirm access using the provided credentials before saving data.
$this->dynamic->set('dynamic', $host);
$this->databaseManager->connection('dynamic')->select('SELECT 1 FROM dual');
// Confirm access using the provided credentials before saving data.
$this->dynamic->set('dynamic', $host);
$this->databaseManager->connection('dynamic')->select('SELECT 1 FROM dual');
$this->connection->commit();
return $host;
return $host;
});
}
}

View file

@ -71,10 +71,9 @@ class HostUpdateService
*
* @param int $hostId
* @param array $data
* @return mixed
* @return \Pterodactyl\Models\DatabaseHost
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Throwable
*/
public function handle(int $hostId, array $data): DatabaseHost
{
@ -84,13 +83,12 @@ class HostUpdateService
unset($data['password']);
}
$this->connection->beginTransaction();
$host = $this->repository->update($hostId, $data);
return $this->connection->transaction(function () use ($data, $hostId) {
$host = $this->repository->update($hostId, $data);
$this->dynamic->set('dynamic', $host);
$this->databaseManager->connection('dynamic')->select('SELECT 1 FROM dual');
$this->dynamic->set('dynamic', $host);
$this->databaseManager->connection('dynamic')->select('SELECT 1 FROM dual');
$this->connection->commit();
return $host;
return $host;
});
}
}

View file

@ -85,6 +85,7 @@ class BuildModificationService
}
$server = $this->repository->withFreshModel()->update($server->id, [
'oom_disabled' => array_get($data, 'oom_disabled'),
'memory' => array_get($data, 'memory'),
'swap' => array_get($data, 'swap'),
'io' => array_get($data, 'io'),
@ -97,6 +98,7 @@ class BuildModificationService
$allocations = $this->allocationRepository->findWhere([['server_id', '=', $server->id]]);
$build['oom_disabled'] = $server->oom_disabled;
$build['memory'] = (int) $server->memory;
$build['swap'] = (int) $server->swap;
$build['io'] = (int) $server->io;

View file

@ -70,6 +70,7 @@ class ServerConfigurationStructureService
return $item->pluck('port');
})->toArray(),
'env' => $this->environment->handle($server),
'oom_disabled' => $server->oom_disabled,
'memory' => (int) $server->memory,
'swap' => (int) $server->swap,
'io' => (int) $server->io,

View file

@ -227,7 +227,7 @@ class ServerCreationService
'disk' => array_get($data, 'disk'),
'io' => array_get($data, 'io'),
'cpu' => array_get($data, 'cpu'),
'oom_disabled' => false,
'oom_disabled' => array_get($data, 'oom_disabled', true),
'allocation_id' => array_get($data, 'allocation_id'),
'nest_id' => array_get($data, 'nest_id'),
'egg_id' => array_get($data, 'egg_id'),

View file

@ -71,7 +71,7 @@ class TwoFactorSetupService
'totp_secret' => $this->encrypter->encrypt($secret),
]);
$company = $this->config->get('app.name');
$company = preg_replace('/\s/', '', $this->config->get('app.name'));
return sprintf(
'otpauth://totp/%1$s:%2$s?secret=%3$s&issuer=%1$s',