client_helper.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <regex>
  22. #include <sstream>
  23. #include <gflags/gflags.h>
  24. #include <grpc/grpc.h>
  25. #include <grpc/support/alloc.h>
  26. #include <grpc/support/log.h>
  27. #include <grpcpp/channel.h>
  28. #include <grpcpp/create_channel.h>
  29. #include <grpcpp/security/credentials.h>
  30. #include "src/cpp/client/secure_credentials.h"
  31. #include "test/core/security/oauth2_utils.h"
  32. #include "test/cpp/util/create_test_channel.h"
  33. #include "test/cpp/util/test_credentials_provider.h"
  34. DECLARE_bool(use_alts);
  35. DECLARE_bool(use_tls);
  36. DECLARE_string(custom_credentials_type);
  37. DECLARE_bool(use_test_ca);
  38. DECLARE_int32(server_port);
  39. DECLARE_string(server_host);
  40. DECLARE_string(server_host_override);
  41. DECLARE_string(test_case);
  42. DECLARE_string(default_service_account);
  43. DECLARE_string(service_account_key_file);
  44. DECLARE_string(oauth_scope);
  45. namespace grpc {
  46. namespace testing {
  47. grpc::string GetServiceAccountJsonKey() {
  48. static grpc::string json_key;
  49. if (json_key.empty()) {
  50. std::ifstream json_key_file(FLAGS_service_account_key_file);
  51. std::stringstream key_stream;
  52. key_stream << json_key_file.rdbuf();
  53. json_key = key_stream.str();
  54. }
  55. return json_key;
  56. }
  57. grpc::string GetOauth2AccessToken() {
  58. std::shared_ptr<CallCredentials> creds = GoogleComputeEngineCredentials();
  59. SecureCallCredentials* secure_creds =
  60. dynamic_cast<SecureCallCredentials*>(creds.get());
  61. GPR_ASSERT(secure_creds != nullptr);
  62. grpc_call_credentials* c_creds = secure_creds->GetRawCreds();
  63. char* token = grpc_test_fetch_oauth2_token_with_credentials(c_creds);
  64. GPR_ASSERT(token != nullptr);
  65. gpr_log(GPR_INFO, "Get raw oauth2 access token: %s", token);
  66. grpc::string access_token(token + sizeof("Bearer ") - 1);
  67. gpr_free(token);
  68. return access_token;
  69. }
  70. void UpdateActions(
  71. std::unordered_map<grpc::string, std::function<bool()>>* actions) {}
  72. std::shared_ptr<Channel> CreateChannelForTestCase(
  73. const grpc::string& test_case,
  74. std::vector<
  75. std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
  76. interceptor_creators) {
  77. GPR_ASSERT(FLAGS_server_port);
  78. const int host_port_buf_size = 1024;
  79. char host_port[host_port_buf_size];
  80. snprintf(host_port, host_port_buf_size, "%s:%d", FLAGS_server_host.c_str(),
  81. FLAGS_server_port);
  82. std::shared_ptr<CallCredentials> creds;
  83. if (test_case == "compute_engine_creds") {
  84. creds = FLAGS_custom_credentials_type == "google_default_credentials"
  85. ? nullptr
  86. : GoogleComputeEngineCredentials();
  87. } else if (test_case == "jwt_token_creds") {
  88. grpc::string json_key = GetServiceAccountJsonKey();
  89. std::chrono::seconds token_lifetime = std::chrono::hours(1);
  90. creds = FLAGS_custom_credentials_type == "google_default_credentials"
  91. ? nullptr
  92. : ServiceAccountJWTAccessCredentials(json_key,
  93. token_lifetime.count());
  94. } else if (test_case == "oauth2_auth_token") {
  95. creds = FLAGS_custom_credentials_type == "google_default_credentials"
  96. ? nullptr
  97. : AccessTokenCredentials(GetOauth2AccessToken());
  98. }
  99. if (FLAGS_custom_credentials_type.empty()) {
  100. transport_security security_type =
  101. FLAGS_use_alts ? ALTS : (FLAGS_use_tls ? TLS : INSECURE);
  102. return CreateTestChannel(host_port, FLAGS_server_host_override,
  103. security_type, !FLAGS_use_test_ca, creds,
  104. std::move(interceptor_creators));
  105. } else {
  106. if (interceptor_creators.empty()) {
  107. return CreateTestChannel(host_port, FLAGS_custom_credentials_type, creds);
  108. } else {
  109. return CreateTestChannel(host_port, FLAGS_custom_credentials_type, creds,
  110. std::move(interceptor_creators));
  111. }
  112. }
  113. }
  114. std::multimap<grpc::string, grpc::string> ParseAdditionalMetadataFlag(
  115. const grpc::string& flag) {
  116. std::multimap<grpc::string, grpc::string> additional_metadata;
  117. // Key in group 1; value in group 2.
  118. std::regex re("([-a-zA-Z0-9]+):([^;]*);?");
  119. auto metadata_entries_begin = std::sregex_iterator(
  120. flag.begin(), flag.end(), re, std::regex_constants::match_continuous);
  121. auto metadata_entries_end = std::sregex_iterator();
  122. for (std::sregex_iterator i = metadata_entries_begin;
  123. i != metadata_entries_end; ++i) {
  124. std::smatch match = *i;
  125. gpr_log(GPR_INFO, "Adding additional metadata with key %s and value %s",
  126. match[1].str().c_str(), match[2].str().c_str());
  127. additional_metadata.insert({match[1].str(), match[2].str()});
  128. }
  129. return additional_metadata;
  130. }
  131. void AdditionalMetadataInterceptor::Intercept(
  132. experimental::InterceptorBatchMethods* methods) {
  133. if (methods->QueryInterceptionHookPoint(
  134. experimental::InterceptionHookPoints::PRE_SEND_INITIAL_METADATA)) {
  135. std::multimap<grpc::string, grpc::string>* metadata =
  136. methods->GetSendInitialMetadata();
  137. for (const auto& entry : additional_metadata_) {
  138. metadata->insert(entry);
  139. }
  140. }
  141. methods->Proceed();
  142. }
  143. } // namespace testing
  144. } // namespace grpc