alts_util_test.cc 10 KB

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