1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\Module\monitor\TestCase; |
6
|
|
|
|
7
|
|
|
use SimpleSAML\Module\monitor\State; |
8
|
|
|
use SimpleSAML\Module\monitor\TestData; |
9
|
|
|
use SimpleSAML\Module\monitor\TestResult; |
10
|
|
|
|
11
|
|
|
use function abs; |
12
|
|
|
use function array_key_exists; |
13
|
|
|
use function intval; |
14
|
|
|
|
15
|
|
|
class Cert extends \SimpleSAML\Module\monitor\TestCaseFactory |
16
|
|
|
{ |
17
|
|
|
/** @var array */ |
18
|
|
|
private array $certInfo = []; |
19
|
|
|
|
20
|
|
|
/** @var integer */ |
21
|
|
|
private int $expiration; |
22
|
|
|
|
23
|
|
|
/** @var integer|null */ |
24
|
|
|
private ?int $certExpirationWarning = null; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \SimpleSAML\Module\monitor\TestData $testData |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
protected function initialize(TestData $testData): void |
33
|
|
|
{ |
34
|
|
|
$this->setCategory($testData->getInputItem('category')); |
|
|
|
|
35
|
|
|
$this->setCertInfo($testData->getInputItem('certData')); |
|
|
|
|
36
|
|
|
$this->setCertExpirationWarning($testData->getInputItem('certExpirationWarning')); |
|
|
|
|
37
|
|
|
|
38
|
|
|
parent::initialize($testData); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
public function getSubject(): string |
46
|
|
|
{ |
47
|
|
|
$certInfo = $this->getCertInfo(); |
48
|
|
|
if ( |
49
|
|
|
isset($certInfo['subject']) |
50
|
|
|
&& !empty($certInfo['subject']) |
51
|
|
|
&& array_key_exists('CN', $certInfo['subject']) |
52
|
|
|
) { |
53
|
|
|
return 'CN=' . $certInfo['subject']['CN']; |
54
|
|
|
} elseif (isset($certInfo['serialNumber'])) { |
55
|
|
|
return 'SN=' . $certInfo['serialNumber']; |
56
|
|
|
} else { |
57
|
|
|
return 'UNKNOWN'; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param array $certInfo |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
protected function setCertInfo(array $certInfo): void |
68
|
|
|
{ |
69
|
|
|
$this->certInfo = $certInfo; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
protected function getCertInfo(): array |
77
|
|
|
{ |
78
|
|
|
return $this->certInfo; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param int $certExpirationWarning |
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
protected function setCertExpirationWarning(int $certExpirationWarning): void |
88
|
|
|
{ |
89
|
|
|
$this->certExpirationWarning = $certExpirationWarning; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return int|null |
95
|
|
|
*/ |
96
|
|
|
protected function getCertExpirationWarning(): ?int |
97
|
|
|
{ |
98
|
|
|
return $this->certExpirationWarning; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return int |
104
|
|
|
*/ |
105
|
|
|
protected function getExpiration(): int |
106
|
|
|
{ |
107
|
|
|
return $this->expiration; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param integer $expiration |
113
|
|
|
* |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
|
|
private function setExpiration(int $expiration): void |
117
|
|
|
{ |
118
|
|
|
$this->expiration = $expiration; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
protected function calculateExpiration(): void |
126
|
|
|
{ |
127
|
|
|
$certInfo = $this->getCertInfo(); |
128
|
|
|
$expiration = intval(($certInfo['validTo_time_t'] - time()) / 86400); |
129
|
|
|
$this->setExpiration($expiration); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
|
|
public function invokeTest(): void |
137
|
|
|
{ |
138
|
|
|
$this->calculateExpiration(); |
139
|
|
|
|
140
|
|
|
$threshold = $this->getCertExpirationWarning(); |
141
|
|
|
$expiration = $this->getExpiration(); |
142
|
|
|
|
143
|
|
|
$days = abs($expiration); |
144
|
|
|
$daysStr = $days . ' ' . (($days === 1) ? 'day' : 'days'); |
145
|
|
|
|
146
|
|
|
$testResult = new TestResult($this->getCategory(), $this->getSubject()); |
147
|
|
|
|
148
|
|
|
if ($expiration < 0) { |
149
|
|
|
$testResult->setState(State::ERROR); |
150
|
|
|
$testResult->setMessage('Certificate has expired ' . $daysStr . ' ago'); |
151
|
|
|
} elseif ($expiration <= $threshold) { |
152
|
|
|
$testResult->setState(State::WARNING); |
153
|
|
|
$testResult->setMessage('Certificate will expire in ' . $daysStr); |
154
|
|
|
} else { |
155
|
|
|
$testResult->setState(State::OK); |
156
|
|
|
$testResult->setMessage('Certificate valid for another ' . $daysStr); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$testResult->addOutput($expiration, 'expiration'); |
160
|
|
|
$this->setTestResult($testResult); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|