-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1628 from aboutcode-org/1627-migrate-pysec
Migrate pysec importer to aboutcode pipeline
- Loading branch information
Showing
7 changed files
with
158 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
vulnerabilities/migrations/0074_update_pysec_advisory_created_by.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Generated by Django 4.2.16 on 2024-10-24 13:51 | ||
|
||
from django.db import migrations | ||
|
||
""" | ||
Update the created_by field on Advisory from the old qualified_name | ||
to the new pipeline_id. | ||
""" | ||
|
||
|
||
def update_created_by(apps, schema_editor): | ||
from vulnerabilities.pipelines.pysec_importer import PyPIImporterPipeline | ||
|
||
Advisory = apps.get_model("vulnerabilities", "Advisory") | ||
Advisory.objects.filter(created_by="vulnerabilities.importers.pysec.PyPIImporter").update( | ||
created_by=PyPIImporterPipeline.pipeline_id | ||
) | ||
|
||
|
||
def reverse_update_created_by(apps, schema_editor): | ||
from vulnerabilities.pipelines.pysec_importer import PyPIImporterPipeline | ||
|
||
Advisory = apps.get_model("vulnerabilities", "Advisory") | ||
Advisory.objects.filter(created_by=PyPIImporterPipeline.pipeline_id).update( | ||
created_by="vulnerabilities.importers.pysec.PyPIImporter" | ||
) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("vulnerabilities", "0073_delete_packagerelatedvulnerability"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(update_created_by, reverse_code=reverse_update_created_by), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# | ||
# Copyright (c) nexB Inc. and others. All rights reserved. | ||
# VulnerableCode is a trademark of nexB Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. | ||
# See https://github.com/aboutcode-org/vulnerablecode for support or download. | ||
# See https://aboutcode.org for more information about nexB OSS projects. | ||
# | ||
import json | ||
import logging | ||
from io import BytesIO | ||
from typing import Iterable | ||
from zipfile import ZipFile | ||
|
||
import requests | ||
|
||
from vulnerabilities.importer import AdvisoryData | ||
from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipeline | ||
|
||
|
||
class PyPIImporterPipeline(VulnerableCodeBaseImporterPipeline): | ||
"""Collect advisories from PyPI.""" | ||
|
||
pipeline_id = "pysec_importer" | ||
|
||
license_url = "https://github.com/pypa/advisory-database/blob/main/LICENSE" | ||
url = "https://osv-vulnerabilities.storage.googleapis.com/PyPI/all.zip" | ||
spdx_license_expression = "CC-BY-4.0" | ||
importer_name = "PyPI Importer" | ||
|
||
@classmethod | ||
def steps(cls): | ||
return ( | ||
cls.fetch_zip, | ||
cls.collect_and_store_advisories, | ||
cls.import_new_advisories, | ||
) | ||
|
||
def fetch_zip(self): | ||
self.log(f"Fetching `{self.url}`") | ||
self.advisory_zip = requests.get(self.url).content | ||
|
||
def advisories_count(self) -> int: | ||
with ZipFile(BytesIO(self.advisory_zip)) as zip: | ||
advisory_count = sum(1 for file in zip.namelist() if file.startswith("PYSEC-")) | ||
return advisory_count | ||
|
||
def collect_advisories(self) -> Iterable[AdvisoryData]: | ||
"""Yield AdvisoryData using a zipped data dump of OSV data""" | ||
from vulnerabilities.importers.osv import parse_advisory_data | ||
|
||
with ZipFile(BytesIO(self.advisory_zip)) as zip_file: | ||
for file_name in zip_file.namelist(): | ||
if not file_name.startswith("PYSEC-"): | ||
self.log( | ||
f"Unsupported PyPI advisory data file: {file_name}", | ||
level=logging.ERROR, | ||
) | ||
continue | ||
with zip_file.open(file_name) as f: | ||
vul_info = json.load(f) | ||
yield parse_advisory_data( | ||
raw_data=vul_info, | ||
supported_ecosystems=["pypi"], | ||
advisory_url=self.url, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters