Skip to content

Commit

Permalink
[fix] Move detector initialization to DefaultDetectors function (#3341)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcastorina authored Sep 26, 2024
1 parent f3630da commit 0328a19
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
19 changes: 18 additions & 1 deletion pkg/engine/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ import (
)

func DefaultDetectors() []detectors.Detector {
return []detectors.Detector{
detectorList := []detectors.Detector{
&heroku.Scanner{},
&pypi.Scanner{},
&linearapi.Scanner{},
Expand Down Expand Up @@ -1635,6 +1635,23 @@ func DefaultDetectors() []detectors.Detector {
nvapi.Scanner{},
railwayapp.Scanner{},
}

// Automatically initialize all detectors that implement
// EndpointCustomizer and/or CloudProvider interfaces.
for _, d := range detectorList {
customizer, ok := d.(detectors.EndpointCustomizer)
if !ok {
continue
}
// Default to always use the cloud endpoints (if available) and the found endpoints.
customizer.UseFoundEndpoints(true)
customizer.UseCloudEndpoint(true)
if cloudProvider, ok := d.(detectors.CloudProvider); ok {
customizer.SetCloudEndpoint(cloudProvider.CloudEndpoint())
}
}

return detectorList
}

func DefaultDetectorTypesImplementing[T any]() map[detectorspb.DetectorType]struct{} {
Expand Down
26 changes: 3 additions & 23 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,21 +267,6 @@ func NewEngine(ctx context.Context, cfg *Config) (*Engine, error) {
if err != nil {
return nil, err
}

// Abuse filters to initialize endpoint customizer detectors.
filters = append(filters, func(d detectors.Detector) bool {
customizer, ok := d.(detectors.EndpointCustomizer)
if !ok {
return true
}
customizer.UseFoundEndpoints(true)
customizer.UseCloudEndpoint(true)
if cloudProvider, ok := d.(detectors.CloudProvider); ok {
customizer.SetCloudEndpoint(cloudProvider.CloudEndpoint())
}
return true
})

if len(detectorsWithCustomVerifierEndpoints) > 0 {
filters = append(filters, func(d detectors.Detector) bool {
urls, ok := getWithDetectorID(d, detectorsWithCustomVerifierEndpoints)
Expand All @@ -293,20 +278,15 @@ func NewEngine(ctx context.Context, cfg *Config) (*Engine, error) {
return false
}

if !cfg.CustomVerifiersOnly || len(urls) == 0 {
customizer.UseFoundEndpoints(true)
customizer.UseCloudEndpoint(true)
if cfg.CustomVerifiersOnly && len(urls) > 0 {
customizer.UseCloudEndpoint(false)
customizer.UseFoundEndpoints(false)
}

if err := customizer.SetConfiguredEndpoints(urls...); err != nil {
return false
}

cloudProvider, ok := d.(detectors.CloudProvider)
if ok {
customizer.SetCloudEndpoint(cloudProvider.CloudEndpoint())
}

return true
})
}
Expand Down

0 comments on commit 0328a19

Please sign in to comment.