utils_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //go:build linux || windows
  2. // +build linux windows
  3. /*
  4. *
  5. * Copyright 2018 gRPC authors.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. */
  20. package alts
  21. import (
  22. "context"
  23. "strings"
  24. "testing"
  25. "time"
  26. "google.golang.org/grpc/codes"
  27. altspb "google.golang.org/grpc/credentials/alts/internal/proto/grpc_gcp"
  28. "google.golang.org/grpc/peer"
  29. "google.golang.org/grpc/status"
  30. )
  31. const (
  32. testServiceAccount1 = "service_account1"
  33. testServiceAccount2 = "service_account2"
  34. testServiceAccount3 = "service_account3"
  35. defaultTestTimeout = 10 * time.Second
  36. )
  37. func (s) TestAuthInfoFromContext(t *testing.T) {
  38. ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
  39. defer cancel()
  40. altsAuthInfo := &fakeALTSAuthInfo{}
  41. p := &peer.Peer{
  42. AuthInfo: altsAuthInfo,
  43. }
  44. for _, tc := range []struct {
  45. desc string
  46. ctx context.Context
  47. success bool
  48. out AuthInfo
  49. }{
  50. {
  51. "working case",
  52. peer.NewContext(ctx, p),
  53. true,
  54. altsAuthInfo,
  55. },
  56. } {
  57. authInfo, err := AuthInfoFromContext(tc.ctx)
  58. if got, want := (err == nil), tc.success; got != want {
  59. t.Errorf("%v: AuthInfoFromContext(_)=(err=nil)=%v, want %v", tc.desc, got, want)
  60. }
  61. if got, want := authInfo, tc.out; got != want {
  62. t.Errorf("%v:, AuthInfoFromContext(_)=(%v, _), want (%v, _)", tc.desc, got, want)
  63. }
  64. }
  65. }
  66. func (s) TestAuthInfoFromPeer(t *testing.T) {
  67. altsAuthInfo := &fakeALTSAuthInfo{}
  68. p := &peer.Peer{
  69. AuthInfo: altsAuthInfo,
  70. }
  71. for _, tc := range []struct {
  72. desc string
  73. p *peer.Peer
  74. success bool
  75. out AuthInfo
  76. }{
  77. {
  78. "working case",
  79. p,
  80. true,
  81. altsAuthInfo,
  82. },
  83. } {
  84. authInfo, err := AuthInfoFromPeer(tc.p)
  85. if got, want := (err == nil), tc.success; got != want {
  86. t.Errorf("%v: AuthInfoFromPeer(_)=(err=nil)=%v, want %v", tc.desc, got, want)
  87. }
  88. if got, want := authInfo, tc.out; got != want {
  89. t.Errorf("%v:, AuthInfoFromPeer(_)=(%v, _), want (%v, _)", tc.desc, got, want)
  90. }
  91. }
  92. }
  93. func (s) TestClientAuthorizationCheck(t *testing.T) {
  94. ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
  95. defer cancel()
  96. altsAuthInfo := &fakeALTSAuthInfo{testServiceAccount1}
  97. p := &peer.Peer{
  98. AuthInfo: altsAuthInfo,
  99. }
  100. for _, tc := range []struct {
  101. desc string
  102. ctx context.Context
  103. expectedServiceAccounts []string
  104. success bool
  105. code codes.Code
  106. }{
  107. {
  108. "working case",
  109. peer.NewContext(ctx, p),
  110. []string{testServiceAccount1, testServiceAccount2},
  111. true,
  112. codes.OK, // err is nil, code is OK.
  113. },
  114. {
  115. "working case (case ignored)",
  116. peer.NewContext(ctx, p),
  117. []string{strings.ToUpper(testServiceAccount1), testServiceAccount2},
  118. true,
  119. codes.OK, // err is nil, code is OK.
  120. },
  121. {
  122. "context does not have AuthInfo",
  123. ctx,
  124. []string{testServiceAccount1, testServiceAccount2},
  125. false,
  126. codes.PermissionDenied,
  127. },
  128. {
  129. "unauthorized client",
  130. peer.NewContext(ctx, p),
  131. []string{testServiceAccount2, testServiceAccount3},
  132. false,
  133. codes.PermissionDenied,
  134. },
  135. } {
  136. err := ClientAuthorizationCheck(tc.ctx, tc.expectedServiceAccounts)
  137. if got, want := (err == nil), tc.success; got != want {
  138. t.Errorf("%v: ClientAuthorizationCheck(_, %v)=(err=nil)=%v, want %v", tc.desc, tc.expectedServiceAccounts, got, want)
  139. }
  140. if got, want := status.Code(err), tc.code; got != want {
  141. t.Errorf("%v: ClientAuthorizationCheck(_, %v).Code=%v, want %v", tc.desc, tc.expectedServiceAccounts, got, want)
  142. }
  143. }
  144. }
  145. type fakeALTSAuthInfo struct {
  146. peerServiceAccount string
  147. }
  148. func (*fakeALTSAuthInfo) AuthType() string { return "" }
  149. func (*fakeALTSAuthInfo) ApplicationProtocol() string { return "" }
  150. func (*fakeALTSAuthInfo) RecordProtocol() string { return "" }
  151. func (*fakeALTSAuthInfo) SecurityLevel() altspb.SecurityLevel {
  152. return altspb.SecurityLevel_SECURITY_NONE
  153. }
  154. func (f *fakeALTSAuthInfo) PeerServiceAccount() string { return f.peerServiceAccount }
  155. func (*fakeALTSAuthInfo) LocalServiceAccount() string { return "" }
  156. func (*fakeALTSAuthInfo) PeerRPCVersions() *altspb.RpcProtocolVersions { return nil }