Coverage for muuntaa/refs.py: 64.29%

38 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2024-07-21 12:16:47 +00:00

1"""References type.""" 

2 

3import logging 

4from typing import Union 

5 

6import lxml.objectify # nosec B410 

7 

8from muuntaa.config import boolify 

9from muuntaa.subtree import Subtree 

10from muuntaa import ConfigType 

11 

12RootType = lxml.objectify.ObjectifiedElement 

13RevHistType = list[dict[str, Union[str, None, tuple[int, ...]]]] 

14 

15 

16class References(Subtree): 

17 """Represents the References objects. 

18 

19 ( 

20 /cvrf:cvrfdoc/cvrf:DocumentReferences, 

21 /cvrf:cvrfdoc/vuln:Vulnerability[i+1]/vuln:References, 

22 ) 

23 """ 

24 

25 force_default_category: bool = False 

26 

27 def __init__(self, config: ConfigType, lc_parent_code: str): # TODO: unlitter me and push data upstream 

28 super().__init__() 

29 boolify(config) 

30 self.force_default_category = config.get('force_insert_default_reference_category', False) # type: ignore 

31 if lc_parent_code not in ('cvrf', 'vuln'): 31 ↛ 32line 31 didn't jump to line 32 because the condition on line 31 was never true

32 raise KeyError('References can only be hosted by cvrf or vuln') 

33 if lc_parent_code == 'cvrf': 33 ↛ 40line 33 didn't jump to line 40 because the condition on line 33 was always true

34 if self.tree.get('document') is None: 34 ↛ 36line 34 didn't jump to line 36 because the condition on line 34 was always true

35 self.tree['document'] = {} 

36 if self.tree['document'].get('references') is None: 36 ↛ 38line 36 didn't jump to line 38 because the condition on line 36 was always true

37 self.tree['document']['references'] = [] 

38 self.hook = self.tree['document']['references'] 

39 else: 

40 if self.tree.get('vulnerabilities') is None: 

41 self.tree['vulnerabilities'] = {} 

42 if self.tree['vulnerabilities'].get('references') is None: 

43 self.tree['vulnerabilities']['references'] = [] 

44 self.hook = self.tree['vulnerabilities']['references'] 

45 

46 def always(self, root: RootType) -> None: 

47 for reference in root.Reference: 

48 ref_csaf = { 

49 'summary': reference.Description.text, # type: ignore 

50 'url': reference.URL.text, # type: ignore 

51 } 

52 if category := reference.attrib.get('Type', ''): 52 ↛ 54line 52 didn't jump to line 54 because the condition on line 52 was always true

53 ref_csaf['category'] = category.lower() 

54 elif self.force_default_category: 

55 ref_csaf['category'] = 'external' 

56 logging.info( 

57 '"Type" attribute not present in "Reference" element, using default value "external".' 

58 ' This can be controlled by "force_insert_default_reference_category" option.' 

59 ) 

60 self.hook.append(ref_csaf) 

61 

62 def sometimes(self, root: RootType) -> None: 

63 pass