| Conditions | 7 |
| Total Lines | 51 |
| Code Lines | 31 |
| 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:
| 1 | # -*- coding: utf-8 -*- |
||
| 39 | def create_config(gmp, cert_bund_name): |
||
| 40 | cert_bund_details = gmp.get_info( |
||
| 41 | info_id=cert_bund_name, info_type=gmp.types.InfoType.CERT_BUND_ADV |
||
| 42 | ) |
||
| 43 | |||
| 44 | list_cves = cert_bund_details.xpath( |
||
| 45 | 'info/cert_bund_adv/raw_data/Advisory/CVEList/CVE/text()' |
||
| 46 | ) |
||
| 47 | |||
| 48 | nvt_dict = dict() |
||
| 49 | counter = 0 |
||
| 50 | |||
| 51 | for cve in list_cves: |
||
| 52 | # Get all nvts of this cve |
||
| 53 | cve_info = gmp.get_info(info_id=cve, info_type=gmp.types.InfoType.CVE) |
||
| 54 | nvts = cve_info.xpath('info/cve/nvts/nvt') |
||
| 55 | |||
| 56 | for nvt in nvts: |
||
| 57 | counter += 1 |
||
| 58 | oid = nvt.xpath('@oid')[0] |
||
| 59 | |||
| 60 | # We need the nvt family to modify scan config |
||
| 61 | nvt_data = gmp.get_nvt(oid) |
||
| 62 | family = nvt_data.xpath('nvt/family/text()')[0] |
||
| 63 | |||
| 64 | # Create key value map |
||
| 65 | if family in nvt_dict and oid not in nvt_dict[family]: |
||
| 66 | nvt_dict[family].append(oid) |
||
| 67 | else: |
||
| 68 | nvt_dict[family] = [oid] |
||
| 69 | |||
| 70 | # Create new config |
||
| 71 | copy_id = '085569ce-73ed-11df-83c3-002264764cea' |
||
| 72 | config_name = 'scanconfig_for_%s' % cert_bund_name |
||
| 73 | config_id = '' |
||
| 74 | |||
| 75 | try: |
||
| 76 | res = gmp.create_config(copy_id, config_name) |
||
| 77 | config_id = res.xpath('@id')[0] |
||
| 78 | |||
| 79 | # Modify the config with the nvts oid |
||
| 80 | for family, nvt_oid in nvt_dict.items(): |
||
| 81 | gmp.modify_config(config_id, nvt_oids=nvt_oid, family=family) |
||
| 82 | |||
| 83 | # This nvts must be present to work |
||
| 84 | family = 'Port scanners' |
||
| 85 | nvts = ['1.3.6.1.4.1.25623.1.0.14259', '1.3.6.1.4.1.25623.1.0.100315'] |
||
| 86 | gmp.modify_config(config_id=config_id, nvt_oids=nvts, family=family) |
||
| 87 | |||
| 88 | except GvmError: |
||
| 89 | print('Config exist') |
||
| 90 | |||
| 106 |