create_test_channel.cc
Go to the documentation of this file.
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 
20 
21 #include "absl/flags/flag.h"
22 
23 #include <grpc/support/log.h>
24 #include <grpcpp/create_channel.h>
26 
28 
29 ABSL_FLAG(std::string, grpc_test_use_grpclb_with_child_policy, "",
30  "If non-empty, set a static service config on channels created by "
31  "grpc::CreateTestChannel, that configures the grpclb LB policy "
32  "with a child policy being the value of this flag (e.g. round_robin "
33  "or pick_first).");
34 
35 namespace grpc {
36 
37 namespace {
38 
39 const char kProdTlsCredentialsType[] = "prod_ssl";
40 
41 class SslCredentialProvider : public testing::CredentialTypeProvider {
42  public:
43  std::shared_ptr<ChannelCredentials> GetChannelCredentials(
44  grpc::ChannelArguments* /*args*/) override {
45  return grpc::SslCredentials(SslCredentialsOptions());
46  }
47  std::shared_ptr<ServerCredentials> GetServerCredentials() override {
48  return nullptr;
49  }
50 };
51 
52 gpr_once g_once_init_add_prod_ssl_provider = GPR_ONCE_INIT;
53 // Register ssl with non-test roots type to the credentials provider.
54 void AddProdSslType() {
56  kProdTlsCredentialsType, std::unique_ptr<testing::CredentialTypeProvider>(
57  new SslCredentialProvider));
58 }
59 
60 void MaybeSetCustomChannelArgs(grpc::ChannelArguments* args) {
61  if (!absl::GetFlag(FLAGS_grpc_test_use_grpclb_with_child_policy).empty()) {
62  args->SetString(
63  "grpc.service_config",
64  "{\"loadBalancingConfig\":[{\"grpclb\":{\"childPolicy\":[{"
65  "\"" +
66  absl::GetFlag(FLAGS_grpc_test_use_grpclb_with_child_policy) +
67  "\":{}}]}}]}");
68  }
69 }
70 
71 } // namespace
72 
73 // When cred_type is 'ssl', if server is empty, override_hostname is used to
74 // create channel. Otherwise, connect to server and override hostname if
75 // override_hostname is provided.
76 // When cred_type is not 'ssl', override_hostname is ignored.
77 // Set use_prod_root to true to use the SSL root for connecting to google.
78 // In this case, path to the roots pem file must be set via environment variable
79 // GRPC_DEFAULT_SSL_ROOTS_FILE_PATH.
80 // Otherwise, root for test SSL cert will be used.
81 // creds will be used to create a channel when cred_type is 'ssl'.
82 // Use examples:
83 // CreateTestChannel(
84 // "1.1.1.1:12345", "ssl", "override.hostname.com", false, creds);
85 // CreateTestChannel("test.google.com:443", "ssl", "", true, creds);
86 // same as above
87 // CreateTestChannel("", "ssl", "test.google.com:443", true, creds);
88 std::shared_ptr<Channel> CreateTestChannel(
89  const std::string& server, const std::string& cred_type,
90  const std::string& override_hostname, bool use_prod_roots,
91  const std::shared_ptr<CallCredentials>& creds,
92  const ChannelArguments& args) {
93  return CreateTestChannel(server, cred_type, override_hostname, use_prod_roots,
94  creds, args,
95  /*interceptor_creators=*/{});
96 }
97 
98 std::shared_ptr<Channel> CreateTestChannel(
99  const std::string& server, const std::string& override_hostname,
100  testing::transport_security security_type, bool use_prod_roots,
101  const std::shared_ptr<CallCredentials>& creds,
102  const ChannelArguments& args) {
103  return CreateTestChannel(server, override_hostname, security_type,
104  use_prod_roots, creds, args,
105  /*interceptor_creators=*/{});
106 }
107 
108 std::shared_ptr<Channel> CreateTestChannel(
109  const std::string& server, const std::string& override_hostname,
110  testing::transport_security security_type, bool use_prod_roots,
111  const std::shared_ptr<CallCredentials>& creds) {
112  return CreateTestChannel(server, override_hostname, security_type,
113  use_prod_roots, creds, ChannelArguments());
114 }
115 
116 std::shared_ptr<Channel> CreateTestChannel(
117  const std::string& server, const std::string& override_hostname,
118  testing::transport_security security_type, bool use_prod_roots) {
119  return CreateTestChannel(server, override_hostname, security_type,
120  use_prod_roots, std::shared_ptr<CallCredentials>());
121 }
122 
123 // Shortcut for end2end and interop tests.
124 std::shared_ptr<Channel> CreateTestChannel(
125  const std::string& server, testing::transport_security security_type) {
126  return CreateTestChannel(server, "foo.test.google.fr", security_type, false);
127 }
128 
129 std::shared_ptr<Channel> CreateTestChannel(
130  const std::string& server, const std::string& credential_type,
131  const std::shared_ptr<CallCredentials>& creds) {
132  ChannelArguments channel_args;
133  MaybeSetCustomChannelArgs(&channel_args);
134  std::shared_ptr<ChannelCredentials> channel_creds =
136  &channel_args);
137  GPR_ASSERT(channel_creds != nullptr);
138  if (creds.get()) {
139  channel_creds = grpc::CompositeChannelCredentials(channel_creds, creds);
140  }
141  return grpc::CreateCustomChannel(server, channel_creds, channel_args);
142 }
143 
144 std::shared_ptr<Channel> CreateTestChannel(
145  const std::string& server, const std::string& cred_type,
146  const std::string& override_hostname, bool use_prod_roots,
147  const std::shared_ptr<CallCredentials>& creds, const ChannelArguments& args,
148  std::vector<
149  std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
150  interceptor_creators) {
151  ChannelArguments channel_args(args);
152  MaybeSetCustomChannelArgs(&channel_args);
153  std::shared_ptr<ChannelCredentials> channel_creds;
154  if (cred_type.empty()) {
155  if (interceptor_creators.empty()) {
157  channel_args);
158  } else {
160  server, InsecureChannelCredentials(), channel_args,
161  std::move(interceptor_creators));
162  }
163  } else if (cred_type == testing::kTlsCredentialsType) { // cred_type == "ssl"
164  if (use_prod_roots) {
165  gpr_once_init(&g_once_init_add_prod_ssl_provider, &AddProdSslType);
167  kProdTlsCredentialsType, &channel_args);
168  if (!server.empty() && !override_hostname.empty()) {
169  channel_args.SetSslTargetNameOverride(override_hostname);
170  }
171  } else {
172  // override_hostname is discarded as the provider handles it.
174  testing::kTlsCredentialsType, &channel_args);
175  }
176  GPR_ASSERT(channel_creds != nullptr);
177 
178  const std::string& connect_to = server.empty() ? override_hostname : server;
179  if (creds.get()) {
180  channel_creds = grpc::CompositeChannelCredentials(channel_creds, creds);
181  }
182  if (interceptor_creators.empty()) {
183  return grpc::CreateCustomChannel(connect_to, channel_creds, channel_args);
184  } else {
186  connect_to, channel_creds, channel_args,
187  std::move(interceptor_creators));
188  }
189  } else {
191  cred_type, &channel_args);
192  GPR_ASSERT(channel_creds != nullptr);
193 
194  if (interceptor_creators.empty()) {
195  return grpc::CreateCustomChannel(server, channel_creds, channel_args);
196  } else {
198  server, channel_creds, channel_args, std::move(interceptor_creators));
199  }
200  }
201 }
202 
203 std::shared_ptr<Channel> CreateTestChannel(
204  const std::string& server, const std::string& override_hostname,
205  testing::transport_security security_type, bool use_prod_roots,
206  const std::shared_ptr<CallCredentials>& creds, const ChannelArguments& args,
207  std::vector<
208  std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
209  interceptor_creators) {
210  std::string credential_type =
211  security_type == testing::ALTS
213  : (security_type == testing::TLS ? testing::kTlsCredentialsType
215  return CreateTestChannel(server, credential_type, override_hostname,
216  use_prod_roots, creds, args,
217  std::move(interceptor_creators));
218 }
219 
220 std::shared_ptr<Channel> CreateTestChannel(
221  const std::string& server, const std::string& override_hostname,
222  testing::transport_security security_type, bool use_prod_roots,
223  const std::shared_ptr<CallCredentials>& creds,
224  std::vector<
225  std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
226  interceptor_creators) {
227  return CreateTestChannel(server, override_hostname, security_type,
228  use_prod_roots, creds, ChannelArguments(),
229  std::move(interceptor_creators));
230 }
231 
232 std::shared_ptr<Channel> CreateTestChannel(
233  const std::string& server, const std::string& credential_type,
234  const std::shared_ptr<CallCredentials>& creds,
235  std::vector<
236  std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
237  interceptor_creators) {
238  ChannelArguments channel_args;
239  MaybeSetCustomChannelArgs(&channel_args);
240  std::shared_ptr<ChannelCredentials> channel_creds =
242  &channel_args);
243  GPR_ASSERT(channel_creds != nullptr);
244  if (creds.get()) {
245  channel_creds = grpc::CompositeChannelCredentials(channel_creds, creds);
246  }
248  server, channel_creds, channel_args, std::move(interceptor_creators));
249 }
250 
251 } // namespace grpc
test_credentials_provider.h
grpc::testing::kTlsCredentialsType
const char kTlsCredentialsType[]
Definition: test_credentials_provider.h:34
log.h
grpc::testing::CredentialsProvider::GetChannelCredentials
virtual std::shared_ptr< ChannelCredentials > GetChannelCredentials(const std::string &type, ChannelArguments *args)=0
grpc
Definition: grpcpp/alarm.h:33
gpr_once
pthread_once_t gpr_once
Definition: impl/codegen/sync_posix.h:50
grpc::testing::CredentialsProvider::AddSecureType
virtual void AddSecureType(const std::string &type, std::unique_ptr< CredentialTypeProvider > type_provider)=0
grpc::SslCredentials
std::shared_ptr< ChannelCredentials > SslCredentials(const SslCredentialsOptions &options)
Builds SSL Credentials given SSL specific options.
Definition: secure_credentials.cc:129
grpc::testing::transport_security
transport_security
Definition: create_test_channel.h:34
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc::CompositeChannelCredentials
std::shared_ptr< ChannelCredentials > CompositeChannelCredentials(const std::shared_ptr< ChannelCredentials > &channel_creds, const std::shared_ptr< CallCredentials > &call_creds)
Definition: secure_credentials.cc:373
GPR_ONCE_INIT
#define GPR_ONCE_INIT
Definition: impl/codegen/sync_posix.h:52
create_test_channel.h
gpr_once_init
GPRAPI void gpr_once_init(gpr_once *once, void(*init_function)(void))
grpc::testing::kInsecureCredentialsType
const char kInsecureCredentialsType[]
Definition: test_credentials_provider.h:31
ABSL_FLAG
ABSL_FLAG(std::string, grpc_test_use_grpclb_with_child_policy, "", "If non-empty, set a static service config on channels created by " "grpc::CreateTestChannel, that configures the grpclb LB policy " "with a child policy being the value of this flag (e.g. round_robin " "or pick_first).")
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc.server
def server(thread_pool, handlers=None, interceptors=None, options=None, maximum_concurrent_rpcs=None, compression=None, xds=False)
Definition: src/python/grpcio/grpc/__init__.py:2034
absl::GetFlag
ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag< T > &flag)
Definition: abseil-cpp/absl/flags/flag.h:98
grpc::CreateTestChannel
std::shared_ptr< Channel > CreateTestChannel(const std::string &server, const std::string &cred_type, const std::string &override_hostname, bool use_prod_roots, const std::shared_ptr< CallCredentials > &creds, const ChannelArguments &args)
Definition: create_test_channel.cc:88
grpc::testing::kAltsCredentialsType
const char kAltsCredentialsType[]
Definition: test_credentials_provider.h:35
google_benchmark.example.empty
def empty(state)
Definition: example.py:31
grpc::testing::ALTS
@ ALTS
Definition: create_test_channel.h:34
grpc::ChannelArguments
Definition: grpcpp/support/channel_arguments.h:39
credentials.h
grpc::ChannelArguments::SetSslTargetNameOverride
void SetSslTargetNameOverride(const std::string &name)
Definition: secure_channel_arguments.cc:29
server
Definition: examples/python/async_streaming/server.py:1
grpc::testing::GetCredentialsProvider
CredentialsProvider * GetCredentialsProvider()
Definition: test_credentials_provider.cc:169
grpc::experimental::CreateCustomChannelWithInterceptors
std::shared_ptr< Channel > CreateCustomChannelWithInterceptors(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds, const ChannelArguments &args, std::vector< std::unique_ptr< experimental::ClientInterceptorFactoryInterface >> interceptor_creators)
grpc::CreateCustomChannel
std::shared_ptr< Channel > CreateCustomChannel(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds, const ChannelArguments &args)
grpc::InsecureChannelCredentials
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
Definition: cpp/client/insecure_credentials.cc:69
create_channel.h
grpc::testing::TLS
@ TLS
Definition: create_test_channel.h:34


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:06