Skip to content

Commit

Permalink
refactor: improve stability on https_connection check
Browse files Browse the repository at this point in the history
  • Loading branch information
aarongerig committed Jun 21, 2023
1 parent a9748bf commit 46903cc
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 18 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"require": {
"php": ">=8.0",
"laminas/laminas-diagnostics": "^1.24",
"pimcore/pimcore": "^10.0"
"pimcore/pimcore": "^10.0",
"spatie/ssl-certificate": "^2.4"
},
"require-dev": {
"deployer/deployer": "^7.1",
Expand Down
20 changes: 3 additions & 17 deletions src/PimcoreMonitorBundle/Check/HttpsConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Laminas\Diagnostics\Result\Skip;
use Laminas\Diagnostics\Result\Success;
use Laminas\Diagnostics\Result\Warning;
use Spatie\SslCertificate\SslCertificate;

class HttpsConnection extends AbstractCheck
{
Expand All @@ -44,24 +45,9 @@ public function check(): ResultInterface
return new Warning('HTTPS encryption could not be checked');
}

// Create a stream context
$stream = \stream_context_create(['ssl' => ['capture_peer_cert' => true]]);
$url = \sprintf('https://%s', $host);
$certificate = SslCertificate::createForHostName($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 (null === $cert) {
if ($certificate->isValid() === false) {
return new Failure('HTTPS encryption not activated', false);
}

Expand Down

0 comments on commit 46903cc

Please sign in to comment.