client_helper.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. *
  3. * Copyright 2015 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 "test/cpp/interop/client_helper.h"
  19. #include <fstream>
  20. #include <memory>
  21. #include <sstream>
  22. #include "absl/flags/declare.h"
  23. #include "absl/flags/flag.h"
  24. #include "absl/strings/match.h"
  25. #include <grpc/grpc.h>
  26. #include <grpc/support/alloc.h>
  27. #include <grpc/support/log.h>
  28. #include <grpcpp/channel.h>
  29. #include <grpcpp/create_channel.h>
  30. #include <grpcpp/security/credentials.h>
  31. #include "src/core/lib/slice/b64.h"
  32. #include "src/cpp/client/secure_credentials.h"
  33. #include "test/core/security/oauth2_utils.h"
  34. #include "test/cpp/util/create_test_channel.h"
  35. #include "test/cpp/util/test_credentials_provider.h"
  36. ABSL_DECLARE_FLAG(bool, use_alts);
  37. ABSL_DECLARE_FLAG(bool, use_tls);
  38. ABSL_DECLARE_FLAG(std::string, custom_credentials_type);
  39. ABSL_DECLARE_FLAG(bool, use_test_ca);
  40. ABSL_DECLARE_FLAG(int32_t, server_port);
  41. ABSL_DECLARE_FLAG(std::string, server_host);
  42. ABSL_DECLARE_FLAG(std::string, server_host_override);
  43. ABSL_DECLARE_FLAG(std::string, test_case);
  44. ABSL_DECLARE_FLAG(std::string, default_service_account);
  45. ABSL_DECLARE_FLAG(std::string, service_account_key_file);
  46. ABSL_DECLARE_FLAG(std::string, oauth_scope);
  47. namespace grpc {
  48. namespace testing {
  49. std::string GetServiceAccountJsonKey() {
  50. static std::string json_key;
  51. if (json_key.empty()) {
  52. std::ifstream json_key_file(absl::GetFlag(FLAGS_service_account_key_file));
  53. std::stringstream key_stream;
  54. key_stream << json_key_file.rdbuf();
  55. json_key = key_stream.str();
  56. }
  57. return json_key;
  58. }
  59. std::string GetOauth2AccessToken() {
  60. std::shared_ptr<CallCredentials> creds = GoogleComputeEngineCredentials();
  61. SecureCallCredentials* secure_creds =
  62. dynamic_cast<SecureCallCredentials*>(creds.get());
  63. GPR_ASSERT(secure_creds != nullptr);
  64. grpc_call_credentials* c_creds = secure_creds->GetRawCreds();
  65. char* token = grpc_test_fetch_oauth2_token_with_credentials(c_creds);
  66. GPR_ASSERT(token != nullptr);
  67. gpr_log(GPR_INFO, "Get raw oauth2 access token: %s", token);
  68. std::string access_token(token + sizeof("Bearer ") - 1);
  69. gpr_free(token);
  70. return access_token;
  71. }
  72. void UpdateActions(
  73. std::unordered_map<std::string, std::function<bool()>>* /*actions*/) {}
  74. std::shared_ptr<Channel> CreateChannelForTestCase(
  75. const std::string& test_case,
  76. std::vector<
  77. std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
  78. interceptor_creators) {
  79. std::string server_uri = absl::GetFlag(FLAGS_server_host);
  80. int32_t port = absl::GetFlag(FLAGS_server_port);
  81. if (port != 0) {
  82. absl::StrAppend(&server_uri, ":", std::to_string(port));
  83. }
  84. std::shared_ptr<CallCredentials> creds;
  85. if (test_case == "compute_engine_creds") {
  86. creds = absl::GetFlag(FLAGS_custom_credentials_type) ==
  87. "google_default_credentials"
  88. ? nullptr
  89. : GoogleComputeEngineCredentials();
  90. } else if (test_case == "jwt_token_creds") {
  91. std::string json_key = GetServiceAccountJsonKey();
  92. std::chrono::seconds token_lifetime = std::chrono::hours(1);
  93. creds = absl::GetFlag(FLAGS_custom_credentials_type) ==
  94. "google_default_credentials"
  95. ? nullptr
  96. : ServiceAccountJWTAccessCredentials(json_key,
  97. token_lifetime.count());
  98. } else if (test_case == "oauth2_auth_token") {
  99. creds = absl::GetFlag(FLAGS_custom_credentials_type) ==
  100. "google_default_credentials"
  101. ? nullptr
  102. : AccessTokenCredentials(GetOauth2AccessToken());
  103. } else if (test_case == "pick_first_unary") {
  104. ChannelArguments channel_args;
  105. // allow the LB policy to be configured with service config
  106. channel_args.SetInt(GRPC_ARG_SERVICE_CONFIG_DISABLE_RESOLUTION, 0);
  107. return CreateTestChannel(
  108. server_uri, absl::GetFlag(FLAGS_custom_credentials_type),
  109. absl::GetFlag(FLAGS_server_host_override),
  110. !absl::GetFlag(FLAGS_use_test_ca), creds, channel_args);
  111. }
  112. if (absl::GetFlag(FLAGS_custom_credentials_type).empty()) {
  113. transport_security security_type =
  114. absl::GetFlag(FLAGS_use_alts)
  115. ? ALTS
  116. : (absl::GetFlag(FLAGS_use_tls) ? TLS : INSECURE);
  117. return CreateTestChannel(server_uri,
  118. absl::GetFlag(FLAGS_server_host_override),
  119. security_type, !absl::GetFlag(FLAGS_use_test_ca),
  120. creds, std::move(interceptor_creators));
  121. } else {
  122. if (interceptor_creators.empty()) {
  123. return CreateTestChannel(
  124. server_uri, absl::GetFlag(FLAGS_custom_credentials_type), creds);
  125. } else {
  126. return CreateTestChannel(server_uri,
  127. absl::GetFlag(FLAGS_custom_credentials_type),
  128. creds, std::move(interceptor_creators));
  129. }
  130. }
  131. }
  132. static void log_metadata_entry(const std::string& prefix,
  133. const grpc::string_ref& key,
  134. const grpc::string_ref& value) {
  135. auto key_str = std::string(key.begin(), key.end());
  136. auto value_str = std::string(value.begin(), value.end());
  137. if (absl::EndsWith(key_str, "-bin")) {
  138. auto converted =
  139. grpc_base64_encode(value_str.c_str(), value_str.length(), 0, 0);
  140. value_str = std::string(converted);
  141. gpr_free(converted);
  142. }
  143. gpr_log(GPR_ERROR, "%s %s: %s", prefix.c_str(), key_str.c_str(),
  144. value_str.c_str());
  145. }
  146. void MetadataAndStatusLoggerInterceptor::Intercept(
  147. experimental::InterceptorBatchMethods* methods) {
  148. if (methods->QueryInterceptionHookPoint(
  149. experimental::InterceptionHookPoints::POST_RECV_INITIAL_METADATA)) {
  150. auto initial_metadata = methods->GetRecvInitialMetadata();
  151. for (const auto& entry : *initial_metadata) {
  152. log_metadata_entry("GRPC_INITIAL_METADATA", entry.first, entry.second);
  153. }
  154. }
  155. if (methods->QueryInterceptionHookPoint(
  156. experimental::InterceptionHookPoints::POST_RECV_STATUS)) {
  157. auto trailing_metadata = methods->GetRecvTrailingMetadata();
  158. for (const auto& entry : *trailing_metadata) {
  159. log_metadata_entry("GRPC_TRAILING_METADATA", entry.first, entry.second);
  160. }
  161. auto status = methods->GetRecvStatus();
  162. gpr_log(GPR_ERROR, "GRPC_STATUS %d", status->error_code());
  163. gpr_log(GPR_ERROR, "GRPC_ERROR_MESSAGE %s",
  164. status->error_message().c_str());
  165. }
  166. methods->Proceed();
  167. }
  168. } // namespace testing
  169. } // namespace grpc