2012-01-10 07:28:04 +01:00
|
|
|
<?php
|
|
|
|
|
2017-01-04 23:16:12 +01:00
|
|
|
namespace SilverStripe\EnvironmentCheck;
|
|
|
|
|
2017-03-21 11:57:07 +01:00
|
|
|
use Psr\Log\LoggerInterface;
|
2019-03-28 05:07:57 +01:00
|
|
|
use Psr\Log\LogLevel;
|
2017-01-04 23:16:12 +01:00
|
|
|
use SilverStripe\Control\Director;
|
|
|
|
use SilverStripe\Control\Email\Email;
|
|
|
|
use SilverStripe\Control\HTTPResponse;
|
|
|
|
use SilverStripe\Control\HTTPResponse_Exception;
|
|
|
|
use SilverStripe\Control\RequestHandler;
|
2017-12-13 08:51:37 +01:00
|
|
|
use SilverStripe\Core\Environment;
|
2017-01-04 23:16:12 +01:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\Permission;
|
2017-12-13 08:51:37 +01:00
|
|
|
use SilverStripe\Security\Security;
|
2017-01-04 23:16:12 +01:00
|
|
|
|
2012-01-10 07:28:04 +01:00
|
|
|
/**
|
|
|
|
* Provides an interface for checking the given EnvironmentCheckSuite.
|
2017-01-04 23:16:12 +01:00
|
|
|
*
|
|
|
|
* @package environmentcheck
|
2012-01-10 07:28:04 +01:00
|
|
|
*/
|
2015-11-21 07:18:35 +01:00
|
|
|
class EnvironmentChecker extends RequestHandler
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-03-21 11:57:07 +01:00
|
|
|
private static $url_handlers = [
|
2015-11-21 07:18:35 +01:00
|
|
|
'' => 'index',
|
2017-03-21 11:57:07 +01:00
|
|
|
];
|
2015-11-21 07:18:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $checkSuiteName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $title;
|
|
|
|
|
2022-07-01 05:13:27 +02:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $includeDetails = false;
|
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $errorCode = 500;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var null|string
|
|
|
|
*/
|
|
|
|
private static $to_email_address = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var null|string
|
|
|
|
*/
|
|
|
|
private static $from_email_address = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $email_results = false;
|
|
|
|
|
|
|
|
/**
|
2017-01-04 23:16:12 +01:00
|
|
|
* @var bool Log results via {@link \Psr\Log\LoggerInterface}
|
2015-11-21 07:18:35 +01:00
|
|
|
*/
|
|
|
|
private static $log_results_warning = false;
|
|
|
|
|
|
|
|
/**
|
2017-01-04 23:16:12 +01:00
|
|
|
* @var string Maps to {@link \Psr\Log\LogLevel} levels. Defaults to LogLevel::WARNING
|
2015-11-21 07:18:35 +01:00
|
|
|
*/
|
2017-01-04 23:16:12 +01:00
|
|
|
private static $log_results_warning_level = LogLevel::WARNING;
|
2015-11-21 07:18:35 +01:00
|
|
|
|
|
|
|
/**
|
2017-01-04 23:16:12 +01:00
|
|
|
* @var bool Log results via a {@link \Psr\Log\LoggerInterface}
|
2015-11-21 07:18:35 +01:00
|
|
|
*/
|
|
|
|
private static $log_results_error = false;
|
|
|
|
|
|
|
|
/**
|
2017-01-04 23:16:12 +01:00
|
|
|
* @var int Maps to {@link \Psr\Log\LogLevel} levels. Defaults to LogLevel::ALERT
|
2015-11-21 07:18:35 +01:00
|
|
|
*/
|
2017-01-04 23:16:12 +01:00
|
|
|
private static $log_results_error_level = LogLevel::ALERT;
|
2015-11-21 07:18:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $checkSuiteName
|
|
|
|
* @param string $title
|
|
|
|
*/
|
|
|
|
public function __construct($checkSuiteName, $title)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
2017-01-04 23:16:12 +01:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
$this->checkSuiteName = $checkSuiteName;
|
|
|
|
$this->title = $title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $permission
|
|
|
|
*
|
2017-01-04 23:16:12 +01:00
|
|
|
* @throws HTTPResponse_Exception
|
2015-11-21 07:18:35 +01:00
|
|
|
*/
|
|
|
|
public function init($permission = 'ADMIN')
|
|
|
|
{
|
|
|
|
// if the environment supports it, provide a basic auth challenge and see if it matches configured credentials
|
2017-12-13 08:51:37 +01:00
|
|
|
if (Environment::getEnv('ENVCHECK_BASICAUTH_USERNAME')
|
|
|
|
&& Environment::getEnv('ENVCHECK_BASICAUTH_PASSWORD')
|
|
|
|
) {
|
2019-03-28 05:07:57 +01:00
|
|
|
// Check that details are both provided, and match
|
|
|
|
if (empty($_SERVER['PHP_AUTH_USER']) || empty($_SERVER['PHP_AUTH_PW'])
|
|
|
|
|| $_SERVER['PHP_AUTH_USER'] != Environment::getEnv('ENVCHECK_BASICAUTH_USERNAME')
|
|
|
|
|| $_SERVER['PHP_AUTH_PW'] != Environment::getEnv('ENVCHECK_BASICAUTH_PASSWORD')
|
|
|
|
) {
|
|
|
|
// Fail check with basic auth challenge
|
2017-01-04 23:16:12 +01:00
|
|
|
$response = new HTTPResponse(null, 401);
|
2015-11-21 07:18:35 +01:00
|
|
|
$response->addHeader('WWW-Authenticate', "Basic realm=\"Environment check\"");
|
2019-03-28 05:07:57 +01:00
|
|
|
throw new HTTPResponse_Exception($response);
|
2015-11-21 07:18:35 +01:00
|
|
|
}
|
2019-03-28 05:07:57 +01:00
|
|
|
} elseif (!$this->canAccess(null, $permission)) {
|
|
|
|
// Fail check with silverstripe login challenge
|
2019-03-28 22:03:02 +01:00
|
|
|
$result = Security::permissionFailure(
|
|
|
|
$this,
|
|
|
|
"You must have the {$permission} permission to access this check"
|
|
|
|
);
|
2019-03-28 05:07:57 +01:00
|
|
|
throw new HTTPResponse_Exception($result);
|
2015-11-21 07:18:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-28 05:07:57 +01:00
|
|
|
* Determine if the current member can access the environment checker
|
2015-11-21 07:18:35 +01:00
|
|
|
*
|
2019-03-28 05:07:57 +01:00
|
|
|
* @param null|int|Member $member
|
|
|
|
* @param string $permission
|
2015-11-21 07:18:35 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-01-04 23:16:12 +01:00
|
|
|
public function canAccess($member = null, $permission = 'ADMIN')
|
2015-11-21 07:18:35 +01:00
|
|
|
{
|
|
|
|
if (!$member) {
|
2017-12-13 08:51:37 +01:00
|
|
|
$member = Security::getCurrentUser();
|
2015-11-21 07:18:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// We allow access to this controller regardless of live-status or ADMIN permission only
|
|
|
|
// if on CLI. Access to this controller is always allowed in "dev-mode", or of the user is ADMIN.
|
2017-01-04 23:16:12 +01:00
|
|
|
if (Director::isDev()
|
2015-11-21 07:18:35 +01:00
|
|
|
|| Director::is_cli()
|
|
|
|
|| empty($permission)
|
|
|
|
|| Permission::checkMember($member, $permission)
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extended access checks.
|
|
|
|
// "Veto" style, return NULL to abstain vote.
|
|
|
|
$canExtended = null;
|
|
|
|
$results = $this->extend('canAccess', $member);
|
|
|
|
if ($results && is_array($results)) {
|
|
|
|
if (!min($results)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-04 23:16:12 +01:00
|
|
|
return true;
|
2015-11-21 07:18:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-04 23:16:12 +01:00
|
|
|
* @return HTTPResponse
|
2015-11-21 07:18:35 +01:00
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2017-01-04 23:16:12 +01:00
|
|
|
$response = new HTTPResponse;
|
2015-11-21 07:18:35 +01:00
|
|
|
$result = EnvironmentCheckSuite::inst($this->checkSuiteName)->run();
|
|
|
|
|
|
|
|
if (!$result->ShouldPass()) {
|
|
|
|
$response->setStatusCode($this->errorCode);
|
|
|
|
}
|
|
|
|
|
2022-07-01 05:13:27 +02:00
|
|
|
$data = [
|
2019-03-28 05:07:57 +01:00
|
|
|
'URL' => Director::absoluteBaseURL(),
|
|
|
|
'Title' => $this->title,
|
|
|
|
'Name' => $this->checkSuiteName,
|
2022-07-01 05:13:27 +02:00
|
|
|
'ErrorCode' => $this->errorCode
|
|
|
|
];
|
|
|
|
|
|
|
|
$emailContent = $result->customise(array_merge($data, [
|
|
|
|
'IncludeDetails' => true
|
|
|
|
]))->renderWith(__CLASS__);
|
|
|
|
|
|
|
|
if (!$this->includeDetails) {
|
|
|
|
$webContent = $result->customise(array_merge($data, [
|
|
|
|
'IncludeDetails' => false
|
|
|
|
]))->renderWith(__CLASS__);
|
|
|
|
} else {
|
|
|
|
$webContent = $emailContent;
|
|
|
|
}
|
2015-11-21 07:18:35 +01:00
|
|
|
|
2017-08-25 05:19:45 +02:00
|
|
|
if ($this->config()->get('email_results') && !$result->ShouldPass()) {
|
2017-01-04 23:16:12 +01:00
|
|
|
$email = new Email(
|
2017-08-25 05:19:45 +02:00
|
|
|
$this->config()->get('from_email_address'),
|
|
|
|
$this->config()->get('to_email_address'),
|
2017-01-04 23:16:12 +01:00
|
|
|
$this->title,
|
2022-07-01 05:13:27 +02:00
|
|
|
$emailContent
|
2017-01-04 23:16:12 +01:00
|
|
|
);
|
2015-11-21 07:18:35 +01:00
|
|
|
$email->send();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optionally log errors and warnings individually
|
|
|
|
foreach ($result->Details() as $detail) {
|
2017-08-25 05:19:45 +02:00
|
|
|
if ($this->config()->get('log_results_warning') && $detail->StatusCode == EnvironmentCheck::WARNING) {
|
2015-11-21 07:18:35 +01:00
|
|
|
$this->log(
|
|
|
|
sprintf('EnvironmentChecker warning at "%s" check. Message: %s', $detail->Check, $detail->Message),
|
2017-08-25 05:19:45 +02:00
|
|
|
$this->config()->get('log_results_warning_level')
|
2015-11-21 07:18:35 +01:00
|
|
|
);
|
2017-08-25 05:19:45 +02:00
|
|
|
} elseif ($this->config()->get('log_results_error') && $detail->StatusCode == EnvironmentCheck::ERROR) {
|
2015-11-21 07:18:35 +01:00
|
|
|
$this->log(
|
|
|
|
sprintf('EnvironmentChecker error at "%s" check. Message: %s', $detail->Check, $detail->Message),
|
2017-08-25 05:19:45 +02:00
|
|
|
$this->config()->get('log_results_error_level')
|
2015-11-21 07:18:35 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// output the result as JSON if requested
|
2017-01-04 23:16:12 +01:00
|
|
|
if ($this->getRequest()->getExtension() == 'json'
|
2022-04-13 00:29:17 +02:00
|
|
|
|| strpos($this->getRequest()->getHeader('Accept') ?? '', 'application/json') !== false
|
2015-11-21 07:18:35 +01:00
|
|
|
) {
|
|
|
|
$response->setBody($result->toJSON());
|
|
|
|
$response->addHeader('Content-Type', 'application/json');
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2022-07-01 05:13:27 +02:00
|
|
|
$response->setBody($webContent);
|
2017-01-04 23:16:12 +01:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-04 23:16:12 +01:00
|
|
|
* Sends a log entry to the configured PSR-3 LoggerInterface
|
|
|
|
*
|
2015-11-21 07:18:35 +01:00
|
|
|
* @param string $message
|
2019-03-28 05:07:57 +01:00
|
|
|
* @param int $level
|
2015-11-21 07:18:35 +01:00
|
|
|
*/
|
|
|
|
public function log($message, $level)
|
|
|
|
{
|
2017-03-21 11:57:07 +01:00
|
|
|
Injector::inst()->get(LoggerInterface::class)->log($level, $message);
|
2015-11-21 07:18:35 +01:00
|
|
|
}
|
|
|
|
|
2022-07-01 05:13:27 +02:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
/**
|
|
|
|
* Set the HTTP status code that should be returned when there's an error.
|
|
|
|
*
|
|
|
|
* @param int $errorCode
|
2022-07-01 05:13:27 +02:00
|
|
|
*
|
|
|
|
* @return $this
|
2015-11-21 07:18:35 +01:00
|
|
|
*/
|
|
|
|
public function setErrorCode($errorCode)
|
|
|
|
{
|
|
|
|
$this->errorCode = $errorCode;
|
2022-07-01 05:13:27 +02:00
|
|
|
|
|
|
|
return $this;
|
2015-11-21 07:18:35 +01:00
|
|
|
}
|
|
|
|
|
2022-07-01 05:13:27 +02:00
|
|
|
/**
|
|
|
|
* Set whether to include the full breakdown of services
|
|
|
|
*
|
|
|
|
* @param bool $includeDetails
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setIncludeDetails($includeDetails)
|
|
|
|
{
|
|
|
|
$this->includeDetails = $includeDetails;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2014-06-19 07:31:15 +02:00
|
|
|
}
|