alts_util_test.cc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. *
  3. * Copyright 2019 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. #include <gtest/gtest.h>
  19. #include "upb/upb.hpp"
  20. #include <grpcpp/security/alts_context.h>
  21. #include <grpcpp/security/alts_util.h>
  22. #include <grpcpp/security/auth_context.h>
  23. #include "src/core/tsi/alts/handshaker/alts_tsi_handshaker.h"
  24. #include "src/cpp/common/secure_auth_context.h"
  25. #include "src/proto/grpc/gcp/altscontext.upb.h"
  26. #include "test/core/util/test_config.h"
  27. #include "test/cpp/util/string_ref_helper.h"
  28. namespace grpc {
  29. namespace {
  30. TEST(AltsUtilTest, NullAuthContext) {
  31. std::unique_ptr<experimental::AltsContext> alts_context =
  32. experimental::GetAltsContextFromAuthContext(nullptr);
  33. EXPECT_EQ(alts_context, nullptr);
  34. }
  35. TEST(AltsUtilTest, EmptyAuthContext) {
  36. grpc_core::RefCountedPtr<grpc_auth_context> ctx =
  37. grpc_core::MakeRefCounted<grpc_auth_context>(nullptr);
  38. const std::shared_ptr<AuthContext> auth_context(
  39. new SecureAuthContext(ctx.get()));
  40. std::unique_ptr<experimental::AltsContext> alts_context =
  41. experimental::GetAltsContextFromAuthContext(auth_context);
  42. EXPECT_EQ(alts_context, nullptr);
  43. }
  44. TEST(AltsUtilTest, AuthContextWithMoreThanOneAltsContext) {
  45. grpc_core::RefCountedPtr<grpc_auth_context> ctx =
  46. grpc_core::MakeRefCounted<grpc_auth_context>(nullptr);
  47. const std::shared_ptr<AuthContext> auth_context(
  48. new SecureAuthContext(ctx.get()));
  49. ctx.reset();
  50. auth_context->AddProperty(TSI_ALTS_CONTEXT, "context1");
  51. auth_context->AddProperty(TSI_ALTS_CONTEXT, "context2");
  52. std::unique_ptr<experimental::AltsContext> alts_context =
  53. experimental::GetAltsContextFromAuthContext(auth_context);
  54. EXPECT_EQ(alts_context, nullptr);
  55. }
  56. TEST(AltsUtilTest, AuthContextWithBadAltsContext) {
  57. grpc_core::RefCountedPtr<grpc_auth_context> ctx =
  58. grpc_core::MakeRefCounted<grpc_auth_context>(nullptr);
  59. const std::shared_ptr<AuthContext> auth_context(
  60. new SecureAuthContext(ctx.get()));
  61. ctx.reset();
  62. auth_context->AddProperty(TSI_ALTS_CONTEXT,
  63. "bad context string serialization");
  64. std::unique_ptr<experimental::AltsContext> alts_context =
  65. experimental::GetAltsContextFromAuthContext(auth_context);
  66. EXPECT_EQ(alts_context, nullptr);
  67. }
  68. TEST(AltsUtilTest, AuthContextWithGoodAltsContextWithoutRpcVersions) {
  69. grpc_core::RefCountedPtr<grpc_auth_context> ctx =
  70. grpc_core::MakeRefCounted<grpc_auth_context>(nullptr);
  71. const std::shared_ptr<AuthContext> auth_context(
  72. new SecureAuthContext(ctx.get()));
  73. ctx.reset();
  74. std::string expected_ap("application protocol");
  75. std::string expected_rp("record protocol");
  76. std::string expected_peer("peer");
  77. std::string expected_local("local");
  78. std::string expected_peer_atrributes_key("peer");
  79. std::string expected_peer_atrributes_value("attributes");
  80. grpc_security_level expected_sl = GRPC_INTEGRITY_ONLY;
  81. upb::Arena context_arena;
  82. grpc_gcp_AltsContext* context = grpc_gcp_AltsContext_new(context_arena.ptr());
  83. grpc_gcp_AltsContext_set_application_protocol(
  84. context,
  85. upb_StringView_FromDataAndSize(expected_ap.data(), expected_ap.length()));
  86. grpc_gcp_AltsContext_set_record_protocol(
  87. context,
  88. upb_StringView_FromDataAndSize(expected_rp.data(), expected_rp.length()));
  89. grpc_gcp_AltsContext_set_security_level(context, expected_sl);
  90. grpc_gcp_AltsContext_set_peer_service_account(
  91. context, upb_StringView_FromDataAndSize(expected_peer.data(),
  92. expected_peer.length()));
  93. grpc_gcp_AltsContext_set_local_service_account(
  94. context, upb_StringView_FromDataAndSize(expected_local.data(),
  95. expected_local.length()));
  96. grpc_gcp_AltsContext_peer_attributes_set(
  97. context,
  98. upb_StringView_FromDataAndSize(expected_peer_atrributes_key.data(),
  99. expected_peer_atrributes_key.length()),
  100. upb_StringView_FromDataAndSize(expected_peer_atrributes_value.data(),
  101. expected_peer_atrributes_value.length()),
  102. context_arena.ptr());
  103. size_t serialized_ctx_length;
  104. char* serialized_ctx = grpc_gcp_AltsContext_serialize(
  105. context, context_arena.ptr(), &serialized_ctx_length);
  106. EXPECT_NE(serialized_ctx, nullptr);
  107. auth_context->AddProperty(TSI_ALTS_CONTEXT,
  108. string(serialized_ctx, serialized_ctx_length));
  109. std::unique_ptr<experimental::AltsContext> alts_context =
  110. experimental::GetAltsContextFromAuthContext(auth_context);
  111. EXPECT_NE(alts_context, nullptr);
  112. EXPECT_EQ(expected_ap, alts_context->application_protocol());
  113. EXPECT_EQ(expected_rp, alts_context->record_protocol());
  114. EXPECT_EQ(expected_peer, alts_context->peer_service_account());
  115. EXPECT_EQ(expected_local, alts_context->local_service_account());
  116. EXPECT_EQ(expected_sl, alts_context->security_level());
  117. // all rpc versions should be 0 if not set
  118. experimental::AltsContext::RpcProtocolVersions rpc_protocol_versions =
  119. alts_context->peer_rpc_versions();
  120. EXPECT_EQ(0, rpc_protocol_versions.max_rpc_version.major_version);
  121. EXPECT_EQ(0, rpc_protocol_versions.max_rpc_version.minor_version);
  122. EXPECT_EQ(0, rpc_protocol_versions.min_rpc_version.major_version);
  123. EXPECT_EQ(0, rpc_protocol_versions.min_rpc_version.minor_version);
  124. EXPECT_EQ(expected_peer_atrributes_value,
  125. alts_context->peer_attributes().at(expected_peer_atrributes_key));
  126. }
  127. TEST(AltsUtilTest, AuthContextWithGoodAltsContext) {
  128. grpc_core::RefCountedPtr<grpc_auth_context> ctx =
  129. grpc_core::MakeRefCounted<grpc_auth_context>(nullptr);
  130. const std::shared_ptr<AuthContext> auth_context(
  131. new SecureAuthContext(ctx.get()));
  132. ctx.reset();
  133. upb::Arena context_arena;
  134. grpc_gcp_AltsContext* context = grpc_gcp_AltsContext_new(context_arena.ptr());
  135. upb::Arena versions_arena;
  136. grpc_gcp_RpcProtocolVersions* versions =
  137. grpc_gcp_RpcProtocolVersions_new(versions_arena.ptr());
  138. upb::Arena max_major_version_arena;
  139. grpc_gcp_RpcProtocolVersions_Version* version =
  140. grpc_gcp_RpcProtocolVersions_Version_new(max_major_version_arena.ptr());
  141. grpc_gcp_RpcProtocolVersions_Version_set_major(version, 10);
  142. grpc_gcp_RpcProtocolVersions_set_max_rpc_version(versions, version);
  143. grpc_gcp_AltsContext_set_peer_rpc_versions(context, versions);
  144. size_t serialized_ctx_length;
  145. char* serialized_ctx = grpc_gcp_AltsContext_serialize(
  146. context, context_arena.ptr(), &serialized_ctx_length);
  147. EXPECT_NE(serialized_ctx, nullptr);
  148. auth_context->AddProperty(TSI_ALTS_CONTEXT,
  149. string(serialized_ctx, serialized_ctx_length));
  150. std::unique_ptr<experimental::AltsContext> alts_context =
  151. experimental::GetAltsContextFromAuthContext(auth_context);
  152. EXPECT_NE(alts_context, nullptr);
  153. EXPECT_EQ("", alts_context->application_protocol());
  154. EXPECT_EQ("", alts_context->record_protocol());
  155. EXPECT_EQ("", alts_context->peer_service_account());
  156. EXPECT_EQ("", alts_context->local_service_account());
  157. EXPECT_EQ(GRPC_SECURITY_NONE, alts_context->security_level());
  158. experimental::AltsContext::RpcProtocolVersions rpc_protocol_versions =
  159. alts_context->peer_rpc_versions();
  160. EXPECT_EQ(10, rpc_protocol_versions.max_rpc_version.major_version);
  161. EXPECT_EQ(0, rpc_protocol_versions.max_rpc_version.minor_version);
  162. EXPECT_EQ(0, rpc_protocol_versions.min_rpc_version.major_version);
  163. EXPECT_EQ(0, rpc_protocol_versions.min_rpc_version.minor_version);
  164. }
  165. TEST(AltsUtilTest, AltsClientAuthzCheck) {
  166. // AltsClientAuthzCheck function should return a permission denied error on
  167. // the bad_auth_context, whose internal ALTS context does not exist
  168. const std::shared_ptr<AuthContext> bad_auth_context(
  169. new SecureAuthContext(nullptr));
  170. std::vector<std::string> service_accounts{"client"};
  171. grpc::Status status =
  172. experimental::AltsClientAuthzCheck(bad_auth_context, service_accounts);
  173. EXPECT_EQ(grpc::StatusCode::PERMISSION_DENIED, status.error_code());
  174. // AltsClientAuthzCheck function should function normally when the peer name
  175. // in ALTS context is listed in service_accounts
  176. grpc_core::RefCountedPtr<grpc_auth_context> ctx =
  177. grpc_core::MakeRefCounted<grpc_auth_context>(nullptr);
  178. const std::shared_ptr<AuthContext> auth_context(
  179. new SecureAuthContext(ctx.get()));
  180. ctx.reset();
  181. std::string peer("good_client");
  182. std::vector<std::string> good_service_accounts{"good_client",
  183. "good_client_1"};
  184. std::vector<std::string> bad_service_accounts{"bad_client", "bad_client_1"};
  185. upb::Arena context_arena;
  186. grpc_gcp_AltsContext* context = grpc_gcp_AltsContext_new(context_arena.ptr());
  187. grpc_gcp_AltsContext_set_peer_service_account(
  188. context, upb_StringView_FromDataAndSize(peer.data(), peer.length()));
  189. size_t serialized_ctx_length;
  190. char* serialized_ctx = grpc_gcp_AltsContext_serialize(
  191. context, context_arena.ptr(), &serialized_ctx_length);
  192. EXPECT_NE(serialized_ctx, nullptr);
  193. auth_context->AddProperty(TSI_ALTS_CONTEXT,
  194. string(serialized_ctx, serialized_ctx_length));
  195. grpc::Status good_status =
  196. experimental::AltsClientAuthzCheck(auth_context, good_service_accounts);
  197. EXPECT_TRUE(good_status.ok());
  198. grpc::Status bad_status =
  199. experimental::AltsClientAuthzCheck(auth_context, bad_service_accounts);
  200. EXPECT_EQ(grpc::StatusCode::PERMISSION_DENIED, bad_status.error_code());
  201. }
  202. } // namespace
  203. } // namespace grpc
  204. int main(int argc, char** argv) {
  205. grpc::testing::TestEnvironment env(argc, argv);
  206. ::testing::InitGoogleTest(&argc, argv);
  207. return RUN_ALL_TESTS();
  208. }