Skip to content

Commit 65ff4ec

Browse files
committed
Use split_markdown_front_matter helper in istio
also, sort imports Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
1 parent a147819 commit 65ff4ec

2 files changed

Lines changed: 11 additions & 48 deletions

File tree

vulnerabilities/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def __lt__(self, other):
170170
def split_markdown_front_matter(text: str) -> Tuple[str, str]:
171171
r"""
172172
Split text into markdown front matter and the markdown body
173-
Returns ("", text) for text with non existing front matter
173+
Return ("", text) for text with non existing front matter
174174
175175
>>> text='''---
176176
... title: DUMMY-SECURITY-2019-001

vulnerabilities/importers/istio.py

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@
2222

2323
import asyncio
2424
import re
25-
from typing import List, Set
26-
import yaml
25+
from typing import Set
2726

27+
import yaml
28+
from packageurl import PackageURL
2829
from univers.version_specifier import VersionSpecifier
2930
from univers.versions import SemverVersion
30-
from packageurl import PackageURL
3131

32-
from vulnerabilities.data_source import Advisory, GitDataSource, Reference
32+
from vulnerabilities.data_source import Advisory
33+
from vulnerabilities.data_source import GitDataSource
34+
from vulnerabilities.helpers import split_markdown_front_matter
3335
from vulnerabilities.package_managers import GitHubTagsAPI
3436
from vulnerabilities.helpers import nearest_patched_package
3537

@@ -78,45 +80,6 @@ def get_pkg_versions_from_ranges(self, version_range_list):
7880
safe_pkg_versions = set(all_version) - set(vuln_pkg_versions)
7981
return safe_pkg_versions, vuln_pkg_versions
8082

81-
def get_data_from_yaml_lines(self, yaml_lines):
82-
"""Return a mapping of data from a iterable of yaml_lines
83-
for example :
84-
['title: ISTIO-SECURITY-2019-001',
85-
'description: Incorrect access control.','cves: [CVE-2019-12243]']
86-
87-
would give {'title':'ISTIO-SECURITY-2019-001',
88-
'description': 'Incorrect access control.',
89-
'cves': '[CVE-2019-12243]'}
90-
"""
91-
92-
return yaml.safe_load("\n".join(yaml_lines))
93-
94-
def get_yaml_lines(self, lines):
95-
"""The istio advisory file contains lines similar to yaml format .
96-
This function extracts those lines and return an iterable of lines
97-
98-
for example :
99-
lines =
100-
---
101-
title: ISTIO-SECURITY-2019-001
102-
description: Incorrect access control.
103-
cves: [CVE-2019-12243]
104-
---
105-
106-
get_yaml_lines(lines) would return
107-
['title: ISTIO-SECURITY-2019-001','description: Incorrect access control.'
108-
,'cves: [CVE-2019-12243]']
109-
"""
110-
111-
for index, line in enumerate(lines):
112-
line = line.strip()
113-
if line.startswith("---") and index == 0:
114-
continue
115-
elif line.endswith("---"):
116-
break
117-
else:
118-
yield line
119-
12083
def process_file(self, path):
12184

12285
advisories = []
@@ -210,10 +173,10 @@ def process_file(self, path):
210173
return advisories
211174

212175
def get_data_from_md(self, path):
213-
"""Return a mapping of vulnerability data from istio . The data is
214-
in the form of yaml_lines inside a .md file.
176+
"""Return a mapping of vulnerability data from istio. The data is
177+
in the form of yaml objects found inside front matter of the .md file.
215178
"""
216179

217180
with open(path) as f:
218-
yaml_lines = self.get_yaml_lines(f)
219-
return self.get_data_from_yaml_lines(yaml_lines)
181+
yaml_lines, _ = split_markdown_front_matter(f.read())
182+
return yaml.safe_load(yaml_lines)

0 commit comments

Comments
 (0)