-
Notifications
You must be signed in to change notification settings - Fork 572
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve CPE and upstream generation logic for Alpine packages (#…
…1567) * fix: improved CPE-generation logic for alpine packages Signed-off-by: Weston Steimel <weston.steimel@anchore.com> * fix: improved alpine upstream name generation Signed-off-by: Weston Steimel <weston.steimel@anchore.com> * fix: improve CPE vendor for alpine Signed-off-by: Weston Steimel <weston.steimel@anchore.com> * fix: python vendor CPE gen Signed-off-by: Weston Steimel <weston.steimel@anchore.com> * fix: alpine cpe gen logic Signed-off-by: Weston Steimel <weston.steimel@anchore.com> * fix: apk CPE update for nodejs-current Signed-off-by: Weston Steimel <weston.steimel@anchore.com> * fix: CPE update for python pip Signed-off-by: Weston Steimel <weston.steimel@anchore.com> * fix: CPE update for some ruby packages Signed-off-by: Weston Steimel <weston.steimel@anchore.com> * fix linting Signed-off-by: Weston Steimel <weston.steimel@anchore.com> --------- Signed-off-by: Weston Steimel <weston.steimel@anchore.com>
- Loading branch information
1 parent
890fb3f
commit 57a13ae
Showing
8 changed files
with
419 additions
and
4 deletions.
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,173 @@ | ||
package cpe | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/anchore/syft/syft/pkg" | ||
) | ||
|
||
var ( | ||
pythonPrefixes = []string{"py-", "py2-", "py3-"} | ||
rubyPrefixes = []string{"ruby-"} | ||
) | ||
|
||
func pythonCandidateVendorsFromName(v string) fieldCandidateSet { | ||
vendors := newFieldCandidateSet() | ||
vendors.add(fieldCandidate{ | ||
value: v, | ||
disallowSubSelections: true, | ||
disallowDelimiterVariations: true, | ||
}) | ||
|
||
vendors.addValue(findAdditionalVendors(defaultCandidateAdditions, pkg.PythonPkg, v, v)...) | ||
vendors.removeByValue(findVendorsToRemove(defaultCandidateRemovals, pkg.PythonPkg, v)...) | ||
|
||
for _, av := range additionalVendorsForPython(v) { | ||
vendors.add(fieldCandidate{ | ||
value: av, | ||
disallowSubSelections: true, | ||
disallowDelimiterVariations: true, | ||
}) | ||
vendors.addValue(findAdditionalVendors(defaultCandidateAdditions, pkg.PythonPkg, av, av)...) | ||
vendors.removeByValue(findVendorsToRemove(defaultCandidateRemovals, pkg.PythonPkg, av)...) | ||
} | ||
|
||
return vendors | ||
} | ||
|
||
func pythonCandidateVendorsFromAPK(m pkg.ApkMetadata) fieldCandidateSet { | ||
vendors := newFieldCandidateSet() | ||
|
||
for _, p := range pythonPrefixes { | ||
if strings.HasPrefix(m.Package, p) { | ||
t := strings.ToLower(strings.TrimPrefix(m.Package, p)) | ||
vendors.union(pythonCandidateVendorsFromName(t)) | ||
} | ||
|
||
if m.OriginPackage != m.Package && strings.HasPrefix(m.OriginPackage, p) { | ||
t := strings.ToLower(strings.TrimPrefix(m.OriginPackage, p)) | ||
vendors.union(pythonCandidateVendorsFromName(t)) | ||
} | ||
} | ||
|
||
return vendors | ||
} | ||
|
||
func pythonCandidateProductsFromName(p string) fieldCandidateSet { | ||
products := newFieldCandidateSet() | ||
products.add(fieldCandidate{ | ||
value: p, | ||
disallowSubSelections: true, | ||
disallowDelimiterVariations: true, | ||
}) | ||
|
||
products.addValue(findAdditionalProducts(defaultCandidateAdditions, pkg.PythonPkg, p)...) | ||
products.removeByValue(findProductsToRemove(defaultCandidateRemovals, pkg.PythonPkg, p)...) | ||
return products | ||
} | ||
|
||
func pythonCandidateProductsFromAPK(m pkg.ApkMetadata) fieldCandidateSet { | ||
products := newFieldCandidateSet() | ||
|
||
for _, p := range pythonPrefixes { | ||
if strings.HasPrefix(m.Package, p) { | ||
t := strings.ToLower(strings.TrimPrefix(m.Package, p)) | ||
products.union(pythonCandidateProductsFromName(t)) | ||
} | ||
|
||
if m.OriginPackage != m.Package && strings.HasPrefix(m.OriginPackage, p) { | ||
t := strings.ToLower(strings.TrimPrefix(m.OriginPackage, p)) | ||
products.union(pythonCandidateProductsFromName(t)) | ||
} | ||
} | ||
|
||
return products | ||
} | ||
|
||
func rubyCandidateVendorsFromName(v string) fieldCandidateSet { | ||
vendors := newFieldCandidateSet() | ||
vendors.add(fieldCandidate{ | ||
value: v, | ||
disallowSubSelections: true, | ||
disallowDelimiterVariations: true, | ||
}) | ||
|
||
vendors.addValue(findAdditionalVendors(defaultCandidateAdditions, pkg.GemPkg, v, v)...) | ||
vendors.removeByValue(findVendorsToRemove(defaultCandidateRemovals, pkg.GemPkg, v)...) | ||
return vendors | ||
} | ||
|
||
func rubyCandidateVendorsFromAPK(m pkg.ApkMetadata) fieldCandidateSet { | ||
vendors := newFieldCandidateSet() | ||
|
||
for _, p := range rubyPrefixes { | ||
if strings.HasPrefix(m.Package, p) { | ||
t := strings.ToLower(strings.TrimPrefix(m.Package, p)) | ||
vendors.union(rubyCandidateVendorsFromName(t)) | ||
} | ||
|
||
if m.OriginPackage != m.Package && strings.HasPrefix(m.OriginPackage, p) { | ||
t := strings.ToLower(strings.TrimPrefix(m.OriginPackage, p)) | ||
vendors.union(rubyCandidateVendorsFromName(t)) | ||
} | ||
} | ||
|
||
return vendors | ||
} | ||
|
||
func rubyCandidateProductsFromName(p string) fieldCandidateSet { | ||
products := newFieldCandidateSet() | ||
products.add(fieldCandidate{ | ||
value: p, | ||
disallowSubSelections: true, | ||
disallowDelimiterVariations: true, | ||
}) | ||
|
||
products.addValue(findAdditionalProducts(defaultCandidateAdditions, pkg.GemPkg, p)...) | ||
products.removeByValue(findProductsToRemove(defaultCandidateRemovals, pkg.GemPkg, p)...) | ||
return products | ||
} | ||
|
||
func rubyCandidateProductsFromAPK(m pkg.ApkMetadata) fieldCandidateSet { | ||
products := newFieldCandidateSet() | ||
|
||
for _, p := range rubyPrefixes { | ||
if strings.HasPrefix(m.Package, p) { | ||
t := strings.ToLower(strings.TrimPrefix(m.Package, p)) | ||
products.union(rubyCandidateProductsFromName(t)) | ||
} | ||
|
||
if m.OriginPackage != m.Package && strings.HasPrefix(m.OriginPackage, p) { | ||
t := strings.ToLower(strings.TrimPrefix(m.OriginPackage, p)) | ||
products.union(rubyCandidateProductsFromName(t)) | ||
} | ||
} | ||
|
||
return products | ||
} | ||
|
||
func candidateVendorsForAPK(p pkg.Package) fieldCandidateSet { | ||
metadata, ok := p.Metadata.(pkg.ApkMetadata) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
vendors := newFieldCandidateSet() | ||
vendors.union(pythonCandidateVendorsFromAPK(metadata)) | ||
vendors.union(rubyCandidateVendorsFromAPK(metadata)) | ||
|
||
return vendors | ||
} | ||
|
||
func candidateProductsForAPK(p pkg.Package) fieldCandidateSet { | ||
metadata, ok := p.Metadata.(pkg.ApkMetadata) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
products := newFieldCandidateSet() | ||
products.union(pythonCandidateProductsFromAPK(metadata)) | ||
products.union(rubyCandidateProductsFromAPK(metadata)) | ||
|
||
return products | ||
} |
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,91 @@ | ||
package cpe | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/anchore/syft/syft/pkg" | ||
) | ||
|
||
func Test_candidateVendorsForAPK(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
pkg pkg.Package | ||
expected []string | ||
}{ | ||
{ | ||
name: "py3-cryptography Package", | ||
pkg: pkg.Package{ | ||
Metadata: pkg.ApkMetadata{ | ||
Package: "py3-cryptography", | ||
}, | ||
}, | ||
expected: []string{"python-cryptography_project", "cryptography", "cryptographyproject", "cryptography_project"}, | ||
}, | ||
{ | ||
name: "py2-pypdf OriginPackage", | ||
pkg: pkg.Package{ | ||
Metadata: pkg.ApkMetadata{ | ||
OriginPackage: "py2-pypdf", | ||
}, | ||
}, | ||
expected: []string{"pypdf", "pypdfproject", "pypdf_project"}, | ||
}, | ||
{ | ||
name: "ruby-armadillo Package", | ||
pkg: pkg.Package{ | ||
Metadata: pkg.ApkMetadata{ | ||
Package: "ruby-armadillo", | ||
}, | ||
}, | ||
expected: []string{"armadillo"}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
assert.ElementsMatch(t, test.expected, candidateVendorsForAPK(test.pkg).values(), "different vendors") | ||
}) | ||
} | ||
} | ||
|
||
func Test_candidateProductsForAPK(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
pkg pkg.Package | ||
expected []string | ||
}{ | ||
{ | ||
name: "py3-cryptography Package", | ||
pkg: pkg.Package{ | ||
Metadata: pkg.ApkMetadata{ | ||
Package: "py3-cryptography", | ||
}, | ||
}, | ||
expected: []string{"cryptography", "python-cryptography"}, | ||
}, | ||
{ | ||
name: "py2-pypdf OriginPackage", | ||
pkg: pkg.Package{ | ||
Metadata: pkg.ApkMetadata{ | ||
OriginPackage: "py2-pypdf", | ||
}, | ||
}, | ||
expected: []string{"pypdf"}, | ||
}, | ||
{ | ||
name: "ruby-armadillo Package", | ||
pkg: pkg.Package{ | ||
Metadata: pkg.ApkMetadata{ | ||
Package: "ruby-armadillo", | ||
}, | ||
}, | ||
expected: []string{"armadillo"}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
assert.ElementsMatch(t, test.expected, candidateProductsForAPK(test.pkg).values(), "different products") | ||
}) | ||
} | ||
} |
Oops, something went wrong.