| Conditions | 12 |
| Total Lines | 92 |
| Code Lines | 35 |
| 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) 2020 Greenbone Networks GmbH |
||
| 150 | @classmethod |
||
| 151 | def process_target_element(cls, scanner_target: Element) -> Dict: |
||
| 152 | """ Receive an XML object with the target, ports and credentials to run |
||
| 153 | a scan against. |
||
| 154 | |||
| 155 | Arguments: |
||
| 156 | Single XML target element. The target has <hosts> and <ports> |
||
| 157 | subelements. Hosts can be a single host, a host range, a |
||
| 158 | comma-separated host list or a network address. |
||
| 159 | <ports> and <credentials> are optional. Therefore each |
||
| 160 | ospd-scanner should check for a valid ones if needed. |
||
| 161 | |||
| 162 | Example form: |
||
| 163 | |||
| 164 | <target> |
||
| 165 | <hosts>192.168.0.0/24</hosts> |
||
| 166 | <ports>22</ports> |
||
| 167 | <credentials> |
||
| 168 | <credential type="up" service="ssh" port="22"> |
||
| 169 | <username>scanuser</username> |
||
| 170 | <password>mypass</password> |
||
| 171 | </credential> |
||
| 172 | <credential type="up" service="smb"> |
||
| 173 | <username>smbuser</username> |
||
| 174 | <password>mypass</password> |
||
| 175 | </credential> |
||
| 176 | </credentials> |
||
| 177 | <alive_test></alive_test> |
||
| 178 | <reverse_lookup_only>1</reverse_lookup_only> |
||
| 179 | <reverse_lookup_unify>0</reverse_lookup_unify> |
||
| 180 | </target> |
||
| 181 | |||
| 182 | Return: |
||
| 183 | A Dict hosts, port, {credentials}, exclude_hosts, options]. |
||
| 184 | |||
| 185 | Example form: |
||
| 186 | |||
| 187 | { |
||
| 188 | 'hosts': '192.168.0.0/24', |
||
| 189 | 'port': '22', |
||
| 190 | 'credentials': {'smb': {'type': type, |
||
| 191 | 'port': port, |
||
| 192 | 'username': username, |
||
| 193 | 'password': pass, |
||
| 194 | } |
||
| 195 | }, |
||
| 196 | |||
| 197 | 'exclude_hosts': '', |
||
| 198 | 'finished_hosts': '', |
||
| 199 | 'options': {'alive_test': 'ALIVE_TEST_CONSIDER_ALIVE', |
||
| 200 | 'reverse_lookup_only': '1', |
||
| 201 | 'reverse_lookup_unify': '0', |
||
| 202 | }, |
||
| 203 | } |
||
| 204 | """ |
||
| 205 | if scanner_target: |
||
| 206 | exclude_hosts = '' |
||
| 207 | finished_hosts = '' |
||
| 208 | ports = '' |
||
| 209 | hosts = None |
||
| 210 | credentials = {} # type: Dict |
||
| 211 | options = {} |
||
| 212 | |||
| 213 | for child in scanner_target: |
||
| 214 | if child.tag == 'hosts': |
||
| 215 | hosts = child.text |
||
| 216 | if child.tag == 'exclude_hosts': |
||
| 217 | exclude_hosts = child.text |
||
| 218 | if child.tag == 'finished_hosts': |
||
| 219 | finished_hosts = child.text |
||
| 220 | if child.tag == 'ports': |
||
| 221 | ports = child.text |
||
| 222 | if child.tag == 'credentials': |
||
| 223 | credentials = cls.process_credentials_elements(child) |
||
| 224 | if child.tag == 'alive_test': |
||
| 225 | options['alive_test'] = child.text |
||
| 226 | if child.tag == 'reverse_lookup_unify': |
||
| 227 | options['reverse_lookup_unify'] = child.text |
||
| 228 | if child.tag == 'reverse_lookup_only': |
||
| 229 | options['reverse_lookup_only'] = child.text |
||
| 230 | |||
| 231 | if hosts: |
||
| 232 | return { |
||
| 233 | 'hosts': hosts, |
||
| 234 | 'ports': ports, |
||
| 235 | 'credentials': credentials, |
||
| 236 | 'exclude_hosts': exclude_hosts, |
||
| 237 | 'finished_hosts': finished_hosts, |
||
| 238 | 'options': options, |
||
| 239 | } |
||
| 240 | else: |
||
| 241 | raise OspdError('No target to scan') |
||
| 242 | |||
| 266 |