Skip to content

Commit ba3776c

Browse files
authored
feat: update scalibr to the latest version (#2446)
This PR updates scalibr to the latests version which includes two major changes: - Plugin Configuration: adopting the new protobuf-based configuration for plugins. The code has been refactored to use this new approach, which simplifies how features like transitive dependency resolution are managed. - Error Handling: Plugin initialization functions in osv-scalibr now return an error, allowing for more robust error handling during setup.
1 parent f10ee5c commit ba3776c

File tree

8 files changed

+67
-66
lines changed

8 files changed

+67
-66
lines changed

cmd/osv-scanner/__snapshots__/main_test.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ OPTIONS:
4747

4848
[Test_run/version - 1]
4949
osv-scanner version: 2.3.1
50-
osv-scalibr version: 0.4.0
50+
osv-scalibr version: 0.4.1
5151
commit: n/a
5252
built at: n/a
5353

cmd/osv-scanner/scan/source/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func action(_ context.Context, cmd *cli.Command, stdout, stderr io.Writer, clien
110110
experimentalScannerActions := helper.GetExperimentalScannerActions(cmd, client)
111111
experimentalScannerActions.RequestUserAgent = "osv-scanner_scan-source/" + version.OSVVersion
112112
// Add `source` specific experimental configs
113-
experimentalScannerActions.TransitiveScanningActions = osvscanner.TransitiveScanningActions{
113+
experimentalScannerActions.TransitiveScanning = osvscanner.TransitiveScanningActions{
114114
Disabled: cmd.Bool("no-resolve"),
115115
NativeDataSource: cmd.String("data-source") == "native",
116116
MavenRegistry: cmd.String("maven-registry"),

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require (
1818
github.com/go-git/go-git/v5 v5.16.4
1919
github.com/goccy/go-yaml v1.19.1
2020
github.com/google/go-cmp v0.7.0
21-
github.com/google/osv-scalibr v0.4.1-0.20251202121049-5e7e15f4a036
21+
github.com/google/osv-scalibr v0.4.2-0.20260109000604-557385a20603
2222
github.com/ianlancetaylor/demangle v0.0.0-20251118225945-96ee0021ea0f
2323
github.com/jedib0t/go-pretty/v6 v6.7.8
2424
github.com/modelcontextprotocol/go-sdk v1.2.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ github.com/google/go-cpy v0.0.0-20211218193943-a9c933c06932 h1:5/4TSDzpDnHQ8rKEE
256256
github.com/google/go-cpy v0.0.0-20211218193943-a9c933c06932/go.mod h1:cC6EdPbj/17GFCPDK39NRarlMI+kt+O60S12cNB5J9Y=
257257
github.com/google/jsonschema-go v0.3.0 h1:6AH2TxVNtk3IlvkkhjrtbUc4S8AvO0Xii0DxIygDg+Q=
258258
github.com/google/jsonschema-go v0.3.0/go.mod h1:r5quNTdLOYEz95Ru18zA0ydNbBuYoo9tgaYcxEYhJVE=
259-
github.com/google/osv-scalibr v0.4.1-0.20251202121049-5e7e15f4a036 h1:a+w+8ZQYYybXPWI1yJD+mXri5fMLcThlP41rIB7XNns=
260-
github.com/google/osv-scalibr v0.4.1-0.20251202121049-5e7e15f4a036/go.mod h1:9Ze2W6nQmu1WX2s95ezOAVZhPDbcA6ZGuEHgFT/sQEU=
259+
github.com/google/osv-scalibr v0.4.2-0.20260109000604-557385a20603 h1:yiXRYOjrw8KOIaJXz1kR1L15D5TtX1lBNYnXlG4c7vU=
260+
github.com/google/osv-scalibr v0.4.2-0.20260109000604-557385a20603/go.mod h1:3O4zXBBTy4lbryN1YZXpt/8r+AUJJMnsi5s0+oRFwJo=
261261
github.com/google/pprof v0.0.0-20250403155104-27863c87afa6 h1:BHT72Gu3keYf3ZEu2J0b1vyeLSOYI8bm5wbJM/8yDe8=
262262
github.com/google/pprof v0.0.0-20250403155104-27863c87afa6/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
263263
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=

internal/scalibrextract/language/java/pomxmlenhanceable/pomxmlenhanceable.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package pomxmlenhanceable
44
import (
55
"context"
66

7+
cpb "github.com/google/osv-scalibr/binary/proto/config_go_proto"
78
"github.com/google/osv-scalibr/extractor/filesystem"
89
"github.com/google/osv-scalibr/extractor/filesystem/language/java/pomxml"
910
"github.com/google/osv-scalibr/extractor/filesystem/language/java/pomxmlnet"
@@ -77,23 +78,26 @@ func (e *Extractor) Extract(ctx context.Context, input *filesystem.ScanInput) (i
7778
var _ filesystem.Extractor = &Extractor{}
7879

7980
type enhanceable interface {
80-
Enhance(config pomxmlnet.Config)
81+
Enhance(config *cpb.PluginConfig) error
8182
}
8283

8384
// Enhance uses the given config to improve the abilities of this extractor,
8485
// at the cost of additional requirements such as networking and direct fs access
85-
func (e *Extractor) Enhance(config pomxmlnet.Config) {
86-
e.online = pomxmlnet.New(config)
86+
func (e *Extractor) Enhance(config *cpb.PluginConfig) (err error) {
87+
e.online, err = pomxmlnet.New(config)
88+
return
8789
}
8890

8991
var _ enhanceable = &Extractor{}
9092

9193
// EnhanceIfPossible calls Extractor.Enhance with the given config if the
9294
// provided plug(in) is an Extractor
93-
func EnhanceIfPossible(plug plugin.Plugin, config pomxmlnet.Config) {
95+
func EnhanceIfPossible(plug plugin.Plugin, config *cpb.PluginConfig) error {
9496
us, ok := plug.(enhanceable)
9597

9698
if ok {
97-
us.Enhance(config)
99+
return us.Enhance(config)
98100
}
101+
102+
return nil
99103
}

internal/scalibrplugin/presets.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,16 +192,19 @@ func baseImageEnricher() enricher.Enricher {
192192
// TODO(b/400910349): Remove once all plugins take config values.
193193
// Copied from osv-scalibr
194194
func noCFG(f func() filesystem.Extractor) extractors.InitFn {
195-
return func(_ *cpb.PluginConfig) filesystem.Extractor { return f() }
195+
return func(_ *cpb.PluginConfig) (filesystem.Extractor, error) { return f(), nil }
196196
}
197197

198198
// Wraps initer functions that don't take any config value to initer functions that do.
199199
// TODO(b/400910349): Remove once all plugins take config values.
200200
// Copied from osv-scalibr
201201
func noCFGEnricher(f func() enricher.Enricher) enricherlist.InitFn {
202-
return func(_ *cpb.PluginConfig) enricher.Enricher { return f() }
202+
return func(_ *cpb.PluginConfig) (enricher.Enricher, error) { return f(), nil }
203203
}
204204

205+
// Wraps initer functions that don't take any config value to initer functions that do.
206+
// TODO(b/400910349): Remove once all plugins take config values.
207+
// Copied from osv-scalibr
205208
func noCFGAnnotator(f func() annotator.Annotator) annotatorlist.InitFn {
206-
return func(_ *cpb.PluginConfig) annotator.Annotator { return f() }
209+
return func(_ *cpb.PluginConfig) (annotator.Annotator, error) { return f(), nil }
207210
}

pkg/osvscanner/osvscanner.go

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ import (
1212
"sort"
1313
"time"
1414

15-
"deps.dev/util/resolve"
1615
scalibr "github.com/google/osv-scalibr"
1716
"github.com/google/osv-scalibr/artifact/image/layerscanning/image"
1817
"github.com/google/osv-scalibr/binary/proto"
18+
cpb "github.com/google/osv-scalibr/binary/proto/config_go_proto"
1919
"github.com/google/osv-scalibr/clients/datasource"
20-
"github.com/google/osv-scalibr/clients/resolution"
2120
"github.com/google/osv-scalibr/enricher/packagedeprecation"
2221
"github.com/google/osv-scalibr/enricher/reachability/java"
2322
"github.com/google/osv-scalibr/extractor"
@@ -71,7 +70,7 @@ type ScannerActions struct {
7170
}
7271

7372
type ExperimentalScannerActions struct {
74-
TransitiveScanningActions
73+
TransitiveScanning TransitiveScanningActions
7574

7675
PluginsEnabled []string
7776
PluginsDisabled []string
@@ -101,15 +100,8 @@ type ExternalAccessors struct {
101100
VulnMatcher clientinterfaces.VulnerabilityMatcher
102101
LicenseMatcher clientinterfaces.LicenseMatcher
103102

104-
// Required for pomxmlnet Extractor
105-
MavenRegistryAPIClient *datasource.MavenRegistryAPIClient
106103
// Required for vendored Extractor
107104
OSVDevClient *osvdev.OSVClient
108-
109-
// DependencyClients is a map of implementations of DependencyClient
110-
// for each ecosystem, the following is currently implemented:
111-
// - [osvschema.EcosystemMaven] required for pomxmlnet Extractor
112-
DependencyClients map[osvconstants.Ecosystem]resolve.Client
113105
}
114106

115107
// ErrNoPackagesFound for when no packages are found during a scan.
@@ -124,10 +116,7 @@ var ErrVulnerabilitiesFound = errors.New("vulnerabilities found")
124116
var ErrAPIFailed = errors.New("API query failed")
125117

126118
func initializeExternalAccessors(actions ScannerActions) (ExternalAccessors, error) {
127-
ctx := context.Background()
128-
externalAccessors := ExternalAccessors{
129-
DependencyClients: map[osvconstants.Ecosystem]resolve.Client{},
130-
}
119+
externalAccessors := ExternalAccessors{}
131120
var err error
132121

133122
userAgent := "osv-scanner-api"
@@ -171,34 +160,6 @@ func initializeExternalAccessors(actions ScannerActions) (ExternalAccessors, err
171160
externalAccessors.OSVDevClient = osvdev.DefaultClient()
172161
externalAccessors.OSVDevClient.Config.UserAgent = userAgent
173162

174-
// --- No Transitive Scanning ---
175-
if actions.Disabled {
176-
return externalAccessors, nil
177-
}
178-
179-
// --- Transitive Scanning Clients ---
180-
externalAccessors.MavenRegistryAPIClient, err = datasource.NewMavenRegistryAPIClient(ctx, datasource.MavenRegistry{
181-
URL: actions.MavenRegistry,
182-
ReleasesEnabled: true,
183-
}, "", false)
184-
185-
if err != nil {
186-
return ExternalAccessors{}, err
187-
}
188-
189-
if !actions.NativeDataSource {
190-
externalAccessors.DependencyClients[osvconstants.EcosystemMaven], err = resolution.NewDepsDevClient(depsdev.DepsdevAPI, userAgent)
191-
} else {
192-
externalAccessors.DependencyClients[osvconstants.EcosystemMaven], err = resolution.NewMavenRegistryClient(ctx, actions.MavenRegistry, "", false)
193-
}
194-
195-
// We only support native registry client for PyPI.
196-
externalAccessors.DependencyClients[osvconstants.EcosystemPyPI] = resolution.NewPyPIRegistryClient("", "")
197-
198-
if err != nil {
199-
return ExternalAccessors{}, err
200-
}
201-
202163
return externalAccessors, nil
203164
}
204165

@@ -316,7 +277,14 @@ func DoContainerScan(actions ScannerActions) (models.VulnerabilityResults, error
316277
}
317278

318279
if actions.FlagDeprecatedPackages {
319-
plugins = append(plugins, packagedeprecation.New())
280+
p, err := packagedeprecation.New(&cpb.PluginConfig{
281+
UserAgent: actions.RequestUserAgent,
282+
})
283+
if err != nil {
284+
cmdlogger.Errorf("Failed to enable packagedeprecation enricher: %v", err)
285+
} else {
286+
plugins = append(plugins, p)
287+
}
320288
}
321289

322290
// --- Initialize Image To Scan ---'

pkg/osvscanner/scan.go

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@ import (
1111
"strings"
1212

1313
scalibr "github.com/google/osv-scalibr"
14+
cpb "github.com/google/osv-scalibr/binary/proto/config_go_proto"
1415
"github.com/google/osv-scalibr/enricher"
1516
"github.com/google/osv-scalibr/enricher/packagedeprecation"
1617
"github.com/google/osv-scalibr/enricher/reachability/java"
1718
transitivedependencyrequirements "github.com/google/osv-scalibr/enricher/transitivedependency/requirements"
1819
"github.com/google/osv-scalibr/extractor"
1920
"github.com/google/osv-scalibr/extractor/filesystem"
20-
"github.com/google/osv-scalibr/extractor/filesystem/language/java/pomxmlnet"
2121
"github.com/google/osv-scalibr/extractor/filesystem/language/python/requirements"
2222
"github.com/google/osv-scalibr/extractor/filesystem/simplefileapi"
2323
"github.com/google/osv-scalibr/fs"
2424
"github.com/google/osv-scalibr/inventory"
25+
"github.com/google/osv-scalibr/log"
2526
"github.com/google/osv-scalibr/plugin"
2627
"github.com/google/osv-scanner/v2/internal/cmdlogger"
2728
"github.com/google/osv-scanner/v2/internal/imodels"
@@ -32,18 +33,29 @@ import (
3233
"github.com/google/osv-scanner/v2/internal/scalibrplugin"
3334
"github.com/google/osv-scanner/v2/internal/testlogger"
3435
"github.com/google/osv-scanner/v2/pkg/osvscanner/internal/scanners"
35-
"github.com/ossf/osv-schema/bindings/go/osvconstants"
3636
)
3737

3838
var ErrExtractorNotFound = errors.New("could not determine extractor suitable to this file")
3939

4040
func configurePlugins(plugins []plugin.Plugin, accessors ExternalAccessors, actions ScannerActions) {
4141
for _, plug := range plugins {
42-
if accessors.DependencyClients[osvconstants.EcosystemMaven] != nil && accessors.MavenRegistryAPIClient != nil {
43-
pomxmlenhanceable.EnhanceIfPossible(plug, pomxmlnet.Config{
44-
DependencyClient: accessors.DependencyClients[osvconstants.EcosystemMaven],
45-
MavenRegistryAPIClient: accessors.MavenRegistryAPIClient,
42+
if !actions.TransitiveScanning.Disabled {
43+
err := pomxmlenhanceable.EnhanceIfPossible(plug, &cpb.PluginConfig{
44+
UserAgent: actions.RequestUserAgent,
45+
PluginSpecific: []*cpb.PluginSpecificConfig{
46+
{
47+
Config: &cpb.PluginSpecificConfig_PomXmlNet{
48+
PomXmlNet: &cpb.POMXMLNetConfig{
49+
UpstreamRegistry: actions.TransitiveScanning.MavenRegistry,
50+
DepsDevRequirements: !actions.TransitiveScanning.NativeDataSource,
51+
},
52+
},
53+
},
54+
},
4655
})
56+
if err != nil {
57+
log.Errorf("Failed to enhance pomxml extractor: %v", err)
58+
}
4759
}
4860

4961
vendored.Configure(plug, vendored.Config{
@@ -81,9 +93,16 @@ func getPlugins(defaultPlugins []string, accessors ExternalAccessors, actions Sc
8193

8294
plugins := scalibrplugin.Resolve(actions.PluginsEnabled, actions.PluginsDisabled)
8395

84-
// todo: use Enricher.RequiredPlugins to check this generically
85-
if accessors.DependencyClients[osvconstants.EcosystemPyPI] != nil && isRequirementsExtractorEnabled(plugins) {
86-
plugins = append(plugins, transitivedependencyrequirements.NewEnricher(accessors.DependencyClients[osvconstants.EcosystemPyPI]))
96+
// TODO: Use Enricher.RequiredPlugins to check this generically
97+
if !actions.TransitiveScanning.Disabled && isRequirementsExtractorEnabled(plugins) {
98+
p, err := transitivedependencyrequirements.New(&cpb.PluginConfig{
99+
UserAgent: actions.RequestUserAgent,
100+
})
101+
if err != nil {
102+
log.Errorf("Failed to make transitivedependencyrequirements enricher: %v", err)
103+
} else {
104+
plugins = append(plugins, p)
105+
}
87106
}
88107

89108
configurePlugins(plugins, accessors, actions)
@@ -126,7 +145,14 @@ func scan(accessors ExternalAccessors, actions ScannerActions) (*imodels.ScanRes
126145
}
127146

128147
if actions.FlagDeprecatedPackages {
129-
plugins = append(plugins, packagedeprecation.New())
148+
p, err := packagedeprecation.New(&cpb.PluginConfig{
149+
UserAgent: actions.RequestUserAgent,
150+
})
151+
if err != nil {
152+
log.Errorf("Failed to make packagedeprecation enricher: %v", err)
153+
} else {
154+
plugins = append(plugins, p)
155+
}
130156
}
131157

132158
scanner := scalibr.New()

0 commit comments

Comments
 (0)