Skip to content
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

chore: improve cyclonedx-mvn bom file check to include files with cus… #524

Merged
merged 15 commits into from
Nov 24, 2023

Conversation

benmss
Copy link
Member

@benmss benmss commented Oct 19, 2023

Allows Macaron to discover cyclonedx-mvn SBOM files that have been created with a non-default name.
Does not provide support for custom directories or cases where multiple SBOM files end up in the expected target directory, as these would require POM parsing to properly detect.
Closes #518

@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Oct 19, 2023
@tromai
Copy link
Member

tromai commented Oct 19, 2023

Is it possible to have a unit test for the scenario of custom file name for the top path SBOM file?

@benmss benmss linked an issue Oct 22, 2023 that may be closed by this pull request
tromai
tromai previously approved these changes Oct 23, 2023
Copy link
Member

@tromai tromai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR looks good to me

@behnazh-w
Copy link
Member

behnazh-w commented Oct 31, 2023

Since the apache/maven repo that we analyze in the integration tests also customizes the name of the BOM file, it would make sense to change the pinned commit to the latest version and test the change in this PR as part of the integration tests.

@benmss benmss force-pushed the 518-support-named-cyclonedx-output branch from 04614ec to f738040 Compare November 6, 2023 23:15

# Collect all the dependency files recursively.
child_paths = [
Path(path)
for path in glob.glob(os.path.join(dir_path, "**", "target", self.file_name), recursive=True)
for path in glob.glob(
os.path.join(dir_path, "**", "target", "*.json" if top_path_altered else self.file_name), recursive=True
Copy link
Member

@tromai tromai Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would happen if there is multiple *.json files in the "children" taget directories, given that there exist a custom-named SBOM file in the top path?

Copy link
Member

@behnazh-w behnazh-w left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the BOM file can have a custom name, we also need to adjust the error message here and avoid printing the default file name to avoid confusion:

raise CycloneDXParserError(f"Unable to locate any BOM files at: {str(file_path.parent)}.")

src/macaron/dependency_analyzer/cyclonedx_mvn.py Outdated Show resolved Hide resolved
Signed-off-by: Ben Selwyn-Smith <[email protected]>
@behnazh-w behnazh-w requested a review from tromai November 23, 2023 22:54
if Path(path) != top_path
]

# Ensure recursively found SBOMs are at most one per directory.
child_paths_set = set()
Copy link
Member

@tromai tromai Nov 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This set contains the parent directory names of all children SBOM files. I think we could re-name it to something like child_sbom_dir_names

@tromai tromai dismissed their stale review November 24, 2023 01:38

I left some comments for the latest changes so I dismissed this approval.

…in projects and subprojects, and clarify in comments

Signed-off-by: Ben Selwyn-Smith <[email protected]>
directory. The name of the file is not considered because projects can be configured to produce a custom named
SBOM, which cannot be overridden if included at the parent POM level.
The presence of multiple JSON files within a target directory differs too greatly from the expectations of the
plugin's output. It is for this reason that an error is thrown in such cases.
Copy link
Member

@tromai tromai Nov 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
plugin's output. It is for this reason that an error is thrown in such cases.
plugin's output. It is for this reason that these cases are treated as error and this method will return an empty dictionary.

I think it's better to be explicitly on how the error are handled. We don't throw any error in this method, but only return an empty dictionary. Please feel free to change the comment accordingly, my suggestion here is only for references only.

@behnazh-w behnazh-w self-requested a review November 24, 2023 05:42
return {}
top_path = Path(possible_paths[0])
top_path_altered = True
possible_paths = glob.glob(os.path.join(dir_path, "target", "*.json"))
Copy link
Member

@behnazh-w behnazh-w Nov 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the previous implementation which first checks for the expected file name. If that file cannot be found, we can look for other JSON files. This can avoid missing the BOM files when a project uses expected names but also produces other JSON files.

@benmss benmss merged commit 2971708 into staging Nov 24, 2023
9 checks passed
@nathanwn nathanwn deleted the 518-support-named-cyclonedx-output branch December 14, 2023 02:23
art1f1c3R pushed a commit that referenced this pull request Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OCA Verified All contributors have signed the Oracle Contributor Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support custom named cyclonedx maven bom output
3 participants