summaryrefslogtreecommitdiff
path: root/vendor/google.golang.org/api/internal/creds.go
diff options
context:
space:
mode:
authorNicolas Paul <n@nc0.fr>2023-04-07 01:18:07 +0200
committerNicolas Paul <nicolaspaul45400@gmail.com>2023-04-07 01:18:36 +0200
commit96c174f226767294186467715a1931cce850678d (patch)
tree553b3a2457387667743ecc7df61b83abd7d3d46e /vendor/google.golang.org/api/internal/creds.go
parent2e4db08bc6360a13e167ad9eca7f21dc8a2f89a9 (diff)
update deps
Diffstat (limited to 'vendor/google.golang.org/api/internal/creds.go')
-rw-r--r--vendor/google.golang.org/api/internal/creds.go76
1 files changed, 70 insertions, 6 deletions
diff --git a/vendor/google.golang.org/api/internal/creds.go b/vendor/google.golang.org/api/internal/creds.go
index 32d5241..63c6609 100644
--- a/vendor/google.golang.org/api/internal/creds.go
+++ b/vendor/google.golang.org/api/internal/creds.go
@@ -6,10 +6,15 @@ package internal
import (
"context"
+ "crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
+ "net"
+ "net/http"
+ "os"
+ "time"
"golang.org/x/oauth2"
"google.golang.org/api/internal/impersonate"
@@ -17,6 +22,8 @@ import (
"golang.org/x/oauth2/google"
)
+const quotaProjectEnvVar = "GOOGLE_CLOUD_QUOTA_PROJECT"
+
// Creds returns credential information obtained from DialSettings, or if none, then
// it returns default credential information.
func Creds(ctx context.Context, ds *DialSettings) (*google.Credentials, error) {
@@ -80,8 +87,25 @@ const (
// - Otherwise, executes standard OAuth 2.0 flow
// More details: google.aip.dev/auth/4111
func credentialsFromJSON(ctx context.Context, data []byte, ds *DialSettings) (*google.Credentials, error) {
+ var params google.CredentialsParams
+ params.Scopes = ds.GetScopes()
+
+ // Determine configurations for the OAuth2 transport, which is separate from the API transport.
+ // The OAuth2 transport and endpoint will be configured for mTLS if applicable.
+ clientCertSource, oauth2Endpoint, err := GetClientCertificateSourceAndEndpoint(oauth2DialSettings(ds))
+ if err != nil {
+ return nil, err
+ }
+ params.TokenURL = oauth2Endpoint
+ if clientCertSource != nil {
+ tlsConfig := &tls.Config{
+ GetClientCertificate: clientCertSource,
+ }
+ ctx = context.WithValue(ctx, oauth2.HTTPClient, customHTTPClient(tlsConfig))
+ }
+
// By default, a standard OAuth 2.0 token source is created
- cred, err := google.CredentialsFromJSON(ctx, data, ds.GetScopes()...)
+ cred, err := google.CredentialsFromJSONWithParams(ctx, data, params)
if err != nil {
return nil, err
}
@@ -131,14 +155,22 @@ func selfSignedJWTTokenSource(data []byte, ds *DialSettings) (oauth2.TokenSource
}
}
-// QuotaProjectFromCreds returns the quota project from the JSON blob in the provided credentials.
-//
-// NOTE(cbro): consider promoting this to a field on google.Credentials.
-func QuotaProjectFromCreds(cred *google.Credentials) string {
+// GetQuotaProject retrieves quota project with precedence being: client option,
+// environment variable, creds file.
+func GetQuotaProject(creds *google.Credentials, clientOpt string) string {
+ if clientOpt != "" {
+ return clientOpt
+ }
+ if env := os.Getenv(quotaProjectEnvVar); env != "" {
+ return env
+ }
+ if creds == nil {
+ return ""
+ }
var v struct {
QuotaProject string `json:"quota_project_id"`
}
- if err := json.Unmarshal(cred.JSON, &v); err != nil {
+ if err := json.Unmarshal(creds.JSON, &v); err != nil {
return ""
}
return v.QuotaProject
@@ -157,3 +189,35 @@ func impersonateCredentials(ctx context.Context, creds *google.Credentials, ds *
ProjectID: creds.ProjectID,
}, nil
}
+
+// oauth2DialSettings returns the settings to be used by the OAuth2 transport, which is separate from the API transport.
+func oauth2DialSettings(ds *DialSettings) *DialSettings {
+ var ods DialSettings
+ ods.DefaultEndpoint = google.Endpoint.TokenURL
+ ods.DefaultMTLSEndpoint = google.MTLSTokenURL
+ ods.ClientCertSource = ds.ClientCertSource
+ return &ods
+}
+
+// customHTTPClient constructs an HTTPClient using the provided tlsConfig, to support mTLS.
+func customHTTPClient(tlsConfig *tls.Config) *http.Client {
+ trans := baseTransport()
+ trans.TLSClientConfig = tlsConfig
+ return &http.Client{Transport: trans}
+}
+
+func baseTransport() *http.Transport {
+ return &http.Transport{
+ Proxy: http.ProxyFromEnvironment,
+ DialContext: (&net.Dialer{
+ Timeout: 30 * time.Second,
+ KeepAlive: 30 * time.Second,
+ DualStack: true,
+ }).DialContext,
+ MaxIdleConns: 100,
+ MaxIdleConnsPerHost: 100,
+ IdleConnTimeout: 90 * time.Second,
+ TLSHandshakeTimeout: 10 * time.Second,
+ ExpectContinueTimeout: 1 * time.Second,
+ }
+}