settings.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. "errors"
  8. "net/http"
  9. "golang.org/x/oauth2"
  10. "golang.org/x/oauth2/google"
  11. "google.golang.org/grpc"
  12. )
  13. // DialSettings holds information needed to establish a connection with a
  14. // Google API service.
  15. type DialSettings struct {
  16. Endpoint string
  17. Scopes []string
  18. TokenSource oauth2.TokenSource
  19. Credentials *google.Credentials
  20. CredentialsFile string // if set, Token Source is ignored.
  21. CredentialsJSON []byte
  22. UserAgent string
  23. APIKey string
  24. Audiences []string
  25. HTTPClient *http.Client
  26. GRPCDialOpts []grpc.DialOption
  27. GRPCConn *grpc.ClientConn
  28. NoAuth bool
  29. TelemetryDisabled bool
  30. // Google API system parameters. For more information please read:
  31. // https://cloud.google.com/apis/docs/system-parameters
  32. QuotaProject string
  33. RequestReason string
  34. }
  35. // Validate reports an error if ds is invalid.
  36. func (ds *DialSettings) Validate() error {
  37. hasCreds := ds.APIKey != "" || ds.TokenSource != nil || ds.CredentialsFile != "" || ds.Credentials != nil
  38. if ds.NoAuth && hasCreds {
  39. return errors.New("options.WithoutAuthentication is incompatible with any option that provides credentials")
  40. }
  41. // Credentials should not appear with other options.
  42. // We currently allow TokenSource and CredentialsFile to coexist.
  43. // TODO(jba): make TokenSource & CredentialsFile an error (breaking change).
  44. nCreds := 0
  45. if ds.Credentials != nil {
  46. nCreds++
  47. }
  48. if ds.CredentialsJSON != nil {
  49. nCreds++
  50. }
  51. if ds.CredentialsFile != "" {
  52. nCreds++
  53. }
  54. if ds.APIKey != "" {
  55. nCreds++
  56. }
  57. if ds.TokenSource != nil {
  58. nCreds++
  59. }
  60. if len(ds.Scopes) > 0 && len(ds.Audiences) > 0 {
  61. return errors.New("WithScopes is incompatible with WithAudience")
  62. }
  63. // Accept only one form of credentials, except we allow TokenSource and CredentialsFile for backwards compatibility.
  64. if nCreds > 1 && !(nCreds == 2 && ds.TokenSource != nil && ds.CredentialsFile != "") {
  65. return errors.New("multiple credential options provided")
  66. }
  67. if ds.HTTPClient != nil && ds.GRPCConn != nil {
  68. return errors.New("WithHTTPClient is incompatible with WithGRPCConn")
  69. }
  70. if ds.HTTPClient != nil && ds.GRPCDialOpts != nil {
  71. return errors.New("WithHTTPClient is incompatible with gRPC dial options")
  72. }
  73. if ds.HTTPClient != nil && ds.QuotaProject != "" {
  74. return errors.New("WithHTTPClient is incompatible with QuotaProject")
  75. }
  76. if ds.HTTPClient != nil && ds.RequestReason != "" {
  77. return errors.New("WithHTTPClient is incompatible with RequestReason")
  78. }
  79. return nil
  80. }