mirror of
https://github.com/silverstripe/silverstripe-environmentcheck
synced 2024-10-22 17:05:40 +02:00
fix: public health/check should not include full details
This commit is contained in:
parent
d3d20e5539
commit
3e3907d14c
@ -43,8 +43,11 @@ class DevCheckController extends Controller
|
||||
$suite = $name;
|
||||
}
|
||||
|
||||
$checker = new EnvironmentChecker($suite, 'Environment status');
|
||||
$checker->setRequest($request);
|
||||
/** @var EnvironmentChecker */
|
||||
$checker = EnvironmentChecker::create($suite, 'Environment status')
|
||||
->setRequest($request)
|
||||
->setIncludeDetails(true);
|
||||
|
||||
$checker->init($this->config()->permission);
|
||||
|
||||
return $checker;
|
||||
|
@ -28,7 +28,6 @@ class DevHealthController extends Controller
|
||||
public function index()
|
||||
{
|
||||
// health check does not require permission to run
|
||||
|
||||
$checker = new EnvironmentChecker('health', 'Site health');
|
||||
$checker->setErrorCode(500);
|
||||
|
||||
|
@ -40,6 +40,11 @@ class EnvironmentChecker extends RequestHandler
|
||||
*/
|
||||
protected $title;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $includeDetails = false;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
@ -172,19 +177,31 @@ class EnvironmentChecker extends RequestHandler
|
||||
$response->setStatusCode($this->errorCode);
|
||||
}
|
||||
|
||||
$resultText = $result->customise([
|
||||
$data = [
|
||||
'URL' => Director::absoluteBaseURL(),
|
||||
'Title' => $this->title,
|
||||
'Name' => $this->checkSuiteName,
|
||||
'ErrorCode' => $this->errorCode,
|
||||
])->renderWith(__CLASS__);
|
||||
'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;
|
||||
}
|
||||
|
||||
if ($this->config()->get('email_results') && !$result->ShouldPass()) {
|
||||
$email = new Email(
|
||||
$this->config()->get('from_email_address'),
|
||||
$this->config()->get('to_email_address'),
|
||||
$this->title,
|
||||
$resultText
|
||||
$emailContent
|
||||
);
|
||||
$email->send();
|
||||
}
|
||||
@ -213,7 +230,7 @@ class EnvironmentChecker extends RequestHandler
|
||||
return $response;
|
||||
}
|
||||
|
||||
$response->setBody($resultText);
|
||||
$response->setBody($webContent);
|
||||
|
||||
return $response;
|
||||
}
|
||||
@ -229,16 +246,36 @@ class EnvironmentChecker extends RequestHandler
|
||||
Injector::inst()->get(LoggerInterface::class)->log($level, $message);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the HTTP status code that should be returned when there's an error.
|
||||
*
|
||||
* @param int $errorCode
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setErrorCode($errorCode)
|
||||
{
|
||||
$this->errorCode = $errorCode;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether to include the full breakdown of services
|
||||
*
|
||||
* @param bool $includeDetails
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setIncludeDetails($includeDetails)
|
||||
{
|
||||
$this->includeDetails = $includeDetails;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* @param string $from
|
||||
|
@ -64,16 +64,18 @@
|
||||
<h1 class="$Status">$Title: $Status</h1>
|
||||
<h2 class="website">Site: $URL</h2>
|
||||
|
||||
<% if $IncludeDetails %>
|
||||
<table>
|
||||
<tr><th>Check</th> <th>Status</th> <th>Message</th></tr>
|
||||
<% loop $Details %>
|
||||
<tr><td>$Check</td> <td class="$Status">$Status</td> <td>$Message.XML</td></tr>
|
||||
<% end_loop %>
|
||||
</table>
|
||||
<% end_if %>
|
||||
|
||||
<% if $ShouldPass %>
|
||||
<p>Site is available</p>
|
||||
<p class="subtext">(you may check for the presence of the text 'Site is available' rather than an HTTP $ErrorCode error on this page, if you prefer.)</p>
|
||||
<p>Site is available</p>
|
||||
<p class="subtext">(you may check for the presence of the text 'Site is available' rather than an HTTP $ErrorCode error on this page, if you prefer.<% if not $IncludeDetails %> Full details are available for logged in users at <a href="{$AbsoluteBaseURL}dev/check/">dev/check</a><% end_if %>)</p>
|
||||
<% else %>
|
||||
<% if $Name == "check" %>
|
||||
<p><b>A subsystem of the site is unavailable, but the site remains operational</b></p>
|
||||
|
@ -28,4 +28,14 @@ class DevCheckControllerTest extends SapphireTest
|
||||
|
||||
$this->assertInstanceOf(EnvironmentChecker::class, $controller->index($request));
|
||||
}
|
||||
|
||||
public function testCheckIncludesDetails()
|
||||
{
|
||||
$controller = new DevCheckController();
|
||||
$request = new HTTPRequest('GET', 'example.com');
|
||||
|
||||
$response = $controller->index($request)->index();
|
||||
|
||||
$this->assertStringContainsString('<table>', $response->getBody());
|
||||
}
|
||||
}
|
||||
|
@ -37,4 +37,15 @@ class DevHealthControllerTest extends SapphireTest
|
||||
|
||||
$this->assertInstanceOf(EnvironmentChecker::class, $controller->index($request));
|
||||
}
|
||||
|
||||
|
||||
public function testHealthDoesNotIncludeDetails()
|
||||
{
|
||||
$controller = new DevHealthController();
|
||||
$request = new HTTPRequest('GET', 'example.com');
|
||||
|
||||
$response = $controller->index($request)->index();
|
||||
|
||||
$this->assertFalse(strpos($response->getBody(), '<table>'));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user