create_test_channel.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. *
  3. * Copyright 2015-2016 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/util/create_test_channel.h"
  19. #include "absl/flags/flag.h"
  20. #include <grpc/support/log.h>
  21. #include <grpcpp/create_channel.h>
  22. #include <grpcpp/security/credentials.h>
  23. #include "test/cpp/util/test_credentials_provider.h"
  24. ABSL_FLAG(std::string, grpc_test_use_grpclb_with_child_policy, "",
  25. "If non-empty, set a static service config on channels created by "
  26. "grpc::CreateTestChannel, that configures the grpclb LB policy "
  27. "with a child policy being the value of this flag (e.g. round_robin "
  28. "or pick_first).");
  29. namespace grpc {
  30. namespace {
  31. const char kProdTlsCredentialsType[] = "prod_ssl";
  32. class SslCredentialProvider : public testing::CredentialTypeProvider {
  33. public:
  34. std::shared_ptr<ChannelCredentials> GetChannelCredentials(
  35. grpc::ChannelArguments* /*args*/) override {
  36. return grpc::SslCredentials(SslCredentialsOptions());
  37. }
  38. std::shared_ptr<ServerCredentials> GetServerCredentials() override {
  39. return nullptr;
  40. }
  41. };
  42. gpr_once g_once_init_add_prod_ssl_provider = GPR_ONCE_INIT;
  43. // Register ssl with non-test roots type to the credentials provider.
  44. void AddProdSslType() {
  45. testing::GetCredentialsProvider()->AddSecureType(
  46. kProdTlsCredentialsType, std::unique_ptr<testing::CredentialTypeProvider>(
  47. new SslCredentialProvider));
  48. }
  49. void MaybeSetCustomChannelArgs(grpc::ChannelArguments* args) {
  50. if (!absl::GetFlag(FLAGS_grpc_test_use_grpclb_with_child_policy).empty()) {
  51. args->SetString(
  52. "grpc.service_config",
  53. "{\"loadBalancingConfig\":[{\"grpclb\":{\"childPolicy\":[{"
  54. "\"" +
  55. absl::GetFlag(FLAGS_grpc_test_use_grpclb_with_child_policy) +
  56. "\":{}}]}}]}");
  57. }
  58. }
  59. } // namespace
  60. // When cred_type is 'ssl', if server is empty, override_hostname is used to
  61. // create channel. Otherwise, connect to server and override hostname if
  62. // override_hostname is provided.
  63. // When cred_type is not 'ssl', override_hostname is ignored.
  64. // Set use_prod_root to true to use the SSL root for connecting to google.
  65. // In this case, path to the roots pem file must be set via environment variable
  66. // GRPC_DEFAULT_SSL_ROOTS_FILE_PATH.
  67. // Otherwise, root for test SSL cert will be used.
  68. // creds will be used to create a channel when cred_type is 'ssl'.
  69. // Use examples:
  70. // CreateTestChannel(
  71. // "1.1.1.1:12345", "ssl", "override.hostname.com", false, creds);
  72. // CreateTestChannel("test.google.com:443", "ssl", "", true, creds);
  73. // same as above
  74. // CreateTestChannel("", "ssl", "test.google.com:443", true, creds);
  75. std::shared_ptr<Channel> CreateTestChannel(
  76. const std::string& server, const std::string& cred_type,
  77. const std::string& override_hostname, bool use_prod_roots,
  78. const std::shared_ptr<CallCredentials>& creds,
  79. const ChannelArguments& args) {
  80. return CreateTestChannel(server, cred_type, override_hostname, use_prod_roots,
  81. creds, args,
  82. /*interceptor_creators=*/{});
  83. }
  84. std::shared_ptr<Channel> CreateTestChannel(
  85. const std::string& server, const std::string& override_hostname,
  86. testing::transport_security security_type, bool use_prod_roots,
  87. const std::shared_ptr<CallCredentials>& creds,
  88. const ChannelArguments& args) {
  89. return CreateTestChannel(server, override_hostname, security_type,
  90. use_prod_roots, creds, args,
  91. /*interceptor_creators=*/{});
  92. }
  93. std::shared_ptr<Channel> CreateTestChannel(
  94. const std::string& server, const std::string& override_hostname,
  95. testing::transport_security security_type, bool use_prod_roots,
  96. const std::shared_ptr<CallCredentials>& creds) {
  97. return CreateTestChannel(server, override_hostname, security_type,
  98. use_prod_roots, creds, ChannelArguments());
  99. }
  100. std::shared_ptr<Channel> CreateTestChannel(
  101. const std::string& server, const std::string& override_hostname,
  102. testing::transport_security security_type, bool use_prod_roots) {
  103. return CreateTestChannel(server, override_hostname, security_type,
  104. use_prod_roots, std::shared_ptr<CallCredentials>());
  105. }
  106. // Shortcut for end2end and interop tests.
  107. std::shared_ptr<Channel> CreateTestChannel(
  108. const std::string& server, testing::transport_security security_type) {
  109. return CreateTestChannel(server, "foo.test.google.fr", security_type, false);
  110. }
  111. std::shared_ptr<Channel> CreateTestChannel(
  112. const std::string& server, const std::string& credential_type,
  113. const std::shared_ptr<CallCredentials>& creds) {
  114. ChannelArguments channel_args;
  115. MaybeSetCustomChannelArgs(&channel_args);
  116. std::shared_ptr<ChannelCredentials> channel_creds =
  117. testing::GetCredentialsProvider()->GetChannelCredentials(credential_type,
  118. &channel_args);
  119. GPR_ASSERT(channel_creds != nullptr);
  120. if (creds.get()) {
  121. channel_creds = grpc::CompositeChannelCredentials(channel_creds, creds);
  122. }
  123. return grpc::CreateCustomChannel(server, channel_creds, channel_args);
  124. }
  125. std::shared_ptr<Channel> CreateTestChannel(
  126. const std::string& server, const std::string& cred_type,
  127. const std::string& override_hostname, bool use_prod_roots,
  128. const std::shared_ptr<CallCredentials>& creds, const ChannelArguments& args,
  129. std::vector<
  130. std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
  131. interceptor_creators) {
  132. ChannelArguments channel_args(args);
  133. MaybeSetCustomChannelArgs(&channel_args);
  134. std::shared_ptr<ChannelCredentials> channel_creds;
  135. if (cred_type.empty()) {
  136. if (interceptor_creators.empty()) {
  137. return grpc::CreateCustomChannel(server, InsecureChannelCredentials(),
  138. channel_args);
  139. } else {
  140. return experimental::CreateCustomChannelWithInterceptors(
  141. server, InsecureChannelCredentials(), channel_args,
  142. std::move(interceptor_creators));
  143. }
  144. } else if (cred_type == testing::kTlsCredentialsType) { // cred_type == "ssl"
  145. if (use_prod_roots) {
  146. gpr_once_init(&g_once_init_add_prod_ssl_provider, &AddProdSslType);
  147. channel_creds = testing::GetCredentialsProvider()->GetChannelCredentials(
  148. kProdTlsCredentialsType, &channel_args);
  149. if (!server.empty() && !override_hostname.empty()) {
  150. channel_args.SetSslTargetNameOverride(override_hostname);
  151. }
  152. } else {
  153. // override_hostname is discarded as the provider handles it.
  154. channel_creds = testing::GetCredentialsProvider()->GetChannelCredentials(
  155. testing::kTlsCredentialsType, &channel_args);
  156. }
  157. GPR_ASSERT(channel_creds != nullptr);
  158. const std::string& connect_to = server.empty() ? override_hostname : server;
  159. if (creds.get()) {
  160. channel_creds = grpc::CompositeChannelCredentials(channel_creds, creds);
  161. }
  162. if (interceptor_creators.empty()) {
  163. return grpc::CreateCustomChannel(connect_to, channel_creds, channel_args);
  164. } else {
  165. return experimental::CreateCustomChannelWithInterceptors(
  166. connect_to, channel_creds, channel_args,
  167. std::move(interceptor_creators));
  168. }
  169. } else {
  170. channel_creds = testing::GetCredentialsProvider()->GetChannelCredentials(
  171. cred_type, &channel_args);
  172. GPR_ASSERT(channel_creds != nullptr);
  173. if (interceptor_creators.empty()) {
  174. return grpc::CreateCustomChannel(server, channel_creds, channel_args);
  175. } else {
  176. return experimental::CreateCustomChannelWithInterceptors(
  177. server, channel_creds, channel_args, std::move(interceptor_creators));
  178. }
  179. }
  180. }
  181. std::shared_ptr<Channel> CreateTestChannel(
  182. const std::string& server, const std::string& override_hostname,
  183. testing::transport_security security_type, bool use_prod_roots,
  184. const std::shared_ptr<CallCredentials>& creds, const ChannelArguments& args,
  185. std::vector<
  186. std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
  187. interceptor_creators) {
  188. std::string credential_type =
  189. security_type == testing::ALTS
  190. ? testing::kAltsCredentialsType
  191. : (security_type == testing::TLS ? testing::kTlsCredentialsType
  192. : testing::kInsecureCredentialsType);
  193. return CreateTestChannel(server, credential_type, override_hostname,
  194. use_prod_roots, creds, args,
  195. std::move(interceptor_creators));
  196. }
  197. std::shared_ptr<Channel> CreateTestChannel(
  198. const std::string& server, const std::string& override_hostname,
  199. testing::transport_security security_type, bool use_prod_roots,
  200. const std::shared_ptr<CallCredentials>& creds,
  201. std::vector<
  202. std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
  203. interceptor_creators) {
  204. return CreateTestChannel(server, override_hostname, security_type,
  205. use_prod_roots, creds, ChannelArguments(),
  206. std::move(interceptor_creators));
  207. }
  208. std::shared_ptr<Channel> CreateTestChannel(
  209. const std::string& server, const std::string& credential_type,
  210. const std::shared_ptr<CallCredentials>& creds,
  211. std::vector<
  212. std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
  213. interceptor_creators) {
  214. ChannelArguments channel_args;
  215. MaybeSetCustomChannelArgs(&channel_args);
  216. std::shared_ptr<ChannelCredentials> channel_creds =
  217. testing::GetCredentialsProvider()->GetChannelCredentials(credential_type,
  218. &channel_args);
  219. GPR_ASSERT(channel_creds != nullptr);
  220. if (creds.get()) {
  221. channel_creds = grpc::CompositeChannelCredentials(channel_creds, creds);
  222. }
  223. return experimental::CreateCustomChannelWithInterceptors(
  224. server, channel_creds, channel_args, std::move(interceptor_creators));
  225. }
  226. } // namespace grpc