| 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-2020 Greenbone Networks GmbH |
||
| 182 | @classmethod |
||
| 183 | def process_target_element(cls, scanner_target: Element) -> Dict: |
||
| 184 | """Receive an XML object with the target, ports and credentials to run |
||
| 185 | a scan against. |
||
| 186 | |||
| 187 | Arguments: |
||
| 188 | Single XML target element. The target has <hosts> and <ports> |
||
| 189 | subelements. Hosts can be a single host, a host range, a |
||
| 190 | comma-separated host list or a network address. |
||
| 191 | <ports> and <credentials> are optional. Therefore each |
||
| 192 | ospd-scanner should check for a valid ones if needed. |
||
| 193 | |||
| 194 | Example form: |
||
| 195 | |||
| 196 | <target> |
||
| 197 | <hosts>192.168.0.0/24</hosts> |
||
| 198 | <ports>22</ports> |
||
| 199 | <credentials> |
||
| 200 | <credential type="up" service="ssh" port="22"> |
||
| 201 | <username>scanuser</username> |
||
| 202 | <password>mypass</password> |
||
| 203 | </credential> |
||
| 204 | <credential type="up" service="smb"> |
||
| 205 | <username>smbuser</username> |
||
| 206 | <password>mypass</password> |
||
| 207 | </credential> |
||
| 208 | </credentials> |
||
| 209 | <alive_test></alive_test> |
||
| 210 | <alive_test_ports></alive_test_ports> |
||
| 211 | <reverse_lookup_only>1</reverse_lookup_only> |
||
| 212 | <reverse_lookup_unify>0</reverse_lookup_unify> |
||
| 213 | </target> |
||
| 214 | |||
| 215 | Return: |
||
| 216 | A Dict hosts, port, {credentials}, exclude_hosts, options]. |
||
| 217 | |||
| 218 | Example form: |
||
| 219 | |||
| 220 | { |
||
| 221 | 'hosts': '192.168.0.0/24', |
||
| 222 | 'port': '22', |
||
| 223 | 'credentials': {'smb': {'type': type, |
||
| 224 | 'port': port, |
||
| 225 | 'username': username, |
||
| 226 | 'password': pass, |
||
| 227 | } |
||
| 228 | }, |
||
| 229 | |||
| 230 | 'exclude_hosts': '', |
||
| 231 | 'finished_hosts': '', |
||
| 232 | 'options': {'alive_test': 'ALIVE_TEST_CONSIDER_ALIVE', |
||
| 233 | 'alive_test_ports: '22,80,123', |
||
| 234 | 'reverse_lookup_only': '1', |
||
| 235 | 'reverse_lookup_unify': '0', |
||
| 236 | }, |
||
| 237 | } |
||
| 238 | """ |
||
| 239 | if scanner_target: |
||
| 240 | exclude_hosts = '' |
||
| 241 | finished_hosts = '' |
||
| 242 | ports = '' |
||
| 243 | hosts = None |
||
| 244 | credentials = {} # type: Dict |
||
| 245 | options = {} |
||
| 246 | |||
| 247 | for child in scanner_target: |
||
| 248 | if child.tag == 'hosts': |
||
| 249 | hosts = child.text |
||
| 250 | if child.tag == 'exclude_hosts': |
||
| 251 | exclude_hosts = child.text |
||
| 252 | if child.tag == 'finished_hosts': |
||
| 253 | finished_hosts = child.text |
||
| 254 | if child.tag == 'ports': |
||
| 255 | ports = child.text |
||
| 256 | if child.tag == 'credentials': |
||
| 257 | credentials = cls.process_credentials_elements(child) |
||
| 258 | if child.tag == 'alive_test_methods': |
||
| 259 | options['alive_test_methods'] = '1' |
||
| 260 | cls.process_alive_test_methods(child, options) |
||
| 261 | if child.tag == 'alive_test': |
||
| 262 | options['alive_test'] = child.text |
||
| 263 | if child.tag == 'alive_test_ports': |
||
| 264 | options['alive_test_ports'] = child.text |
||
| 265 | if child.tag == 'reverse_lookup_unify': |
||
| 266 | options['reverse_lookup_unify'] = child.text |
||
| 267 | if child.tag == 'reverse_lookup_only': |
||
| 268 | options['reverse_lookup_only'] = child.text |
||
| 269 | |||
| 270 | if hosts: |
||
| 271 | return { |
||
| 272 | 'hosts': hosts, |
||
| 273 | 'ports': ports, |
||
| 274 | 'credentials': credentials, |
||
| 275 | 'exclude_hosts': exclude_hosts, |
||
| 276 | 'finished_hosts': finished_hosts, |
||
| 277 | 'options': options, |
||
| 278 | } |
||
| 279 | else: |
||
| 280 | raise OspdError('No target to scan') |
||
| 281 | |||
| 305 |