-
Notifications
You must be signed in to change notification settings - Fork 26
Generate NuGet PURL's #642
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
Open
KendallHarterAtWork
wants to merge
11
commits into
main
Choose a base branch
from
nuget-package-url
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c7572da
Generate NuGet PURL's
KendallHarterAtWork 397ac52
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4a7ea2f
Address pylint problems
KendallHarterAtWork 14d23af
Download and check .nuget packages now
KendallHarterAtWork ca162e6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 40f0e68
Remove duplicate requests from dependencies
KendallHarterAtWork 4d5591e
Adjust based on PR comments
KendallHarterAtWork 9abeef6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] eee03d8
Remove configuring request timeout until TUI support for config numbe…
KendallHarterAtWork 7b4e49b
Fix some minor bugs
nightlark f852cd3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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
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
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,114 @@ | ||
| # Copyright 2026 Lawrence Livermore National Security, LLC | ||
| # See the top-level LICENSE file for details. | ||
| # | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| import io | ||
| import pathlib | ||
| import zipfile | ||
|
|
||
| import requests | ||
| from loguru import logger | ||
|
|
||
| import surfactant.plugin | ||
| from surfactant.sbomtypes import SBOM, NameEntry, Relationship, Software | ||
|
|
||
|
|
||
|
KendallHarterAtWork marked this conversation as resolved.
|
||
| class __NuGetManager: | ||
|
KendallHarterAtWork marked this conversation as resolved.
Outdated
|
||
| def __init__(self): | ||
| self.disabled = True | ||
| self.package_base_addresses = [] | ||
|
|
||
|
KendallHarterAtWork marked this conversation as resolved.
|
||
| def init_urls(self): | ||
| # Get the base PackageBaseAddress URL | ||
| r = requests.get("https://api.nuget.org/v3/index.json") | ||
|
nightlark marked this conversation as resolved.
Outdated
|
||
| if r.status_code != 200: | ||
| logger.warning(f"NuGet API returned {r.status_code}; disabling") | ||
| self.disabled = True | ||
| return | ||
|
|
||
| self.disabled = False | ||
| self.package_base_addresses = [ | ||
| x["@id"] for x in r.json()["resources"] if x["@type"] == "PackageBaseAddress/3.0.0" | ||
| ] | ||
|
KendallHarterAtWork marked this conversation as resolved.
|
||
| # remove trailing "/" if present | ||
| for i, pba in enumerate(self.package_base_addresses): | ||
| if pba[-1] == "/": | ||
| self.package_base_addresses[i] = pba[:-1] | ||
|
|
||
| def download_nuget(self, package_name: str, package_version: str) -> zipfile.ZipFile | None: | ||
| for url in self.package_base_addresses: | ||
| pn_low = package_name.lower() | ||
| ver_low = package_version.lower() | ||
| r = requests.get(f"{url}/{pn_low}/{ver_low}/{pn_low}.{ver_low}.nupkg", stream=True) | ||
| if r.status_code != 200: | ||
| continue | ||
| try: | ||
| # For some reason, have to wrap r.raw (a file-like object) | ||
| # into an io.BytesIO object to get it to read correctly. | ||
| # No idea why. | ||
| return zipfile.ZipFile(io.BytesIO(r.raw.read())) | ||
| except zipfile.BadZipFile as e: | ||
| logger.warning(f"Could not unpack {pn_low}.{ver_low}.nupkg - {e}") | ||
| return None | ||
|
KendallHarterAtWork marked this conversation as resolved.
|
||
|
|
||
| def file_is_in_package(self, file_name: str, package_name: str, package_version: str) -> bool: | ||
| if nuget := self.download_nuget(package_name, package_version): | ||
| for f in nuget.infolist(): | ||
| if pathlib.Path(f.filename).name == file_name: | ||
| return True | ||
| return False | ||
|
|
||
|
nightlark marked this conversation as resolved.
Outdated
|
||
| def get_package_url( | ||
|
KendallHarterAtWork marked this conversation as resolved.
|
||
| self, file_name: str, package_name: str, package_version: str | ||
| ) -> str | None: | ||
| if self.disabled: | ||
| return None | ||
|
|
||
| for url in self.package_base_addresses: | ||
| r = requests.get(f"{url}/{package_name.lower()}/index.json") | ||
| if r.status_code != 200: | ||
| continue | ||
|
|
||
| if versions := r.json()["versions"]: | ||
| if package_version in versions: | ||
| # Found a matching package version, check that specific version | ||
| if self.file_is_in_package(file_name, package_name, package_version): | ||
| return f"pkg:nuget/{package_name}@{package_version}" | ||
| else: | ||
| # Unknown package version; check the latest package version | ||
| latest_version = versions[-1] | ||
| if self.file_is_in_package(file_name, package_name, latest_version): | ||
| return f"pkg:nuget/{package_name}" | ||
|
|
||
| return None | ||
|
nightlark marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| __nuget = __NuGetManager() | ||
|
KendallHarterAtWork marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| @surfactant.plugin.hookimpl | ||
| def init_hook(command_name: str | None = None): | ||
| __nuget.init_urls() | ||
|
KendallHarterAtWork marked this conversation as resolved.
Outdated
|
||
|
|
||
|
KendallHarterAtWork marked this conversation as resolved.
|
||
|
|
||
| @surfactant.plugin.hookimpl | ||
| def establish_relationships(sbom: SBOM, software: Software, metadata) -> list[Relationship] | None: | ||
| """Checks NuGet for a package name and adds it as a name if it exists""" | ||
|
|
||
| if __nuget.disabled: | ||
|
KendallHarterAtWork marked this conversation as resolved.
Outdated
|
||
| return | ||
|
|
||
| if "dotnetAssembly" not in metadata: | ||
| logger.debug( | ||
| f"[nuget_purl] Skipping: No dotnetAssembly info for NuGet PURL in {software.UUID}" | ||
| ) | ||
| return | ||
|
KendallHarterAtWork marked this conversation as resolved.
Outdated
|
||
|
|
||
| for dna in metadata["dotnetAssembly"]: | ||
| if software.fileName: | ||
| for name in software.fileName: | ||
| if purl := __nuget.get_package_url(name, dna["Name"], dna["Version"]): | ||
|
nightlark marked this conversation as resolved.
Outdated
|
||
| if software.name is None: | ||
| software.name = [] | ||
| software.name.append(NameEntry(purl, "PURL")) | ||
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.