Skip to content

Commit

Permalink
Clean imported data after import process
Browse files Browse the repository at this point in the history
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
  • Loading branch information
TG1999 committed Apr 26, 2023
1 parent 2646d7e commit c4e3f91
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 70 deletions.
38 changes: 5 additions & 33 deletions vulnerabilities/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ class InvalidSPDXLicense(Exception):
pass


class ForkError(Exception):
pass


class Importer:
"""
An Importer collects data from various upstreams and returns corresponding AdvisoryData objects
Expand Down Expand Up @@ -325,46 +329,14 @@ def advisory_data(self) -> Iterable[AdvisoryData]:

def clone(self, repo_url):
try:
self.repo_url = repo_url
self.vcs_response = fetch_via_vcs(repo_url)
except Exception as e:
msg = f"Failed to fetch {repo_url} via vcs: {e}"
logger.error(msg)
raise ForkError(msg) from e


class ForkError(Exception):
pass


class GitImporter(Importer):
def __init__(self, repo_url):
super().__init__()
self.repo_url = repo_url
self.vcs_response = None

def __enter__(self):
super().__enter__()
self.clone()
return self

def __exit__(self):
self.vcs_response.delete()

def clone(self):
try:
self.vcs_response = fetch_via_vcs(self.repo_url)
except Exception as e:
msg = f"Failed to fetch {self.repo_url} via vcs: {e}"
logger.error(msg)
raise ForkError(msg) from e

def advisory_data(self) -> Iterable[AdvisoryData]:
"""
Return AdvisoryData objects corresponding to the data being imported
"""
raise NotImplementedError


# TODO: Needs rewrite
class OvalImporter(Importer):
"""
Expand Down
10 changes: 4 additions & 6 deletions vulnerabilities/importers/fireeye.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,27 @@
from typing import List

from vulnerabilities.importer import AdvisoryData
from vulnerabilities.importer import GitImporter
from vulnerabilities.importer import Importer
from vulnerabilities.importer import Reference
from vulnerabilities.utils import build_description
from vulnerabilities.utils import dedupe

logger = logging.getLogger(__name__)


class FireyeImporter(GitImporter):
class FireyeImporter(Importer):
spdx_license_expression = "CC-BY-SA-4.0 AND MIT"
license_url = "https://github.com/mandiant/Vulnerability-Disclosures/blob/master/README.md"
repo_url = "git+https://github.com/mandiant/Vulnerability-Disclosures"
notice = """
Copyright (c) Mandiant
The following licenses/licensing apply to this Mandiant repository:
1. CC BY-SA 4.0 - For CVE related information not including source code (such as PoCs)
2. MIT - For source code contained within provided CVE information
"""

def __init__(self):
super().__init__(repo_url="git+https://github.com/mandiant/Vulnerability-Disclosures")

def advisory_data(self) -> Iterable[AdvisoryData]:
self.clone()
self.clone(repo_url="git+https://github.com/mandiant/Vulnerability-Disclosures")
files = filter(
lambda p: p.suffix in [".md", ".MD"], Path(self.vcs_response.dest_dir).glob("**/*")
)
Expand Down
12 changes: 5 additions & 7 deletions vulnerabilities/importers/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

from vulnerabilities.importer import AdvisoryData
from vulnerabilities.importer import AffectedPackage
from vulnerabilities.importer import GitImporter
from vulnerabilities.importer import Importer
from vulnerabilities.importer import Reference
from vulnerabilities.utils import build_description

Expand All @@ -48,16 +48,14 @@
GITLAB_SCHEME_BY_PURL_TYPE = {v: k for k, v in PURL_TYPE_BY_GITLAB_SCHEME.items()}


class GitLabAPIImporter(GitImporter):
class GitLabAPIImporter(Importer):
spdx_license_expression = "MIT"
license_url = "https://gitlab.com/gitlab-org/advisories-community/-/blob/main/LICENSE"
repo_url = "git+https://gitlab.com/gitlab-org/advisories-community/"

def __init__(self):
super().__init__(repo_url="git+https://gitlab.com/gitlab-org/advisories-community/")

def advisory_data(self, _keep_clone=True) -> Iterable[AdvisoryData]:
def advisory_data(self, _keep_clone=False) -> Iterable[AdvisoryData]:
try:
self.clone()
self.clone(self.repo_url)
base_path = Path(self.vcs_response.dest_dir)

