-
Notifications
You must be signed in to change notification settings - Fork 985
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
Sbom command and plugin support #17203
Draft
ErniGH
wants to merge
14
commits into
conan-io:develop2
Choose a base branch
from
ErniGH:erni/sbom-implementation
base: develop2
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.
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c2bcece
initial
ErniGH 7d10e59
use sbom in install
ErniGH 005fb39
Revert "initial"
ErniGH 94dd274
Revert "use sbom in install"
ErniGH e4f9591
wip
ErniGH bc25a61
wip
ErniGH 2511245
wip
ErniGH 036fb82
sbom generator
ErniGH 7e4cf57
review suggestions
AbrilRBS 00c5781
Cleanup
AbrilRBS b7f90ed
Update conans/client/graph/sbom.py
AbrilRBS 51f1189
rename and move sbom method, move spdx script to different file and
ErniGH 73ea04e
add subgraph implementation and test
ErniGH e1833ae
use new subgraph api, update generator, spdx and test
ErniGH 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 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
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
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,7 @@ | ||
from conans.client.graph.spdx import spdx_json_generator | ||
from conan.internal.cache.home_paths import HomePaths | ||
|
||
def migrate_sbom_file(cache_folder): | ||
from conans.client.migrations import update_file | ||
sbom_path = HomePaths(cache_folder).sbom_manifest_plugin_path | ||
update_file(sbom_path, spdx_json_generator) |
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,56 @@ | ||
spdx_json_generator = """ | ||
import time | ||
import json | ||
from datetime import datetime, timezone | ||
from conan import conan_version | ||
from conan.errors import ConanException | ||
|
||
import pathlib | ||
|
||
def generate_sbom(graph, **kwargs): | ||
name = graph.root.name | ||
version = graph.root.ref.version | ||
date = datetime.fromtimestamp(time.time(), tz=timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ') | ||
packages = [] | ||
for dependency in graph.nodes: | ||
packages.append( | ||
{ | ||
"name": dependency.ref.name, | ||
"SPDXID": f"SPDXRef-{dependency.ref}", | ||
"version": str(dependency.ref.version), | ||
"license": dependency.conanfile.license or "NOASSERTION", | ||
}) | ||
files = [] | ||
# https://spdx.github.io/spdx-spec/v2.2.2/package-information/ | ||
data = { | ||
"SPDXVersion": "SPDX-2.2", | ||
"DataLicense": "CC0-1.0", | ||
"SPDXID": "SPDXRef-DOCUMENT", | ||
"DocumentName": f"{name}-{version}", | ||
"DocumentNamespace": f"http://spdx.org/spdxdocs/{name}-{version}-{date}", # the date or hash to make it unique | ||
"Creator": f"Tool: Conan-{conan_version}", | ||
"Created": date, #YYYY-MM-DDThh:mm:ssZ | ||
"Packages": [{ | ||
"PackageName": p["name"], | ||
"SPDXID": p["SPDXID"], | ||
"PackageVersion": p["version"], | ||
"PackageDownloadLocation": "NOASSERTION", | ||
"FilesAnalyzed": False, | ||
"PackageLicenseConcluded": p["license"], | ||
"PackageLicenseDeclared": p["license"], | ||
} for p in packages], | ||
# "Files": [{ | ||
# "FileName": f["path"], # Path to file | ||
# "SPDXID": f["SPDXID"], | ||
# "FileChecksum": f'{f["checksum_algorithm"]}: {f["checksum_algorithm_value"]}', | ||
# "LicenseConcluded": f["licence"], | ||
# "LicenseInfoInFile": f["licence"], | ||
# "FileCopyrightText": "NOASSERTION" | ||
# } for f in files], | ||
} | ||
try: | ||
with open(f"../{name}-{version}.spdx.json", 'w') as f: | ||
json.dump(data, f, indent=4) | ||
except Exception as e: | ||
ConanException("error generating spdx file") | ||
""" |
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
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,44 @@ | ||
import json | ||
import os | ||
import textwrap | ||
|
||
from conan.test.assets.genconanfile import GenConanfile | ||
from conan.test.utils.tools import TestClient | ||
from conans.util.files import load | ||
|
||
|
||
def test_subgraph_reports(): | ||
c = TestClient() | ||
subgraph_hook = textwrap.dedent("""\ | ||
import os, json | ||
from conan.tools.files import save | ||
from conans.model.graph_lock import Lockfile | ||
def post_package(conanfile): | ||
subgraph = conanfile.subgraph | ||
save(conanfile, os.path.join(conanfile.package_folder, "..", "..", f"{conanfile.name}-conangraph.json"), | ||
json.dumps(subgraph.serialize(), indent=2)) | ||
save(conanfile, os.path.join(conanfile.package_folder, "..", "..", f"{conanfile.name}-conan.lock"), | ||
Lockfile(subgraph).dumps()) | ||
""") | ||
|
||
c.save_home({"extensions/hooks/subgraph_hook/hook_subgraph.py": subgraph_hook}) | ||
c.save({"dep/conanfile.py": GenConanfile("dep", "0.1"), | ||
"pkg/conanfile.py": GenConanfile("pkg", "0.1").with_requirement("dep/0.1"), | ||
"app/conanfile.py": GenConanfile("app", "0.1").with_requirement("pkg/0.1")}) | ||
c.run("export dep") | ||
c.run("export pkg") | ||
# app -> pkg -> dep | ||
c.run("create app --build=missing --format=json") | ||
|
||
app_graph = json.loads(load(os.path.join(c.cache.builds_folder, "app-conangraph.json"))) | ||
pkg_graph = json.loads(load(os.path.join(c.cache.builds_folder, "pkg-conangraph.json"))) | ||
dep_graph = json.loads(load(os.path.join(c.cache.builds_folder, "dep-conangraph.json"))) | ||
|
||
app_lock = json.loads(load(os.path.join(c.cache.builds_folder, "app-conan.lock"))) | ||
pkg_lock = json.loads(load(os.path.join(c.cache.builds_folder, "pkg-conan.lock"))) | ||
dep_lock = json.loads(load(os.path.join(c.cache.builds_folder, "dep-conan.lock"))) | ||
|
||
assert len(app_graph["nodes"]) == len(app_lock["requires"]) | ||
assert len(pkg_graph["nodes"]) == len(pkg_lock["requires"]) | ||
assert len(dep_graph["nodes"]) == len(dep_lock["requires"]) | ||
|
Empty file.
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,24 @@ | ||
from conan.test.assets.genconanfile import GenConanfile | ||
from conan.test.utils.tools import TestClient | ||
import os | ||
|
||
|
||
def test_sbom_generation_create(): | ||
tc = TestClient() | ||
tc.save({"dep/conanfile.py": GenConanfile("dep", "1.0"), | ||
"conanfile.py": GenConanfile("foo", "1.0").with_requires("dep/1.0")}) | ||
tc.run("export dep") | ||
tc.run("create . --build=missing") | ||
foo_layout = tc.created_layout() | ||
|
||
assert os.path.exists(os.path.join(foo_layout.build(), "foo-1.0.spdx.json")) | ||
|
||
def test_sbom_generation_install(): | ||
tc = TestClient() | ||
tc.save({"dep/conanfile.py": GenConanfile("dep", "1.0"), | ||
"conanfile.py": GenConanfile("foo", "1.0").with_requires("dep/1.0")}) | ||
tc.run("export dep") | ||
tc.run("create . --build=missing") | ||
|
||
tc.run("install --requires=foo/1.0") | ||
assert os.path.exists(os.path.join(tc.current_folder, "foo-1.0.spdx.json")) #TODO FIX this test |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which date should be here? Maybe the revision date is more correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the creation date identify when the SPDX document was originally created
https://spdx.github.io/spdx-spec/v2-draft/document-creation-information/#69-created-field