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 <grpcpp/create_channel.h>
22 #include <grpcpp/security/credentials.h>
23
24 #include "absl/flags/flag.h"
25 #include "absl/log/check.h"
26 #include "src/core/util/crash.h"
27 #include "test/cpp/util/test_credentials_provider.h"
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:
GetChannelCredentials(grpc::ChannelArguments *)43 std::shared_ptr<ChannelCredentials> GetChannelCredentials(
44 grpc::ChannelArguments* /*args*/) override {
45 return grpc::SslCredentials(SslCredentialsOptions());
46 }
GetServerCredentials()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.
AddProdSslType()54 void AddProdSslType() {
55 testing::GetCredentialsProvider()->AddSecureType(
56 kProdTlsCredentialsType, std::unique_ptr<testing::CredentialTypeProvider>(
57 new SslCredentialProvider));
58 }
59
MaybeSetCustomChannelArgs(grpc::ChannelArguments * args)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);
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)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
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)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
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)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
CreateTestChannel(const std::string & server,const std::string & override_hostname,testing::transport_security security_type,bool use_prod_roots)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.
CreateTestChannel(const std::string & server,testing::transport_security security_type)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
CreateTestChannel(const std::string & server,const std::string & credential_type,const std::shared_ptr<CallCredentials> & creds)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 =
135 testing::GetCredentialsProvider()->GetChannelCredentials(credential_type,
136 &channel_args);
137 CHECK_NE(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
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)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()) {
156 return grpc::CreateCustomChannel(server, InsecureChannelCredentials(),
157 channel_args);
158 } else {
159 return experimental::CreateCustomChannelWithInterceptors(
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);
166 channel_creds = testing::GetCredentialsProvider()->GetChannelCredentials(
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.
173 channel_creds = testing::GetCredentialsProvider()->GetChannelCredentials(
174 testing::kTlsCredentialsType, &channel_args);
175 }
176 CHECK_NE(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 {
185 return experimental::CreateCustomChannelWithInterceptors(
186 connect_to, channel_creds, channel_args,
187 std::move(interceptor_creators));
188 }
189 } else {
190 channel_creds = testing::GetCredentialsProvider()->GetChannelCredentials(
191 cred_type, &channel_args);
192 CHECK_NE(channel_creds, nullptr);
193
194 if (interceptor_creators.empty()) {
195 return grpc::CreateCustomChannel(server, channel_creds, channel_args);
196 } else {
197 return experimental::CreateCustomChannelWithInterceptors(
198 server, channel_creds, channel_args, std::move(interceptor_creators));
199 }
200 }
201 }
202
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)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
212 ? testing::kAltsCredentialsType
213 : (security_type == testing::TLS ? testing::kTlsCredentialsType
214 : testing::kInsecureCredentialsType);
215 return CreateTestChannel(server, credential_type, override_hostname,
216 use_prod_roots, creds, args,
217 std::move(interceptor_creators));
218 }
219
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)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
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)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 return CreateTestChannel(server, credential_type, creds,
239 std::move(interceptor_creators),
240 {} /* channel_args */);
241 }
242
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)243 std::shared_ptr<Channel> CreateTestChannel(
244 const std::string& server, const std::string& credential_type,
245 const std::shared_ptr<CallCredentials>& creds,
246 std::vector<
247 std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
248 interceptor_creators,
249 ChannelArguments channel_args) {
250 MaybeSetCustomChannelArgs(&channel_args);
251 std::shared_ptr<ChannelCredentials> channel_creds =
252 testing::GetCredentialsProvider()->GetChannelCredentials(credential_type,
253 &channel_args);
254 CHECK_NE(channel_creds, nullptr);
255 if (creds.get()) {
256 channel_creds = grpc::CompositeChannelCredentials(channel_creds, creds);
257 }
258 return experimental::CreateCustomChannelWithInterceptors(
259 server, channel_creds, channel_args, std::move(interceptor_creators));
260 }
261
262 } // namespace grpc
263