Fixes bug causing MySQL user accounts to be corrupted when resetting a password via the panel.

closes #352
This commit is contained in:
Dane Everitt 2017-03-17 17:17:36 -04:00
parent d5352e2b1e
commit 202dd52e2b
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
5 changed files with 23 additions and 11 deletions

View File

@ -22,6 +22,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
* `[pre.4]` — Fixes server listing on frontend not displaying a page selector when more than 10 servers exist. * `[pre.4]` — Fixes server listing on frontend not displaying a page selector when more than 10 servers exist.
* `[pre.4]` — Fixes non-admin users being unable to create personal API keys. * `[pre.4]` — Fixes non-admin users being unable to create personal API keys.
* Fixes bug where daemon was unable to register that certain games had fully booted and were ready to play on. * Fixes bug where daemon was unable to register that certain games had fully booted and were ready to play on.
* Fixes bug causing MySQL user accounts to be corrupted when resetting a password via the panel.
### Added ### Added
* Ability to assign multiple allocations at once when creating a new server. * Ability to assign multiple allocations at once when creating a new server.

View File

@ -239,15 +239,14 @@ class ServerController extends Controller
{ {
$server = Models\Server::byUuid($uuid); $server = Models\Server::byUuid($uuid);
$this->authorize('view-databases', $server); $this->authorize('view-databases', $server);
$server->load('node', 'databases.host');
$server->js(); $server->js();
return view('server.settings.databases', [ return view('server.settings.databases', [
'server' => $server, 'server' => $server,
'node' => $server->node, 'node' => $server->node,
'databases' => Models\Database::select('databases.*', 'database_servers.host as a_host', 'database_servers.port as a_port') 'databases' => $server->databases,
->where('server_id', $server->id)
->join('database_servers', 'database_servers.id', '=', 'databases.db_server')
->get(),
]); ]);
} }

View File

@ -68,7 +68,7 @@ class Database extends Model
*/ */
public function host() public function host()
{ {
return $this->belongsTo(DatabaseHost::class); return $this->belongsTo(DatabaseHost::class, 'database_host_id');
} }
/** /**

View File

@ -48,10 +48,10 @@ class DatabaseRepository
*/ */
public function create($id, array $data) public function create($id, array $data)
{ {
$server = Server::findOrFail($server); $server = Server::findOrFail($id);
$validator = Validator::make($data, [ $validator = Validator::make($data, [
'host' => 'required|exists:database_servers,id', 'host' => 'required|exists:database_hosts,id',
'database' => 'required|regex:/^\w{1,100}$/', 'database' => 'required|regex:/^\w{1,100}$/',
'connection' => 'required|regex:/^[0-9%.]{1,15}$/', 'connection' => 'required|regex:/^[0-9%.]{1,15}$/',
]); ]);
@ -64,7 +64,7 @@ class DatabaseRepository
DB::beginTransaction(); DB::beginTransaction();
try { try {
$database = Models\Database::firstOrNew([ $database = Database::firstOrNew([
'server_id' => $server->id, 'server_id' => $server->id,
'database_host_id' => $data['host'], 'database_host_id' => $data['host'],
'database' => sprintf('s%d_%s', $server->id, $data['database']), 'database' => sprintf('s%d_%s', $server->id, $data['database']),
@ -131,10 +131,12 @@ class DatabaseRepository
* @param int $id * @param int $id
* @param string $password * @param string $password
* @return void * @return void
*
* @todo Fix logic behind resetting passwords.
*/ */
public function password($id, $password) public function password($id, $password)
{ {
$database = Models\Database::with('host')->findOrFail($id); $database = Database::with('host')->findOrFail($id);
DB::transaction(function () use ($database, $password) { DB::transaction(function () use ($database, $password) {
$database->password = Crypt::encrypt($password); $database->password = Crypt::encrypt($password);
@ -150,10 +152,20 @@ class DatabaseRepository
'collation' => 'utf8_unicode_ci', 'collation' => 'utf8_unicode_ci',
]); ]);
// We have to do the whole delete user, create user thing rather than
// SET PASSWORD ... because MariaDB and PHP statements ends up inserting
// a corrupted password. A way around this is strtoupper(sha1(sha1($password, true)))
// but no garuntees that will work correctly with every system.
DB::connection('dynamic')->statement(sprintf('DROP USER IF EXISTS `%s`@`%s`', $database->username, $database->remote));
DB::connection('dynamic')->statement(sprintf( DB::connection('dynamic')->statement(sprintf(
'SET PASSWORD FOR `%s`@`%s` = PASSWORD(\'%s\')', 'CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'',
$database->username, $database->remote, $password $database->username, $database->remote, $password
)); ));
DB::connection('dynamic')->statement(sprintf(
'GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX ON `%s`.* TO `%s`@`%s`',
$database->database, $database->username, $database->remote
));
DB::connection('dynamic')->statement('FLUSH PRIVILEGES');
$database->save(); $database->save();
}); });

View File

@ -56,7 +56,7 @@
<td class="middle">{{ $database->database }}</td> <td class="middle">{{ $database->database }}</td>
<td class="middle">{{ $database->username }}</td> <td class="middle">{{ $database->username }}</td>
<td class="middle"><code data-attr="set-password">{{ Crypt::decrypt($database->password) }}</code></td> <td class="middle"><code data-attr="set-password">{{ Crypt::decrypt($database->password) }}</code></td>
<td class="middle"><code>{{ $database->a_host }}:{{ $database->a_port }}</code></td> <td class="middle"><code>{{ $database->host->host }}:{{ $database->host->port }}</code></td>
@can('reset-db-password', $server) @can('reset-db-password', $server)
<td> <td>
<button class="btn btn-xs btn-primary pull-right" data-action="reset-password" data-id="{{ $database->id }}"><i class="fa fa-fw fa-refresh"></i> @lang('server.config.database.reset_password')</button> <button class="btn btn-xs btn-primary pull-right" data-action="reset-password" data-id="{{ $database->id }}"><i class="fa fa-fw fa-refresh"></i> @lang('server.config.database.reset_password')</button>