Skip to content

Commit

Permalink
[fix] Correctly initialize detectors with cloud endpoint customization (
Browse files Browse the repository at this point in the history
#3333)

* [fix] Correctly initialize detectors with cloud endpoint customization

We were only initializing if the detector was configured with a custom
endpoint, but not in the default case.

* Add test

* Fix gitlab.v2 detector
  • Loading branch information
mcastorina authored Sep 25, 2024
1 parent eb40243 commit 4484bf4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/detectors/gitlab/v2/gitlab_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ var _ detectors.Detector = (*Scanner)(nil)
var _ detectors.EndpointCustomizer = (*Scanner)(nil)
var _ detectors.Versioner = (*Scanner)(nil)

func (Scanner) Version() int { return 2 }
func (Scanner) DefaultEndpoint() string { return "https://gitlab.com" }
func (Scanner) Version() int { return 2 }
func (Scanner) CloudEndpoint() string { return "https://gitlab.com" }

var (
defaultClient = common.SaneHttpClient()
Expand Down Expand Up @@ -77,7 +77,7 @@ func (s Scanner) verifyGitlab(ctx context.Context, resMatch string) (bool, error
if client == nil {
client = defaultClient
}
for _, baseURL := range s.Endpoints(s.DefaultEndpoint()) {
for _, baseURL := range s.Endpoints() {
// test `read_user` scope
req, err := http.NewRequestWithContext(ctx, "GET", baseURL+"/api/v4/user", nil)
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,20 @@ func NewEngine(ctx context.Context, cfg *Config) (*Engine, error) {
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 Down
23 changes: 23 additions & 0 deletions pkg/engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1043,3 +1043,26 @@ func TestEngine_ShouldVerifyChunk(t *testing.T) {
}
}
}

func TestEngineInitializesCloudProviderDetectors(t *testing.T) {
ctx := context.Background()
conf := Config{
Concurrency: 1,
Detectors: DefaultDetectors(),
Verify: false,
SourceManager: sources.NewManager(),
Dispatcher: NewPrinterDispatcher(new(discardPrinter)),
}

e, err := NewEngine(ctx, &conf)
assert.NoError(t, err)

for _, det := range e.detectors {
if endpoints, ok := det.(interface{ Endpoints(...string) []string }); ok {
id := config.GetDetectorID(det)
if len(endpoints.Endpoints()) == 0 {
t.Fatalf("detector %q Endpoints() is empty", id.String())
}
}
}
}

0 comments on commit 4484bf4

Please sign in to comment.