Skip to content

Commit 86ea7f3

Browse files
Slachaider-chat-bot
andcommitted
fix: GCS connection error with multiple credential options
Co-authored-by: aider (gemini/gemini-3-flash-preview) <aider@aider.chat>
1 parent 46321ba commit 86ea7f3

1 file changed

Lines changed: 41 additions & 60 deletions

File tree

‎pkg/storage/gcs.go‎

Lines changed: 41 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"google.golang.org/api/impersonate"
2222
"google.golang.org/api/iterator"
2323
"google.golang.org/api/option"
24-
"google.golang.org/api/option/internaloption"
2524
googleHTTPTransport "google.golang.org/api/transport/http"
2625
)
2726

@@ -116,73 +115,55 @@ func (gcs *GCS) Connect(ctx context.Context) error {
116115
clientOptions = append(clientOptions, option.WithoutAuthentication())
117116
}
118117

119-
// storageClientOptions will be used for storage.NewClient
120-
// we need to separate them to avoid "multiple credential options provided" error
121-
storageClientOptions := make([]option.ClientOption, len(clientOptions))
122-
copy(storageClientOptions, clientOptions)
123-
124-
if gcs.Config.ForceHttp {
125-
customTransport := &http.Transport{
126-
WriteBufferSize: 128 * 1024,
127-
Proxy: http.ProxyFromEnvironment,
128-
DialContext: (&net.Dialer{
129-
Timeout: 30 * time.Second,
130-
KeepAlive: 30 * time.Second,
131-
}).DialContext,
132-
MaxIdleConns: 1,
133-
MaxIdleConnsPerHost: 1,
134-
IdleConnTimeout: 90 * time.Second,
135-
TLSHandshakeTimeout: 10 * time.Second,
136-
ExpectContinueTimeout: 1 * time.Second,
137-
}
138-
// must set ForceAttemptHTTP2 to false so that when a custom TLSClientConfig
139-
// is provided Golang does not setup HTTP/2 transport
140-
customTransport.ForceAttemptHTTP2 = false
141-
customTransport.TLSClientConfig = &tls.Config{
142-
NextProtos: []string{"http/1.1"},
143-
}
144-
// These clientOptions are passed in by storage.NewClient. However, to set a custom HTTP client
145-
// we must pass all these in manually.
146-
147-
if gcs.Config.Endpoint == "" {
148-
clientOptions = append([]option.ClientOption{option.WithScopes(storage.ScopeFullControl)}, clientOptions...)
149-
}
150-
clientOptions = append(clientOptions, internaloption.WithDefaultEndpoint(endpoint))
118+
storageClientOptions := clientOptions
151119

152-
customRoundTripper := &rewriteTransport{base: customTransport}
153-
gcpTransport, _, err := googleHTTPTransport.NewClient(ctx, clientOptions...)
154-
transport, err := googleHTTPTransport.NewTransport(ctx, customRoundTripper, clientOptions...)
155-
gcpTransport.Transport = transport
156-
if err != nil {
157-
return errors.Wrap(err, "failed to create GCP transport")
120+
if gcs.Config.ForceHttp || gcs.Config.Debug {
121+
dialOptions := make([]option.ClientOption, len(clientOptions))
122+
copy(dialOptions, clientOptions)
123+
if gcs.Config.Endpoint == "" && !gcs.Config.SkipCredentials {
124+
dialOptions = append(dialOptions, option.WithScopes(storage.ScopeFullControl))
158125
}
159126

160-
storageClientOptions = []option.ClientOption{
161-
option.WithTelemetryDisabled(),
162-
option.WithHTTPClient(gcpTransport),
163-
}
164-
if gcs.Config.Endpoint != "" {
165-
storageClientOptions = append(storageClientOptions, option.WithEndpoint(endpoint))
166-
}
167-
}
168-
169-
if gcs.Config.Debug {
170-
if gcs.Config.Endpoint == "" {
171-
clientOptions = append([]option.ClientOption{option.WithScopes(storage.ScopeFullControl)}, clientOptions...)
172-
}
173-
clientOptions = append(clientOptions, internaloption.WithDefaultEndpoint(endpoint))
174-
if strings.HasPrefix(endpoint, "https://") {
175-
clientOptions = append(clientOptions, internaloption.WithDefaultMTLSEndpoint(endpoint))
127+
var httpClient *http.Client
128+
if gcs.Config.ForceHttp {
129+
customTransport := &http.Transport{
130+
WriteBufferSize: 128 * 1024,
131+
Proxy: http.ProxyFromEnvironment,
132+
DialContext: (&net.Dialer{
133+
Timeout: 30 * time.Second,
134+
KeepAlive: 30 * time.Second,
135+
}).DialContext,
136+
MaxIdleConns: 1,
137+
MaxIdleConnsPerHost: 1,
138+
IdleConnTimeout: 90 * time.Second,
139+
TLSHandshakeTimeout: 10 * time.Second,
140+
ExpectContinueTimeout: 1 * time.Second,
141+
}
142+
// must set ForceAttemptHTTP2 to false so that when a custom TLSClientConfig
143+
// is provided Golang does not setup HTTP/2 transport
144+
customTransport.ForceAttemptHTTP2 = false
145+
customTransport.TLSClientConfig = &tls.Config{
146+
NextProtos: []string{"http/1.1"},
147+
}
148+
customRoundTripper := &rewriteTransport{base: customTransport}
149+
transport, err := googleHTTPTransport.NewTransport(ctx, customRoundTripper, dialOptions...)
150+
if err != nil {
151+
return errors.Wrap(err, "failed to create GCP transport")
152+
}
153+
httpClient = &http.Client{Transport: transport}
154+
} else {
155+
httpClient, _, err = googleHTTPTransport.NewClient(ctx, dialOptions...)
156+
if err != nil {
157+
return errors.Wrap(err, "googleHTTPTransport.NewClient error")
158+
}
176159
}
177160

178-
debugClient, _, err := googleHTTPTransport.NewClient(ctx, clientOptions...)
179-
if err != nil {
180-
return errors.Wrap(err, "googleHTTPTransport.NewClient error")
161+
if gcs.Config.Debug {
162+
httpClient.Transport = debugGCSTransport{base: httpClient.Transport}
181163
}
182-
debugClient.Transport = debugGCSTransport{base: debugClient.Transport}
183164
storageClientOptions = []option.ClientOption{
184165
option.WithTelemetryDisabled(),
185-
option.WithHTTPClient(debugClient),
166+
option.WithHTTPClient(httpClient),
186167
}
187168
if gcs.Config.Endpoint != "" {
188169
storageClientOptions = append(storageClientOptions, option.WithEndpoint(endpoint))

0 commit comments

Comments
 (0)