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

Fix issues from PR #136 #140

Merged
merged 2 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ jobs:
- name: Setup Poetry
run: python -m pip install poetry
- name: Setup Conan
run: |
python -m pip install conan
conan profile detect
run: python -m pip install conan
- name: Setup Gradle
uses: gradle/gradle-build-action@v2
with:
Expand Down
19 changes: 13 additions & 6 deletions commands/audit/sca/conan/conan.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os/exec"

"github.com/jfrog/gofrog/datastructures"
"github.com/jfrog/gofrog/io"
"github.com/jfrog/gofrog/version"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
Expand Down Expand Up @@ -95,6 +96,17 @@ type conanGraphOutput struct {
} `json:"graph"`
}

func calculateUniqueDependencies(nodes map[string]conanRef) []string {
uniqueDepsSet := datastructures.MakeSet[string]()
for id, dep := range nodes {
if id == "0" { // ignore the root node
continue
}
uniqueDepsSet.Add(dep.NodeName())
}
return uniqueDepsSet.ToSlice()
}

func calculateDependencies(executablePath, workingDir string, params utils.AuditParams) (dependencyTrees []*xrayUtils.GraphNode, uniqueDeps []string, err error) {
graphInfo := append([]string{"info", ".", "--format=json"}, params.Args()...)
conanGraphInfoContent, err := getConanCmd(executablePath, workingDir, "graph", graphInfo...).RunWithOutput()
Expand All @@ -114,12 +126,7 @@ func calculateDependencies(executablePath, workingDir string, params utils.Audit
}
dependencyTrees = append(dependencyTrees, rootNode)

for id, dep := range output.Graph.Nodes {
if id == "0" {
continue
}
uniqueDeps = append(uniqueDeps, dep.NodeName())
}
uniqueDeps = calculateUniqueDependencies(output.Graph.Nodes)

return
}
Expand Down
19 changes: 17 additions & 2 deletions commands/audit/sca/conan/conan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var expectedResult = &xrayUtils.GraphNode{
{Id: "conan://meson:1.4.1", Nodes: []*xrayUtils.GraphNode{{Id: "conan://ninja:1.11.1"}}},
},
}
var expectedUniqueDeps = []string{"conan://openssl:3.0.9", "conan://zlib:1.3.1", "conan://meson:1.4.1", "conan://ninja:1.11.1"}

func TestParseConanDependencyTree(t *testing.T) {
_, cleanUp := sca.CreateTestWorkspace(t, filepath.Join("other", "conan"))
Expand All @@ -41,14 +42,28 @@ func TestParseConanDependencyTree(t *testing.T) {
}

func TestBuildDependencyTree(t *testing.T) {
_, cleanUp := sca.CreateTestWorkspace(t, filepath.Join("projects", "package-managers", "conan"))
dir, cleanUp := sca.CreateTestWorkspace(t, filepath.Join("projects", "package-managers", "conan"))
defer cleanUp()
expectedUniqueDeps := []string{"conan://openssl:3.0.9", "conan://zlib:1.3.1", "conan://meson:1.4.1", "conan://ninja:1.11.1"}
params := &utils.AuditBasicParams{}
params.SetConanProfile(filepath.Join(dir, "profile"))
graph, uniqueDeps, err := BuildDependencyTree(params)
assert.NoError(t, err)
if !tests.CompareTree(expectedResult, graph[0]) {
t.Errorf("expected %+v, got: %+v", expectedResult.Nodes, graph)
}
assert.ElementsMatch(t, uniqueDeps, expectedUniqueDeps, "First is actual, Second is Expected")
}

func TestCalculateUniqueDeps(t *testing.T) {
input := map[string]conanRef{
"0": {Name: "root node", Version: "please ignore"}, // root node, should be removed
"1": {Name: "zlib", Version: "1.3.1"},
"2": {Name: "openssl", Version: "3.0.9"},
"3": {Name: "meson", Version: "1.4.1"},
"4": {Name: "ninja", Version: "1.11.1"},
"5": {Name: "openssl", Version: "3.0.9"}, // duplicate, should be removed
}

uniqueDeps := calculateUniqueDependencies(input)
assert.ElementsMatch(t, uniqueDeps, expectedUniqueDeps, "First is actual, Second is Expected")
}
8 changes: 8 additions & 0 deletions tests/testdata/projects/package-managers/conan/profile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[settings]
arch=x86_64
build_type=Release
compiler=gcc
compiler.cppstd=gnu17
compiler.libcxx=libstdc++11
compiler.version=11
os=Linux
5 changes: 5 additions & 0 deletions utils/auditbasicparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ func (abp *AuditBasicParams) SetNpmScope(depType string) *AuditBasicParams {
return abp
}

func (abp *AuditBasicParams) SetConanProfile(file string) *AuditBasicParams {
abp.args = append(abp.args, "--profile:build", file)
return abp
}

func (abp *AuditBasicParams) OutputFormat() format.OutputFormat {
return abp.outputFormat
}
Expand Down
Loading