1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace whm\Smoke\Rules\Http; |
4
|
|
|
|
5
|
|
|
use whm\Smoke\Rules\Attribute; |
6
|
|
|
use whm\Smoke\Rules\CheckResult; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* This rule checks if a given https certificate expire in a few days. |
10
|
|
|
*/ |
11
|
|
|
class HttpsCertificateExpireRule extends HttpsRule |
12
|
|
|
{ |
13
|
|
|
private $expireWarningTime; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param int $expireWarningTime in days |
17
|
|
|
*/ |
18
|
|
|
public function init($expireWarningTime = 14) |
19
|
|
|
{ |
20
|
|
|
$this->expireWarningTime = $expireWarningTime; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
protected function doValidate($certInfo) |
24
|
|
|
{ |
25
|
|
|
$validFrom = date('d.m.Y H:i:s', $certInfo['validFrom_time_t']); |
26
|
|
|
$validTo = date('d.m.Y H:i:s', $certInfo['validTo_time_t']); |
27
|
|
|
|
28
|
|
|
if ($certInfo === false) { |
29
|
|
|
$result = new CheckResult(CheckResult::STATUS_FAILURE, 'Https certificate is not valid.'); |
30
|
|
|
return $result; |
31
|
|
|
} else { |
32
|
|
|
|
33
|
|
|
if ($certInfo['validFrom_time_t'] > time() || $certInfo['validTo_time_t'] < time()) { |
34
|
|
|
$errorMessage = 'Certificate is expired. [' . $validFrom . ' - ' . $validTo . ']'; |
35
|
|
|
|
36
|
|
|
$result = new CheckResult(CheckResult::STATUS_FAILURE, $errorMessage); |
37
|
|
|
$infoJson = json_encode($certInfo); |
38
|
|
|
if ($infoJson === false) { |
39
|
|
|
$result->addAttribute(new Attribute('json_encode error', json_last_error_msg(), false)); |
40
|
|
|
} else { |
41
|
|
|
$result->addAttribute(new Attribute('certificate information', $infoJson, true)); |
42
|
|
|
} |
43
|
|
|
return $result; |
44
|
|
|
} elseif ($certInfo['validTo_time_t'] < strtotime('+' . $this->expireWarningTime . 'days')) { |
45
|
|
|
$errorMessage = 'Certificate warning, expires in less than ' . $this->expireWarningTime . ' days. Certificate expires at: ' . $validTo; |
46
|
|
|
|
47
|
|
|
$result = new CheckResult(CheckResult::STATUS_FAILURE, $errorMessage); |
48
|
|
|
$result->addAttribute(new Attribute('certificate information', json_encode($certInfo), true)); |
49
|
|
|
return $result; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return new CheckResult(CheckResult::STATUS_SUCCESS, 'The certificate does not expire within the next ' . $this->expireWarningTime . ' days. Expire date: ' . $validTo . '.'); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|