-
Notifications
You must be signed in to change notification settings - Fork 989
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
Add format 'cyclonedx' as output format for the graph command #14405
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .graph import format_graph_html | ||
from .graph import format_graph_dot | ||
from .graph import format_graph_json | ||
from .graph import format_graph_cyclonedx |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,3 +144,44 @@ def format_graph_json(result): | |
cli_out_write(json_result) | ||
if graph.error: | ||
raise graph.error | ||
|
||
|
||
def format_graph_cyclonedx(result): | ||
""" | ||
# creates a CycloneDX JSON according to https://cyclonedx.org/docs/1.4/json/ | ||
""" | ||
def licenses(conanfilelic): | ||
def entry(id): | ||
return {"license": { | ||
"id": id | ||
}} | ||
if conanfilelic is None: | ||
return [] | ||
elif isinstance(conanfilelic, str): | ||
return [entry(conanfilelic)] | ||
else: | ||
return [entry(i) for i in conanfilelic] | ||
|
||
result["graph"].serialize() # fills ids | ||
deps = result["graph"].nodes[1:] # first node is app itself | ||
cyclonedx = { | ||
"bomFormat": "CycloneDX", | ||
"specVersion": "1.4", | ||
"version": 1, | ||
"dependencies": [n.id for n in deps], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code looks wrong, according to CycloneDX spec. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, that is not correct. I will fix it in the conan extension |
||
"components": [ | ||
{ | ||
"type": "library", | ||
"bom-ref": n.id, | ||
"purl": n.package_url().to_string(), | ||
"licenses": licenses(n.conanfile.license), | ||
"name": n.name, | ||
"version": n.conanfile.version, | ||
"supplier": { | ||
"url": [n.conanfile.homepage] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. homepage - why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the advantage of using that over supplier? external references are not part of the BOM: https://cyclonedx.org/docs/1.4/json/#components_items_supplier VS https://cyclonedx.org/docs/1.4/json/#components_items_externalReferences There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It's a matter of correctness. From what I've seen, I found that Linking a component's metadata and URLs via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I will use the homepage attribute and move it to |
||
} | ||
} for n in deps | ||
] | ||
} | ||
json_result = json.dumps(cyclonedx, indent=4) | ||
cli_out_write(json_result) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
from collections import OrderedDict | ||
|
||
from packageurl import PackageURL | ||
|
||
from conans.model.package_ref import PkgReference | ||
from conans.model.recipe_ref import RecipeReference | ||
|
||
|
@@ -229,6 +231,29 @@ def serialize(self): | |
result["test"] = self.test | ||
return result | ||
|
||
def package_url(self): | ||
""" | ||
Creates a PURL following https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you want me to change the comment, or is there an error in how I build the JSON? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wanted to put a more precise link here, so nobody needs to scroll trough the whole document. |
||
""" | ||
qualifiers = { | ||
"prev": self.prev | ||
} | ||
if self.ref.user: | ||
qualifiers["user"] = self.ref.user | ||
if self.ref.channel: | ||
qualifiers["channel"] = self.ref.channel | ||
if self.ref.revision: | ||
qualifiers["rref"] = self.ref.revision | ||
if self.remote: | ||
qualifiers["repository_url"] = self.remote | ||
else: | ||
qualifiers["repository_url"] = "https://center.conan.io" | ||
return PackageURL( | ||
type="conan", | ||
name=self.name, | ||
version=str(self.ref.version), | ||
qualifiers=qualifiers) | ||
|
||
def overrides(self): | ||
|
||
def transitive_subgraph(): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ fasteners>=0.15 | |
distro>=1.4.0, <=1.8.0; sys_platform == 'linux' or sys_platform == 'linux2' | ||
Jinja2>=3.0, <4.0.0 | ||
python-dateutil>=2.8.0, <3 | ||
packageurl-python>=0.10.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are avoiding adding new dependencies like this one, unless it is extremely necessary. Every additional Python dependency has proven to be a liability (the state of dependency management in Python is not great), so we prefer to avoid this. |
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.
would you add the schema to the json, so its more clear how to validate the result?