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