You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

google.go 3.9 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. newPerRPCCreds: func() credentials.PerRPCCredentials {
  38. ctx, cancel := context.WithTimeout(context.Background(), tokenRequestTimeout)
  39. defer cancel()
  40. perRPCCreds, err := oauth.NewApplicationDefault(ctx)
  41. if err != nil {
  42. grpclog.Warningf("google default creds: failed to create application oauth: %v", err)
  43. }
  44. return perRPCCreds
  45. },
  46. }
  47. bundle, err := c.NewWithMode(internal.CredsBundleModeFallback)
  48. if err != nil {
  49. grpclog.Warningf("google default creds: failed to create new creds: %v", err)
  50. }
  51. return bundle
  52. }
  53. // NewComputeEngineCredentials returns a credentials bundle that is configured to work
  54. // with google services. This API must only be used when running on GCE. Authentication configured
  55. // by this API represents the GCE VM's default service account.
  56. //
  57. // This API is experimental.
  58. func NewComputeEngineCredentials() credentials.Bundle {
  59. c := &creds{
  60. newPerRPCCreds: func() credentials.PerRPCCredentials {
  61. return oauth.NewComputeEngine()
  62. },
  63. }
  64. bundle, err := c.NewWithMode(internal.CredsBundleModeFallback)
  65. if err != nil {
  66. grpclog.Warningf("compute engine creds: failed to create new creds: %v", err)
  67. }
  68. return bundle
  69. }
  70. // creds implements credentials.Bundle.
  71. type creds struct {
  72. // Supported modes are defined in internal/internal.go.
  73. mode string
  74. // The transport credentials associated with this bundle.
  75. transportCreds credentials.TransportCredentials
  76. // The per RPC credentials associated with this bundle.
  77. perRPCCreds credentials.PerRPCCredentials
  78. // Creates new per RPC credentials
  79. newPerRPCCreds func() credentials.PerRPCCredentials
  80. }
  81. func (c *creds) TransportCredentials() credentials.TransportCredentials {
  82. return c.transportCreds
  83. }
  84. func (c *creds) PerRPCCredentials() credentials.PerRPCCredentials {
  85. if c == nil {
  86. return nil
  87. }
  88. return c.perRPCCreds
  89. }
  90. // NewWithMode should make a copy of Bundle, and switch mode. Modifying the
  91. // existing Bundle may cause races.
  92. func (c *creds) NewWithMode(mode string) (credentials.Bundle, error) {
  93. newCreds := &creds{
  94. mode: mode,
  95. newPerRPCCreds: c.newPerRPCCreds,
  96. }
  97. // Create transport credentials.
  98. switch mode {
  99. case internal.CredsBundleModeFallback:
  100. newCreds.transportCreds = credentials.NewTLS(nil)
  101. case internal.CredsBundleModeBackendFromBalancer, internal.CredsBundleModeBalancer:
  102. // Only the clients can use google default credentials, so we only need
  103. // to create new ALTS client creds here.
  104. newCreds.transportCreds = alts.NewClientCreds(alts.DefaultClientOptions())
  105. default:
  106. return nil, fmt.Errorf("unsupported mode: %v", mode)
  107. }
  108. if mode == internal.CredsBundleModeFallback || mode == internal.CredsBundleModeBackendFromBalancer {
  109. newCreds.perRPCCreds = newCreds.newPerRPCCreds()
  110. }
  111. return newCreds, nil
  112. }