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