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