chore: redhat cataloger error when sqlite not regsitered (#4150)

Signed-off-by: Keith Zantow <kzantow@gmail.com>
This commit is contained in:
Keith Zantow 2025-08-21 10:55:47 -04:00 committed by GitHub
parent 26792fc12d
commit ca21ccf21d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 47 additions and 10 deletions

View File

@ -7,6 +7,7 @@ import (
"os" "os"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
_ "modernc.org/sqlite" // required for rpmdb and other features
"github.com/anchore/syft/syft" "github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/cataloging" "github.com/anchore/syft/syft/cataloging"

View File

@ -5,6 +5,8 @@ import (
"fmt" "fmt"
"os" "os"
_ "modernc.org/sqlite" // required for rpmdb and other features
"github.com/anchore/syft/syft" "github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/format" "github.com/anchore/syft/syft/format"
"github.com/anchore/syft/syft/format/syftjson" "github.com/anchore/syft/syft/format/syftjson"

View File

@ -5,6 +5,8 @@ import (
"encoding/json" "encoding/json"
"os" "os"
_ "modernc.org/sqlite" // required for rpmdb and other features
"github.com/anchore/syft/syft" "github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/cataloging" "github.com/anchore/syft/syft/cataloging"
"github.com/anchore/syft/syft/cataloging/pkgcataloging" "github.com/anchore/syft/syft/cataloging/pkgcataloging"

View File

@ -4,6 +4,8 @@ import (
"context" "context"
"os" "os"
_ "modernc.org/sqlite" // required for rpmdb and other features
"github.com/anchore/syft/syft" "github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/format/syftjson" "github.com/anchore/syft/syft/format/syftjson"
) )

View File

@ -53,6 +53,7 @@ func (p resolvingProcessorWrapper) process(ctx context.Context, resolver file.Re
type Cataloger struct { type Cataloger struct {
processors []processExecutor processors []processExecutor
requesters []requester requesters []requester
checks []func() error
upstreamCataloger string upstreamCataloger string
} }
@ -127,6 +128,11 @@ func (c *Cataloger) WithResolvingProcessors(processors ...ResolvingProcessor) *C
return c return c
} }
func (c *Cataloger) WithChecks(checks ...func() error) *Cataloger {
c.checks = append(c.checks, checks...)
return c
}
func makeRequests(parser Parser, locations []file.Location) []request { func makeRequests(parser Parser, locations []file.Location) []request {
var requests []request var requests []request
for _, l := range locations { for _, l := range locations {
@ -152,6 +158,12 @@ func (c *Cataloger) Name() string {
// Catalog is given an object to resolve file references and content, this function returns any discovered Packages after analyzing the catalog source. // Catalog is given an object to resolve file references and content, this function returns any discovered Packages after analyzing the catalog source.
func (c *Cataloger) Catalog(ctx context.Context, resolver file.Resolver) ([]pkg.Package, []artifact.Relationship, error) { func (c *Cataloger) Catalog(ctx context.Context, resolver file.Resolver) ([]pkg.Package, []artifact.Relationship, error) {
for _, check := range c.checks {
if err := check(); err != nil {
return nil, nil, err
}
}
var packages []pkg.Package var packages []pkg.Package
var relationships []artifact.Relationship var relationships []artifact.Relationship

View File

@ -5,8 +5,8 @@ package redhat
import ( import (
"database/sql" "database/sql"
"fmt"
"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/artifact" "github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/pkg" "github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/pkg/cataloger/generic" "github.com/anchore/syft/syft/pkg/cataloger/generic"
@ -15,15 +15,11 @@ import (
// NewDBCataloger returns a new RPM DB cataloger object. // NewDBCataloger returns a new RPM DB cataloger object.
func NewDBCataloger() pkg.Cataloger { func NewDBCataloger() pkg.Cataloger {
// check if a sqlite driver is available
if !isSqliteDriverAvailable() {
log.Debugf("sqlite driver is not available, newer RPM databases might not be cataloged")
}
return generic.NewCataloger("rpm-db-cataloger"). return generic.NewCataloger("rpm-db-cataloger").
WithParserByGlobs(parseRpmDB, pkg.RpmDBGlob). WithParserByGlobs(parseRpmDB, pkg.RpmDBGlob).
WithParserByGlobs(parseRpmManifest, pkg.RpmManifestGlob). WithParserByGlobs(parseRpmManifest, pkg.RpmManifestGlob).
WithProcessors(dependency.Processor(dbEntryDependencySpecifier), denySelfReferences) WithProcessors(dependency.Processor(dbEntryDependencySpecifier), denySelfReferences).
WithChecks(ensureSqliteDriverAvailable)
} }
func denySelfReferences(pkgs []pkg.Package, rels []artifact.Relationship, err error) ([]pkg.Package, []artifact.Relationship, error) { func denySelfReferences(pkgs []pkg.Package, rels []artifact.Relationship, err error) ([]pkg.Package, []artifact.Relationship, error) {
@ -47,11 +43,11 @@ func NewArchiveCataloger() pkg.Cataloger {
WithParserByGlobs(parseRpmArchive, "**/*.rpm") WithParserByGlobs(parseRpmArchive, "**/*.rpm")
} }
func isSqliteDriverAvailable() bool { func ensureSqliteDriverAvailable() error {
db, err := sql.Open("sqlite", ":memory:") db, err := sql.Open("sqlite", ":memory:")
if err != nil { if err != nil {
return false return fmt.Errorf("sqlite driver is required for cataloging newer RPM databases, none registered: %v", err)
} }
_ = db.Close() _ = db.Close()
return true return nil
} }

View File

@ -0,0 +1,22 @@
package sqlitetest
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/anchore/syft/syft/internal/fileresolver"
"github.com/anchore/syft/syft/pkg/cataloger/redhat"
)
func Test_noSQLiteDriverError(t *testing.T) {
// this test package does must not import the sqlite library
file := "../test-fixtures/Packages"
resolver, err := fileresolver.NewFromFile(file, file)
require.NoError(t, err)
cataloger := redhat.NewDBCataloger()
_, _, err = cataloger.Catalog(context.TODO(), resolver)
require.ErrorContains(t, err, "sqlite")
}