Skip to content

Commit 61bc49d

Browse files
authored
refactor: simplify internal plugin disabling and configuration (#2248)
Rather than configuring our internal plugins to be disabled, we can just explicitly add them to the disabled plugins list as part of fetching the plugins; we also don't need to support not including the git root in the `gitrepo` extractor since we always disable the plugin if that option is not enabled
1 parent 3ce6933 commit 61bc49d

File tree

4 files changed

+15
-67
lines changed

4 files changed

+15
-67
lines changed

internal/scalibrextract/filesystem/vendored/vendored.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,13 @@ type Config struct {
6565
// this is used to avoid duplicate results, once from git scanning, once from vendoredDir scanning
6666
ScanGitDir bool
6767
OSVClient *osvdev.OSVClient
68-
Disabled bool
6968
}
7069

7170
type Extractor struct {
7271
// ScanGitDir determines whether a vendored library with a git directory is scanned or not,
7372
// this is used to avoid duplicate results, once from git scanning, once from vendoredDir scanning
7473
ScanGitDir bool
7574
OSVClient *osvdev.OSVClient
76-
Disabled bool
7775
}
7876

7977
// New returns a new instance of the extractor.
@@ -96,10 +94,6 @@ func (e *Extractor) Requirements() *plugin.Capabilities {
9694

9795
// FileRequired returns true for likely directories to contain vendored c/c++ code
9896
func (e *Extractor) FileRequired(fapi filesystem.FileAPI) bool {
99-
if e.Disabled {
100-
return false
101-
}
102-
10397
// Check if parent directory is one of the vendoredLibName
10498
// Clean first before Dir call to avoid trailing slashes causing problems
10599
parentDir := filepath.Base(filepath.Dir(filepath.Clean(fapi.Path())))
@@ -120,11 +114,6 @@ func (e *Extractor) FileRequired(fapi filesystem.FileAPI) bool {
120114
// Extract determines the most likely package version from the directory and returns them as
121115
// commit hash inventory entries
122116
func (e *Extractor) Extract(ctx context.Context, input *filesystem.ScanInput) (inventory.Inventory, error) {
123-
// todo: maybe we should return an error instead? need to double check we're always using FileRequired correctly first
124-
if e.Disabled {
125-
return inventory.Inventory{}, nil
126-
}
127-
128117
var packages []*extractor.Package
129118

130119
results, err := e.queryDetermineVersions(ctx, input.Path, input.FS, e.ScanGitDir)
@@ -229,7 +218,6 @@ type configurable interface {
229218
func (e *Extractor) Configure(config Config) {
230219
e.ScanGitDir = config.ScanGitDir
231220
e.OSVClient = config.OSVClient
232-
e.Disabled = config.Disabled
233221
}
234222

235223
var _ configurable = &Extractor{}

internal/scalibrextract/vcs/gitrepo/extractor.go

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,9 @@ const (
1919
Name = "vcs/gitrepo"
2020
)
2121

22-
type Config struct {
23-
IncludeRootGit bool
24-
Disabled bool
25-
}
26-
2722
// Extractor extracts git repository hashes including submodule hashes.
2823
// This extractor will not return an error, and will just return no results if we fail to extract
29-
type Extractor struct {
30-
IncludeRootGit bool
31-
Disabled bool
32-
}
24+
type Extractor struct{}
3325

3426
func getCommitSHA(repo *git.Repository) (string, error) {
3527
head, err := repo.Head()
@@ -89,10 +81,6 @@ func (e *Extractor) Requirements() *plugin.Capabilities {
8981

9082
// FileRequired returns true for git repositories .git dirs
9183
func (e *Extractor) FileRequired(fapi filesystem.FileAPI) bool {
92-
if e.Disabled {
93-
return false
94-
}
95-
9684
if filepath.Base(fapi.Path()) != ".git" {
9785
return false
9886
}
@@ -108,11 +96,6 @@ func (e *Extractor) FileRequired(fapi filesystem.FileAPI) bool {
10896

10997
// Extract extracts git commits from HEAD and from submodules
11098
func (e *Extractor) Extract(_ context.Context, input *filesystem.ScanInput) (inventory.Inventory, error) {
111-
// todo: maybe we should return an error instead? need to double check we're always using FileRequired correctly first
112-
if e.Disabled {
113-
return inventory.Inventory{}, nil
114-
}
115-
11699
// The input path is the .git directory, but git.PlainOpen expects the actual directory containing the .git dir.
117100
// So call filepath.Dir to get the parent path
118101
// Assume this is fully on a real filesystem
@@ -124,14 +107,12 @@ func (e *Extractor) Extract(_ context.Context, input *filesystem.ScanInput) (inv
124107

125108
var inv inventory.Inventory
126109

127-
if e.IncludeRootGit {
128-
commitSHA, err := getCommitSHA(repo)
110+
commitSHA, err := getCommitSHA(repo)
129111

130-
// If error is not nil, then ignore this and continue, as it is not fatal.
131-
// The error could be because there are no commits in the repository
132-
if err == nil {
133-
inv.Packages = append(inv.Packages, createCommitQueryInventory(commitSHA, input.Path))
134-
}
112+
// If error is not nil, then ignore this and continue, as it is not fatal.
113+
// The error could be because there are no commits in the repository
114+
if err == nil {
115+
inv.Packages = append(inv.Packages, createCommitQueryInventory(commitSHA, input.Path))
135116
}
136117

137118
// If we can't get submodules, just return with what we have.
@@ -159,22 +140,3 @@ func (e *Extractor) Ecosystem(_ *extractor.Package) string {
159140
}
160141

161142
var _ filesystem.Extractor = &Extractor{}
162-
163-
type configurable interface {
164-
Configure(config Config)
165-
}
166-
167-
func (e *Extractor) Configure(config Config) {
168-
e.IncludeRootGit = config.IncludeRootGit
169-
e.Disabled = config.Disabled
170-
}
171-
172-
var _ configurable = &Extractor{}
173-
174-
func Configure(plug plugin.Plugin, config Config) {
175-
us, ok := plug.(configurable)
176-
177-
if ok {
178-
us.Configure(config)
179-
}
180-
}

internal/scalibrextract/vcs/gitrepo/extractor_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ func TestExtractor_Extract(t *testing.T) {
4949
for _, tt := range tests {
5050
t.Run(tt.Name, func(t *testing.T) {
5151
t.Parallel()
52-
extr := gitrepo.Extractor{
53-
IncludeRootGit: true,
54-
}
52+
extr := gitrepo.Extractor{}
5553
parent := filepath.Dir(tt.InputConfig.Path)
5654
err := os.Rename(path.Join(parent, "git-hidden"), path.Join(parent, ".git"))
5755
if err != nil {

pkg/osvscanner/scan.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,10 @@ func configurePlugins(plugins []plugin.Plugin, accessors ExternalAccessors, acti
4646
})
4747
}
4848

49-
// todo: the "disabled" aspect should probably be worked into the extractor being present in the first place
50-
// since "IncludeRootGit" is always true
51-
gitrepo.Configure(plug, gitrepo.Config{
52-
IncludeRootGit: actions.IncludeGitRoot,
53-
Disabled: !actions.IncludeGitRoot,
54-
})
55-
5649
vendored.Configure(plug, vendored.Config{
5750
// Only attempt to vendor check git directories if we are not skipping scanning root git directories
5851
ScanGitDir: !actions.IncludeGitRoot,
5952
OSVClient: accessors.OSVDevClient,
60-
Disabled: accessors.OSVDevClient == nil,
6153
})
6254
}
6355
}
@@ -67,6 +59,14 @@ func getPlugins(defaultPlugins []string, accessors ExternalAccessors, actions Sc
6759
actions.PluginsEnabled = append(actions.PluginsEnabled, defaultPlugins...)
6860
}
6961

62+
if !actions.IncludeGitRoot {
63+
actions.PluginsDisabled = append(actions.PluginsDisabled, gitrepo.Name)
64+
}
65+
66+
if accessors.OSVDevClient == nil {
67+
actions.PluginsDisabled = append(actions.PluginsDisabled, vendored.Name)
68+
}
69+
7070
plugins := scalibrplugin.Resolve(actions.PluginsEnabled, actions.PluginsDisabled)
7171

7272
configurePlugins(plugins, accessors, actions)

0 commit comments

Comments
 (0)