-
Notifications
You must be signed in to change notification settings - Fork 635
feat: add Bitnami matcher #2538
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
Draft
juan131
wants to merge
11
commits into
anchore:main
Choose a base branch
from
juan131:feat/bitnami-matcher
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9ec9cca
feat: add Bitnami matcher
juan131 b6e1858
feat: add MatchPackageByPURL
juan131 105bea2
feat: add basic unit test
juan131 ec6bd6b
Merge branch 'main' into feat/bitnami-matcher
juan131 b2cca25
bugfix: use 'truncated' bitnami for parsing versions & semver for con…
juan131 f8421fd
bugfix: reuse semver logic
b5c8bd8
docs: comment explaining tests
juan131 5cd2987
Merge branch 'main' into feat/bitnami-matcher
juan131 33d372f
chore: unit tests
juan131 f19911d
bugfix: no-op line
juan131 ebc36ac
fix: add bitnami to format str slice
willmurphyscode 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 hidden or 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 hidden or 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 hidden or 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,27 @@ | ||
package bitnami | ||
|
||
import ( | ||
"github.com/anchore/grype/grype/match" | ||
"github.com/anchore/grype/grype/matcher/internal" | ||
"github.com/anchore/grype/grype/pkg" | ||
"github.com/anchore/grype/grype/vulnerability" | ||
syftPkg "github.com/anchore/syft/syft/pkg" | ||
) | ||
|
||
type Matcher struct{} | ||
|
||
func (m *Matcher) PackageTypes() []syftPkg.Type { | ||
return []syftPkg.Type{syftPkg.BitnamiPkg} | ||
} | ||
|
||
func (m *Matcher) Type() match.MatcherType { | ||
return match.BitnamiMatcher | ||
} | ||
|
||
func (m *Matcher) Match(store vulnerability.Provider, p pkg.Package) ([]match.Match, []match.IgnoredMatch, error) { | ||
// Bitnami packages' metadata are built from the package URL which contains | ||
// info such as the package name, version, revision, distro or architecture. | ||
// ref: https://github.com/anchore/syft/blob/main/syft/pkg/bitnami.go#L3-L13 | ||
// ref: https://github.com/anchore/syft/blob/main/syft/pkg/cataloger/bitnami/package.go#L18-L45 | ||
return internal.MatchPackageByEcosystemPackageName(store, p, p.Name, m.Type()) | ||
} |
This file contains hidden or 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 hidden or 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,68 @@ | ||
package version | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestVersionBitnami(t *testing.T) { | ||
tests := []testCase{ | ||
// empty values | ||
{version: "2.3.1", constraint: "", satisfied: true}, | ||
// typical cases | ||
{version: "1.5.0", constraint: "> 0.1.0, < 0.5.0 || > 1.0.0, < 2.0.0", satisfied: true}, | ||
{version: "0.2.0", constraint: "> 0.1.0, < 0.5.0 || > 1.0.0, < 2.0.0", satisfied: true}, | ||
{version: "0.0.1", constraint: "> 0.1.0, < 0.5.0 || > 1.0.0, < 2.0.0", satisfied: false}, | ||
{version: "0.6.0", constraint: "> 0.1.0, < 0.5.0 || > 1.0.0, < 2.0.0", satisfied: false}, | ||
{version: "2.5.0", constraint: "> 0.1.0, < 0.5.0 || > 1.0.0, < 2.0.0", satisfied: false}, | ||
{version: "2.3.1", constraint: "2.3.1", satisfied: true}, | ||
{version: "2.3.1", constraint: "= 2.3.1", satisfied: true}, | ||
{version: "2.3.1", constraint: " = 2.3.1", satisfied: true}, | ||
{version: "2.3.1", constraint: ">= 2.3.1", satisfied: true}, | ||
{version: "2.3.1", constraint: "> 2.0.0", satisfied: true}, | ||
{version: "2.3.1", constraint: "> 2.0", satisfied: true}, | ||
{version: "2.3.1", constraint: "> 2", satisfied: true}, | ||
{version: "2.3.1", constraint: "> 2, < 3", satisfied: true}, | ||
{version: "2.3.1", constraint: "> 2.3, < 3.1", satisfied: true}, | ||
{version: "2.3.1", constraint: "> 2.3.0, < 3.1", satisfied: true}, | ||
{version: "2.3.1", constraint: ">= 2.3.1, < 3.1", satisfied: true}, | ||
{version: "2.3.1", constraint: " = 2.3.2", satisfied: false}, | ||
{version: "2.3.1", constraint: ">= 2.3.2", satisfied: false}, | ||
{version: "2.3.1", constraint: "> 2.3.1", satisfied: false}, | ||
{version: "2.3.1", constraint: "< 2.0.0", satisfied: false}, | ||
{version: "2.3.1", constraint: "< 2.0", satisfied: false}, | ||
{version: "2.3.1", constraint: "< 2", satisfied: false}, | ||
{version: "2.3.1", constraint: "< 2, > 3", satisfied: false}, | ||
{version: "2.3.1-1", constraint: "2.3.1", satisfied: true}, | ||
{version: "2.3.1-1", constraint: "= 2.3.1", satisfied: true}, | ||
{version: "2.3.1-1", constraint: " = 2.3.1", satisfied: true}, | ||
{version: "2.3.1-1", constraint: ">= 2.3.1", satisfied: true}, | ||
{version: "2.3.1-1", constraint: "> 2.0.0", satisfied: true}, | ||
{version: "2.3.1-1", constraint: "> 2.0", satisfied: true}, | ||
{version: "2.3.1-1", constraint: "> 2", satisfied: true}, | ||
{version: "2.3.1-1", constraint: "> 2, < 3", satisfied: true}, | ||
{version: "2.3.1-1", constraint: "> 2.3, < 3.1", satisfied: true}, | ||
{version: "2.3.1-1", constraint: "> 2.3.0, < 3.1", satisfied: true}, | ||
{version: "2.3.1-1", constraint: ">= 2.3.1, < 3.1", satisfied: true}, | ||
{version: "2.3.1-1", constraint: " = 2.3.2", satisfied: false}, | ||
{version: "2.3.1-1", constraint: ">= 2.3.2", satisfied: false}, | ||
{version: "2.3.1-1", constraint: "< 2.0.0", satisfied: false}, | ||
{version: "2.3.1-1", constraint: "< 2.0", satisfied: false}, | ||
{version: "2.3.1-1", constraint: "< 2", satisfied: false}, | ||
{version: "2.3.1-1", constraint: "< 2, > 3", satisfied: false}, | ||
// Ignoring revisions | ||
{version: "2.3.1-1", constraint: "> 2.3.1", satisfied: false}, | ||
{version: "2.3.1-1", constraint: "< 2.3.1-2", satisfied: false}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
// We use newSemanticConstraint but using BitnamiFormat as the format | ||
constraint, err := newSemanticConstraint(test.constraint) | ||
|
||
assert.NoError(t, err, "unexpected error from newSemanticConstraint: %v", err) | ||
test.assertVersionConstraint(t, BitnamiFormat, constraint) | ||
}) | ||
} | ||
} |
This file contains hidden or 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,20 @@ | ||
package version | ||
|
||
import ( | ||
"fmt" | ||
|
||
bitnami "github.com/bitnami/go-version/pkg/version" | ||
) | ||
|
||
func newBitnamiVersion(raw string) (*semanticVersion, error) { | ||
bitnamiVersion, err := bitnami.Parse(raw) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// We can't assume Bitnami revisions can potentially address a | ||
// known vulnerability given Bitnami package revisions use | ||
// exactly the same upstream source code used to create the | ||
// previous version. Then, we discard it. | ||
return newSemanticVersion(fmt.Sprintf("%d.%d.%d", bitnamiVersion.Major(), bitnamiVersion.Minor(), bitnamiVersion.Patch())) | ||
} |
This file contains hidden or 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,129 @@ | ||
package version | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBitnamiVersionCompare(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
thisVersion string | ||
otherVersion string | ||
otherFormat Format | ||
expectError bool | ||
errorSubstring string | ||
}{ | ||
{ | ||
name: "same format successful comparison", | ||
thisVersion: "1.2.3-4", | ||
otherVersion: "1.2.3-5", | ||
otherFormat: BitnamiFormat, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "semantic versioning successful comparison", | ||
thisVersion: "1.2.3-4", | ||
otherVersion: "1.2.3", | ||
otherFormat: SemanticFormat, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "different format returns error - deb", | ||
thisVersion: "1.2.3-4", | ||
otherVersion: "1.2.3-1", | ||
otherFormat: DebFormat, | ||
expectError: true, | ||
errorSubstring: "unsupported version format for comparison", | ||
}, | ||
{ | ||
name: "unknown format attempts upgrade - valid semver format", | ||
thisVersion: "1.2.3-4", | ||
otherVersion: "1.2.3-5", | ||
otherFormat: UnknownFormat, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "unknown format attempts upgrade - invalid semver format", | ||
thisVersion: "1.2.3-4", | ||
otherVersion: "not-valid-semver-format", | ||
otherFormat: UnknownFormat, | ||
expectError: true, | ||
errorSubstring: "unsupported version format for comparison", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
thisVer, err := newBitnamiVersion(test.thisVersion) | ||
require.NoError(t, err) | ||
|
||
otherVer, err := NewVersion(test.otherVersion, test.otherFormat) | ||
require.NoError(t, err) | ||
|
||
result, err := thisVer.Compare(otherVer) | ||
|
||
if test.expectError { | ||
assert.Error(t, err) | ||
if test.errorSubstring != "" { | ||
assert.True(t, strings.Contains(err.Error(), test.errorSubstring), | ||
"Expected error to contain '%s', got: %v", test.errorSubstring, err) | ||
} | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Contains(t, []int{-1, 0, 1}, result, "Expected comparison result to be -1, 0, or 1") | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestBitnamiVersionCompareEdgeCases(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
setupFunc func() (*semanticVersion, *Version) | ||
expectError bool | ||
errorSubstring string | ||
}{ | ||
{ | ||
name: "nil version object", | ||
setupFunc: func() (*semanticVersion, *Version) { | ||
thisVer, _ := newBitnamiVersion("1.2.3-4") | ||
return thisVer, nil | ||
}, | ||
expectError: true, | ||
errorSubstring: "no version provided for comparison", | ||
}, | ||
{ | ||
name: "empty semanticVersion in other object", | ||
setupFunc: func() (*semanticVersion, *Version) { | ||
thisVer, _ := newBitnamiVersion("1.2.3-4") | ||
otherVer := &Version{ | ||
Raw: "1.2.3-5", | ||
Format: SemanticFormat, | ||
rich: rich{}, | ||
} | ||
|
||
return thisVer, otherVer | ||
}, | ||
expectError: true, | ||
errorSubstring: "given empty semanticVersion object", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
thisVer, otherVer := test.setupFunc() | ||
|
||
_, err := thisVer.Compare(otherVer) | ||
|
||
assert.Error(t, err) | ||
if test.errorSubstring != "" { | ||
assert.True(t, strings.Contains(err.Error(), test.errorSubstring), | ||
"Expected error to contain '%s', got: %v", test.errorSubstring, err) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -20,6 +20,7 @@ const ( | |
PortageFormat | ||
GolangFormat | ||
JVMFormat | ||
BitnamiFormat | ||
) | ||
|
||
type Format int | ||
|
@@ -37,6 +38,7 @@ var formatStr = []string{ | |
"Portage", | ||
"Go", | ||
"JVM", | ||
"Bitnami", | ||
} | ||
|
||
var Formats = []Format{ | ||
10000
|
@@ -51,6 +53,7 @@ var Formats = []Format{ | |
PortageFormat, | ||
GolangFormat, | ||
JVMFormat, | ||
BitnamiFormat, | ||
} | ||
|
||
func ParseFormat(userStr string) Format { | ||
|
@@ -59,6 +62,8 @@ func ParseFormat(userStr string) Format { | |
return SemanticFormat | ||
case strings.ToLower(ApkFormat.String()), "apk": | ||
return ApkFormat | ||
case strings.ToLower(BitnamiFormat.String()), "bitnami": | ||
return BitnamiFormat | ||
case strings.ToLower(DebFormat.String()), "dpkg": | ||
return DebFormat | ||
case strings.ToLower(GolangFormat.String()), "go": | ||
|
@@ -85,6 +90,8 @@ func FormatFromPkg(p pkg.Package) Format { | |
switch p.Type { | ||
case syftPkg.ApkPkg: | ||
return ApkFormat | ||
case syftPkg.BitnamiPkg: | ||
return BitnamiFormat | ||
case syftPkg.DebPkg: | ||
return DebFormat | ||
case syftPkg.JavaPkg: | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.