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

feat(go): construct dependencies of go.mod main module in the parser #7977

Merged
merged 6 commits into from
Nov 22, 2024

Conversation

DmitriyLewen
Copy link
Contributor

Description

Continuation of #7973

Related PRs

Checklist

  • I've read the guidelines for contributing to this repository.
  • I've followed the conventions in the PR title.
  • I've added tests that prove my fix is effective or that my feature works.
  • I've updated the documentation with the relevant information (if needed).
  • I've added usage information (if the PR introduces new options)
  • I've included a "before" and "after" example to the description (if the PR is a user interface change).

@DmitriyLewen DmitriyLewen self-assigned this Nov 21, 2024
@DmitriyLewen DmitriyLewen marked this pull request as ready for review November 21, 2024 10:11
return pkg.ID, pkg.Relationship == ftypes.RelationshipDirect
})

if len(dependsOn) > 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since we need to put empty elements in CycloneDX, shouldn't we include this relationship regardless of whether len(dependsOn) is empty?

Components or services that do not have their own dependencies must be declared as empty elements within the graph.

https://cyclonedx.org/docs/1.6/json/#dependencies

Copy link
Contributor Author

@DmitriyLewen DmitriyLewen Nov 21, 2024

Choose a reason for hiding this comment

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

There is no difference now .
pkg.DependsOn will be nil (with len(dependsOn) > 0 check) or empty slice (without this check):

pkgs[i].DependsOn = deps[pkg.ID]

When we add relationships in BOM core - we just check length of DependsOn field:

trivy/pkg/sbom/io/encode.go

Lines 249 to 261 in 5dd94eb

for _, dep := range pkg.DependsOn {
dependsOn, ok := dependencies[dep]
if !ok {
continue
}
e.bom.AddRelationship(c, dependsOn, core.RelationshipDependsOn)
}
// Components that do not have their own dependencies MUST be declared as empty elements within the graph.
// TODO: Should check if the component has actually no dependencies or the dependency graph is not supported.
if len(pkg.DependsOn) == 0 {
e.bom.AddRelationship(c, nil, "")
}

But it can help with TODO: Should check if the component has actually no dependencies or the dependency graph is not supported..
empty slice - pkg supports dependency graph, but doesn't have dependencies.
nil - pkg doesn't support dependency graph.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated in 29d73fb

Copy link
Collaborator

@knqyf263 knqyf263 left a comment

Choose a reason for hiding this comment

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

In some cases, the dependency relationships cannot be correctly identified due to missing $GOPATH or $GOPATH/**/go.mod. We need to add a relationship between a main module and those indirect packages.

// $GOPATH/pkg/mod
modPath := filepath.Join(gopath, "pkg", "mod")
if !fsutils.DirExists(modPath) {
a.logger.Debug("GOPATH not found. Need 'go mod download' to fill licenses and dependency relationships",
log.String("GOPATH", modPath))
return nil
}

} else if dep.ID == "" {
// go.mod not found
continue

Signed-off-by: knqyf263 <[email protected]>
@knqyf263 knqyf263 added this pull request to the merge queue Nov 22, 2024
Merged via the queue into aquasecurity:main with commit 5448ba2 Nov 22, 2024
12 checks passed
@DmitriyLewen DmitriyLewen deleted the feat/gomod-deps branch November 28, 2024 05:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants