settings.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2017 Google LLC.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package internal supports the options and transport packages.
  5. package internal
  6. import (
  7. "crypto/tls"
  8. "errors"
  9. "net/http"
  10. "golang.org/x/oauth2"
  11. "golang.org/x/oauth2/google"
  12. "google.golang.org/api/internal/impersonate"
  13. "google.golang.org/grpc"
  14. )
  15. // DialSettings holds information needed to establish a connection with a
  16. // Google API service.
  17. type DialSettings struct {
  18. Endpoint string
  19. DefaultEndpoint string
  20. DefaultMTLSEndpoint string
  21. Scopes []string
  22. DefaultScopes []string
  23. EnableJwtWithScope bool
  24. TokenSource oauth2.TokenSource
  25. Credentials *google.Credentials
  26. CredentialsFile string // if set, Token Source is ignored.
  27. CredentialsJSON []byte
  28. InternalCredentials *google.Credentials
  29. UserAgent string
  30. APIKey string
  31. Audiences []string
  32. DefaultAudience string
  33. HTTPClient *http.Client
  34. GRPCDialOpts []grpc.DialOption
  35. GRPCConn *grpc.ClientConn
  36. GRPCConnPool ConnPool
  37. GRPCConnPoolSize int
  38. NoAuth bool
  39. TelemetryDisabled bool
  40. ClientCertSource func(*tls.CertificateRequestInfo) (*tls.Certificate, error)
  41. CustomClaims map[string]interface{}
  42. SkipValidation bool
  43. ImpersonationConfig *impersonate.Config
  44. EnableDirectPath bool
  45. EnableDirectPathXds bool
  46. AllowNonDefaultServiceAccount bool
  47. // Google API system parameters. For more information please read:
  48. // https://cloud.google.com/apis/docs/system-parameters
  49. QuotaProject string
  50. RequestReason string
  51. }
  52. // GetScopes returns the user-provided scopes, if set, or else falls back to the
  53. // default scopes.
  54. func (ds *DialSettings) GetScopes() []string {
  55. if len(ds.Scopes) > 0 {
  56. return ds.Scopes
  57. }
  58. return ds.DefaultScopes
  59. }
  60. // GetAudience returns the user-provided audience, if set, or else falls back to the default audience.
  61. func (ds *DialSettings) GetAudience() string {
  62. if ds.HasCustomAudience() {
  63. return ds.Audiences[0]
  64. }
  65. return ds.DefaultAudience
  66. }
  67. // HasCustomAudience returns true if a custom audience is provided by users.
  68. func (ds *DialSettings) HasCustomAudience() bool {
  69. return len(ds.Audiences) > 0
  70. }
  71. // Validate reports an error if ds is invalid.
  72. func (ds *DialSettings) Validate() error {
  73. if ds.SkipValidation {
  74. return nil
  75. }
  76. hasCreds := ds.APIKey != "" || ds.TokenSource != nil || ds.CredentialsFile != "" || ds.Credentials != nil
  77. if ds.NoAuth && hasCreds {
  78. return errors.New("options.WithoutAuthentication is incompatible with any option that provides credentials")
  79. }
  80. // Credentials should not appear with other options.
  81. // We currently allow TokenSource and CredentialsFile to coexist.
  82. // TODO(jba): make TokenSource & CredentialsFile an error (breaking change).
  83. nCreds := 0
  84. if ds.Credentials != nil {
  85. nCreds++
  86. }
  87. if ds.CredentialsJSON != nil {
  88. nCreds++
  89. }
  90. if ds.CredentialsFile != "" {
  91. nCreds++
  92. }
  93. if ds.APIKey != "" {
  94. nCreds++
  95. }
  96. if ds.TokenSource != nil {
  97. nCreds++
  98. }
  99. if len(ds.Scopes) > 0 && len(ds.Audiences) > 0 {
  100. return errors.New("WithScopes is incompatible with WithAudience")
  101. }
  102. // Accept only one form of credentials, except we allow TokenSource and CredentialsFile for backwards compatibility.
  103. if nCreds > 1 && !(nCreds == 2 && ds.TokenSource != nil && ds.CredentialsFile != "") {
  104. return errors.New("multiple credential options provided")
  105. }
  106. if ds.GRPCConn != nil && ds.GRPCConnPool != nil {
  107. return errors.New("WithGRPCConn is incompatible with WithConnPool")
  108. }
  109. if ds.HTTPClient != nil && ds.GRPCConnPool != nil {
  110. return errors.New("WithHTTPClient is incompatible with WithConnPool")
  111. }
  112. if ds.HTTPClient != nil && ds.GRPCConn != nil {
  113. return errors.New("WithHTTPClient is incompatible with WithGRPCConn")
  114. }
  115. if ds.HTTPClient != nil && ds.GRPCDialOpts != nil {
  116. return errors.New("WithHTTPClient is incompatible with gRPC dial options")
  117. }
  118. if ds.HTTPClient != nil && ds.QuotaProject != "" {
  119. return errors.New("WithHTTPClient is incompatible with QuotaProject")
  120. }
  121. if ds.HTTPClient != nil && ds.RequestReason != "" {
  122. return errors.New("WithHTTPClient is incompatible with RequestReason")
  123. }
  124. if ds.HTTPClient != nil && ds.ClientCertSource != nil {
  125. return errors.New("WithHTTPClient is incompatible with WithClientCertSource")
  126. }
  127. if ds.ClientCertSource != nil && (ds.GRPCConn != nil || ds.GRPCConnPool != nil || ds.GRPCConnPoolSize != 0 || ds.GRPCDialOpts != nil) {
  128. return errors.New("WithClientCertSource is currently only supported for HTTP. gRPC settings are incompatible")
  129. }
  130. if ds.ImpersonationConfig != nil && len(ds.ImpersonationConfig.Scopes) == 0 && len(ds.Scopes) == 0 {
  131. return errors.New("WithImpersonatedCredentials requires scopes being provided")
  132. }
  133. return nil
  134. }