Conditions | 10 |
Total Lines | 117 |
Code Lines | 63 |
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 metadata.NotusMetadataHandler.update_metadata() 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 -*- |
||
245 | def update_metadata(self) -> None: |
||
246 | """Parse all CSV files that are present in the |
||
247 | Notus metadata directory, perform a checksum check, |
||
248 | read their metadata, format some fields |
||
249 | and write this information to the Redis KB. |
||
250 | """ |
||
251 | |||
252 | logger.debug("Starting the Notus metadata load up") |
||
253 | # 1. Read each CSV file |
||
254 | for csv_abs_path in self.__csv_abs_filepaths_list: |
||
255 | # 2. Check the checksums, unless they have been disabled |
||
256 | if not self.is_checksum_correct(csv_abs_path): |
||
257 | # Skip this file if the checksum does not match |
||
258 | logger.debug("Checksum failed") |
||
259 | continue |
||
260 | |||
261 | logger.debug("Checksum check for %s successful", csv_abs_path) |
||
262 | with csv_abs_path.open("r") as csv_file: |
||
263 | # Skip the license header, so the actual content |
||
264 | # can be parsed by the DictReader |
||
265 | general_metadata_dict = dict() |
||
266 | for line_string in csv_file: |
||
267 | if line_string.startswith("{"): |
||
268 | general_metadata_dict = ast.literal_eval(line_string) |
||
269 | break |
||
270 | # Check if the file can be parsed by the CSV module |
||
271 | reader = csv.DictReader(csv_file) |
||
272 | # Check if the CSV file has the expected field names, |
||
273 | # else skip the file |
||
274 | is_correct = self._check_field_names_lsc(reader.fieldnames) |
||
275 | if not is_correct: |
||
276 | continue |
||
277 | |||
278 | for advisory_dict in reader: |
||
279 | # Make sure that no element is missing in the advisory_dict, |
||
280 | # else skip that advisory |
||
281 | is_correct = self._check_advisory_dict(advisory_dict) |
||
282 | if not is_correct: |
||
283 | continue |
||
284 | |||
285 | # 3. For each advisory_dict, |
||
286 | # write its contents to the Redis KB as metadata. |
||
287 | # Create a list with all the metadata. Refer to: |
||
288 | # https://github.com/greenbone/ospd-openvas/blob/232d04e72d2af0199d60324e8820d9e73498a831/ospd_openvas/db.py#L39 |
||
289 | advisory_metadata_list = list() |
||
290 | # File name |
||
291 | advisory_metadata_list.append( |
||
292 | "{0}{1}".format( |
||
293 | self.__metadata_relative_path_string, |
||
294 | os.path.basename(csv_file.name), |
||
295 | ) |
||
296 | ) |
||
297 | # Required keys |
||
298 | advisory_metadata_list.append("") |
||
299 | # Mandatory keys |
||
300 | advisory_metadata_list.append("") |
||
301 | # Excluded keys |
||
302 | advisory_metadata_list.append("") |
||
303 | # Required UDP ports |
||
304 | advisory_metadata_list.append("") |
||
305 | # Required ports |
||
306 | advisory_metadata_list.append("") |
||
307 | # Dependencies |
||
308 | advisory_metadata_list.append("") |
||
309 | # Tags |
||
310 | tags_string = ( |
||
311 | "cvss_base_vector={}|last_modification={}|" |
||
312 | "creation_date={}|summary={}|vuldetect={}|" |
||
313 | "insight={}|affected={}|solution={}|" |
||
314 | "solution_type={}|qod_type={}" |
||
315 | ) |
||
316 | tags_string = tags_string.format( |
||
317 | advisory_dict["CVSS_BASE_VECTOR"], |
||
318 | advisory_dict["LAST_MODIFICATION"], |
||
319 | advisory_dict["CREATION_DATE"], |
||
320 | advisory_dict["DESCRIPTION"], |
||
321 | general_metadata_dict["VULDETECT"], |
||
322 | advisory_dict["INSIGHT"], |
||
323 | advisory_dict["AFFECTED"], |
||
324 | general_metadata_dict["SOLUTION"], |
||
325 | general_metadata_dict["SOLUTION_TYPE"], |
||
326 | general_metadata_dict["QOD_TYPE"], |
||
327 | ) |
||
328 | advisory_metadata_list.append(tags_string) |
||
329 | # CVEs |
||
330 | advisory_metadata_list.append( |
||
331 | ", ".join(ast.literal_eval(advisory_dict["CVE_LIST"])) |
||
332 | ) |
||
333 | # BIDs |
||
334 | advisory_metadata_list.append("") |
||
335 | # XREFS |
||
336 | advisory_metadata_list.append( |
||
337 | self._format_xrefs( |
||
338 | advisory_dict["ADVISORY_XREF"], |
||
339 | ast.literal_eval(advisory_dict["XREFS"]), |
||
340 | ) |
||
341 | ) |
||
342 | # Script Category |
||
343 | advisory_metadata_list.append("3") |
||
344 | # Timeout |
||
345 | advisory_metadata_list.append("0") |
||
346 | # Script Family |
||
347 | advisory_metadata_list.append("Notus_LSC_Metadata") |
||
348 | # Script Name / Title |
||
349 | advisory_metadata_list.append(advisory_dict["TITLE"]) |
||
350 | |||
351 | # Write the metadata list to the respective Redis KB key, |
||
352 | # overwriting any existing values |
||
353 | kb_key_string = "nvt:{}".format(advisory_dict["OID"]) |
||
354 | try: |
||
355 | self.__nvti_cache.add_vt_to_cache( |
||
356 | vt_id=kb_key_string, vt=advisory_metadata_list |
||
357 | ) |
||
358 | except OspdOpenvasError: |
||
359 | # The advisory_metadata_list was either not |
||
360 | # a list or does not include 15 entries |
||
361 | break |
||
362 |