1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleSAML\Module\monitor\Controller; |
4
|
|
|
|
5
|
|
|
use SimpleSAML\Configuration; |
6
|
|
|
use SimpleSAML\Module\monitor\DependencyInjection; |
7
|
|
|
use SimpleSAML\Module\monitor\State; |
8
|
|
|
use SimpleSAML\Module\monitor\TestConfiguration; |
9
|
|
|
use SimpleSAML\Module\monitor\Monitor; |
10
|
|
|
use SimpleSAML\XHTML\Template; |
11
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
13
|
|
|
|
14
|
|
|
use function array_merge; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Controller class for the monitor module. |
18
|
|
|
* |
19
|
|
|
* This class serves the different views available in the module. |
20
|
|
|
* |
21
|
|
|
* @package SimpleSAML\Module\monitor |
22
|
|
|
*/ |
23
|
|
|
class MonitorController |
24
|
|
|
{ |
25
|
|
|
/** @var \SimpleSAML\Configuration */ |
26
|
|
|
protected $config; |
27
|
|
|
|
28
|
|
|
/** @var \SimpleSAML\Configuration */ |
29
|
|
|
protected $moduleConfig; |
30
|
|
|
|
31
|
|
|
/** @var \SimpleSAML\Configuration */ |
32
|
|
|
protected $authsourceConfig; |
33
|
|
|
|
34
|
|
|
/** @var \SimpleSAML\Module\monitor\DependencyInjection */ |
35
|
|
|
protected $serverVars; |
36
|
|
|
|
37
|
|
|
/** @var \SimpleSAML\Module\monitor\DependencyInjection */ |
38
|
|
|
protected $requestVars; |
39
|
|
|
|
40
|
|
|
/** @var array */ |
41
|
|
|
private $healthInfo = [ |
42
|
|
|
State::SKIPPED => ['SKIPPED', 'yellow'], |
43
|
|
|
State::FATAL => ['FATAL', 'purple'], |
44
|
|
|
State::ERROR => ['NOK', 'red' ], |
45
|
|
|
State::NOSTATE => ['NOSTATE', 'cyan'], |
46
|
|
|
State::WARNING => ['WARNING', 'orange'], |
47
|
|
|
State::OK => ['OK', 'green' ] |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
/** @var \SimpleSAML\Module\monitor\TestConfiguration */ |
51
|
|
|
protected $testConfiguration; |
52
|
|
|
|
53
|
|
|
/** @var int */ |
54
|
|
|
protected $state; |
55
|
|
|
|
56
|
|
|
/** @var int */ |
57
|
|
|
protected $responseCode = 200; |
58
|
|
|
|
59
|
|
|
/** @var \SimpleSAML\Module\monitor\Monitor */ |
60
|
|
|
protected $monitor; |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Controller constructor. |
65
|
|
|
* |
66
|
|
|
* It initializes the global configuration and auth source configuration for the controllers implemented here. |
67
|
|
|
* |
68
|
|
|
* @param \SimpleSAML\Configuration $config The configuration to use by the controllers. |
69
|
|
|
* @throws \Exception |
70
|
|
|
*/ |
71
|
|
|
public function __construct(Configuration $config) |
72
|
|
|
{ |
73
|
|
|
$this->config = $config; |
74
|
|
|
$this->moduleConfig = Configuration::getOptionalConfig('module_monitor.php'); |
75
|
|
|
$this->authsourceConfig = Configuration::getOptionalConfig('authsources.php'); |
76
|
|
|
|
77
|
|
|
$this->serverVars = new DependencyInjection($_SERVER); |
78
|
|
|
$this->requestVars = new DependencyInjection($_REQUEST); |
79
|
|
|
|
80
|
|
|
$this->testConfiguration = new TestConfiguration( |
81
|
|
|
$this->serverVars, |
82
|
|
|
$this->requestVars, |
83
|
|
|
$this->config, |
84
|
|
|
$this->authsourceConfig, |
85
|
|
|
$this->moduleConfig |
86
|
|
|
); |
87
|
|
|
$this->monitor = new Monitor($this->testConfiguration); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Display the main monitoring page. |
93
|
|
|
* |
94
|
|
|
* @param string $format Default is XHTML output |
95
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
96
|
|
|
*/ |
97
|
|
|
public function main(string $format): Response |
98
|
|
|
{ |
99
|
|
|
$this->monitor->invokeTestSuites(); |
100
|
|
|
|
101
|
|
|
$this->state = $this->monitor->getState(); |
102
|
|
|
if ($this->state === State::OK) { |
103
|
|
|
$this->responseCode = 200; |
104
|
|
|
} elseif ($this->state === State::WARNING) { |
105
|
|
|
$this->responseCode = $this->moduleConfig->getInteger('warningStatusCode', 202); |
106
|
|
|
} else { |
107
|
|
|
$this->responseCode = $this->moduleConfig->getInteger('errorStatusCode', 500); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$results = $this->monitor->getResults(); |
111
|
|
|
|
112
|
|
|
switch ($format) { |
113
|
|
|
case 'xml': |
114
|
|
|
$t = $this->processXml(); |
115
|
|
|
break; |
116
|
|
|
case 'json': |
117
|
|
|
return $this->processJson($results); |
118
|
|
|
case 'text': |
119
|
|
|
$t = $this->processText(); |
120
|
|
|
break; |
121
|
|
|
default: |
122
|
|
|
$t = new Template($this->config, 'monitor:monitor.twig'); |
123
|
|
|
break; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$t->data['header'] = 'Monitor'; |
127
|
|
|
$t->data = array_merge($t->data, $results); |
128
|
|
|
|
129
|
|
|
$t->data['overall'] = $this->state; |
130
|
|
|
$t->data['healthInfo'] = $this->healthInfo; |
131
|
|
|
$t->data['responseCode'] = $this->responseCode; |
132
|
|
|
|
133
|
|
|
return $t; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return \SimpleSAML\XHTML\Template |
139
|
|
|
*/ |
140
|
|
|
private function processXml(): Template |
141
|
|
|
{ |
142
|
|
|
$t = new Template($this->config, 'monitor:monitor.xml.twig'); |
143
|
|
|
$t->headers->set('Content-Type', 'text/xml'); |
144
|
|
|
return $t; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param array $results |
150
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse |
151
|
|
|
*/ |
152
|
|
|
private function processJson(array $results): JsonResponse |
153
|
|
|
{ |
154
|
|
|
return JsonResponse::create( |
|
|
|
|
155
|
|
|
['overall' => $this->healthInfo[$this->state][0], |
156
|
|
|
'results' => $results], |
157
|
|
|
$this->responseCode |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return \SimpleSAML\XHTML\Template |
164
|
|
|
*/ |
165
|
|
|
private function processText(): Template |
166
|
|
|
{ |
167
|
|
|
$t = new Template($this->config, 'monitor:monitor.text.twig'); |
168
|
|
|
|
169
|
|
|
if ($this->state === State::OK) { |
170
|
|
|
$t->data['status'] = 'OK'; |
171
|
|
|
} elseif ($this->state === State::WARNING) { |
172
|
|
|
$t->data['status'] = 'WARN'; |
173
|
|
|
} else { |
174
|
|
|
$t->data['status'] = 'FAIL'; |
175
|
|
|
} |
176
|
|
|
return $t; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.