-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix * drop subgraph param * Update conan/tools/sbom/cyclonedx.py Co-authored-by: James <[email protected]> --------- Co-authored-by: Carlos Zoido <[email protected]> Co-authored-by: James <[email protected]>
- Loading branch information
1 parent
5eadc26
commit 5d07f4d
Showing
3 changed files
with
115 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from conan.tools.sbom.cyclonedx import cyclonedx_1_4 |
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 |
---|---|---|
|
@@ -13,10 +13,10 @@ | |
import os | ||
from conan.errors import ConanException | ||
from conan.api.output import ConanOutput | ||
from conan.tools.sbom.cyclonedx import cyclonedx_1_4 | ||
from conan.tools.sbom import cyclonedx_1_4 | ||
def post_package(conanfile): | ||
sbom_cyclonedx_1_4 = cyclonedx_1_4(conanfile.subgraph) | ||
sbom_cyclonedx_1_4 = cyclonedx_1_4(conanfile, add_build=%s, add_tests=%s) | ||
metadata_folder = conanfile.package_metadata_folder | ||
file_name = "sbom.cdx.json" | ||
with open(os.path.join(metadata_folder, file_name), 'w') as f: | ||
|
@@ -28,14 +28,28 @@ def post_package(conanfile): | |
def hook_setup_post_package(): | ||
tc = TestClient() | ||
hook_path = os.path.join(tc.paths.hooks_path, "hook_sbom.py") | ||
save(hook_path, sbom_hook_post_package) | ||
save(hook_path, sbom_hook_post_package % ("True", "True")) | ||
return tc | ||
|
||
@pytest.fixture() | ||
def hook_setup_post_package_no_tool_requires(): | ||
tc = TestClient() | ||
hook_path = os.path.join(tc.paths.hooks_path, "hook_sbom.py") | ||
save(hook_path, sbom_hook_post_package % ("False", "True")) | ||
return tc | ||
|
||
@pytest.fixture() | ||
def hook_setup_post_package_no_test(): | ||
tc = TestClient() | ||
hook_path = os.path.join(tc.paths.hooks_path, "hook_sbom.py") | ||
save(hook_path, sbom_hook_post_package % ("True", "False")) | ||
return tc | ||
|
||
@pytest.fixture() | ||
def hook_setup_post_package_tl(transitive_libraries): | ||
tc = transitive_libraries | ||
hook_path = os.path.join(tc.paths.hooks_path, "hook_sbom.py") | ||
save(hook_path, sbom_hook_post_package) | ||
save(hook_path, sbom_hook_post_package % ("True", "True")) | ||
return tc | ||
|
||
|
||
|
@@ -65,17 +79,66 @@ def test_sbom_generation_skipped_dependencies(hook_setup_post_package): | |
# A skipped dependency also shows up in the sbom | ||
assert "pkg:conan/[email protected]?rref=6a99f55e933fb6feeb96df134c33af44" in content | ||
|
||
def test_sbom_generation_no_tool_requires(hook_setup_post_package_no_tool_requires): | ||
tc = hook_setup_post_package_no_tool_requires | ||
tc.save({"app/conanfile.py": GenConanfile("app", "1.0") | ||
.with_package_type("application"), | ||
"conanfile.py": GenConanfile("foo", "1.0").with_tool_requires("app/1.0")}) | ||
tc.run("create app") | ||
tc.run("create .") | ||
create_layout = tc.created_layout() | ||
|
||
cyclone_path = os.path.join(create_layout.metadata(), "sbom.cdx.json") | ||
content = tc.load(cyclone_path) | ||
|
||
assert "pkg:conan/app" not in content | ||
|
||
def test_sbom_generation_transitive_test_requires(hook_setup_post_package_no_test): | ||
tc = hook_setup_post_package_no_test | ||
tc.save({"test_re/conanfile.py": GenConanfile("test_re", "1.0"), | ||
"app/conanfile.py": GenConanfile("app", "1.0") | ||
.with_package_type("application") | ||
.with_test_requires("test_re/1.0"), | ||
"conanfile.py": GenConanfile("foo", "1.0").with_tool_requires("app/1.0")}) | ||
tc.run("create test_re") | ||
|
||
tc.run("create app") | ||
create_layout = tc.created_layout() | ||
cyclone_path = os.path.join(create_layout.metadata(), "sbom.cdx.json") | ||
content = tc.load(cyclone_path) | ||
assert "pkg:conan/[email protected]" not in content | ||
|
||
tc.run("create .") | ||
create_layout = tc.created_layout() | ||
cyclone_path = os.path.join(create_layout.metadata(), "sbom.cdx.json") | ||
content = tc.load(cyclone_path) | ||
assert "pkg:conan/[email protected]" not in content | ||
|
||
def test_sbom_generation_dependency_test_require(hook_setup_post_package_no_test): | ||
tc = hook_setup_post_package_no_test | ||
tc.save({"special/conanfile.py": GenConanfile("special", "1.0"), | ||
"foo/conanfile.py": GenConanfile("foo", "1.0") | ||
.with_test_requires("special/1.0"), | ||
"conanfile.py": GenConanfile("bar", "1.0").with_tool_requires("foo/1.0").with_require("special/1.0")}) | ||
tc.run("create special") | ||
tc.run("create foo") | ||
|
||
tc.run("create .") | ||
create_layout = tc.created_layout() | ||
cyclone_path = os.path.join(create_layout.metadata(), "sbom.cdx.json") | ||
content = tc.load(cyclone_path) | ||
assert "pkg:conan/[email protected]" in content | ||
|
||
# Using the sbom tool with "conan install" | ||
sbom_hook_post_generate = """ | ||
import json | ||
import os | ||
from conan.errors import ConanException | ||
from conan.api.output import ConanOutput | ||
from conan.tools.sbom.cyclonedx import cyclonedx_1_4 | ||
from conan.tools.sbom import cyclonedx_1_4 | ||
def post_generate(conanfile): | ||
sbom_cyclonedx_1_4 = cyclonedx_1_4(conanfile.subgraph) | ||
sbom_cyclonedx_1_4 = cyclonedx_1_4(conanfile, name=%s) | ||
generators_folder = conanfile.generators_folder | ||
file_name = "sbom.cdx.json" | ||
os.mkdir(os.path.join(generators_folder, "sbom")) | ||
|
@@ -88,7 +151,7 @@ def post_generate(conanfile): | |
def hook_setup_post_generate(): | ||
tc = TestClient() | ||
hook_path = os.path.join(tc.paths.hooks_path, "hook_sbom.py") | ||
save(hook_path, sbom_hook_post_generate) | ||
save(hook_path, sbom_hook_post_generate % "None") | ||
return tc | ||
|
||
def test_sbom_generation_install_requires(hook_setup_post_generate): | ||
|
@@ -136,3 +199,17 @@ def test_sbom_generation_install_path_txt(hook_setup_post_generate): | |
#foo -> dep | ||
tc.run("install .") | ||
assert os.path.exists(os.path.join(tc.current_folder, "sbom", "sbom.cdx.json")) | ||
|
||
@pytest.mark.parametrize("name, result", [ | ||
("None", "conan-sbom"), | ||
('"custom-name"', "custom-name") | ||
]) | ||
def test_sbom_generation_custom_name(name, result): | ||
tc = TestClient() | ||
hook_path = os.path.join(tc.paths.hooks_path, "hook_sbom.py") | ||
save(hook_path, sbom_hook_post_generate % name) | ||
|
||
tc.save({"conanfile.py": GenConanfile()}) | ||
tc.run("install .") | ||
assert os.path.exists(os.path.join(tc.current_folder, "sbom", "sbom.cdx.json")) | ||
assert f'"name": "{result}"' in tc.load(os.path.join(tc.current_folder, "sbom", "sbom.cdx.json")) |