mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Improved Exception handling (#9844)
* Update LibreNMS exception output Use built-in laravel exception rendering. DuskUnsafeExcpetion is triggered before Blade(view) is booted, so, try to boot it * Cleaner blade registration * Change 500 error page to generic LibreNMS error page. * Handle generic exceptions from laravel better. * Custome LdapMissingException * Cleanup, don't need to override convertExceptionToResponse anymore * Update unauthenticated code to current upstream style * UpgradableException interface * LdapMissingException extend AuthenticationException * Code climate cleanups * Fix code style
This commit is contained in:
@@ -25,8 +25,29 @@
|
||||
|
||||
namespace LibreNMS\Exceptions;
|
||||
|
||||
class DatabaseConnectException extends \Exception
|
||||
use Illuminate\Database\QueryException;
|
||||
use LibreNMS\Interfaces\Exceptions\UpgradeableException;
|
||||
|
||||
class DatabaseConnectException extends \Exception implements UpgradeableException
|
||||
{
|
||||
/**
|
||||
* Try to convert the given Exception to a DatabaseConnectException
|
||||
*
|
||||
* @param \Exception $exception
|
||||
* @return static|null
|
||||
*/
|
||||
public static function upgrade($exception)
|
||||
{
|
||||
// connect exception, convert to our standard connection exception
|
||||
return $exception instanceof QueryException && in_array($exception->getCode(), [1044, 1045, 2002]) ?
|
||||
new static(
|
||||
config('app.debug') ? $exception->getMessage() : $exception->getPrevious()->getMessage(),
|
||||
$exception->getCode(),
|
||||
$exception
|
||||
) :
|
||||
null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the exception into an HTTP or JSON response.
|
||||
*
|
||||
@@ -35,16 +56,14 @@ class DatabaseConnectException extends \Exception
|
||||
*/
|
||||
public function render(\Illuminate\Http\Request $request)
|
||||
{
|
||||
if ($request->wantsJson()) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Error connecting to database: ' . $this->getMessage(),
|
||||
]);
|
||||
} else {
|
||||
return response()->view('errors.generic', [
|
||||
'title' => 'Error connecting to database.',
|
||||
'content' => $this->getMessage(),
|
||||
]);
|
||||
}
|
||||
$title = __('Error connecting to database');
|
||||
|
||||
return $request->wantsJson() ? response()->json([
|
||||
'status' => 'error',
|
||||
'message' => "$title: " . $this->getMessage(),
|
||||
]) : response()->view('errors.generic', [
|
||||
'title' => $title,
|
||||
'content' => $this->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
64
LibreNMS/Exceptions/DuskUnsafeException.php
Normal file
64
LibreNMS/Exceptions/DuskUnsafeException.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
/**
|
||||
* DuskUnsafeException.php
|
||||
*
|
||||
* Dusk is installed and the application is in production
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @package LibreNMS
|
||||
* @link http://librenms.org
|
||||
* @copyright 2019 Tony Murray
|
||||
* @author Tony Murray <murraytony@gmail.com>
|
||||
*/
|
||||
|
||||
namespace LibreNMS\Exceptions;
|
||||
|
||||
use LibreNMS\Interfaces\Exceptions\UpgradeableException;
|
||||
|
||||
class DuskUnsafeException extends \Exception implements UpgradeableException
|
||||
{
|
||||
/**
|
||||
* Try to convert the given Exception to this exception
|
||||
*
|
||||
* @param \Exception $exception
|
||||
* @return static
|
||||
*/
|
||||
public static function upgrade($exception)
|
||||
{
|
||||
return $exception->getMessage() == 'It is unsafe to run Dusk in production.' ?
|
||||
new static($exception->getMessage(), $exception->getCode(), $exception) :
|
||||
null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the exception into an HTTP or JSON response.
|
||||
*
|
||||
* @param \Illuminate\Http\Request
|
||||
* @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
public function render(\Illuminate\Http\Request $request)
|
||||
{
|
||||
$title = __('It is unsafe to run Dusk in production');
|
||||
$message = __('Run ":command" to remove Dusk or if you are a developer set the appropriate APP_ENV', ['command' => './scripts/composer_wrapper.php install --no-dev']);
|
||||
|
||||
return $request->wantsJson() ? response()->json([
|
||||
'status' => 'error',
|
||||
'message' => "$title: $message",
|
||||
]) : response()->view('errors.generic', [
|
||||
'title' => $title,
|
||||
'content' => $message,
|
||||
]);
|
||||
}
|
||||
}
|
149
LibreNMS/Exceptions/FilePermissionsException.php
Normal file
149
LibreNMS/Exceptions/FilePermissionsException.php
Normal file
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
/**
|
||||
* FilePermissionsException.php
|
||||
*
|
||||
* Required folders/files aren't writable
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @package LibreNMS
|
||||
* @link http://librenms.org
|
||||
* @copyright 2019 Tony Murray
|
||||
* @author Tony Murray <murraytony@gmail.com>
|
||||
*/
|
||||
|
||||
namespace LibreNMS\Exceptions;
|
||||
|
||||
use LibreNMS\Config;
|
||||
use LibreNMS\Interfaces\Exceptions\UpgradeableException;
|
||||
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
|
||||
|
||||
class FilePermissionsException extends \Exception implements UpgradeableException
|
||||
{
|
||||
/**
|
||||
* Try to convert the given Exception to a FilePermissionsException
|
||||
*
|
||||
* @param \Exception $exception
|
||||
* @return static
|
||||
*/
|
||||
public static function upgrade($exception)
|
||||
{
|
||||
// cannot write to storage directory
|
||||
if ($exception instanceof \ErrorException &&
|
||||
starts_with($exception->getMessage(), 'file_put_contents(') &&
|
||||
str_contains($exception->getMessage(), '/storage/')) {
|
||||
return new static();
|
||||
}
|
||||
|
||||
// cannot write to bootstrap directory
|
||||
if ($exception instanceof \Exception && $exception->getMessage() == 'The bootstrap/cache directory must be present and writable.') {
|
||||
return new static ();
|
||||
}
|
||||
|
||||
// monolog cannot init log file
|
||||
if ($exception instanceof \UnexpectedValueException && str_contains($exception->getFile(), 'Monolog/Handler/StreamHandler.php')) {
|
||||
return new static();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the exception into an HTTP or JSON response.
|
||||
*
|
||||
* @param \Illuminate\Http\Request
|
||||
* @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
public function render(\Illuminate\Http\Request $request)
|
||||
{
|
||||
$log_file = config('app.log') ?: Config::get('log_file', base_path('logs/librenms.log'));
|
||||
$commands = $this->generateCommands($log_file);
|
||||
|
||||
// use pre-compiled template because we probably can't compile it.
|
||||
$template = file_get_contents(base_path('resources/views/errors/static/file_permissions.html'));
|
||||
$content = str_replace('!!!!CONTENT!!!!', '<p>' . implode('</p><p>', $commands) . '</p>', $template);
|
||||
$content = str_replace('!!!!LOG_FILE!!!!', $log_file, $content);
|
||||
|
||||
return SymfonyResponse::create($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Config\Repository $user
|
||||
* @param \Illuminate\Config\Repository $group
|
||||
* @param $log_file
|
||||
* @return array
|
||||
*/
|
||||
private function generateCommands($log_file): array
|
||||
{
|
||||
$user = config('librenms.user');
|
||||
$group = config('librenms.group');
|
||||
$install_dir = base_path();
|
||||
$commands = [];
|
||||
$dirs = [
|
||||
base_path('bootstrap/cache'),
|
||||
base_path('storage'),
|
||||
Config::get('log_dir', base_path('logs')),
|
||||
Config::get('rrd_dir', base_path('rrd')),
|
||||
];
|
||||
|
||||
// check if folders are missing
|
||||
$mkdirs = [
|
||||
base_path('bootstrap/cache'),
|
||||
base_path('storage/framework/sessions'),
|
||||
base_path('storage/framework/views'),
|
||||
base_path('storage/framework/cache'),
|
||||
Config::get('log_dir', base_path('logs')),
|
||||
Config::get('rrd_dir', base_path('rrd')),
|
||||
];
|
||||
|
||||
$mk_dirs = array_filter($mkdirs, function ($file) {
|
||||
return !file_exists($file);
|
||||
});
|
||||
|
||||
if (!empty($mk_dirs)) {
|
||||
$commands[] = 'sudo mkdir -p ' . implode(' ', $mk_dirs);
|
||||
}
|
||||
|
||||
// always print chwon/setfacl/chmod commands
|
||||
$commands[] = "sudo chown -R $user:$group $install_dir";
|
||||
$commands[] = 'sudo setfacl -d -m g::rwx ' . implode(' ', $dirs);
|
||||
$commands[] = 'sudo chmod -R ug=rwX ' . implode(' ', $dirs);
|
||||
|
||||
// check if webserver is in the librenms group
|
||||
$current_groups = explode(' ', trim(exec('groups')));
|
||||
if (!in_array($group, $current_groups)) {
|
||||
$current_user = trim(exec('whoami'));
|
||||
$commands[] = "usermod -a -G $group $current_user";
|
||||
}
|
||||
|
||||
// check for invalid log setting
|
||||
if (!is_file($log_file) || !is_writable($log_file)) {
|
||||
// override for proper error output
|
||||
$dirs = [$log_file];
|
||||
$install_dir = $log_file;
|
||||
$commands = [
|
||||
'<h3>Cannot write to log file: "' . $log_file . '"</h3>',
|
||||
'Make sure it exists and is writable, or change your LOG_DIR setting.'
|
||||
];
|
||||
}
|
||||
|
||||
// selinux:
|
||||
$commands[] = '<h4>If using SELinux you may also need:</h4>';
|
||||
foreach ($dirs as $dir) {
|
||||
$commands[] = "semanage fcontext -a -t httpd_sys_rw_content_t '$dir(/.*)?'";
|
||||
}
|
||||
$commands[] = "restorecon -RFv $install_dir";
|
||||
return $commands;
|
||||
}
|
||||
}
|
57
LibreNMS/Exceptions/LdapMissingException.php
Normal file
57
LibreNMS/Exceptions/LdapMissingException.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/**
|
||||
* LdapMissingException.php
|
||||
*
|
||||
* -Description-
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @package LibreNMS
|
||||
* @link http://librenms.org
|
||||
* @copyright 2019 Tony Murray
|
||||
* @author Tony Murray <murraytony@gmail.com>
|
||||
*/
|
||||
|
||||
namespace LibreNMS\Exceptions;
|
||||
|
||||
class LdapMissingException extends AuthenticationException
|
||||
{
|
||||
public function __construct(
|
||||
string $message = 'PHP does not support LDAP, please install or enable the PHP LDAP extension',
|
||||
int $code = 0,
|
||||
\Exception $previous = null
|
||||
) {
|
||||
parent::__construct($message, false, $code, $previous);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the exception into an HTTP or JSON response.
|
||||
*
|
||||
* @param \Illuminate\Http\Request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function render(\Illuminate\Http\Request $request)
|
||||
{
|
||||
$title = __('PHP LDAP support missing');
|
||||
$message = __($this->getMessage());
|
||||
|
||||
return $request->wantsJson() ? response()->json([
|
||||
'status' => 'error',
|
||||
'message' => "$title: $message",
|
||||
]) : response()->view('errors.generic', [
|
||||
'title' => $title,
|
||||
'content' => $message,
|
||||
]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user