Total Complexity | 48 |
Total Lines | 384 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like RFC7585Tests often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RFC7585Tests, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class RFC7585Tests extends AbstractTest |
||
39 | { |
||
40 | |||
41 | /** |
||
42 | * maintains state for the question: has the NAPTR existence check already been executed? Holds the number of NAPTR records found if so. |
||
43 | * |
||
44 | * @var integer |
||
45 | */ |
||
46 | private $NAPTR_executed; |
||
47 | |||
48 | /** |
||
49 | * maintains state for the question: has the NAPTR compliance check already been executed? |
||
50 | * |
||
51 | * @var integer |
||
52 | */ |
||
53 | private $NAPTR_compliance_executed; |
||
54 | |||
55 | /** |
||
56 | * maintains state for the question: has the NAPTR SRV check already been executed? Holds the number of SRV records if so. |
||
57 | * |
||
58 | * @var integer |
||
59 | */ |
||
60 | private $NAPTR_SRV_executed; |
||
61 | |||
62 | /** |
||
63 | * maintains state for the question: has the existrence of hostnames been checked already? Holds the number of IP:port pairs if so. |
||
64 | * |
||
65 | * @var integer |
||
66 | */ |
||
67 | private $NAPTR_hostname_executed; |
||
68 | |||
69 | /** |
||
70 | * holds the list of NAPTR records found. Exposed because we may want to |
||
71 | * double-check the replacement against NRO expectations |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | public $NAPTR_records; |
||
76 | |||
77 | /** |
||
78 | * holds the list of SRV records found |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | private $NAPTR_SRV_records; |
||
83 | |||
84 | /** |
||
85 | * stores the various errors encountered during the checks |
||
86 | * |
||
87 | * @var array |
||
88 | */ |
||
89 | private $errorlist; |
||
90 | |||
91 | /** |
||
92 | * stores the IP address / port pairs (strings) which were ultimately found |
||
93 | * as candidate RADIUS/TLS servers |
||
94 | * |
||
95 | * @var array |
||
96 | */ |
||
97 | public $NAPTR_hostname_records; |
||
98 | // return codes specific to NAPTR existence checks |
||
99 | |||
100 | /** |
||
101 | * test hasn't been run yet |
||
102 | */ |
||
103 | const RETVAL_NOTRUNYET = -1; |
||
104 | |||
105 | /** |
||
106 | * no NAPTRs for domain; this is not an error, simply means that realm is not doing dynamic discovery for any service |
||
107 | */ |
||
108 | const RETVAL_NONAPTR = -104; |
||
109 | |||
110 | /** |
||
111 | * no eduroam NAPTR for domain; this is not an error, simply means that realm is not doing dynamic discovery for eduroam |
||
112 | */ |
||
113 | const RETVAL_ONLYUNRELATEDNAPTR = -105; |
||
114 | |||
115 | /** |
||
116 | * This private variable contains the realm to be checked. Is filled in the |
||
117 | * class constructor. |
||
118 | * |
||
119 | * @var string |
||
120 | */ |
||
121 | private $realm; |
||
122 | |||
123 | /** |
||
124 | * An instance of the Net_DNS2 resolver to do lookups with |
||
125 | * |
||
126 | * @var \Net_DNS2_Resolver |
||
127 | */ |
||
128 | private $resolver; |
||
129 | |||
130 | /** |
||
131 | * maintains state whether all DNS responses were DNSSEC-secured |
||
132 | * |
||
133 | * @var bool |
||
134 | */ |
||
135 | public $allResponsesSecure; |
||
136 | |||
137 | /** |
||
138 | * the discovery tag to use for the DNS lookups |
||
139 | * |
||
140 | * @var string |
||
141 | */ |
||
142 | private $discoveryTag; |
||
143 | |||
144 | /** |
||
145 | * Initialises the dynamic discovery test instance for a specific realm that is to be tested |
||
146 | * |
||
147 | * @param string $realm the realm to be tested |
||
148 | */ |
||
149 | public function __construct(string $realm, $discoverytag = \config\Diagnostics::RADIUSTESTS['TLS-discoverytag']) |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Tests if this realm exists in DNS and has NAPTR records matching the |
||
187 | * configured consortium NAPTR target. |
||
188 | * |
||
189 | * possible RETVALs: |
||
190 | * - RETVAL_NOTCONFIGURED; needs \config\Diagnostics::RADIUSTESTS['TLS-discoverytag'] |
||
191 | * - RETVAL_ONLYUNRELATEDNAPTR |
||
192 | * - RETVAL_NONAPTR |
||
193 | * |
||
194 | * @return int Either a RETVAL constant or a positive number (count of relevant NAPTR records) |
||
195 | */ |
||
196 | public function relevantNAPTR() |
||
197 | { |
||
198 | if ($this->discoveryTag == "") { |
||
199 | $this->NAPTR_executed = RADIUSTests::RETVAL_NOTCONFIGURED; |
||
200 | return RADIUSTests::RETVAL_NOTCONFIGURED; |
||
201 | } |
||
202 | $NAPTRs = []; |
||
203 | try { |
||
204 | $response = $this->resolver->query($this->realm, 'NAPTR'); |
||
205 | $securedAnswer = $response->header->ad ?? 0; |
||
206 | if ($securedAnswer == 0) { |
||
207 | $this->allResponsesSecure = FALSE; |
||
208 | } |
||
209 | foreach ($response->answer as $oneAnswer) { |
||
210 | $NAPTRs[] = [ |
||
211 | 'services' => $oneAnswer->services, |
||
212 | 'flags' => $oneAnswer->flags, |
||
213 | 'regexp' => $oneAnswer->regexp, |
||
214 | 'replacement' => $oneAnswer->replacement, |
||
215 | 'ad' => $securedAnswer, |
||
216 | ]; |
||
217 | } |
||
218 | } catch (\Net_DNS2_Exception $e) { |
||
219 | // NXDOMAIN is an Exception, but we don't care - no result means no result |
||
220 | } |
||
221 | if (count($NAPTRs) == 0) { |
||
222 | $this->NAPTR_executed = RFC7585Tests::RETVAL_NONAPTR; |
||
223 | return RFC7585Tests::RETVAL_NONAPTR; |
||
224 | } |
||
225 | $NAPTRs_consortium = []; |
||
226 | foreach ($NAPTRs as $naptr) { |
||
227 | if ($naptr["services"] == $this->discoveryTag) { |
||
228 | $NAPTRs_consortium[] = $naptr; |
||
229 | } |
||
230 | } |
||
231 | if (count($NAPTRs_consortium) == 0) { |
||
232 | $this->NAPTR_executed = RFC7585Tests::RETVAL_ONLYUNRELATEDNAPTR; |
||
233 | return RFC7585Tests::RETVAL_ONLYUNRELATEDNAPTR; |
||
234 | } |
||
235 | $this->NAPTR_records = $NAPTRs_consortium; |
||
236 | $this->NAPTR_executed = count($NAPTRs_consortium); |
||
237 | return count($NAPTRs_consortium); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Tests if all the discovered NAPTR entries conform to the consortium's requirements |
||
242 | * |
||
243 | * possible RETVALs: |
||
244 | * - RETVAL_NOTCONFIGURED; needs \config\Diagnostics::RADIUSTESTS['TLS-discoverytag'] |
||
245 | * - RETVAL_INVALID (at least one format error) |
||
246 | * - RETVAL_OK (all fine) |
||
247 | |||
248 | * @return int one of two RETVALs above |
||
249 | */ |
||
250 | public function relevantNAPTRcompliance() |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Tests if NAPTR records can be resolved to SRVs. Will only run if NAPTR |
||
292 | * checks completed without error. |
||
293 | * |
||
294 | * possible RETVALs: |
||
295 | * - RETVAL_INVALID |
||
296 | * - RETVAL_SKIPPED |
||
297 | * |
||
298 | * @return int one of the RETVALs above or the number of SRV records which were resolved |
||
299 | */ |
||
300 | public function relevantNAPTRsrvResolution() |
||
301 | { |
||
302 | // see if preceding checks have been run, and run them if not |
||
303 | // compliance check will cascade NAPTR check on its own |
||
304 | if ($this->NAPTR_compliance_executed == RFC7585Tests::RETVAL_NOTRUNYET) { |
||
305 | $this->relevantNAPTRcompliance(); |
||
306 | } |
||
307 | // we only run the SRV checks if all records are compliant and more than one relevant NAPTR exists |
||
308 | if ($this->NAPTR_executed <= 0 || $this->NAPTR_compliance_executed == RADIUSTests::RETVAL_INVALID) { |
||
309 | $this->NAPTR_SRV_executed = RADIUSTests::RETVAL_SKIPPED; |
||
310 | return RADIUSTests::RETVAL_SKIPPED; |
||
311 | } |
||
312 | $sRVerrors = []; |
||
313 | $sRVtargets = []; |
||
314 | $sRVcount = 0; |
||
315 | |||
316 | foreach ($this->NAPTR_records as $edupointer) { |
||
317 | try { |
||
318 | $response = $this->resolver->query($edupointer["replacement"], 'SRV'); |
||
319 | $securedAnswer = $response->header->ad ?? 0; |
||
320 | if ($securedAnswer == 0) { |
||
321 | $this->allResponsesSecure = FALSE; |
||
322 | } |
||
323 | foreach ($response->answer as $oneAnswer) { |
||
324 | $sRVtargets[] = [ |
||
325 | 'hostname' => $oneAnswer->target, |
||
326 | 'port' => $oneAnswer->port, |
||
327 | 'ad' => $securedAnswer, |
||
328 | ]; |
||
329 | } |
||
330 | } catch (\Net_DNS2_Exception $e) { |
||
331 | // NXDOMAIN is an Exception, but we don't care - no result means no result |
||
332 | } |
||
333 | if (count($sRVtargets) == $sRVcount) { // no new target added... defunct replacement |
||
334 | $sRVerrors[] = ["TYPE" => "SRV_NOT_RESOLVING", "TARGET" => $edupointer['replacement']]; |
||
335 | } |
||
336 | $sRVcount = count($sRVtargets); |
||
337 | } |
||
338 | $this->NAPTR_SRV_records = $sRVtargets; |
||
339 | if (count($sRVerrors) > 0) { |
||
340 | $this->NAPTR_SRV_executed = RADIUSTests::RETVAL_INVALID; |
||
341 | $this->errorlist = array_merge($this->errorlist, $sRVerrors); |
||
342 | return RADIUSTests::RETVAL_INVALID; |
||
343 | } |
||
344 | $this->NAPTR_SRV_executed = count($sRVtargets); |
||
345 | return count($sRVtargets); |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Checks whether the previously discovered hostnames have actual IP addresses in DNS. |
||
350 | * |
||
351 | * The actual list is stored in the class property NAPTR_hostname_records. |
||
352 | * |
||
353 | * @return int count of IP / port pairs for all the hostnames |
||
354 | */ |
||
355 | public function relevantNAPTRhostnameResolution() |
||
422 | } |
||
423 | } |
||
424 |