Conditions | 14 |
Total Lines | 99 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like ospd.protocol.OspRequest.process_target_element() 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.
1 | # Copyright (C) 2014-2021 Greenbone Networks GmbH |
||
184 | @classmethod |
||
185 | def process_target_element(cls, scanner_target: Element) -> Dict: |
||
186 | """Receive an XML object with the target, ports and credentials to run |
||
187 | a scan against. |
||
188 | |||
189 | Arguments: |
||
190 | Single XML target element. The target has <hosts> and <ports> |
||
191 | subelements. Hosts can be a single host, a host range, a |
||
192 | comma-separated host list or a network address. |
||
193 | <ports> and <credentials> are optional. Therefore each |
||
194 | ospd-scanner should check for a valid ones if needed. |
||
195 | |||
196 | Example form: |
||
197 | |||
198 | <target> |
||
199 | <hosts>192.168.0.0/24</hosts> |
||
200 | <ports>22</ports> |
||
201 | <credentials> |
||
202 | <credential type="up" service="ssh" port="22"> |
||
203 | <username>scanuser</username> |
||
204 | <password>mypass</password> |
||
205 | </credential> |
||
206 | <credential type="up" service="smb"> |
||
207 | <username>smbuser</username> |
||
208 | <password>mypass</password> |
||
209 | </credential> |
||
210 | </credentials> |
||
211 | <alive_test></alive_test> |
||
212 | <alive_test_ports></alive_test_ports> |
||
213 | <reverse_lookup_only>1</reverse_lookup_only> |
||
214 | <reverse_lookup_unify>0</reverse_lookup_unify> |
||
215 | </target> |
||
216 | |||
217 | Return: |
||
218 | A Dict hosts, port, {credentials}, exclude_hosts, options]. |
||
219 | |||
220 | Example form: |
||
221 | |||
222 | { |
||
223 | 'hosts': '192.168.0.0/24', |
||
224 | 'port': '22', |
||
225 | 'credentials': {'smb': {'type': type, |
||
226 | 'port': port, |
||
227 | 'username': username, |
||
228 | 'password': pass, |
||
229 | } |
||
230 | }, |
||
231 | |||
232 | 'exclude_hosts': '', |
||
233 | 'finished_hosts': '', |
||
234 | 'options': {'alive_test': 'ALIVE_TEST_CONSIDER_ALIVE', |
||
235 | 'alive_test_ports: '22,80,123', |
||
236 | 'reverse_lookup_only': '1', |
||
237 | 'reverse_lookup_unify': '0', |
||
238 | }, |
||
239 | } |
||
240 | """ |
||
241 | if scanner_target: |
||
242 | exclude_hosts = '' |
||
243 | finished_hosts = '' |
||
244 | ports = '' |
||
245 | hosts = None |
||
246 | credentials = {} # type: Dict |
||
247 | options = {} |
||
248 | |||
249 | for child in scanner_target: |
||
250 | if child.tag == 'hosts': |
||
251 | hosts = child.text |
||
252 | if child.tag == 'exclude_hosts': |
||
253 | exclude_hosts = child.text |
||
254 | if child.tag == 'finished_hosts': |
||
255 | finished_hosts = child.text |
||
256 | if child.tag == 'ports': |
||
257 | ports = child.text |
||
258 | if child.tag == 'credentials': |
||
259 | credentials = cls.process_credentials_elements(child) |
||
260 | if child.tag == 'alive_test_methods': |
||
261 | options['alive_test_methods'] = '1' |
||
262 | cls.process_alive_test_methods(child, options) |
||
263 | if child.tag == 'alive_test': |
||
264 | options['alive_test'] = child.text |
||
265 | if child.tag == 'alive_test_ports': |
||
266 | options['alive_test_ports'] = child.text |
||
267 | if child.tag == 'reverse_lookup_unify': |
||
268 | options['reverse_lookup_unify'] = child.text |
||
269 | if child.tag == 'reverse_lookup_only': |
||
270 | options['reverse_lookup_only'] = child.text |
||
271 | |||
272 | if hosts: |
||
273 | return { |
||
274 | 'hosts': hosts, |
||
275 | 'ports': ports, |
||
276 | 'credentials': credentials, |
||
277 | 'exclude_hosts': exclude_hosts, |
||
278 | 'finished_hosts': finished_hosts, |
||
279 | 'options': options, |
||
280 | } |
||
281 | else: |
||
282 | raise OspdError('No target to scan') |
||
283 | |||
307 |