for file_path in base_path.glob("**/*.yml"):
Expand Down
26 changes: 15 additions & 11 deletions vulnerabilities/importers/istio.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,21 @@ class IstioImporter(Importer):
repo_url = "git+https://github.com/istio/istio.io/"

def advisory_data(self) -> Set[AdvisoryData]:
self.clone(self.repo_url)
path = Path(self.vcs_response.dest_dir)
vuln = path / "content/en/news/security/"
for file in vuln.glob("**/*.md"):
# Istio website has files with name starting with underscore, these contain metadata
# required for rendering the website. We're not interested in these.
# See also https://github.com/nexB/vulnerablecode/issues/563
file = str(file)
if file.endswith("_index.md"):
continue
yield from self.process_file(file)
try:
self.clone(self.repo_url)
path = Path(self.vcs_response.dest_dir)
vuln = path / "content/en/news/security/"
for file in vuln.glob("**/*.md"):
# Istio website has files with name starting with underscore, these contain metadata
# required for rendering the website. We're not interested in these.
# See also https://github.com/nexB/vulnerablecode/issues/563
file = str(file)
if file.endswith("_index.md"):
continue
yield from self.process_file(file)
finally:
if self.vcs_response:
self.vcs_response.delete()

def process_file(self, path):

Expand Down
4 changes: 2 additions & 2 deletions vulnerabilities/importers/kaybee.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
from packageurl import PackageURL

from vulnerabilities.importer import AdvisoryData
from vulnerabilities.importer import GitImporter
from vulnerabilities.importer import Importer
from vulnerabilities.importer import Reference
from vulnerabilities.utils import load_yaml
from vulnerabilities.utils import nearest_patched_package


class KaybeeImporter(GitImporter):
class KaybeeImporter(Importer):
def __enter__(self):
super(KaybeeImporter, self).__enter__()
self._added_files, self._updated_files = self.file_changes(
Expand Down
4 changes: 2 additions & 2 deletions vulnerabilities/importers/ruby.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
from univers.versions import SemverVersion

from vulnerabilities.importer import AdvisoryData
from vulnerabilities.importer import GitImporter
from vulnerabilities.importer import Importer
from vulnerabilities.importer import Reference
from vulnerabilities.package_managers import RubyVersionAPI
from vulnerabilities.utils import load_yaml
from vulnerabilities.utils import nearest_patched_package


class RubyImporter(GitImporter):
class RubyImporter(Importer):
def __enter__(self):
super(RubyImporter, self).__enter__()

Expand Down
4 changes: 2 additions & 2 deletions vulnerabilities/importers/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
from univers.versions import SemverVersion

from vulnerabilities.importer import AdvisoryData
from vulnerabilities.importer import GitImporter
from vulnerabilities.importer import Importer
from vulnerabilities.importer import Reference
from vulnerabilities.package_managers import CratesVersionAPI
from vulnerabilities.utils import nearest_patched_package


class RustImporter(GitImporter):
class RustImporter(Importer):
def __enter__(self):
super(RustImporter, self).__enter__()

Expand Down
15 changes: 8 additions & 7 deletions vulnerabilities/tests/test_data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from packageurl import PackageURL

from vulnerabilities.importer import GitImporter
from vulnerabilities.importer import Importer
from vulnerabilities.importer import OvalImporter
from vulnerabilities.oval_parser import OvalParser

Expand All @@ -35,7 +35,7 @@ class MockOvalImporter(OvalImporter):
spdx_license_expression = "FOO-BAR"


class MockGitImporter(GitImporter):
class MockGitImporter(Importer):
spdx_license_expression = "FOO-BAR"


Expand Down Expand Up @@ -83,12 +83,13 @@ def test__collect_pkgs():
assert found_ubuntu_pkgs == expected_ubuntu_pkgs


def clone(self):
def mock_function(repo_url):
pass


@patch("vulnerabilities.importer.GitImporter.clone")
@patch("vulnerabilities.importer.fetch_via_vcs")
def test_git_importer(mock_clone):
mock_clone.return_value = clone
imp = MockGitImporter("test-url")
assert imp.repo_url == "test-url"
mock_clone.return_value = mock_function
git_importer = MockGitImporter()
git_importer.clone("test-url")
assert git_importer.repo_url == "test-url"

0 comments on commit c4e3f91

Please sign in to comment.