-
-
Notifications
You must be signed in to change notification settings - Fork 285
Migrate Alpine importer to advisory V2 #2111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
547d742
Migrate Alpine importer to advisory V2
ziadhany bba1080
Fix typo Alpine Linux importer pipeline inherits from VulnerableCodeB…
ziadhany 86a1c65
Update Alpine to avoid fetching the same URL links multiple times
ziadhany fdfef36
Update alpine linux so for every vulnerability id AdvisoryData
ziadhany 8da445f
Update Alpine pipeline to use aboutcode-mirror-alpine-secdb
ziadhany eff2f9b
Update advisories_count function
ziadhany d61a505
Update Alpine Linux to use the new AdvisoryDataV2
ziadhany 421c089
Merge branch 'main' into alpine-migrate
ziadhany File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
245 changes: 245 additions & 0 deletions
245
vulnerabilities/pipelines/v2_importers/alpine_linux_importer.py
This file contains hidden or 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,245 @@ | ||
| # | ||
| # 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 logging | ||
| from pathlib import Path | ||
| from typing import Any | ||
| from typing import Iterable | ||
| from typing import List | ||
| from typing import Mapping | ||
|
|
||
| from fetchcode.vcs import fetch_via_vcs | ||
| from packageurl import PackageURL | ||
| from univers.version_range import AlpineLinuxVersionRange | ||
| from univers.versions import InvalidVersion | ||
|
|
||
| from vulnerabilities.importer import AdvisoryDataV2 | ||
| from vulnerabilities.importer import AffectedPackageV2 | ||
| from vulnerabilities.importer import ReferenceV2 | ||
| from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipelineV2 | ||
| from vulnerabilities.references import WireSharkReferenceV2 | ||
| from vulnerabilities.references import XsaReferenceV2 | ||
| from vulnerabilities.references import ZbxReferenceV2 | ||
| from vulnerabilities.utils import get_advisory_url | ||
| from vulnerabilities.utils import load_json | ||
|
|
||
|
|
||
| class AlpineLinuxImporterPipeline(VulnerableCodeBaseImporterPipelineV2): | ||
| """Collect Alpine Linux advisories.""" | ||
|
|
||
| pipeline_id = "alpine_linux_importer_v2" | ||
| spdx_license_expression = "CC-BY-SA-4.0" | ||
| license_url = "https://secdb.alpinelinux.org/license.txt" | ||
| repo_url = "git+https://github.com/aboutcode-org/aboutcode-mirror-alpine-secdb/" | ||
|
|
||
| @classmethod | ||
| def steps(cls): | ||
| return ( | ||
| cls.clone, | ||
| cls.collect_and_store_advisories, | ||
| ) | ||
|
|
||
| def advisories_count(self) -> int: | ||
| base_path = Path(self.vcs_response.dest_dir) / "secdb" | ||
| return sum( | ||
| len(pkg.get("advisories", [])) | ||
| for data in (load_json(p) for p in base_path.rglob("*.json")) | ||
| for pkg in data.get("packages", []) | ||
| ) | ||
|
|
||
| def clone(self): | ||
| self.log(f"Cloning `{self.repo_url}`") | ||
| self.vcs_response = fetch_via_vcs(self.repo_url) | ||
|
|
||
| def collect_advisories(self) -> Iterable[AdvisoryDataV2]: | ||
| base_path = Path(self.vcs_response.dest_dir) / "secdb" | ||
| for file_path in base_path.glob("**/*.json"): | ||
| advisory_url = get_advisory_url( | ||
| file=file_path, | ||
| base_path=base_path, | ||
| url="https://secdb.alpinelinux.org/", | ||
| ) | ||
|
|
||
| record = load_json(file_path) | ||
| if not record or not record["packages"]: | ||
| self.log( | ||
| f'"packages" not found in {advisory_url!r}', | ||
| level=logging.DEBUG, | ||
| ) | ||
| continue | ||
| yield from process_record(record=record, url=advisory_url, logger=self.log) | ||
|
|
||
| def clean_downloads(self): | ||
| """Cleanup any temporary repository data.""" | ||
| if self.vcs_response: | ||
| self.log(f"Removing cloned repository") | ||
| self.vcs_response.delete() | ||
|
|
||
| def on_failure(self): | ||
| """Ensure cleanup is always performed on failure.""" | ||
| self.clean_downloads() | ||
|
|
||
|
|
||
| def check_for_attributes(record, logger) -> bool: | ||
| attributes = ["distroversion", "reponame", "archs"] | ||
| for attribute in attributes: | ||
| if attribute not in record: | ||
| logger( | ||
| f'"{attribute!r}" not found in {record!r}', | ||
| level=logging.DEBUG, | ||
| ) | ||
| return False | ||
| return True | ||
|
|
||
|
|
||
| def process_record(record: dict, url: str, logger: callable) -> Iterable[AdvisoryDataV2]: | ||
| """ | ||
| Return a list of AdvisoryData objects by processing data | ||
| present in that `record` | ||
| """ | ||
| if not record.get("packages"): | ||
| logger( | ||
| f'"packages" not found in this record {record!r}', | ||
| level=logging.DEBUG, | ||
| ) | ||
| return [] | ||
|
|
||
| for package in record["packages"]: | ||
| if not package["pkg"]: | ||
| logger( | ||
| f'"pkg" not found in this package {package!r}', | ||
| level=logging.DEBUG, | ||
| ) | ||
| continue | ||
| if not check_for_attributes(record, logger): | ||
| continue | ||
| yield from load_advisories( | ||
| pkg_infos=package["pkg"], | ||
| distroversion=record["distroversion"], | ||
| reponame=record["reponame"], | ||
| archs=record["archs"], | ||
| url=url, | ||
| logger=logger, | ||
| ) | ||
|
|
||
|
|
||
| def load_advisories( | ||
| pkg_infos: Mapping[str, Any], | ||
| distroversion: str, | ||
| reponame: str, | ||
| archs: List[str], | ||
| url: str, | ||
| logger: callable, | ||
| ) -> Iterable[AdvisoryDataV2]: | ||
| """ | ||
| Yield AdvisoryData by mapping data from `pkg_infos` | ||
| and form PURL for AffectedPackages by using | ||
| `distroversion`, `reponame`, `archs` | ||
| """ | ||
| if not pkg_infos.get("name"): | ||
| logger( | ||
| f'"name" is not available in package {pkg_infos!r}', | ||
| level=logging.DEBUG, | ||
| ) | ||
| return [] | ||
|
|
||
| for version, fixed_vulns in pkg_infos["secfixes"].items(): | ||
| if not fixed_vulns: | ||
| logger( | ||
| f"No fixed vulnerabilities in version {version!r}", | ||
| level=logging.DEBUG, | ||
| ) | ||
| continue | ||
|
|
||
| # fixed_vulns is a list of strings and each string is a space-separated | ||
| # list of aliases and CVES | ||
| for vuln_ids in fixed_vulns: | ||
| aliases = vuln_ids.strip().split(" ") | ||
| vuln_id = aliases[0] | ||
|
|
||
| references = [] | ||
| if vuln_id.startswith("XSA"): | ||
| references.append(XsaReferenceV2.from_id(xsa_id=vuln_id)) | ||
|
|
||
| elif vuln_id.startswith("ZBX"): | ||
| references.append(ZbxReferenceV2.from_id(zbx_id=vuln_id)) | ||
|
|
||
| elif vuln_id.startswith("wnpa-sec"): | ||
| references.append(WireSharkReferenceV2.from_id(wnpa_sec_id=vuln_id)) | ||
|
|
||
| elif vuln_id.startswith("CVE"): | ||
| references.append( | ||
| ReferenceV2( | ||
| reference_id=vuln_id, | ||
| url=f"https://nvd.nist.gov/vuln/detail/{vuln_id}", | ||
| ) | ||
| ) | ||
|
|
||
| qualifiers = { | ||
| "distroversion": distroversion, | ||
| "reponame": reponame, | ||
| } | ||
|
|
||
| affected_packages = [] | ||
|
|
||
| fixed_version_range = None | ||
| try: | ||
| fixed_version_range = AlpineLinuxVersionRange.from_versions([version]) | ||
| except InvalidVersion as e: | ||
| logger( | ||
| f"{version!r} is not a valid AlpineVersion {e!r}", | ||
| level=logging.DEBUG, | ||
| ) | ||
|
|
||
| if not isinstance(archs, List): | ||
| logger( | ||
| f"{archs!r} is not of `List` instance", | ||
| level=logging.DEBUG, | ||
| ) | ||
| continue | ||
|
|
||
| if archs and fixed_version_range: | ||
| for arch in archs: | ||
| qualifiers["arch"] = arch | ||
| purl = PackageURL( | ||
| type="apk", | ||
| namespace="alpine", | ||
| name=pkg_infos["name"], | ||
| qualifiers=qualifiers, | ||
| ) | ||
| affected_packages.append( | ||
| AffectedPackageV2( | ||
| package=purl, | ||
| fixed_version_range=fixed_version_range, | ||
| ) | ||
| ) | ||
|
|
||
| if not archs and fixed_version_range: | ||
| # there is no arch, this is not an arch-specific package | ||
| purl = PackageURL( | ||
| type="apk", | ||
| namespace="alpine", | ||
| name=pkg_infos["name"], | ||
| qualifiers=qualifiers, | ||
| ) | ||
| affected_packages.append( | ||
| AffectedPackageV2( | ||
| package=purl, | ||
| fixed_version_range=fixed_version_range, | ||
| ) | ||
| ) | ||
|
|
||
| advisory_id = f"{pkg_infos['name']}/{qualifiers['distroversion']}/{version}/{vuln_id}" | ||
| yield AdvisoryDataV2( | ||
| advisory_id=advisory_id, | ||
| aliases=aliases, | ||
| references=references, | ||
| affected_packages=affected_packages, | ||
| url=url, | ||
| ) | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.