google_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. *
  3. * Copyright 2021 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
  19. import (
  20. "context"
  21. "net"
  22. "testing"
  23. "google.golang.org/grpc/credentials"
  24. "google.golang.org/grpc/internal"
  25. icredentials "google.golang.org/grpc/internal/credentials"
  26. "google.golang.org/grpc/internal/grpctest"
  27. "google.golang.org/grpc/resolver"
  28. )
  29. type s struct {
  30. grpctest.Tester
  31. }
  32. func Test(t *testing.T) {
  33. grpctest.RunSubTests(t, s{})
  34. }
  35. type testCreds struct {
  36. credentials.TransportCredentials
  37. typ string
  38. }
  39. func (c *testCreds) ClientHandshake(ctx context.Context, authority string, rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
  40. return nil, &testAuthInfo{typ: c.typ}, nil
  41. }
  42. func (c *testCreds) ServerHandshake(conn net.Conn) (net.Conn, credentials.AuthInfo, error) {
  43. return nil, &testAuthInfo{typ: c.typ}, nil
  44. }
  45. type testAuthInfo struct {
  46. typ string
  47. }
  48. func (t *testAuthInfo) AuthType() string {
  49. return t.typ
  50. }
  51. var (
  52. testTLS = &testCreds{typ: "tls"}
  53. testALTS = &testCreds{typ: "alts"}
  54. )
  55. func overrideNewCredsFuncs() func() {
  56. origNewTLS := newTLS
  57. newTLS = func() credentials.TransportCredentials {
  58. return testTLS
  59. }
  60. origNewALTS := newALTS
  61. newALTS = func() credentials.TransportCredentials {
  62. return testALTS
  63. }
  64. origNewADC := newADC
  65. newADC = func(context.Context) (credentials.PerRPCCredentials, error) {
  66. // We do not use perRPC creds in this test. It is safe to return nil here.
  67. return nil, nil
  68. }
  69. return func() {
  70. newTLS = origNewTLS
  71. newALTS = origNewALTS
  72. newADC = origNewADC
  73. }
  74. }
  75. // TestClientHandshakeBasedOnClusterName that by default (without switching
  76. // modes), ClientHandshake does either tls or alts base on the cluster name in
  77. // attributes.
  78. func (s) TestClientHandshakeBasedOnClusterName(t *testing.T) {
  79. defer overrideNewCredsFuncs()()
  80. for bundleTyp, tc := range map[string]credentials.Bundle{
  81. "defaultCredsWithOptions": NewDefaultCredentialsWithOptions(DefaultCredentialsOptions{}),
  82. "defaultCreds": NewDefaultCredentials(),
  83. "computeCreds": NewComputeEngineCredentials(),
  84. } {
  85. tests := []struct {
  86. name string
  87. ctx context.Context
  88. wantTyp string
  89. }{
  90. {
  91. name: "no cluster name",
  92. ctx: context.Background(),
  93. wantTyp: "tls",
  94. },
  95. {
  96. name: "with non-CFE cluster name",
  97. ctx: icredentials.NewClientHandshakeInfoContext(context.Background(), credentials.ClientHandshakeInfo{
  98. Attributes: internal.SetXDSHandshakeClusterName(resolver.Address{}, "lalala").Attributes,
  99. }),
  100. // non-CFE backends should use alts.
  101. wantTyp: "alts",
  102. },
  103. {
  104. name: "with CFE cluster name",
  105. ctx: icredentials.NewClientHandshakeInfoContext(context.Background(), credentials.ClientHandshakeInfo{
  106. Attributes: internal.SetXDSHandshakeClusterName(resolver.Address{}, "google_cfe_bigtable.googleapis.com").Attributes,
  107. }),
  108. // CFE should use tls.
  109. wantTyp: "tls",
  110. },
  111. {
  112. name: "with xdstp CFE cluster name",
  113. ctx: icredentials.NewClientHandshakeInfoContext(context.Background(), credentials.ClientHandshakeInfo{
  114. Attributes: internal.SetXDSHandshakeClusterName(resolver.Address{}, "xdstp://traffic-director-c2p.xds.googleapis.com/envoy.config.cluster.v3.Cluster/google_cfe_bigtable.googleapis.com").Attributes,
  115. }),
  116. // CFE should use tls.
  117. wantTyp: "tls",
  118. },
  119. {
  120. name: "with xdstp non-CFE cluster name",
  121. ctx: icredentials.NewClientHandshakeInfoContext(context.Background(), credentials.ClientHandshakeInfo{
  122. Attributes: internal.SetXDSHandshakeClusterName(resolver.Address{}, "xdstp://other.com/envoy.config.cluster.v3.Cluster/google_cfe_bigtable.googleapis.com").Attributes,
  123. }),
  124. // non-CFE should use atls.
  125. wantTyp: "alts",
  126. },
  127. }
  128. for _, tt := range tests {
  129. t.Run(bundleTyp+" "+tt.name, func(t *testing.T) {
  130. _, info, err := tc.TransportCredentials().ClientHandshake(tt.ctx, "", nil)
  131. if err != nil {
  132. t.Fatalf("ClientHandshake failed: %v", err)
  133. }
  134. if gotType := info.AuthType(); gotType != tt.wantTyp {
  135. t.Fatalf("unexpected authtype: %v, want: %v", gotType, tt.wantTyp)
  136. }
  137. _, infoServer, err := tc.TransportCredentials().ServerHandshake(nil)
  138. if err != nil {
  139. t.Fatalf("ClientHandshake failed: %v", err)
  140. }
  141. // ServerHandshake should always do TLS.
  142. if gotType := infoServer.AuthType(); gotType != "tls" {
  143. t.Fatalf("unexpected server authtype: %v, want: %v", gotType, "tls")
  144. }
  145. })
  146. }
  147. }
  148. }