Skip to content

Commit

Permalink
refactor: make HTTPS connection check work without request available …
Browse files Browse the repository at this point in the history
…(CLI mode)
  • Loading branch information
Aaron Gerig committed Mar 14, 2022
1 parent ffa5798 commit 8d1c3b2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
35 changes: 25 additions & 10 deletions src/PimcoreMonitorBundle/Check/HttpsConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@
use Laminas\Diagnostics\Result\Skip;
use Laminas\Diagnostics\Result\Success;
use Laminas\Diagnostics\Result\Warning;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class HttpsConnection extends AbstractCheck
{
protected const IDENTIFIER = 'system:https_connection';

protected bool $skip;
protected ?Request $request;
protected array $systemConfig;

public function __construct(bool $skip, RequestStack $requestStack)
public function __construct(bool $skip, array $systemConfig)
{
$this->skip = $skip;
$this->request = $requestStack->getMainRequest() ?: $requestStack->getCurrentRequest();
$this->systemConfig = $systemConfig;
}

/**
Expand All @@ -32,17 +30,34 @@ public function check(): ResultInterface
return new Skip('Check was skipped');
}

if (null === $this->request) {
$host = $this->systemConfig['general']['domain'] ?? null;

if (null === $host) {
return new Warning('HTTPS encryption could not be checked');
}

$enabled = $this->request->isSecure();
// Create a stream context
$stream = stream_context_create(['ssl' => ['capture_peer_cert' => true]]);
$url = sprintf('https://%s', $host);

try {
// Bind the resource $url to $stream
$read = fopen($url, 'rb', false, $stream);

// Get the stream parameters
$params = stream_context_get_params($read);
} catch (\Exception) {
// Ignore exceptions thrown ...
}

// Check if SSL certificate is present
$cert = $params['options']['ssl']['peer_certificate'] ?? null;

if (! $enabled) {
return new Failure('HTTPS encryption not activated', $enabled);
if (null === $cert) {
return new Failure('HTTPS encryption not activated', false);
}

return new Success('HTTPS encryption activated', $enabled);
return new Success('HTTPS encryption activated', true);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ services:
Wvision\Bundle\PimcoreMonitorBundle\Check\HttpsConnection:
arguments:
- '%pimcore_monitor.checks.https_connection.skip%'
- '@Symfony\Component\HttpFoundation\RequestStack'
- '%pimcore.config%'
tags:
- { name: pimcore_monitor.check, alias: https_connection }

Expand Down

0 comments on commit 8d1c3b2

Please sign in to comment.