mirror of
https://github.com/anchore/syft.git
synced 2026-11-26 13:44:11 +01:00
fix: skip Linux release identification if there is nothing to parse (#5482)
* fix: skip Linux release identification if there is nothing to parse Signed-off-by: Caleb Allen <caleb.allen@upwind.io> * test: skip parsing os release on catalogers with no glob matches Signed-off-by: Will Murphy <willmurphyscode@users.noreply.github.com> --------- Signed-off-by: Caleb Allen <caleb.allen@upwind.io> Signed-off-by: Will Murphy <willmurphyscode@users.noreply.github.com> Co-authored-by: Will Murphy <willmurphyscode@users.noreply.github.com>
This commit is contained in:
co-authored by
Will Murphy
parent
ed7c7059c5
commit
1c76975600
@@ -191,16 +191,20 @@ func (c *Cataloger) Catalog(ctx context.Context, resolver file.Resolver) ([]pkg.
|
|||||||
|
|
||||||
lgr := log.Nested("cataloger", c.upstreamCataloger)
|
lgr := log.Nested("cataloger", c.upstreamCataloger)
|
||||||
|
|
||||||
env := Environment{
|
requests := c.selectFiles(resolver)
|
||||||
|
|
||||||
|
var env Environment
|
||||||
|
if len(requests) > 0 {
|
||||||
|
// The release is only needed by parsers, so skip identifying it when there is nothing to parse.
|
||||||
// TODO: consider passing into the cataloger, this would affect the cataloger interface (and all implementations). This can be deferred until later.
|
// TODO: consider passing into the cataloger, this would affect the cataloger interface (and all implementations). This can be deferred until later.
|
||||||
LinuxRelease: linux.IdentifyRelease(resolver),
|
env.LinuxRelease = linux.IdentifyRelease(resolver)
|
||||||
}
|
}
|
||||||
|
|
||||||
type result struct {
|
type result struct {
|
||||||
pkgs []pkg.Package
|
pkgs []pkg.Package
|
||||||
rels []artifact.Relationship
|
rels []artifact.Relationship
|
||||||
}
|
}
|
||||||
errs := sync.Collect(&ctx, cataloging.ExecutorFile, sync.ToSeq(c.selectFiles(resolver)), func(req request) (result, error) {
|
errs := sync.Collect(&ctx, cataloging.ExecutorFile, sync.ToSeq(requests), func(req request) (result, error) {
|
||||||
location, parser := req.Location, req.Parser
|
location, parser := req.Location, req.Parser
|
||||||
|
|
||||||
log.WithFields("path", location.FullNestedPath()).Trace("parsing file contents")
|
log.WithFields("path", location.FullNestedPath()).Trace("parsing file contents")
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package generic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/anchore/syft/syft/artifact"
|
||||||
|
"github.com/anchore/syft/syft/file"
|
||||||
|
"github.com/anchore/syft/syft/pkg"
|
||||||
|
)
|
||||||
|
|
||||||
|
// releaseLookupCountingResolver records how many times the cataloger asks for /etc/os-release,
|
||||||
|
// the first file linux.IdentifyRelease looks up.
|
||||||
|
type releaseLookupCountingResolver struct {
|
||||||
|
file.Resolver
|
||||||
|
osReleaseLookups int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *releaseLookupCountingResolver) FilesByPath(paths ...string) ([]file.Location, error) {
|
||||||
|
for _, p := range paths {
|
||||||
|
if p == "/etc/os-release" {
|
||||||
|
r.osReleaseLookups++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return r.Resolver.FilesByPath(paths...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Cataloger_SkipsOSReleaseWhenNoFilesMatch(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
files []string // what the resolver holds; the cataloger globs for **/a-path.txt
|
||||||
|
wantReadOSRelease bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "no files",
|
||||||
|
files: nil,
|
||||||
|
wantReadOSRelease: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "files, but none match the glob",
|
||||||
|
files: []string{"testdata/another-path.txt"},
|
||||||
|
wantReadOSRelease: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "a file matches the glob",
|
||||||
|
files: []string{"testdata/a-path.txt"},
|
||||||
|
wantReadOSRelease: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
parser := func(_ context.Context, _ file.Resolver, _ *Environment, _ file.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
|
||||||
|
return nil, nil, nil
|
||||||
|
}
|
||||||
|
resolver := &releaseLookupCountingResolver{Resolver: file.NewMockResolverForPaths(tt.files...)}
|
||||||
|
|
||||||
|
_, _, err := NewCataloger("test-cataloger").WithParserByGlobs(parser, "**/a-path.txt").Catalog(context.Background(), resolver)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, tt.wantReadOSRelease, resolver.osReleaseLookups > 0, "os-release lookups: %d", resolver.osReleaseLookups)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user