-
Notifications
You must be signed in to change notification settings - Fork 380
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 gradle lockfile support #46
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
## | ||
## Comments | ||
## | ||
|
||
org.springframework.boot:spring-boot-autoconfigure:2.7.4=compileClasspath,developmentOnly,productionRuntimeClasspath,runtimeClasspath | ||
org.springframework.boot:spring-boot-configuration-processor:2.7.5=annotationProcessor,compileClasspath | ||
org.springframework.boot:spring-boot-devtools:2.7.6=developmentOnly,runtimeClasspath | ||
org.springframework.boot:spring-boot-starter-aop:2.7.7=compileClasspath,productionRuntimeClasspath,runtimeClasspath | ||
org.springframework.boot:spring-boot-starter-data-jpa:2.7.8=compileClasspath,productionRuntimeClasspath,runtimeClasspath | ||
empty= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Comment example | ||
# | ||
|
||
org.springframework.security:spring-security-crypto:5.7.3=compileClasspath,productionRuntimeClasspath,runtimeClasspath | ||
empty= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# This | ||
# is | ||
# a | ||
# comment | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
empty= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# This sample gradle lockfile has bad lines | ||
# | ||
>>> | ||
//// | ||
empty======= | ||
|
||
org.springframework.boot:spring-boot-autoconfigure:2.7.4=compileClasspath,developmentOnly,productionRuntimeClasspath,runtimeClasspath | ||
|
||
a | ||
b | ||
|
||
|
||
|
||
org.springframework.boot:spring-boot-configuration-processor:2.7.5=compileClasspath,developmentOnly,productionRuntimeClasspath,runtimeClasspath | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package lockfile | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"strings" | ||
) | ||
|
||
const ( | ||
gradleLockFileCommentPrefix = "#" | ||
gradleLockFileEmptyPrefix = "empty=" | ||
) | ||
|
||
func isGradleLockFileDepLine(line string) bool { | ||
ret := strings.HasPrefix(line, gradleLockFileCommentPrefix) || | ||
strings.HasPrefix(line, gradleLockFileEmptyPrefix) | ||
|
||
return !ret | ||
} | ||
|
||
func parseToGradlePackageDetail(line string) (PackageDetails, error) { | ||
parts := strings.SplitN(line, ":", 3) | ||
if len(parts) < 3 { | ||
return PackageDetails{}, fmt.Errorf("invalid line in gradle lockfile: %s", line) | ||
} | ||
|
||
group, artifact, version := parts[0], parts[1], parts[2] | ||
version = strings.SplitN(version, "=", 2)[0] | ||
|
||
return PackageDetails{ | ||
Name: fmt.Sprintf("%s:%s", group, artifact), | ||
Version: version, | ||
Ecosystem: MavenEcosystem, | ||
CompareAs: MavenEcosystem, | ||
}, nil | ||
} | ||
|
||
func ParseGradleLock(pathToLockfile string) ([]PackageDetails, error) { | ||
lockFile, err := os.Open(pathToLockfile) | ||
if err != nil { | ||
return []PackageDetails{}, fmt.Errorf("could not open %s: %w", pathToLockfile, err) | ||
} | ||
|
||
defer lockFile.Close() | ||
|
||
pkgs := make([]PackageDetails, 0, 0) | ||
scanner := bufio.NewScanner(lockFile) | ||
|
||
for scanner.Scan() { | ||
lockLine := strings.TrimSpace(scanner.Text()) | ||
if !isGradleLockFileDepLine(lockLine) { | ||
continue | ||
} | ||
|
||
pkg, err := parseToGradlePackageDetail(lockLine) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "failed to parse lockline: %s\n", err.Error()) | ||
continue | ||
} | ||
|
||
pkgs = append(pkgs, pkg) | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
return []PackageDetails{}, fmt.Errorf("failed to read %s: %w", pathToLockfile, err) | ||
} | ||
|
||
return pkgs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package lockfile_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/osv-scanner/pkg/lockfile" | ||
) | ||
|
||
func TestParseGradleLock_FileDoesNotExist(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := lockfile.ParseGradleLock("fixtures/gradle/does-not-exist") | ||
|
||
expectErrContaining(t, err, "could not open") | ||
expectPackages(t, packages, []lockfile.PackageDetails{}) | ||
} | ||
|
||
func TestParseGradleLock_OnlyComments(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := lockfile.ParseGradleLock("fixtures/gradle/only-comments") | ||
|
||
if err != nil { | ||
t.Errorf("Got unexpected error: %v", err) | ||
} | ||
|
||
expectPackages(t, packages, []lockfile.PackageDetails{}) | ||
} | ||
|
||
func TestParseGradleLock_EmptyStatement(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := lockfile.ParseGradleLock("fixtures/gradle/only-empty") | ||
|
||
if err != nil { | ||
t.Errorf("Got unexpected error: %v", err) | ||
} | ||
|
||
expectPackages(t, packages, []lockfile.PackageDetails{}) | ||
} | ||
|
||
func TestParseGradleLock_OnePackage(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := lockfile.ParseGradleLock("fixtures/gradle/one-pkg") | ||
|
||
if err != nil { | ||
t.Errorf("Got unexpected error: %v", err) | ||
} | ||
|
||
expectPackages(t, packages, []lockfile.PackageDetails{ | ||
{ | ||
Name: "org.springframework.security:spring-security-crypto", | ||
Version: "5.7.3", | ||
Ecosystem: lockfile.MavenEcosystem, | ||
CompareAs: lockfile.MavenEcosystem, | ||
}, | ||
}) | ||
} | ||
|
||
func TestParseGradleLock_MultiplePackage(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := lockfile.ParseGradleLock("fixtures/gradle/5-pkg") | ||
|
||
if err != nil { | ||
t.Errorf("Got unexpected error: %v", err) | ||
} | ||
|
||
expectPackages(t, packages, []lockfile.PackageDetails{ | ||
{ | ||
Name: "org.springframework.boot:spring-boot-autoconfigure", | ||
Version: "2.7.4", | ||
Ecosystem: lockfile.MavenEcosystem, | ||
CompareAs: lockfile.MavenEcosystem, | ||
}, | ||
{ | ||
Name: "org.springframework.boot:spring-boot-configuration-processor", | ||
Version: "2.7.5", | ||
Ecosystem: lockfile.MavenEcosystem, | ||
CompareAs: lockfile.MavenEcosystem, | ||
}, | ||
{ | ||
Name: "org.springframework.boot:spring-boot-devtools", | ||
Version: "2.7.6", | ||
Ecosystem: lockfile.MavenEcosystem, | ||
CompareAs: lockfile.MavenEcosystem, | ||
}, | ||
|
||
{ | ||
Name: "org.springframework.boot:spring-boot-starter-aop", | ||
Version: "2.7.7", | ||
Ecosystem: lockfile.MavenEcosystem, | ||
CompareAs: lockfile.MavenEcosystem, | ||
}, | ||
{ | ||
Name: "org.springframework.boot:spring-boot-starter-data-jpa", | ||
Version: "2.7.8", | ||
Ecosystem: lockfile.MavenEcosystem, | ||
CompareAs: lockfile.MavenEcosystem, | ||
}, | ||
}) | ||
} | ||
|
||
func TestParseGradleLock_WithInvalidLines(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := lockfile.ParseGradleLock("fixtures/gradle/with-bad-pkg") | ||
|
||
if err != nil { | ||
t.Errorf("Got unexpected error: %v", err) | ||
} | ||
|
||
expectPackages(t, packages, []lockfile.PackageDetails{ | ||
{ | ||
Name: "org.springframework.boot:spring-boot-autoconfigure", | ||
Version: "2.7.4", | ||
Ecosystem: lockfile.MavenEcosystem, | ||
CompareAs: lockfile.MavenEcosystem, | ||
}, | ||
{ | ||
Name: "org.springframework.boot:spring-boot-configuration-processor", | ||
Version: "2.7.5", | ||
Ecosystem: lockfile.MavenEcosystem, | ||
CompareAs: lockfile.MavenEcosystem, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
buildscript-gradle.lockfile
should be included on this list (effectively this should have all lockfiles that are listed in the readme as being supported)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.
Updated to include
buildscript-gradle.lockfile
. But had to decrement the asserted count because bothgradle.lockfile
andbuildscript-gradle.lockfile
use the same parser.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.
sweet as - that test is more meant to help catch people forgetting to update this section of tests if a new parser is added, which is always a bit fuzzy (arguably I should have used a
map
against the function name and then check that each was called at least once, which I think could be possible, but not worth the effort imo)