| Total Complexity | 44 |
| Total Lines | 419 |
| Duplicated Lines | 47.49 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like gvm.protocols.gmpv208.entities.policies 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 | # -*- coding: utf-8 -*- |
||
| 2 | # Copyright (C) 2021 Greenbone Networks GmbH |
||
| 3 | # |
||
| 4 | # SPDX-License-Identifier: GPL-3.0-or-later |
||
| 5 | # |
||
| 6 | # This program is free software: you can redistribute it and/or modify |
||
| 7 | # it under the terms of the GNU General Public License as published by |
||
| 8 | # the Free Software Foundation, either version 3 of the License, or |
||
| 9 | # (at your option) any later version. |
||
| 10 | # |
||
| 11 | # This program is distributed in the hope that it will be useful, |
||
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 14 | # GNU General Public License for more details. |
||
| 15 | # |
||
| 16 | # You should have received a copy of the GNU General Public License |
||
| 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
| 18 | |||
| 19 | # pylint: disable=redefined-builtin |
||
| 20 | # MAYBE we should change filter to filter_string (everywhere) |
||
| 21 | |||
| 22 | |||
| 23 | from typing import Any, List, Optional, Tuple |
||
| 24 | |||
| 25 | from gvm.errors import RequiredArgument, InvalidArgument, InvalidArgumentType |
||
| 26 | from gvm.utils import add_filter, to_base64, to_bool, is_list_like |
||
| 27 | from gvm.xml import XmlCommand |
||
| 28 | |||
| 29 | _EMPTY_POLICY_ID = '085569ce-73ed-11df-83c3-002264764cea' |
||
| 30 | |||
| 31 | |||
| 32 | class PoliciesMixin: |
||
| 33 | def clone_policy(self, policy_id: str) -> Any: |
||
| 34 | """Clone a policy from an existing one |
||
| 35 | |||
| 36 | Arguments: |
||
| 37 | policy_id: UUID of the existing policy |
||
| 38 | |||
| 39 | Returns: |
||
| 40 | The response. See :py:meth:`send_command` for details. |
||
| 41 | """ |
||
| 42 | if not policy_id: |
||
| 43 | raise RequiredArgument( |
||
| 44 | function=self.clone_policy.__name__, argument='policy_id' |
||
| 45 | ) |
||
| 46 | |||
| 47 | cmd = XmlCommand("create_config") |
||
| 48 | cmd.add_element("copy", policy_id) |
||
| 49 | return self._send_xml_command(cmd) |
||
| 50 | |||
| 51 | def create_policy( |
||
| 52 | self, name: str, *, policy_id: str = None, comment: Optional[str] = None |
||
| 53 | ) -> Any: |
||
| 54 | """Create a new policy |
||
| 55 | |||
| 56 | Arguments: |
||
| 57 | name: Name of the new policy |
||
| 58 | policy_id: UUID of an existing policy as base. By default the empty |
||
| 59 | policy is used. |
||
| 60 | comment: A comment on the policy |
||
| 61 | |||
| 62 | Returns: |
||
| 63 | The response. See :py:meth:`send_command` for details. |
||
| 64 | """ |
||
| 65 | cmd = XmlCommand("create_config") |
||
| 66 | |||
| 67 | if policy_id is None: |
||
| 68 | policy_id = _EMPTY_POLICY_ID |
||
| 69 | if not name: |
||
| 70 | raise RequiredArgument( |
||
| 71 | function=self.create_policy.__name__, argument='name' |
||
| 72 | ) |
||
| 73 | |||
| 74 | if comment is not None: |
||
| 75 | cmd.add_element("comment", comment) |
||
| 76 | cmd.add_element("copy", policy_id) |
||
| 77 | cmd.add_element("name", name) |
||
| 78 | cmd.add_element("usage_type", "policy") |
||
| 79 | return self._send_xml_command(cmd) |
||
| 80 | |||
| 81 | def delete_policy( |
||
| 82 | self, policy_id: str, *, ultimate: Optional[bool] = False |
||
| 83 | ) -> Any: |
||
| 84 | """Deletes an existing policy |
||
| 85 | Arguments: |
||
| 86 | policy_id: UUID of the policy to be deleted. |
||
| 87 | ultimate: Whether to remove entirely, or to the trashcan. |
||
| 88 | """ |
||
| 89 | if not policy_id: |
||
| 90 | raise RequiredArgument( |
||
| 91 | function=self.delete_policy.__name__, argument='policy_id' |
||
| 92 | ) |
||
| 93 | |||
| 94 | cmd = XmlCommand("delete_config") |
||
| 95 | cmd.set_attribute("config_id", policy_id) |
||
| 96 | cmd.set_attribute("ultimate", to_bool(ultimate)) |
||
| 97 | |||
| 98 | return self._send_xml_command(cmd) |
||
| 99 | |||
| 100 | View Code Duplication | def get_policies( |
|
|
|
|||
| 101 | self, |
||
| 102 | *, |
||
| 103 | audits: Optional[bool] = None, |
||
| 104 | filter: Optional[str] = None, |
||
| 105 | filter_id: Optional[str] = None, |
||
| 106 | details: Optional[bool] = None, |
||
| 107 | families: Optional[bool] = None, |
||
| 108 | preferences: Optional[bool] = None, |
||
| 109 | trash: Optional[bool] = None, |
||
| 110 | ) -> Any: |
||
| 111 | """Request a list of policies |
||
| 112 | |||
| 113 | Arguments: |
||
| 114 | audits: Whether to get audits using the policy |
||
| 115 | filter: Filter term to use for the query |
||
| 116 | filter_id: UUID of an existing filter to use for the query |
||
| 117 | details: Whether to get families, preferences, nvt selectors |
||
| 118 | and tasks. |
||
| 119 | families: Whether to include the families if no details are |
||
| 120 | requested |
||
| 121 | preferences: Whether to include the preferences if no details are |
||
| 122 | requested |
||
| 123 | trash: Whether to get the trashcan audits instead |
||
| 124 | |||
| 125 | Returns: |
||
| 126 | The response. See :py:meth:`send_command` for details. |
||
| 127 | """ |
||
| 128 | cmd = XmlCommand("get_configs") |
||
| 129 | cmd.set_attribute("usage_type", "policy") |
||
| 130 | |||
| 131 | add_filter(cmd, filter, filter_id) |
||
| 132 | |||
| 133 | if trash is not None: |
||
| 134 | cmd.set_attribute("trash", to_bool(trash)) |
||
| 135 | |||
| 136 | if details is not None: |
||
| 137 | cmd.set_attribute("details", to_bool(details)) |
||
| 138 | |||
| 139 | if families is not None: |
||
| 140 | cmd.set_attribute("families", to_bool(families)) |
||
| 141 | |||
| 142 | if preferences is not None: |
||
| 143 | cmd.set_attribute("preferences", to_bool(preferences)) |
||
| 144 | |||
| 145 | if audits is not None: |
||
| 146 | cmd.set_attribute("tasks", to_bool(audits)) |
||
| 147 | |||
| 148 | return self._send_xml_command(cmd) |
||
| 149 | |||
| 150 | def get_policy( |
||
| 151 | self, policy_id: str, *, audits: Optional[bool] = None |
||
| 152 | ) -> Any: |
||
| 153 | """Request a single policy |
||
| 154 | |||
| 155 | Arguments: |
||
| 156 | policy_id: UUID of an existing policy |
||
| 157 | audits: Whether to get audits using this policy |
||
| 158 | |||
| 159 | Returns: |
||
| 160 | The response. See :py:meth:`send_command` for details. |
||
| 161 | """ |
||
| 162 | if not policy_id: |
||
| 163 | raise RequiredArgument( |
||
| 164 | function=self.get_policy.__name__, argument='policy_id' |
||
| 165 | ) |
||
| 166 | |||
| 167 | cmd = XmlCommand("get_configs") |
||
| 168 | cmd.set_attribute("config_id", policy_id) |
||
| 169 | |||
| 170 | cmd.set_attribute("usage_type", "policy") |
||
| 171 | |||
| 172 | if audits is not None: |
||
| 173 | cmd.set_attribute("tasks", to_bool(audits)) |
||
| 174 | |||
| 175 | # for single entity always request all details |
||
| 176 | cmd.set_attribute("details", "1") |
||
| 177 | |||
| 178 | return self._send_xml_command(cmd) |
||
| 179 | |||
| 180 | View Code Duplication | def modify_policy_set_nvt_preference( |
|
| 181 | self, |
||
| 182 | policy_id: str, |
||
| 183 | name: str, |
||
| 184 | nvt_oid: str, |
||
| 185 | *, |
||
| 186 | value: Optional[str] = None, |
||
| 187 | ) -> Any: |
||
| 188 | """Modifies the nvt preferences of an existing policy. |
||
| 189 | |||
| 190 | Arguments: |
||
| 191 | policy_id: UUID of policy to modify. |
||
| 192 | name: Name for preference to change. |
||
| 193 | nvt_oid: OID of the NVT associated with preference to modify |
||
| 194 | value: New value for the preference. None to delete the preference |
||
| 195 | and to use the default instead. |
||
| 196 | """ |
||
| 197 | if not policy_id: |
||
| 198 | raise RequiredArgument( |
||
| 199 | function=self.modify_policy_set_nvt_preference.__name__, |
||
| 200 | argument='policy_id', |
||
| 201 | ) |
||
| 202 | |||
| 203 | if not nvt_oid: |
||
| 204 | raise RequiredArgument( |
||
| 205 | function=self.modify_policy_set_nvt_preference.__name__, |
||
| 206 | argument='nvt_oid', |
||
| 207 | ) |
||
| 208 | |||
| 209 | if not name: |
||
| 210 | raise RequiredArgument( |
||
| 211 | function=self.modify_policy_set_nvt_preference.__name__, |
||
| 212 | argument='name', |
||
| 213 | ) |
||
| 214 | |||
| 215 | cmd = XmlCommand("modify_config") |
||
| 216 | cmd.set_attribute("config_id", str(policy_id)) |
||
| 217 | |||
| 218 | _xmlpref = cmd.add_element("preference") |
||
| 219 | |||
| 220 | _xmlpref.add_element("nvt", attrs={"oid": nvt_oid}) |
||
| 221 | _xmlpref.add_element("name", name) |
||
| 222 | |||
| 223 | if value: |
||
| 224 | _xmlpref.add_element("value", to_base64(value)) |
||
| 225 | |||
| 226 | return self._send_xml_command(cmd) |
||
| 227 | |||
| 228 | def modify_policy_set_name(self, policy_id: str, name: str) -> Any: |
||
| 229 | """Modifies the name of an existing policy |
||
| 230 | |||
| 231 | Arguments: |
||
| 232 | policy_id: UUID of policy to modify. |
||
| 233 | name: New name for the policy. |
||
| 234 | """ |
||
| 235 | if not policy_id: |
||
| 236 | raise RequiredArgument( |
||
| 237 | function=self.modify_policy_set_name.__name__, |
||
| 238 | argument='policy_id', |
||
| 239 | ) |
||
| 240 | |||
| 241 | if not name: |
||
| 242 | raise RequiredArgument( |
||
| 243 | function=self.modify_policy_set_name.__name__, |
||
| 244 | argument='name', |
||
| 245 | ) |
||
| 246 | |||
| 247 | cmd = XmlCommand("modify_config") |
||
| 248 | cmd.set_attribute("config_id", str(policy_id)) |
||
| 249 | |||
| 250 | cmd.add_element("name", name) |
||
| 251 | |||
| 252 | return self._send_xml_command(cmd) |
||
| 253 | |||
| 254 | def modify_policy_set_comment( |
||
| 255 | self, policy_id: str, comment: Optional[str] = None |
||
| 256 | ) -> Any: |
||
| 257 | """Modifies the comment of an existing policy |
||
| 258 | |||
| 259 | Arguments: |
||
| 260 | policy_id: UUID of policy to modify. |
||
| 261 | comment: Comment to set on a policy. Default is an |
||
| 262 | empty comment and the previous comment will be |
||
| 263 | removed. |
||
| 264 | """ |
||
| 265 | if not policy_id: |
||
| 266 | raise RequiredArgument( |
||
| 267 | function=self.modify_policy_set_comment.__name__, |
||
| 268 | argument='policy_id', |
||
| 269 | ) |
||
| 270 | |||
| 271 | cmd = XmlCommand("modify_config") |
||
| 272 | cmd.set_attribute("config_id", str(policy_id)) |
||
| 273 | if not comment: |
||
| 274 | comment = "" |
||
| 275 | cmd.add_element("comment", comment) |
||
| 276 | |||
| 277 | return self._send_xml_command(cmd) |
||
| 278 | |||
| 279 | def modify_policy_set_scanner_preference( |
||
| 280 | self, policy_id: str, name: str, *, value: Optional[str] = None |
||
| 281 | ) -> Any: |
||
| 282 | """Modifies the scanner preferences of an existing policy |
||
| 283 | |||
| 284 | Arguments: |
||
| 285 | policy_id: UUID of policy to modify. |
||
| 286 | name: Name of the scanner preference to change |
||
| 287 | value: New value for the preference. None to delete the preference |
||
| 288 | and to use the default instead. |
||
| 289 | |||
| 290 | """ |
||
| 291 | if not policy_id: |
||
| 292 | raise RequiredArgument( |
||
| 293 | function=(self.modify_policy_set_scanner_preference.__name__), |
||
| 294 | argument='policy_id', |
||
| 295 | ) |
||
| 296 | |||
| 297 | if not name: |
||
| 298 | raise RequiredArgument( |
||
| 299 | function=(self.modify_policy_set_scanner_preference.__name__), |
||
| 300 | argument='name argument', |
||
| 301 | ) |
||
| 302 | |||
| 303 | cmd = XmlCommand("modify_config") |
||
| 304 | cmd.set_attribute("config_id", str(policy_id)) |
||
| 305 | |||
| 306 | _xmlpref = cmd.add_element("preference") |
||
| 307 | |||
| 308 | _xmlpref.add_element("name", name) |
||
| 309 | |||
| 310 | if value: |
||
| 311 | _xmlpref.add_element("value", to_base64(value)) |
||
| 312 | |||
| 313 | return self._send_xml_command(cmd) |
||
| 314 | |||
| 315 | View Code Duplication | def modify_policy_set_nvt_selection( |
|
| 316 | self, policy_id: str, family: str, nvt_oids: List[str] |
||
| 317 | ) -> Any: |
||
| 318 | """Modifies the selected nvts of an existing policy |
||
| 319 | |||
| 320 | The manager updates the given family in the policy to include only the |
||
| 321 | given NVTs. |
||
| 322 | |||
| 323 | Arguments: |
||
| 324 | policy_id: UUID of policy to modify. |
||
| 325 | family: Name of the NVT family to include NVTs from |
||
| 326 | nvt_oids: List of NVTs to select for the family. |
||
| 327 | """ |
||
| 328 | if not policy_id: |
||
| 329 | raise RequiredArgument( |
||
| 330 | function=self.modify_policy_set_nvt_selection.__name__, |
||
| 331 | argument='policy_id', |
||
| 332 | ) |
||
| 333 | |||
| 334 | if not family: |
||
| 335 | raise RequiredArgument( |
||
| 336 | function=self.modify_policy_set_nvt_selection.__name__, |
||
| 337 | argument='family argument', |
||
| 338 | ) |
||
| 339 | |||
| 340 | if not is_list_like(nvt_oids): |
||
| 341 | raise InvalidArgumentType( |
||
| 342 | function=self.modify_policy_set_nvt_selection.__name__, |
||
| 343 | argument='nvt_oids', |
||
| 344 | arg_type='list', |
||
| 345 | ) |
||
| 346 | |||
| 347 | cmd = XmlCommand("modify_config") |
||
| 348 | cmd.set_attribute("config_id", str(policy_id)) |
||
| 349 | |||
| 350 | _xmlnvtsel = cmd.add_element("nvt_selection") |
||
| 351 | _xmlnvtsel.add_element("family", family) |
||
| 352 | |||
| 353 | for nvt in nvt_oids: |
||
| 354 | _xmlnvtsel.add_element("nvt", attrs={"oid": nvt}) |
||
| 355 | |||
| 356 | return self._send_xml_command(cmd) |
||
| 357 | |||
| 358 | View Code Duplication | def modify_policy_set_family_selection( |
|
| 359 | self, |
||
| 360 | policy_id: str, |
||
| 361 | families: List[Tuple[str, bool, bool]], |
||
| 362 | *, |
||
| 363 | auto_add_new_families: Optional[bool] = True, |
||
| 364 | ) -> Any: |
||
| 365 | """ |
||
| 366 | Selected the NVTs of a policy at a family level. |
||
| 367 | |||
| 368 | Arguments: |
||
| 369 | policy_id: UUID of policy to modify. |
||
| 370 | families: A list of tuples with the first entry being the name |
||
| 371 | of the NVT family selected, second entry a boolean indicating |
||
| 372 | whether new NVTs should be added to the family automatically, |
||
| 373 | and third entry a boolean indicating whether all nvts from |
||
| 374 | the family should be included. |
||
| 375 | auto_add_new_families: Whether new families should be added to the |
||
| 376 | policy automatically. Default: True. |
||
| 377 | """ |
||
| 378 | if not policy_id: |
||
| 379 | raise RequiredArgument( |
||
| 380 | function=self.modify_policy_set_family_selection.__name__, |
||
| 381 | argument='policy_id', |
||
| 382 | ) |
||
| 383 | |||
| 384 | if not is_list_like(families): |
||
| 385 | raise InvalidArgumentType( |
||
| 386 | function=self.modify_policy_set_family_selection.__name__, |
||
| 387 | argument='families', |
||
| 388 | arg_type='list', |
||
| 389 | ) |
||
| 390 | |||
| 391 | cmd = XmlCommand("modify_config") |
||
| 392 | cmd.set_attribute("config_id", str(policy_id)) |
||
| 393 | |||
| 394 | _xmlfamsel = cmd.add_element("family_selection") |
||
| 395 | _xmlfamsel.add_element("growing", to_bool(auto_add_new_families)) |
||
| 396 | |||
| 397 | for family in families: |
||
| 398 | _xmlfamily = _xmlfamsel.add_element("family") |
||
| 399 | _xmlfamily.add_element("name", family[0]) |
||
| 400 | |||
| 401 | if len(family) != 3: |
||
| 402 | raise InvalidArgument( |
||
| 403 | "Family must be a tuple of 3. (str, bool, bool)" |
||
| 404 | ) |
||
| 405 | |||
| 406 | if not isinstance(family[1], bool) or not isinstance( |
||
| 407 | family[2], bool |
||
| 408 | ): |
||
| 409 | raise InvalidArgumentType( |
||
| 410 | function=(self.modify_policy_set_family_selection.__name__), |
||
| 411 | argument='families', |
||
| 412 | arg_type='[tuple(str, bool, bool)]', |
||
| 413 | ) |
||
| 414 | |||
| 415 | _xmlfamily.add_element("all", to_bool(family[2])) |
||
| 416 | _xmlfamily.add_element("growing", to_bool(family[1])) |
||
| 417 | |||
| 418 | return self._send_xml_command(cmd) |
||
| 419 |