googleapis / google-cloud-go

Google Cloud Client Libraries for Go.
https://cloud.google.com/go/docs/reference
Apache License 2.0
3.78k stars 1.3k forks source link

panic when calling gsheet.NewService (google.golang.org/api v0.188.0)packagename: cloud.google.com/go/auth v0.7.0 #10925

Closed lagigliaivan closed 1 month ago

lagigliaivan commented 1 month ago

Context

    sheetsService, err := gsheet.NewService(ctx, *clientOption)
    if err != nil {
        return nil, errors.Wrap(err, "failed to create google sheets service")
    }
``
## Client

httptransport.NewClient

## Environment

$ go version 1.22.2

## Code and Dependencies

file: cloud.google.com/go/auth/httptransport/transport.go

```go
func defaultBaseTransport(clientCertSource cert.Provider, dialTLSContext func(context.Context, string, string) (net.Conn, error)) http.RoundTripper {
    **trans := http.DefaultTransport.(*http.Transport).Clone()** //this line panics
    trans.MaxIdleConnsPerHost = 100

    if clientCertSource != nil {
        trans.TLSClientConfig = &tls.Config{
            GetClientCertificate: clientCertSource,
        }
    }
    if dialTLSContext != nil {
        // If DialTLSContext is set, TLSClientConfig wil be ignored
        trans.DialTLSContext = dialTLSContext
    }

    // Configures the ReadIdleTimeout HTTP/2 option for the
    // transport. This allows broken idle connections to be pruned more quickly,
    // preventing the client from attempting to re-use connections that will no
    // longer work.
    http2Trans, err := http2.ConfigureTransports(trans)
    if err == nil {
        http2Trans.ReadIdleTimeout = time.Second * 31
    }

    return trans
}

Expected behavior

Don't panic

Actual behavior

Panic

panic: interface conversion: http.RoundTripper is *loghttp.Transport, not *http.Transport

goroutine 1 [running]:
cloud.google.com/go/auth/httptransport.defaultBaseTransport(0x14027d9a1e0?, 0x104ae2f00?)
    /Users/elagiglia/repos/pkg/mod/cloud.google.com/go/auth@v0.9.0/httptransport/transport.go:97 +0x118
cloud.google.com/go/auth/httptransport.NewClient(0x14000acbee0)
    /Users/elagiglia/repos/pkg/mod/cloud.google.com/go/auth@v0.9.0/httptransport/httptransport.go:203 +0x1b0
google.golang.org/api/transport/http.newClientNewAuth({0x104f91a50, 0x14027d9a1e0}, {0x0, 0x0}, 0x14027db0000)
    /Users/elagiglia/repos/pkg/mod/google.golang.org/api@v0.188.0/transport/http/dial.go:114 +0x670
google.golang.org/api/transport/http.NewClient({0x104f91a50, 0x14027d9a1e0}, {0x140000e6e80?, 0x102b65008?, 0x104b1f400?})
    /Users/elagiglia/repos/pkg/mod/google.golang.org/api@v0.188.0/transport/http/dial.go:51 +0x84
google.golang.org/api/sheets/v4.NewService({0x104f91a50, 0x14027d9a1e0}, {0x14000acc128, 0x1, 0x104df5020?})
    /Users/elagiglia/repos/pkg/mod/google.golang.org/api@v0.188.0/sheets/v4/sheets-gen.go:135 +0x35c
github.com/propertechnologies/ledgerlord/router.GoogleSheetsService({0x140000dc9c0, 0x197})
    /Users/elagiglia/repos/ledgerlord/router/routes.go:1848 +0xb4
github.com/propertechnologies/ledgerlord/router.SetUp.func5({{0x140000dc9c0?, 0x140000dc9c0?}, {0x14000a5e380?, 0x197?}})
    /Users/elagiglia/repos/ledgerlord/router/routes.go:367 +0x34
github.com/propertechnologies/ledgerlord/internal/integrations/google/sheets.(*ServicePool).createServices(0x14000b36640)
    /Users/elagiglia/repos/ledgerlord/internal/integrations/google/sheets/service_pool.go:95 +0x264
github.com/propertechnologies/ledgerlord/internal/integrations/google/sheets.NewServicePool({0x140007a1900, 0x104f6c328, 0xd18c2e2800, 0x14f46b0400, {0x104f85da0, 0x1063b9d20}})
    /Users/elagiglia/repos/ledgerlord/internal/integrations/google/sheets/service_pool.go:65 +0xf0
github.com/propertechnologies/ledgerlord/router.SetUp(0x14000296008)
    /Users/elagiglia/repos/ledgerlord/router/routes.go:362 +0xb38
main.main()
    /Users/elagiglia/repos/ledgerlord/cmd/ledgerlord/main.go:46 +0x68

Additional context

google.golang.org/api/sheets/v4/sheets-gen.go v0.188.0

The following code is the one that enables new library. opts = append(opts, internaloption.EnableNewAuthLibrary())

// NewService creates a new Service.
func NewService(ctx context.Context, opts ...option.ClientOption) (*Service, error) {
    scopesOption := internaloption.WithDefaultScopes(
        "https://www.googleapis.com/auth/drive",
        "https://www.googleapis.com/auth/drive.file",
        "https://www.googleapis.com/auth/drive.readonly",
        "https://www.googleapis.com/auth/spreadsheets",
        "https://www.googleapis.com/auth/spreadsheets.readonly",
    )
    // NOTE: prepend, so we don't override user-specified scopes.
    opts = append([]option.ClientOption{scopesOption}, opts...)
    opts = append(opts, internaloption.WithDefaultEndpoint(basePath))
    opts = append(opts, internaloption.WithDefaultEndpointTemplate(basePathTemplate))
    opts = append(opts, internaloption.WithDefaultMTLSEndpoint(mtlsBasePath))
    opts = append(opts, internaloption.EnableNewAuthLibrary())
    client, endpoint, err := htransport.NewClient(ctx, opts...)
    if err != nil {
        return nil, err
    }
    s, err := New(client)
    if err != nil {
        return nil, err
    }
    if endpoint != "" {
        s.BasePath = endpoint
    }
    return s, nil
}
quartzmo commented 1 month ago

Duplicate of 10925