Skip to content

Commit e2fc550

Browse files
another-rexcopybara-github
authored andcommitted
Respect disableGoogleAuth when initializing Google HTTP client in scalibr.
PiperOrigin-RevId: 950637626
1 parent 6ee2055 commit e2fc550

4 files changed

Lines changed: 100 additions & 7 deletions

File tree

clients/datasource/maven_registry.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func NewMavenRegistryAPIClient(
8484
ctx context.Context,
8585
registry MavenRegistry,
8686
localRegistry string,
87-
disableGoogleClient bool,
87+
disableGoogleAuth bool,
8888
httpClient *http.Client,
8989
googleClient *http.Client,
9090
) (*MavenRegistryAPIClient, error) {
@@ -120,7 +120,7 @@ func NewMavenRegistryAPIClient(
120120
mu: &sync.Mutex{},
121121
responses: NewRequestCache[string, response](),
122122
registryAuths: MakeMavenAuth(globalSettings, userSettings),
123-
disableGoogleAuth: disableGoogleClient,
123+
disableGoogleAuth: disableGoogleAuth,
124124
httpClient: httpClient,
125125
googleClient: googleClient,
126126
}
@@ -362,7 +362,7 @@ func (m *MavenRegistryAPIClient) get(ctx context.Context, auth *HTTPAuthenticati
362362
if isArtifactRegistry {
363363
requestURL.Scheme = "https"
364364
// For Artifact Registry, use google.DefaultClient for ADC if available.
365-
if m.googleClient != nil {
365+
if m.googleClient != nil && !m.disableGoogleAuth {
366366
httpClient = m.googleClient
367367
}
368368
}

clients/datasource/maven_registry_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ package datasource_test
1616

1717
import (
1818
"bytes"
19+
"io"
1920
"net/http"
2021
"os"
2122
"path/filepath"
2223
"reflect"
24+
"sync"
2325
"testing"
2426

2527
"deps.dev/util/maven"
@@ -278,3 +280,90 @@ func TestMavenLocalRegistry(t *testing.T) {
278280
t.Errorf("unexpected file content: got %s, want %s", string(content), string(resp))
279281
}
280282
}
283+
284+
type trackingTransport struct {
285+
mu sync.Mutex
286+
called bool
287+
}
288+
289+
func (t *trackingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
290+
t.mu.Lock()
291+
t.called = true
292+
t.mu.Unlock()
293+
return &http.Response{
294+
StatusCode: http.StatusOK,
295+
Body: io.NopCloser(bytes.NewReader([]byte("<project><groupId>g</groupId><artifactId>a</artifactId><version>v</version></project>"))),
296+
}, nil
297+
}
298+
299+
func (t *trackingTransport) wasCalled() bool {
300+
t.mu.Lock()
301+
defer t.mu.Unlock()
302+
return t.called
303+
}
304+
305+
// TestDisableGoogleAuthRespected tests that setting disableGoogleAuth = true in
306+
// NewMavenRegistryAPIClient prevents the Google client from being used for
307+
// Artifact Registry requests, falling back to the standard HTTP client.
308+
func TestDisableGoogleAuthRespected(t *testing.T) {
309+
standardTransport := &trackingTransport{}
310+
googleTransport := &trackingTransport{}
311+
312+
standardClient := &http.Client{Transport: standardTransport}
313+
googleClient := &http.Client{Transport: googleTransport}
314+
315+
client, err := datasource.NewMavenRegistryAPIClient(
316+
t.Context(),
317+
datasource.MavenRegistry{URL: "artifactregistry://example.com", ReleasesEnabled: true},
318+
"", // localRegistry
319+
true, // disableGoogleAuth
320+
standardClient,
321+
googleClient,
322+
)
323+
if err != nil {
324+
t.Fatalf("NewMavenRegistryAPIClient failed: %v", err)
325+
}
326+
327+
_, _ = client.GetProject(t.Context(), "g", "a", "v")
328+
329+
if googleTransport.wasCalled() {
330+
t.Errorf("Google client was called when disableGoogleAuth is true")
331+
}
332+
if !standardTransport.wasCalled() {
333+
t.Errorf("Standard client was not called")
334+
}
335+
}
336+
337+
// TestDisableGoogleAuthMethodRespected tests that dynamically calling
338+
// DisableGoogleAuth() post-construction prevents the Google client from being
339+
// used for Artifact Registry requests.
340+
func TestDisableGoogleAuthMethodRespected(t *testing.T) {
341+
standardTransport := &trackingTransport{}
342+
googleTransport := &trackingTransport{}
343+
344+
standardClient := &http.Client{Transport: standardTransport}
345+
googleClient := &http.Client{Transport: googleTransport}
346+
347+
client, err := datasource.NewMavenRegistryAPIClient(
348+
t.Context(),
349+
datasource.MavenRegistry{URL: "artifactregistry://example.com", ReleasesEnabled: true},
350+
"", // localRegistry
351+
false, // disableGoogleAuth
352+
standardClient,
353+
googleClient,
354+
)
355+
if err != nil {
356+
t.Fatalf("NewMavenRegistryAPIClient failed: %v", err)
357+
}
358+
359+
client.DisableGoogleAuth()
360+
361+
_, _ = client.GetProject(t.Context(), "g", "a", "v")
362+
363+
if googleTransport.wasCalled() {
364+
t.Errorf("Google client was called after DisableGoogleAuth()")
365+
}
366+
if !standardTransport.wasCalled() {
367+
t.Errorf("Standard client was not called")
368+
}
369+
}

enricher/transitivedependency/pomxml/pomxml.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"errors"
2121
"fmt"
2222
"maps"
23+
"net/http"
2324
"slices"
2425
"strings"
2526

@@ -110,9 +111,13 @@ func New(cfg *config.PluginConfig) (enricher.Enricher, error) {
110111
}
111112

112113
httpClient := cfg.ClientFactories.HTTPClient()
113-
googleClient, err := cfg.ClientFactories.GoogleHTTPClient(context.Background(), "https://www.googleapis.com/auth/cloud-platform")
114-
if err != nil {
115-
log.Warnf("Google default client unavailable, Artifact Registry will not be readable: %v", err)
114+
var googleClient *http.Client
115+
var err error
116+
if !disableGoogleAuth {
117+
googleClient, err = cfg.ClientFactories.GoogleHTTPClient(context.Background(), "https://www.googleapis.com/auth/cloud-platform")
118+
if err != nil {
119+
log.Warnf("Google default client unavailable, Artifact Registry will not be readable: %v", err)
120+
}
116121
}
117122

118123
mavenClient, err := datasource.NewMavenRegistryAPIClient(

enricher/transitivedependency/pomxml/pomxml_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,6 @@ func TestEnricher_Enrich_LocalModules(t *testing.T) {
963963
sort.Slice(wantInventory.Packages, func(i, j int) bool {
964964
return wantInventory.Packages[i].Name < wantInventory.Packages[j].Name
965965
})
966-
967966
if diff := cmp.Diff(wantInventory, inv); diff != "" {
968967
t.Errorf("%s.Enrich() diff (-want +got):\n%s", enrichy.Name(), diff)
969968
}

0 commit comments

Comments
 (0)