google.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. *
  3. * Copyright 2018 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. // Package google defines credentials for google cloud services.
  19. package google
  20. import (
  21. "context"
  22. "fmt"
  23. "time"
  24. "google.golang.org/grpc/credentials"
  25. "google.golang.org/grpc/credentials/alts"
  26. "google.golang.org/grpc/credentials/oauth"
  27. "google.golang.org/grpc/grpclog"
  28. "google.golang.org/grpc/internal"
  29. )
  30. const tokenRequestTimeout = 30 * time.Second
  31. // NewDefaultCredentials returns a credentials bundle that is configured to work
  32. // with google services.
  33. //
  34. // This API is experimental.
  35. func NewDefaultCredentials() credentials.Bundle {
  36. c := &creds{}
  37. bundle, err := c.NewWithMode(internal.CredsBundleModeFallback)
  38. if err != nil {
  39. grpclog.Warningf("google default creds: failed to create new creds: %v", err)
  40. }
  41. return bundle
  42. }
  43. // creds implements credentials.Bundle.
  44. type creds struct {
  45. // Supported modes are defined in internal/internal.go.
  46. mode string
  47. // The transport credentials associated with this bundle.
  48. transportCreds credentials.TransportCredentials
  49. // The per RPC credentials associated with this bundle.
  50. perRPCCreds credentials.PerRPCCredentials
  51. }
  52. func (c *creds) TransportCredentials() credentials.TransportCredentials {
  53. return c.transportCreds
  54. }
  55. func (c *creds) PerRPCCredentials() credentials.PerRPCCredentials {
  56. if c == nil {
  57. return nil
  58. }
  59. return c.perRPCCreds
  60. }
  61. // NewWithMode should make a copy of Bundle, and switch mode. Modifying the
  62. // existing Bundle may cause races.
  63. func (c *creds) NewWithMode(mode string) (credentials.Bundle, error) {
  64. newCreds := &creds{mode: mode}
  65. // Create transport credentials.
  66. switch mode {
  67. case internal.CredsBundleModeFallback:
  68. newCreds.transportCreds = credentials.NewTLS(nil)
  69. case internal.CredsBundleModeBackendFromBalancer, internal.CredsBundleModeBalancer:
  70. // Only the clients can use google default credentials, so we only need
  71. // to create new ALTS client creds here.
  72. newCreds.transportCreds = alts.NewClientCreds(alts.DefaultClientOptions())
  73. default:
  74. return nil, fmt.Errorf("google default creds: unsupported mode: %v", mode)
  75. }
  76. if mode == internal.CredsBundleModeFallback || mode == internal.CredsBundleModeBackendFromBalancer {
  77. // Create per RPC credentials.
  78. // For the time being, we required per RPC credentials for both TLS and
  79. // ALTS. In the future, this will only be required for TLS.
  80. ctx, cancel := context.WithTimeout(context.Background(), tokenRequestTimeout)
  81. defer cancel()
  82. var err error
  83. newCreds.perRPCCreds, err = oauth.NewApplicationDefault(ctx)
  84. if err != nil {
  85. grpclog.Warningf("google default creds: failed to create application oauth: %v", err)
  86. }
  87. }
  88. return newCreds, nil
  89. }