@@ -20,188 +20,188 @@ |
||
20 | 20 | */ |
21 | 21 | class CacheHeadersCheck implements EnvironmentCheck |
22 | 22 | { |
23 | - use Fetcher; |
|
24 | - |
|
25 | - /** |
|
26 | - * Settings that must be included in the Cache-Control header |
|
27 | - * |
|
28 | - * @var array |
|
29 | - */ |
|
30 | - protected $mustInclude = []; |
|
31 | - |
|
32 | - /** |
|
33 | - * Settings that must be excluded in the Cache-Control header |
|
34 | - * |
|
35 | - * @var array |
|
36 | - */ |
|
37 | - protected $mustExclude = []; |
|
38 | - |
|
39 | - /** |
|
40 | - * Result to keep track of status and messages for all checks, reuses |
|
41 | - * ValidationResult for convenience. |
|
42 | - * |
|
43 | - * @var ValidationResult |
|
44 | - */ |
|
45 | - protected $result; |
|
46 | - |
|
47 | - /** |
|
48 | - * Set up with URL, arrays of header settings to check. |
|
49 | - * |
|
50 | - * @param string $url |
|
51 | - * @param array $mustInclude Settings that must be included in Cache-Control |
|
52 | - * @param array $mustExclude Settings that must be excluded in Cache-Control |
|
53 | - */ |
|
54 | - public function __construct($url = '', $mustInclude = [], $mustExclude = []) |
|
55 | - { |
|
56 | - $this->setURL($url); |
|
57 | - $this->mustInclude = $mustInclude; |
|
58 | - $this->mustExclude = $mustExclude; |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Check that correct caching headers are present. |
|
63 | - * |
|
64 | - * @return void |
|
65 | - */ |
|
66 | - public function check() |
|
67 | - { |
|
68 | - // Using a validation result to capture messages |
|
69 | - $this->result = new ValidationResult(); |
|
70 | - |
|
71 | - $response = $this->client->get($this->getURL()); |
|
72 | - $fullURL = $this->getURL(); |
|
73 | - if ($response === null) { |
|
74 | - return [ |
|
75 | - EnvironmentCheck::ERROR, |
|
76 | - "Cache headers check request failed for $fullURL", |
|
77 | - ]; |
|
78 | - } |
|
79 | - |
|
80 | - //Check that Etag exists |
|
81 | - $this->checkEtag($response); |
|
82 | - |
|
83 | - // Check Cache-Control settings |
|
84 | - $this->checkCacheControl($response); |
|
85 | - |
|
86 | - if ($this->result->isValid()) { |
|
87 | - return [ |
|
88 | - EnvironmentCheck::OK, |
|
89 | - $this->getMessage(), |
|
90 | - ]; |
|
91 | - } else { |
|
92 | - // @todo Ability to return a warning |
|
93 | - return [ |
|
94 | - EnvironmentCheck::ERROR, |
|
95 | - $this->getMessage(), |
|
96 | - ]; |
|
97 | - } |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Collate messages from ValidationResult so that it is clear which parts |
|
102 | - * of the check passed and which failed. |
|
103 | - * |
|
104 | - * @return string |
|
105 | - */ |
|
106 | - private function getMessage() |
|
107 | - { |
|
108 | - $ret = ''; |
|
109 | - // Filter good messages |
|
110 | - $goodTypes = [ValidationResult::TYPE_GOOD, ValidationResult::TYPE_INFO]; |
|
111 | - $good = array_filter( |
|
112 | - $this->result->getMessages(), |
|
113 | - function ($val, $key) use ($goodTypes) { |
|
114 | - if (in_array($val['messageType'], $goodTypes)) { |
|
115 | - return true; |
|
116 | - } |
|
117 | - return false; |
|
118 | - }, |
|
119 | - ARRAY_FILTER_USE_BOTH |
|
120 | - ); |
|
121 | - if (!empty($good)) { |
|
122 | - $ret .= "GOOD: " . implode('; ', array_column($good, 'message')) . " "; |
|
123 | - } |
|
124 | - |
|
125 | - // Filter bad messages |
|
126 | - $badTypes = [ValidationResult::TYPE_ERROR, ValidationResult::TYPE_WARNING]; |
|
127 | - $bad = array_filter( |
|
128 | - $this->result->getMessages(), |
|
129 | - function ($val, $key) use ($badTypes) { |
|
130 | - if (in_array($val['messageType'], $badTypes)) { |
|
131 | - return true; |
|
132 | - } |
|
133 | - return false; |
|
134 | - }, |
|
135 | - ARRAY_FILTER_USE_BOTH |
|
136 | - ); |
|
137 | - if (!empty($bad)) { |
|
138 | - $ret .= "BAD: " . implode('; ', array_column($bad, 'message')); |
|
139 | - } |
|
140 | - return $ret; |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * Check that ETag header exists |
|
145 | - * |
|
146 | - * @param ResponseInterface $response |
|
147 | - * @return void |
|
148 | - */ |
|
149 | - private function checkEtag(ResponseInterface $response) |
|
150 | - { |
|
151 | - $eTag = $response->getHeaderLine('ETag'); |
|
152 | - $fullURL = Controller::join_links(Director::absoluteBaseURL(), $this->url); |
|
153 | - |
|
154 | - if ($eTag) { |
|
155 | - $this->result->addMessage( |
|
156 | - "$fullURL includes an Etag header in response", |
|
157 | - ValidationResult::TYPE_GOOD |
|
158 | - ); |
|
159 | - return; |
|
160 | - } |
|
161 | - $this->result->addError( |
|
162 | - "$fullURL is missing an Etag header", |
|
163 | - ValidationResult::TYPE_WARNING |
|
164 | - ); |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Check that the correct header settings are either included or excluded. |
|
169 | - * |
|
170 | - * @param ResponseInterface $response |
|
171 | - * @return void |
|
172 | - */ |
|
173 | - private function checkCacheControl(ResponseInterface $response) |
|
174 | - { |
|
175 | - $cacheControl = $response->getHeaderLine('Cache-Control'); |
|
176 | - $vals = array_map('trim', explode(',', $cacheControl)); |
|
177 | - $fullURL = Controller::join_links(Director::absoluteBaseURL(), $this->url); |
|
178 | - |
|
179 | - // All entries from must contain should be present |
|
180 | - if ($this->mustInclude == array_intersect($this->mustInclude, $vals)) { |
|
181 | - $matched = implode(",", $this->mustInclude); |
|
182 | - $this->result->addMessage( |
|
183 | - "$fullURL includes all settings: {$matched}", |
|
184 | - ValidationResult::TYPE_GOOD |
|
185 | - ); |
|
186 | - } else { |
|
187 | - $missing = implode(",", array_diff($this->mustInclude, $vals)); |
|
188 | - $this->result->addError( |
|
189 | - "$fullURL is excluding some settings: {$missing}" |
|
190 | - ); |
|
191 | - } |
|
192 | - |
|
193 | - // All entries from must exclude should not be present |
|
194 | - if (empty(array_intersect($this->mustExclude, $vals))) { |
|
195 | - $missing = implode(",", $this->mustExclude); |
|
196 | - $this->result->addMessage( |
|
197 | - "$fullURL excludes all settings: {$missing}", |
|
198 | - ValidationResult::TYPE_GOOD |
|
199 | - ); |
|
200 | - } else { |
|
201 | - $matched = implode(",", array_intersect($this->mustExclude, $vals)); |
|
202 | - $this->result->addError( |
|
203 | - "$fullURL is including some settings: {$matched}" |
|
204 | - ); |
|
205 | - } |
|
206 | - } |
|
23 | + use Fetcher; |
|
24 | + |
|
25 | + /** |
|
26 | + * Settings that must be included in the Cache-Control header |
|
27 | + * |
|
28 | + * @var array |
|
29 | + */ |
|
30 | + protected $mustInclude = []; |
|
31 | + |
|
32 | + /** |
|
33 | + * Settings that must be excluded in the Cache-Control header |
|
34 | + * |
|
35 | + * @var array |
|
36 | + */ |
|
37 | + protected $mustExclude = []; |
|
38 | + |
|
39 | + /** |
|
40 | + * Result to keep track of status and messages for all checks, reuses |
|
41 | + * ValidationResult for convenience. |
|
42 | + * |
|
43 | + * @var ValidationResult |
|
44 | + */ |
|
45 | + protected $result; |
|
46 | + |
|
47 | + /** |
|
48 | + * Set up with URL, arrays of header settings to check. |
|
49 | + * |
|
50 | + * @param string $url |
|
51 | + * @param array $mustInclude Settings that must be included in Cache-Control |
|
52 | + * @param array $mustExclude Settings that must be excluded in Cache-Control |
|
53 | + */ |
|
54 | + public function __construct($url = '', $mustInclude = [], $mustExclude = []) |
|
55 | + { |
|
56 | + $this->setURL($url); |
|
57 | + $this->mustInclude = $mustInclude; |
|
58 | + $this->mustExclude = $mustExclude; |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Check that correct caching headers are present. |
|
63 | + * |
|
64 | + * @return void |
|
65 | + */ |
|
66 | + public function check() |
|
67 | + { |
|
68 | + // Using a validation result to capture messages |
|
69 | + $this->result = new ValidationResult(); |
|
70 | + |
|
71 | + $response = $this->client->get($this->getURL()); |
|
72 | + $fullURL = $this->getURL(); |
|
73 | + if ($response === null) { |
|
74 | + return [ |
|
75 | + EnvironmentCheck::ERROR, |
|
76 | + "Cache headers check request failed for $fullURL", |
|
77 | + ]; |
|
78 | + } |
|
79 | + |
|
80 | + //Check that Etag exists |
|
81 | + $this->checkEtag($response); |
|
82 | + |
|
83 | + // Check Cache-Control settings |
|
84 | + $this->checkCacheControl($response); |
|
85 | + |
|
86 | + if ($this->result->isValid()) { |
|
87 | + return [ |
|
88 | + EnvironmentCheck::OK, |
|
89 | + $this->getMessage(), |
|
90 | + ]; |
|
91 | + } else { |
|
92 | + // @todo Ability to return a warning |
|
93 | + return [ |
|
94 | + EnvironmentCheck::ERROR, |
|
95 | + $this->getMessage(), |
|
96 | + ]; |
|
97 | + } |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Collate messages from ValidationResult so that it is clear which parts |
|
102 | + * of the check passed and which failed. |
|
103 | + * |
|
104 | + * @return string |
|
105 | + */ |
|
106 | + private function getMessage() |
|
107 | + { |
|
108 | + $ret = ''; |
|
109 | + // Filter good messages |
|
110 | + $goodTypes = [ValidationResult::TYPE_GOOD, ValidationResult::TYPE_INFO]; |
|
111 | + $good = array_filter( |
|
112 | + $this->result->getMessages(), |
|
113 | + function ($val, $key) use ($goodTypes) { |
|
114 | + if (in_array($val['messageType'], $goodTypes)) { |
|
115 | + return true; |
|
116 | + } |
|
117 | + return false; |
|
118 | + }, |
|
119 | + ARRAY_FILTER_USE_BOTH |
|
120 | + ); |
|
121 | + if (!empty($good)) { |
|
122 | + $ret .= "GOOD: " . implode('; ', array_column($good, 'message')) . " "; |
|
123 | + } |
|
124 | + |
|
125 | + // Filter bad messages |
|
126 | + $badTypes = [ValidationResult::TYPE_ERROR, ValidationResult::TYPE_WARNING]; |
|
127 | + $bad = array_filter( |
|
128 | + $this->result->getMessages(), |
|
129 | + function ($val, $key) use ($badTypes) { |
|
130 | + if (in_array($val['messageType'], $badTypes)) { |
|
131 | + return true; |
|
132 | + } |
|
133 | + return false; |
|
134 | + }, |
|
135 | + ARRAY_FILTER_USE_BOTH |
|
136 | + ); |
|
137 | + if (!empty($bad)) { |
|
138 | + $ret .= "BAD: " . implode('; ', array_column($bad, 'message')); |
|
139 | + } |
|
140 | + return $ret; |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * Check that ETag header exists |
|
145 | + * |
|
146 | + * @param ResponseInterface $response |
|
147 | + * @return void |
|
148 | + */ |
|
149 | + private function checkEtag(ResponseInterface $response) |
|
150 | + { |
|
151 | + $eTag = $response->getHeaderLine('ETag'); |
|
152 | + $fullURL = Controller::join_links(Director::absoluteBaseURL(), $this->url); |
|
153 | + |
|
154 | + if ($eTag) { |
|
155 | + $this->result->addMessage( |
|
156 | + "$fullURL includes an Etag header in response", |
|
157 | + ValidationResult::TYPE_GOOD |
|
158 | + ); |
|
159 | + return; |
|
160 | + } |
|
161 | + $this->result->addError( |
|
162 | + "$fullURL is missing an Etag header", |
|
163 | + ValidationResult::TYPE_WARNING |
|
164 | + ); |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Check that the correct header settings are either included or excluded. |
|
169 | + * |
|
170 | + * @param ResponseInterface $response |
|
171 | + * @return void |
|
172 | + */ |
|
173 | + private function checkCacheControl(ResponseInterface $response) |
|
174 | + { |
|
175 | + $cacheControl = $response->getHeaderLine('Cache-Control'); |
|
176 | + $vals = array_map('trim', explode(',', $cacheControl)); |
|
177 | + $fullURL = Controller::join_links(Director::absoluteBaseURL(), $this->url); |
|
178 | + |
|
179 | + // All entries from must contain should be present |
|
180 | + if ($this->mustInclude == array_intersect($this->mustInclude, $vals)) { |
|
181 | + $matched = implode(",", $this->mustInclude); |
|
182 | + $this->result->addMessage( |
|
183 | + "$fullURL includes all settings: {$matched}", |
|
184 | + ValidationResult::TYPE_GOOD |
|
185 | + ); |
|
186 | + } else { |
|
187 | + $missing = implode(",", array_diff($this->mustInclude, $vals)); |
|
188 | + $this->result->addError( |
|
189 | + "$fullURL is excluding some settings: {$missing}" |
|
190 | + ); |
|
191 | + } |
|
192 | + |
|
193 | + // All entries from must exclude should not be present |
|
194 | + if (empty(array_intersect($this->mustExclude, $vals))) { |
|
195 | + $missing = implode(",", $this->mustExclude); |
|
196 | + $this->result->addMessage( |
|
197 | + "$fullURL excludes all settings: {$missing}", |
|
198 | + ValidationResult::TYPE_GOOD |
|
199 | + ); |
|
200 | + } else { |
|
201 | + $matched = implode(",", array_intersect($this->mustExclude, $vals)); |
|
202 | + $this->result->addError( |
|
203 | + "$fullURL is including some settings: {$matched}" |
|
204 | + ); |
|
205 | + } |
|
206 | + } |
|
207 | 207 | } |
@@ -11,41 +11,41 @@ |
||
11 | 11 | */ |
12 | 12 | trait Fetcher |
13 | 13 | { |
14 | - /** |
|
15 | - * Client for making requests, set vi Injector. |
|
16 | - * |
|
17 | - * @see SilverStripe\EnvironmentCheck\Services |
|
18 | - * |
|
19 | - * @var GuzzleHttp\Client |
|
20 | - */ |
|
21 | - public $client = null; |
|
14 | + /** |
|
15 | + * Client for making requests, set vi Injector. |
|
16 | + * |
|
17 | + * @see SilverStripe\EnvironmentCheck\Services |
|
18 | + * |
|
19 | + * @var GuzzleHttp\Client |
|
20 | + */ |
|
21 | + public $client = null; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Absolute URL for requests. |
|
25 | - * |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - protected $url; |
|
23 | + /** |
|
24 | + * Absolute URL for requests. |
|
25 | + * |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + protected $url; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Set URL for requests. |
|
32 | - * |
|
33 | - * @param string $url Relative URL |
|
34 | - * @return self |
|
35 | - */ |
|
36 | - public function setURL($url) |
|
37 | - { |
|
38 | - $this->url = Director::absoluteURL($url); |
|
39 | - return $this; |
|
40 | - } |
|
30 | + /** |
|
31 | + * Set URL for requests. |
|
32 | + * |
|
33 | + * @param string $url Relative URL |
|
34 | + * @return self |
|
35 | + */ |
|
36 | + public function setURL($url) |
|
37 | + { |
|
38 | + $this->url = Director::absoluteURL($url); |
|
39 | + return $this; |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Getter for URL |
|
44 | - * |
|
45 | - * @return string |
|
46 | - */ |
|
47 | - public function getURL() |
|
48 | - { |
|
49 | - return $this->url; |
|
50 | - } |
|
42 | + /** |
|
43 | + * Getter for URL |
|
44 | + * |
|
45 | + * @return string |
|
46 | + */ |
|
47 | + public function getURL() |
|
48 | + { |
|
49 | + return $this->url; |
|
50 | + } |
|
51 | 51 | } |
@@ -32,184 +32,184 @@ |
||
32 | 32 | */ |
33 | 33 | class EnvironmentCheckSuite |
34 | 34 | { |
35 | - use Configurable; |
|
36 | - use Injectable; |
|
37 | - use Extensible; |
|
38 | - /** |
|
39 | - * Name of this suite. |
|
40 | - * |
|
41 | - * @var string |
|
42 | - */ |
|
43 | - protected $name; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var array |
|
47 | - */ |
|
48 | - protected $checks = []; |
|
49 | - |
|
50 | - /** |
|
51 | - * Associative array of named checks registered via the config system. Each check should specify: |
|
52 | - * - definition (e.g. 'MyHealthCheck("my param")') |
|
53 | - * - title (e.g. 'Is my feature working?') |
|
54 | - * - state (setting this to 'disabled' will cause suites to skip this check entirely. |
|
55 | - * |
|
56 | - * @var array |
|
57 | - */ |
|
58 | - private static $registered_checks = []; |
|
59 | - |
|
60 | - /** |
|
61 | - * Associative array of named suites registered via the config system. Each suite should enumerate |
|
62 | - * named checks that have been configured in 'registered_checks'. |
|
63 | - * |
|
64 | - * @var array |
|
65 | - */ |
|
66 | - private static $registered_suites = []; |
|
67 | - |
|
68 | - /** |
|
69 | - * Load checks for this suite from the configuration system. This is an alternative to the |
|
70 | - * EnvironmentCheckSuite::register - both can be used, checks will be appended to the suite. |
|
71 | - * |
|
72 | - * @param string $suiteName The name of this suite. |
|
73 | - */ |
|
74 | - public function __construct($suiteName) |
|
75 | - { |
|
76 | - if (empty($this->config()->registered_suites[$suiteName])) { |
|
77 | - // Not registered via config system, but it still may be configured later via self::register. |
|
78 | - return; |
|
79 | - } |
|
80 | - |
|
81 | - foreach ($this->config()->registered_suites[$suiteName] as $checkName) { |
|
82 | - if (empty($this->config()->registered_checks[$checkName])) { |
|
83 | - throw new InvalidArgumentException( |
|
84 | - "Bad EnvironmentCheck: '$checkName' - the named check has not been registered." |
|
85 | - ); |
|
86 | - } |
|
87 | - |
|
88 | - $check = $this->config()->registered_checks[$checkName]; |
|
89 | - |
|
90 | - // Existing named checks can be disabled by setting their 'state' to 'disabled'. |
|
91 | - // This is handy for disabling checks mandated by modules. |
|
92 | - if (!empty($check['state']) && $check['state'] === 'disabled') { |
|
93 | - continue; |
|
94 | - } |
|
95 | - |
|
96 | - // Add the check to this suite. |
|
97 | - $this->push($check['definition'], $check['title']); |
|
98 | - } |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * Run this test suite and return the result code of the worst result. |
|
103 | - * |
|
104 | - * @return EnvironmentCheckSuiteResult |
|
105 | - */ |
|
106 | - public function run() |
|
107 | - { |
|
108 | - $result = new EnvironmentCheckSuiteResult(); |
|
109 | - |
|
110 | - foreach ($this->checkInstances() as $check) { |
|
111 | - list($checkClass, $checkTitle) = $check; |
|
112 | - try { |
|
113 | - list($status, $message) = $checkClass->check(); |
|
114 | - // If the check fails, register that as an error |
|
115 | - } catch (Exception $e) { |
|
116 | - $status = EnvironmentCheck::ERROR; |
|
117 | - $message = $e->getMessage(); |
|
118 | - } |
|
119 | - $result->addResult($status, $message, $checkTitle); |
|
120 | - } |
|
121 | - |
|
122 | - return $result; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Get instances of all the environment checks. |
|
127 | - * |
|
128 | - * @return EnvironmentChecker[] |
|
129 | - * @throws InvalidArgumentException |
|
130 | - */ |
|
131 | - protected function checkInstances() |
|
132 | - { |
|
133 | - $output = []; |
|
134 | - foreach ($this->checks as $check) { |
|
135 | - list($checkClass, $checkTitle) = $check; |
|
136 | - if (is_string($checkClass)) { |
|
137 | - $checkInst = Injector::inst()->create($checkClass); |
|
138 | - if ($checkInst instanceof EnvironmentCheck) { |
|
139 | - $output[] = [$checkInst, $checkTitle]; |
|
140 | - } else { |
|
141 | - throw new InvalidArgumentException( |
|
142 | - "Bad EnvironmentCheck: '$checkClass' - the named class doesn't implement EnvironmentCheck" |
|
143 | - ); |
|
144 | - } |
|
145 | - } elseif ($checkClass instanceof EnvironmentCheck) { |
|
146 | - $output[] = [$checkClass, $checkTitle]; |
|
147 | - } else { |
|
148 | - throw new InvalidArgumentException("Bad EnvironmentCheck: " . var_export($check, true)); |
|
149 | - } |
|
150 | - } |
|
151 | - return $output; |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Add a check to this suite. |
|
156 | - * |
|
157 | - * @param mixed $check |
|
158 | - * @param string $title |
|
159 | - */ |
|
160 | - public function push($check, $title = null) |
|
161 | - { |
|
162 | - if (!$title) { |
|
163 | - $title = is_string($check) ? $check : get_class($check); |
|
164 | - } |
|
165 | - $this->checks[] = [$check, $title]; |
|
166 | - } |
|
167 | - |
|
168 | - ///////////////////////////////////////////////////////////////////////////////////////////// |
|
169 | - |
|
170 | - /** |
|
171 | - * @var array |
|
172 | - */ |
|
173 | - protected static $instances = []; |
|
174 | - |
|
175 | - /** |
|
176 | - * Return a named instance of EnvironmentCheckSuite. |
|
177 | - * |
|
178 | - * @param string $name |
|
179 | - * |
|
180 | - * @return EnvironmentCheckSuite |
|
181 | - */ |
|
182 | - public static function inst($name) |
|
183 | - { |
|
184 | - if (!isset(self::$instances[$name])) { |
|
185 | - self::$instances[$name] = new EnvironmentCheckSuite($name); |
|
186 | - } |
|
187 | - return self::$instances[$name]; |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Register a check against the named check suite. |
|
192 | - * |
|
193 | - * @param string|array $names |
|
194 | - * @param EnvironmentCheck $check |
|
195 | - * @param string|array |
|
196 | - */ |
|
197 | - public static function register($names, $check, $title = null) |
|
198 | - { |
|
199 | - if (!is_array($names)) { |
|
200 | - $names = [$names]; |
|
201 | - } |
|
202 | - |
|
203 | - foreach ($names as $name) { |
|
204 | - self::inst($name)->push($check, $title); |
|
205 | - } |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * Unregisters all checks. |
|
210 | - */ |
|
211 | - public static function reset() |
|
212 | - { |
|
213 | - self::$instances = []; |
|
214 | - } |
|
35 | + use Configurable; |
|
36 | + use Injectable; |
|
37 | + use Extensible; |
|
38 | + /** |
|
39 | + * Name of this suite. |
|
40 | + * |
|
41 | + * @var string |
|
42 | + */ |
|
43 | + protected $name; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var array |
|
47 | + */ |
|
48 | + protected $checks = []; |
|
49 | + |
|
50 | + /** |
|
51 | + * Associative array of named checks registered via the config system. Each check should specify: |
|
52 | + * - definition (e.g. 'MyHealthCheck("my param")') |
|
53 | + * - title (e.g. 'Is my feature working?') |
|
54 | + * - state (setting this to 'disabled' will cause suites to skip this check entirely. |
|
55 | + * |
|
56 | + * @var array |
|
57 | + */ |
|
58 | + private static $registered_checks = []; |
|
59 | + |
|
60 | + /** |
|
61 | + * Associative array of named suites registered via the config system. Each suite should enumerate |
|
62 | + * named checks that have been configured in 'registered_checks'. |
|
63 | + * |
|
64 | + * @var array |
|
65 | + */ |
|
66 | + private static $registered_suites = []; |
|
67 | + |
|
68 | + /** |
|
69 | + * Load checks for this suite from the configuration system. This is an alternative to the |
|
70 | + * EnvironmentCheckSuite::register - both can be used, checks will be appended to the suite. |
|
71 | + * |
|
72 | + * @param string $suiteName The name of this suite. |
|
73 | + */ |
|
74 | + public function __construct($suiteName) |
|
75 | + { |
|
76 | + if (empty($this->config()->registered_suites[$suiteName])) { |
|
77 | + // Not registered via config system, but it still may be configured later via self::register. |
|
78 | + return; |
|
79 | + } |
|
80 | + |
|
81 | + foreach ($this->config()->registered_suites[$suiteName] as $checkName) { |
|
82 | + if (empty($this->config()->registered_checks[$checkName])) { |
|
83 | + throw new InvalidArgumentException( |
|
84 | + "Bad EnvironmentCheck: '$checkName' - the named check has not been registered." |
|
85 | + ); |
|
86 | + } |
|
87 | + |
|
88 | + $check = $this->config()->registered_checks[$checkName]; |
|
89 | + |
|
90 | + // Existing named checks can be disabled by setting their 'state' to 'disabled'. |
|
91 | + // This is handy for disabling checks mandated by modules. |
|
92 | + if (!empty($check['state']) && $check['state'] === 'disabled') { |
|
93 | + continue; |
|
94 | + } |
|
95 | + |
|
96 | + // Add the check to this suite. |
|
97 | + $this->push($check['definition'], $check['title']); |
|
98 | + } |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * Run this test suite and return the result code of the worst result. |
|
103 | + * |
|
104 | + * @return EnvironmentCheckSuiteResult |
|
105 | + */ |
|
106 | + public function run() |
|
107 | + { |
|
108 | + $result = new EnvironmentCheckSuiteResult(); |
|
109 | + |
|
110 | + foreach ($this->checkInstances() as $check) { |
|
111 | + list($checkClass, $checkTitle) = $check; |
|
112 | + try { |
|
113 | + list($status, $message) = $checkClass->check(); |
|
114 | + // If the check fails, register that as an error |
|
115 | + } catch (Exception $e) { |
|
116 | + $status = EnvironmentCheck::ERROR; |
|
117 | + $message = $e->getMessage(); |
|
118 | + } |
|
119 | + $result->addResult($status, $message, $checkTitle); |
|
120 | + } |
|
121 | + |
|
122 | + return $result; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Get instances of all the environment checks. |
|
127 | + * |
|
128 | + * @return EnvironmentChecker[] |
|
129 | + * @throws InvalidArgumentException |
|
130 | + */ |
|
131 | + protected function checkInstances() |
|
132 | + { |
|
133 | + $output = []; |
|
134 | + foreach ($this->checks as $check) { |
|
135 | + list($checkClass, $checkTitle) = $check; |
|
136 | + if (is_string($checkClass)) { |
|
137 | + $checkInst = Injector::inst()->create($checkClass); |
|
138 | + if ($checkInst instanceof EnvironmentCheck) { |
|
139 | + $output[] = [$checkInst, $checkTitle]; |
|
140 | + } else { |
|
141 | + throw new InvalidArgumentException( |
|
142 | + "Bad EnvironmentCheck: '$checkClass' - the named class doesn't implement EnvironmentCheck" |
|
143 | + ); |
|
144 | + } |
|
145 | + } elseif ($checkClass instanceof EnvironmentCheck) { |
|
146 | + $output[] = [$checkClass, $checkTitle]; |
|
147 | + } else { |
|
148 | + throw new InvalidArgumentException("Bad EnvironmentCheck: " . var_export($check, true)); |
|
149 | + } |
|
150 | + } |
|
151 | + return $output; |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Add a check to this suite. |
|
156 | + * |
|
157 | + * @param mixed $check |
|
158 | + * @param string $title |
|
159 | + */ |
|
160 | + public function push($check, $title = null) |
|
161 | + { |
|
162 | + if (!$title) { |
|
163 | + $title = is_string($check) ? $check : get_class($check); |
|
164 | + } |
|
165 | + $this->checks[] = [$check, $title]; |
|
166 | + } |
|
167 | + |
|
168 | + ///////////////////////////////////////////////////////////////////////////////////////////// |
|
169 | + |
|
170 | + /** |
|
171 | + * @var array |
|
172 | + */ |
|
173 | + protected static $instances = []; |
|
174 | + |
|
175 | + /** |
|
176 | + * Return a named instance of EnvironmentCheckSuite. |
|
177 | + * |
|
178 | + * @param string $name |
|
179 | + * |
|
180 | + * @return EnvironmentCheckSuite |
|
181 | + */ |
|
182 | + public static function inst($name) |
|
183 | + { |
|
184 | + if (!isset(self::$instances[$name])) { |
|
185 | + self::$instances[$name] = new EnvironmentCheckSuite($name); |
|
186 | + } |
|
187 | + return self::$instances[$name]; |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Register a check against the named check suite. |
|
192 | + * |
|
193 | + * @param string|array $names |
|
194 | + * @param EnvironmentCheck $check |
|
195 | + * @param string|array |
|
196 | + */ |
|
197 | + public static function register($names, $check, $title = null) |
|
198 | + { |
|
199 | + if (!is_array($names)) { |
|
200 | + $names = [$names]; |
|
201 | + } |
|
202 | + |
|
203 | + foreach ($names as $name) { |
|
204 | + self::inst($name)->push($check, $title); |
|
205 | + } |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * Unregisters all checks. |
|
210 | + */ |
|
211 | + public static function reset() |
|
212 | + { |
|
213 | + self::$instances = []; |
|
214 | + } |
|
215 | 215 | } |
@@ -14,39 +14,39 @@ |
||
14 | 14 | */ |
15 | 15 | class DevCheckController extends Controller |
16 | 16 | { |
17 | - /** |
|
18 | - * @var array |
|
19 | - */ |
|
20 | - private static $allowed_actions = [ |
|
21 | - 'index' |
|
22 | - ]; |
|
23 | - |
|
24 | - /** |
|
25 | - * Permission code to check for access to this controller. |
|
26 | - * |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - private static $permission = 'ADMIN'; |
|
30 | - |
|
31 | - /** |
|
32 | - * @param HTTPRequest $request |
|
33 | - * |
|
34 | - * @return EnvironmentChecker |
|
35 | - * |
|
36 | - * @throws HTTPResponse_Exception |
|
37 | - */ |
|
38 | - public function index($request) |
|
39 | - { |
|
40 | - $suite = 'check'; |
|
41 | - |
|
42 | - if ($name = $request->param('Suite')) { |
|
43 | - $suite = $name; |
|
44 | - } |
|
45 | - |
|
46 | - $checker = new EnvironmentChecker($suite, 'Environment status'); |
|
47 | - $checker->setRequest($request); |
|
48 | - $checker->init($this->config()->permission); |
|
49 | - |
|
50 | - return $checker; |
|
51 | - } |
|
17 | + /** |
|
18 | + * @var array |
|
19 | + */ |
|
20 | + private static $allowed_actions = [ |
|
21 | + 'index' |
|
22 | + ]; |
|
23 | + |
|
24 | + /** |
|
25 | + * Permission code to check for access to this controller. |
|
26 | + * |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + private static $permission = 'ADMIN'; |
|
30 | + |
|
31 | + /** |
|
32 | + * @param HTTPRequest $request |
|
33 | + * |
|
34 | + * @return EnvironmentChecker |
|
35 | + * |
|
36 | + * @throws HTTPResponse_Exception |
|
37 | + */ |
|
38 | + public function index($request) |
|
39 | + { |
|
40 | + $suite = 'check'; |
|
41 | + |
|
42 | + if ($name = $request->param('Suite')) { |
|
43 | + $suite = $name; |
|
44 | + } |
|
45 | + |
|
46 | + $checker = new EnvironmentChecker($suite, 'Environment status'); |
|
47 | + $checker->setRequest($request); |
|
48 | + $checker->init($this->config()->permission); |
|
49 | + |
|
50 | + return $checker; |
|
51 | + } |
|
52 | 52 | } |
@@ -23,279 +23,279 @@ |
||
23 | 23 | */ |
24 | 24 | class EnvironmentChecker extends RequestHandler |
25 | 25 | { |
26 | - /** |
|
27 | - * @var array |
|
28 | - */ |
|
29 | - private static $url_handlers = [ |
|
30 | - '' => 'index', |
|
31 | - ]; |
|
32 | - |
|
33 | - /** |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - protected $checkSuiteName; |
|
37 | - |
|
38 | - /** |
|
39 | - * @var string |
|
40 | - */ |
|
41 | - protected $title; |
|
42 | - |
|
43 | - /** |
|
44 | - * @var int |
|
45 | - */ |
|
46 | - protected $errorCode = 500; |
|
47 | - |
|
48 | - /** |
|
49 | - * @var null|string |
|
50 | - */ |
|
51 | - private static $to_email_address = null; |
|
52 | - |
|
53 | - /** |
|
54 | - * @var null|string |
|
55 | - */ |
|
56 | - private static $from_email_address = null; |
|
57 | - |
|
58 | - /** |
|
59 | - * @var bool |
|
60 | - */ |
|
61 | - private static $email_results = false; |
|
62 | - |
|
63 | - /** |
|
64 | - * @var bool Log results via {@link \Psr\Log\LoggerInterface} |
|
65 | - */ |
|
66 | - private static $log_results_warning = false; |
|
67 | - |
|
68 | - /** |
|
69 | - * @var string Maps to {@link \Psr\Log\LogLevel} levels. Defaults to LogLevel::WARNING |
|
70 | - */ |
|
71 | - private static $log_results_warning_level = LogLevel::WARNING; |
|
72 | - |
|
73 | - /** |
|
74 | - * @var bool Log results via a {@link \Psr\Log\LoggerInterface} |
|
75 | - */ |
|
76 | - private static $log_results_error = false; |
|
77 | - |
|
78 | - /** |
|
79 | - * @var int Maps to {@link \Psr\Log\LogLevel} levels. Defaults to LogLevel::ALERT |
|
80 | - */ |
|
81 | - private static $log_results_error_level = LogLevel::ALERT; |
|
82 | - |
|
83 | - /** |
|
84 | - * @param string $checkSuiteName |
|
85 | - * @param string $title |
|
86 | - */ |
|
87 | - public function __construct($checkSuiteName, $title) |
|
88 | - { |
|
89 | - parent::__construct(); |
|
90 | - |
|
91 | - $this->checkSuiteName = $checkSuiteName; |
|
92 | - $this->title = $title; |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * @param string $permission |
|
97 | - * |
|
98 | - * @throws HTTPResponse_Exception |
|
99 | - */ |
|
100 | - public function init($permission = 'ADMIN') |
|
101 | - { |
|
102 | - // if the environment supports it, provide a basic auth challenge and see if it matches configured credentials |
|
103 | - if (Environment::getEnv('ENVCHECK_BASICAUTH_USERNAME') |
|
104 | - && Environment::getEnv('ENVCHECK_BASICAUTH_PASSWORD') |
|
105 | - ) { |
|
106 | - // Check that details are both provided, and match |
|
107 | - if (empty($_SERVER['PHP_AUTH_USER']) || empty($_SERVER['PHP_AUTH_PW']) |
|
108 | - || $_SERVER['PHP_AUTH_USER'] != Environment::getEnv('ENVCHECK_BASICAUTH_USERNAME') |
|
109 | - || $_SERVER['PHP_AUTH_PW'] != Environment::getEnv('ENVCHECK_BASICAUTH_PASSWORD') |
|
110 | - ) { |
|
111 | - // Fail check with basic auth challenge |
|
112 | - $response = new HTTPResponse(null, 401); |
|
113 | - $response->addHeader('WWW-Authenticate', "Basic realm=\"Environment check\""); |
|
114 | - throw new HTTPResponse_Exception($response); |
|
115 | - } |
|
116 | - } elseif (!$this->canAccess(null, $permission)) { |
|
117 | - // Fail check with silverstripe login challenge |
|
118 | - $result = Security::permissionFailure( |
|
119 | - $this, |
|
120 | - "You must have the {$permission} permission to access this check" |
|
121 | - ); |
|
122 | - throw new HTTPResponse_Exception($result); |
|
123 | - } |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Determine if the current member can access the environment checker |
|
128 | - * |
|
129 | - * @param null|int|Member $member |
|
130 | - * @param string $permission |
|
131 | - * @return bool |
|
132 | - */ |
|
133 | - public function canAccess($member = null, $permission = 'ADMIN') |
|
134 | - { |
|
135 | - if (!$member) { |
|
136 | - $member = Security::getCurrentUser(); |
|
137 | - } |
|
138 | - |
|
139 | - // We allow access to this controller regardless of live-status or ADMIN permission only |
|
140 | - // if on CLI. Access to this controller is always allowed in "dev-mode", or of the user is ADMIN. |
|
141 | - if (Director::isDev() |
|
142 | - || Director::is_cli() |
|
143 | - || empty($permission) |
|
144 | - || Permission::checkMember($member, $permission) |
|
145 | - ) { |
|
146 | - return true; |
|
147 | - } |
|
148 | - |
|
149 | - // Extended access checks. |
|
150 | - // "Veto" style, return NULL to abstain vote. |
|
151 | - $canExtended = null; |
|
152 | - $results = $this->extend('canAccess', $member); |
|
153 | - if ($results && is_array($results)) { |
|
154 | - if (!min($results)) { |
|
155 | - return false; |
|
156 | - } |
|
157 | - return true; |
|
158 | - } |
|
159 | - |
|
160 | - return false; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * @return HTTPResponse |
|
165 | - */ |
|
166 | - public function index() |
|
167 | - { |
|
168 | - $response = new HTTPResponse; |
|
169 | - $result = EnvironmentCheckSuite::inst($this->checkSuiteName)->run(); |
|
170 | - |
|
171 | - if (!$result->ShouldPass()) { |
|
172 | - $response->setStatusCode($this->errorCode); |
|
173 | - } |
|
174 | - |
|
175 | - $resultText = $result->customise([ |
|
176 | - 'URL' => Director::absoluteBaseURL(), |
|
177 | - 'Title' => $this->title, |
|
178 | - 'Name' => $this->checkSuiteName, |
|
179 | - 'ErrorCode' => $this->errorCode, |
|
180 | - ])->renderWith(__CLASS__); |
|
181 | - |
|
182 | - if ($this->config()->get('email_results') && !$result->ShouldPass()) { |
|
183 | - $email = new Email( |
|
184 | - $this->config()->get('from_email_address'), |
|
185 | - $this->config()->get('to_email_address'), |
|
186 | - $this->title, |
|
187 | - $resultText |
|
188 | - ); |
|
189 | - $email->send(); |
|
190 | - } |
|
191 | - |
|
192 | - // Optionally log errors and warnings individually |
|
193 | - foreach ($result->Details() as $detail) { |
|
194 | - if ($this->config()->get('log_results_warning') && $detail->StatusCode == EnvironmentCheck::WARNING) { |
|
195 | - $this->log( |
|
196 | - sprintf('EnvironmentChecker warning at "%s" check. Message: %s', $detail->Check, $detail->Message), |
|
197 | - $this->config()->get('log_results_warning_level') |
|
198 | - ); |
|
199 | - } elseif ($this->config()->get('log_results_error') && $detail->StatusCode == EnvironmentCheck::ERROR) { |
|
200 | - $this->log( |
|
201 | - sprintf('EnvironmentChecker error at "%s" check. Message: %s', $detail->Check, $detail->Message), |
|
202 | - $this->config()->get('log_results_error_level') |
|
203 | - ); |
|
204 | - } |
|
205 | - } |
|
206 | - |
|
207 | - // output the result as JSON if requested |
|
208 | - if ($this->getRequest()->getExtension() == 'json' |
|
209 | - || strpos($this->getRequest()->getHeader('Accept'), 'application/json') !== false |
|
210 | - ) { |
|
211 | - $response->setBody($result->toJSON()); |
|
212 | - $response->addHeader('Content-Type', 'application/json'); |
|
213 | - return $response; |
|
214 | - } |
|
215 | - |
|
216 | - $response->setBody($resultText); |
|
217 | - |
|
218 | - return $response; |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Sends a log entry to the configured PSR-3 LoggerInterface |
|
223 | - * |
|
224 | - * @param string $message |
|
225 | - * @param int $level |
|
226 | - */ |
|
227 | - public function log($message, $level) |
|
228 | - { |
|
229 | - Injector::inst()->get(LoggerInterface::class)->log($level, $message); |
|
230 | - } |
|
231 | - |
|
232 | - /** |
|
233 | - * Set the HTTP status code that should be returned when there's an error. |
|
234 | - * |
|
235 | - * @param int $errorCode |
|
236 | - */ |
|
237 | - public function setErrorCode($errorCode) |
|
238 | - { |
|
239 | - $this->errorCode = $errorCode; |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * @deprecated |
|
244 | - * @param string $from |
|
245 | - */ |
|
246 | - public static function set_from_email_address($from) |
|
247 | - { |
|
248 | - Deprecation::notice('2.0', 'Use config API instead'); |
|
249 | - static::config()->set('from_email_address', $from); |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * @deprecated |
|
254 | - * @return null|string |
|
255 | - */ |
|
256 | - public static function get_from_email_address() |
|
257 | - { |
|
258 | - Deprecation::notice('2.0', 'Use config API instead'); |
|
259 | - return static::config()->get('from_email_address'); |
|
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * @deprecated |
|
264 | - * @param string $to |
|
265 | - */ |
|
266 | - public static function set_to_email_address($to) |
|
267 | - { |
|
268 | - Deprecation::notice('2.0', 'Use config API instead'); |
|
269 | - static::config()->set('to_email_address', $to); |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * @deprecated |
|
274 | - * @return null|string |
|
275 | - */ |
|
276 | - public static function get_to_email_address() |
|
277 | - { |
|
278 | - Deprecation::notice('2.0', 'Use config API instead'); |
|
279 | - return static::config()->get('to_email_address'); |
|
280 | - } |
|
281 | - |
|
282 | - /** |
|
283 | - * @deprecated |
|
284 | - * @param bool $results |
|
285 | - */ |
|
286 | - public static function set_email_results($results) |
|
287 | - { |
|
288 | - Deprecation::notice('2.0', 'Use config API instead'); |
|
289 | - static::config()->set('email_results', $results); |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * @deprecated |
|
294 | - * @return bool |
|
295 | - */ |
|
296 | - public static function get_email_results() |
|
297 | - { |
|
298 | - Deprecation::notice('2.0', 'Use config API instead'); |
|
299 | - return static::config()->get('email_results'); |
|
300 | - } |
|
26 | + /** |
|
27 | + * @var array |
|
28 | + */ |
|
29 | + private static $url_handlers = [ |
|
30 | + '' => 'index', |
|
31 | + ]; |
|
32 | + |
|
33 | + /** |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + protected $checkSuiteName; |
|
37 | + |
|
38 | + /** |
|
39 | + * @var string |
|
40 | + */ |
|
41 | + protected $title; |
|
42 | + |
|
43 | + /** |
|
44 | + * @var int |
|
45 | + */ |
|
46 | + protected $errorCode = 500; |
|
47 | + |
|
48 | + /** |
|
49 | + * @var null|string |
|
50 | + */ |
|
51 | + private static $to_email_address = null; |
|
52 | + |
|
53 | + /** |
|
54 | + * @var null|string |
|
55 | + */ |
|
56 | + private static $from_email_address = null; |
|
57 | + |
|
58 | + /** |
|
59 | + * @var bool |
|
60 | + */ |
|
61 | + private static $email_results = false; |
|
62 | + |
|
63 | + /** |
|
64 | + * @var bool Log results via {@link \Psr\Log\LoggerInterface} |
|
65 | + */ |
|
66 | + private static $log_results_warning = false; |
|
67 | + |
|
68 | + /** |
|
69 | + * @var string Maps to {@link \Psr\Log\LogLevel} levels. Defaults to LogLevel::WARNING |
|
70 | + */ |
|
71 | + private static $log_results_warning_level = LogLevel::WARNING; |
|
72 | + |
|
73 | + /** |
|
74 | + * @var bool Log results via a {@link \Psr\Log\LoggerInterface} |
|
75 | + */ |
|
76 | + private static $log_results_error = false; |
|
77 | + |
|
78 | + /** |
|
79 | + * @var int Maps to {@link \Psr\Log\LogLevel} levels. Defaults to LogLevel::ALERT |
|
80 | + */ |
|
81 | + private static $log_results_error_level = LogLevel::ALERT; |
|
82 | + |
|
83 | + /** |
|
84 | + * @param string $checkSuiteName |
|
85 | + * @param string $title |
|
86 | + */ |
|
87 | + public function __construct($checkSuiteName, $title) |
|
88 | + { |
|
89 | + parent::__construct(); |
|
90 | + |
|
91 | + $this->checkSuiteName = $checkSuiteName; |
|
92 | + $this->title = $title; |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * @param string $permission |
|
97 | + * |
|
98 | + * @throws HTTPResponse_Exception |
|
99 | + */ |
|
100 | + public function init($permission = 'ADMIN') |
|
101 | + { |
|
102 | + // if the environment supports it, provide a basic auth challenge and see if it matches configured credentials |
|
103 | + if (Environment::getEnv('ENVCHECK_BASICAUTH_USERNAME') |
|
104 | + && Environment::getEnv('ENVCHECK_BASICAUTH_PASSWORD') |
|
105 | + ) { |
|
106 | + // Check that details are both provided, and match |
|
107 | + if (empty($_SERVER['PHP_AUTH_USER']) || empty($_SERVER['PHP_AUTH_PW']) |
|
108 | + || $_SERVER['PHP_AUTH_USER'] != Environment::getEnv('ENVCHECK_BASICAUTH_USERNAME') |
|
109 | + || $_SERVER['PHP_AUTH_PW'] != Environment::getEnv('ENVCHECK_BASICAUTH_PASSWORD') |
|
110 | + ) { |
|
111 | + // Fail check with basic auth challenge |
|
112 | + $response = new HTTPResponse(null, 401); |
|
113 | + $response->addHeader('WWW-Authenticate', "Basic realm=\"Environment check\""); |
|
114 | + throw new HTTPResponse_Exception($response); |
|
115 | + } |
|
116 | + } elseif (!$this->canAccess(null, $permission)) { |
|
117 | + // Fail check with silverstripe login challenge |
|
118 | + $result = Security::permissionFailure( |
|
119 | + $this, |
|
120 | + "You must have the {$permission} permission to access this check" |
|
121 | + ); |
|
122 | + throw new HTTPResponse_Exception($result); |
|
123 | + } |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Determine if the current member can access the environment checker |
|
128 | + * |
|
129 | + * @param null|int|Member $member |
|
130 | + * @param string $permission |
|
131 | + * @return bool |
|
132 | + */ |
|
133 | + public function canAccess($member = null, $permission = 'ADMIN') |
|
134 | + { |
|
135 | + if (!$member) { |
|
136 | + $member = Security::getCurrentUser(); |
|
137 | + } |
|
138 | + |
|
139 | + // We allow access to this controller regardless of live-status or ADMIN permission only |
|
140 | + // if on CLI. Access to this controller is always allowed in "dev-mode", or of the user is ADMIN. |
|
141 | + if (Director::isDev() |
|
142 | + || Director::is_cli() |
|
143 | + || empty($permission) |
|
144 | + || Permission::checkMember($member, $permission) |
|
145 | + ) { |
|
146 | + return true; |
|
147 | + } |
|
148 | + |
|
149 | + // Extended access checks. |
|
150 | + // "Veto" style, return NULL to abstain vote. |
|
151 | + $canExtended = null; |
|
152 | + $results = $this->extend('canAccess', $member); |
|
153 | + if ($results && is_array($results)) { |
|
154 | + if (!min($results)) { |
|
155 | + return false; |
|
156 | + } |
|
157 | + return true; |
|
158 | + } |
|
159 | + |
|
160 | + return false; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * @return HTTPResponse |
|
165 | + */ |
|
166 | + public function index() |
|
167 | + { |
|
168 | + $response = new HTTPResponse; |
|
169 | + $result = EnvironmentCheckSuite::inst($this->checkSuiteName)->run(); |
|
170 | + |
|
171 | + if (!$result->ShouldPass()) { |
|
172 | + $response->setStatusCode($this->errorCode); |
|
173 | + } |
|
174 | + |
|
175 | + $resultText = $result->customise([ |
|
176 | + 'URL' => Director::absoluteBaseURL(), |
|
177 | + 'Title' => $this->title, |
|
178 | + 'Name' => $this->checkSuiteName, |
|
179 | + 'ErrorCode' => $this->errorCode, |
|
180 | + ])->renderWith(__CLASS__); |
|
181 | + |
|
182 | + if ($this->config()->get('email_results') && !$result->ShouldPass()) { |
|
183 | + $email = new Email( |
|
184 | + $this->config()->get('from_email_address'), |
|
185 | + $this->config()->get('to_email_address'), |
|
186 | + $this->title, |
|
187 | + $resultText |
|
188 | + ); |
|
189 | + $email->send(); |
|
190 | + } |
|
191 | + |
|
192 | + // Optionally log errors and warnings individually |
|
193 | + foreach ($result->Details() as $detail) { |
|
194 | + if ($this->config()->get('log_results_warning') && $detail->StatusCode == EnvironmentCheck::WARNING) { |
|
195 | + $this->log( |
|
196 | + sprintf('EnvironmentChecker warning at "%s" check. Message: %s', $detail->Check, $detail->Message), |
|
197 | + $this->config()->get('log_results_warning_level') |
|
198 | + ); |
|
199 | + } elseif ($this->config()->get('log_results_error') && $detail->StatusCode == EnvironmentCheck::ERROR) { |
|
200 | + $this->log( |
|
201 | + sprintf('EnvironmentChecker error at "%s" check. Message: %s', $detail->Check, $detail->Message), |
|
202 | + $this->config()->get('log_results_error_level') |
|
203 | + ); |
|
204 | + } |
|
205 | + } |
|
206 | + |
|
207 | + // output the result as JSON if requested |
|
208 | + if ($this->getRequest()->getExtension() == 'json' |
|
209 | + || strpos($this->getRequest()->getHeader('Accept'), 'application/json') !== false |
|
210 | + ) { |
|
211 | + $response->setBody($result->toJSON()); |
|
212 | + $response->addHeader('Content-Type', 'application/json'); |
|
213 | + return $response; |
|
214 | + } |
|
215 | + |
|
216 | + $response->setBody($resultText); |
|
217 | + |
|
218 | + return $response; |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Sends a log entry to the configured PSR-3 LoggerInterface |
|
223 | + * |
|
224 | + * @param string $message |
|
225 | + * @param int $level |
|
226 | + */ |
|
227 | + public function log($message, $level) |
|
228 | + { |
|
229 | + Injector::inst()->get(LoggerInterface::class)->log($level, $message); |
|
230 | + } |
|
231 | + |
|
232 | + /** |
|
233 | + * Set the HTTP status code that should be returned when there's an error. |
|
234 | + * |
|
235 | + * @param int $errorCode |
|
236 | + */ |
|
237 | + public function setErrorCode($errorCode) |
|
238 | + { |
|
239 | + $this->errorCode = $errorCode; |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * @deprecated |
|
244 | + * @param string $from |
|
245 | + */ |
|
246 | + public static function set_from_email_address($from) |
|
247 | + { |
|
248 | + Deprecation::notice('2.0', 'Use config API instead'); |
|
249 | + static::config()->set('from_email_address', $from); |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * @deprecated |
|
254 | + * @return null|string |
|
255 | + */ |
|
256 | + public static function get_from_email_address() |
|
257 | + { |
|
258 | + Deprecation::notice('2.0', 'Use config API instead'); |
|
259 | + return static::config()->get('from_email_address'); |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * @deprecated |
|
264 | + * @param string $to |
|
265 | + */ |
|
266 | + public static function set_to_email_address($to) |
|
267 | + { |
|
268 | + Deprecation::notice('2.0', 'Use config API instead'); |
|
269 | + static::config()->set('to_email_address', $to); |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * @deprecated |
|
274 | + * @return null|string |
|
275 | + */ |
|
276 | + public static function get_to_email_address() |
|
277 | + { |
|
278 | + Deprecation::notice('2.0', 'Use config API instead'); |
|
279 | + return static::config()->get('to_email_address'); |
|
280 | + } |
|
281 | + |
|
282 | + /** |
|
283 | + * @deprecated |
|
284 | + * @param bool $results |
|
285 | + */ |
|
286 | + public static function set_email_results($results) |
|
287 | + { |
|
288 | + Deprecation::notice('2.0', 'Use config API instead'); |
|
289 | + static::config()->set('email_results', $results); |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * @deprecated |
|
294 | + * @return bool |
|
295 | + */ |
|
296 | + public static function get_email_results() |
|
297 | + { |
|
298 | + Deprecation::notice('2.0', 'Use config API instead'); |
|
299 | + return static::config()->get('email_results'); |
|
300 | + } |
|
301 | 301 | } |
@@ -14,58 +14,58 @@ |
||
14 | 14 | */ |
15 | 15 | class SessionCheck implements EnvironmentCheck |
16 | 16 | { |
17 | - use Fetcher; |
|
17 | + use Fetcher; |
|
18 | 18 | |
19 | - /** |
|
20 | - * Set up check with URL |
|
21 | - * |
|
22 | - * @param string $url The route, excluding the domain |
|
23 | - * @inheritdoc |
|
24 | - */ |
|
25 | - public function __construct($url = '') |
|
26 | - { |
|
27 | - $this->setURL($url); |
|
28 | - } |
|
19 | + /** |
|
20 | + * Set up check with URL |
|
21 | + * |
|
22 | + * @param string $url The route, excluding the domain |
|
23 | + * @inheritdoc |
|
24 | + */ |
|
25 | + public function __construct($url = '') |
|
26 | + { |
|
27 | + $this->setURL($url); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * Check that the response for URL does not create a session |
|
32 | - * |
|
33 | - * @return array |
|
34 | - */ |
|
35 | - public function check() |
|
36 | - { |
|
37 | - $response = $this->client->get($this->getURL()); |
|
38 | - $cookie = $this->getCookie($response); |
|
39 | - $fullURL = $this->getURL(); |
|
30 | + /** |
|
31 | + * Check that the response for URL does not create a session |
|
32 | + * |
|
33 | + * @return array |
|
34 | + */ |
|
35 | + public function check() |
|
36 | + { |
|
37 | + $response = $this->client->get($this->getURL()); |
|
38 | + $cookie = $this->getCookie($response); |
|
39 | + $fullURL = $this->getURL(); |
|
40 | 40 | |
41 | - if ($cookie) { |
|
42 | - return [ |
|
43 | - EnvironmentCheck::ERROR, |
|
44 | - "Sessions are being set for {$fullURL} : Set-Cookie => " . $cookie, |
|
45 | - ]; |
|
46 | - } |
|
47 | - return [ |
|
48 | - EnvironmentCheck::OK, |
|
49 | - "Sessions are not being created for {$fullURL} |