| Conditions | 12 |
| Total Lines | 94 |
| Code Lines | 37 |
| 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_targets_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 |
||
| 132 | @classmethod |
||
| 133 | def process_targets_element(cls, scanner_target: et.Element) -> List: |
||
| 134 | """ Receive an XML object with the target, ports and credentials to run |
||
| 135 | a scan against. |
||
| 136 | |||
| 137 | @param: XML element with target subelements. Each target has <hosts> |
||
| 138 | and <ports> subelements. Hosts can be a single host, a host range, |
||
| 139 | a comma-separated host list or a network address. |
||
| 140 | <ports> and <credentials> are optional. Therefore each ospd-scanner |
||
| 141 | should check for a valid ones if needed. |
||
| 142 | |||
| 143 | Example form: |
||
| 144 | <targets> |
||
| 145 | <target> |
||
| 146 | <hosts>localhosts</hosts> |
||
| 147 | <exclude_hosts>localhost1</exclude_hosts> |
||
| 148 | <ports>80,443</ports> |
||
| 149 | <alive_test></alive_test> |
||
| 150 | <reverse_lookup_only>1</reverse_lookup_only> |
||
| 151 | <reverse_lookup_unify>0</reverse_lookup_unify> |
||
| 152 | </target> |
||
| 153 | <target> |
||
| 154 | <hosts>192.168.0.0/24</hosts> |
||
| 155 | <ports>22</ports> |
||
| 156 | <credentials> |
||
| 157 | <credential type="up" service="ssh" port="22"> |
||
| 158 | <username>scanuser</username> |
||
| 159 | <password>mypass</password> |
||
| 160 | </credential> |
||
| 161 | <credential type="up" service="smb"> |
||
| 162 | <username>smbuser</username> |
||
| 163 | <password>mypass</password> |
||
| 164 | </credential> |
||
| 165 | </credentials> |
||
| 166 | </target> |
||
| 167 | </targets> |
||
| 168 | |||
| 169 | @return: A list of [hosts, port, {credentials}, exclude_hosts, options]. |
||
| 170 | Example form: |
||
| 171 | [['localhosts', '80,43', '', 'localhosts1', |
||
| 172 | {'alive_test': 'ALIVE_TEST_CONSIDER_ALIVE', |
||
| 173 | 'reverse_lookup_only': '1', |
||
| 174 | 'reverse_lookup_unify': '0', |
||
| 175 | } |
||
| 176 | ], |
||
| 177 | ['192.168.0.0/24', '22', {'smb': {'type': type, |
||
| 178 | 'port': port, |
||
| 179 | 'username': username, |
||
| 180 | 'password': pass, |
||
| 181 | }}, '', {}]] |
||
| 182 | """ |
||
| 183 | |||
| 184 | target_list = [] |
||
| 185 | |||
| 186 | for target in scanner_target: |
||
| 187 | exclude_hosts = '' |
||
| 188 | finished_hosts = '' |
||
| 189 | ports = '' |
||
| 190 | credentials = {} # type: Dict |
||
| 191 | options = {} |
||
| 192 | |||
| 193 | for child in target: |
||
| 194 | if child.tag == 'hosts': |
||
| 195 | hosts = child.text |
||
| 196 | if child.tag == 'exclude_hosts': |
||
| 197 | exclude_hosts = child.text |
||
| 198 | if child.tag == 'finished_hosts': |
||
| 199 | finished_hosts = child.text |
||
| 200 | if child.tag == 'ports': |
||
| 201 | ports = child.text |
||
| 202 | if child.tag == 'credentials': |
||
| 203 | credentials = cls.process_credentials_elements(child) |
||
| 204 | if child.tag == 'alive_test': |
||
| 205 | options['alive_test'] = child.text |
||
| 206 | if child.tag == 'reverse_lookup_unify': |
||
| 207 | options['reverse_lookup_unify'] = child.text |
||
| 208 | if child.tag == 'reverse_lookup_only': |
||
| 209 | options['reverse_lookup_only'] = child.text |
||
| 210 | |||
| 211 | if hosts: |
||
|
|
|||
| 212 | target_list.append( |
||
| 213 | [ |
||
| 214 | hosts, |
||
| 215 | ports, |
||
| 216 | credentials, |
||
| 217 | exclude_hosts, |
||
| 218 | finished_hosts, |
||
| 219 | options, |
||
| 220 | ] |
||
| 221 | ) |
||
| 222 | else: |
||
| 223 | raise OspdError('No target to scan') |
||
| 224 | |||
| 225 | return target_list |
||
| 226 | |||
| 250 |