Skip to content

Commit

Permalink
feat(gradle): add *gradle.lockfile parser (aquasecurity#127)
Browse files Browse the repository at this point in the history
* feat: add *gradle.lockfile parser

* refactor
  • Loading branch information
DmitriyLewen authored and Sq34sy committed Jul 28, 2023
1 parent ba86e95 commit f6e65e5
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
39 changes: 39 additions & 0 deletions pkg/gradle/lockfile/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package lockfile

import (
"bufio"
"strings"

dio "github.com/aquasecurity/go-dep-parser/pkg/io"
"github.com/aquasecurity/go-dep-parser/pkg/types"
"github.com/aquasecurity/go-dep-parser/pkg/utils"
)

type Parser struct{}

func NewParser() types.Parser {
return &Parser{}
}

func (Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
var libs []types.Library
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "#") { // skip comments
continue
}

// dependency format: group:artifact:version=classPaths
dep := strings.Split(line, ":")
if len(dep) != 3 { // skip the last line with lists of empty configurations
continue
}
libs = append(libs, types.Library{
Name: strings.Join(dep[:2], ":"),
Version: strings.Split(dep[2], "=")[0], // remove classPaths
})

}
return utils.UniqueLibraries(libs), nil, nil
}
52 changes: 52 additions & 0 deletions pkg/gradle/lockfile/parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package lockfile

import (
"os"
"testing"

"github.com/aquasecurity/go-dep-parser/pkg/types"
"github.com/stretchr/testify/assert"
)

func TestParser_Parse(t *testing.T) {
tests := []struct {
name string
inputFile string
want []types.Library
}{
{
name: "happy path",
inputFile: "testdata/happy.lockfile",
want: []types.Library{
{
Name: "cglib:cglib-nodep",
Version: "2.1.2",
},
{
Name: "org.springframework:spring-asm",
Version: "3.1.3.RELEASE",
},
{
Name: "org.springframework:spring-beans",
Version: "5.0.5.RELEASE",
},
},
},
{
name: "empty",
inputFile: "testdata/empty.lockfile",
want: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
parser := NewParser()
f, err := os.Open(tt.inputFile)
assert.NoError(t, err)

libs, _, _ := parser.Parse(f)
assert.Equal(t, tt.want, libs)
})
}
}
4 changes: 4 additions & 0 deletions pkg/gradle/lockfile/testdata/empty.lockfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This is a Gradle generated file for dependency locking.
# Manual edits can break the build and are not advised.
# This file is expected to be part of source control.
empty=incomingCatalog
8 changes: 8 additions & 0 deletions pkg/gradle/lockfile/testdata/happy.lockfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This is a Gradle generated file for dependency locking.
# Manual edits can break the build and are not advised.
# This file is expected to be part of source control.
cglib:cglib-nodep:2.1.2=testRuntimeClasspath,classpath
org.springframework:spring-asm:3.1.3.RELEASE=classpath
org.springframework:spring-beans:5.0.5.RELEASE=compileClasspath, runtimeClasspath
# io.grpc:grpc-api:1.21.1=classpath
empty=

0 comments on commit f6e65e5

Please sign in to comment.