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 <gflags/gflags.h>
22
23 #include <grpc/support/log.h>
24 #include <grpcpp/create_channel.h>
25 #include <grpcpp/security/credentials.h>
26
27 #include "test/cpp/util/test_credentials_provider.h"
28
29 DEFINE_string(
30 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 (FLAGS_grpc_test_use_grpclb_with_child_policy.size() > 0) {
63 args->SetString("grpc.service_config",
64 "{\"loadBalancingConfig\":[{\"grpclb\":{\"childPolicy\":[{"
65 "\"" +
66 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 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
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 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,
184 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 ChannelArguments channel_args;
240 MaybeSetCustomChannelArgs(&channel_args);
241 std::shared_ptr<ChannelCredentials> channel_creds =
242 testing::GetCredentialsProvider()->GetChannelCredentials(credential_type,
243 &channel_args);
244 GPR_ASSERT(channel_creds != nullptr);
245 if (creds.get()) {
246 channel_creds = grpc::CompositeChannelCredentials(channel_creds, creds);
247 }
248 return experimental::CreateCustomChannelWithInterceptors(
249 server, channel_creds, channel_args, std::move(interceptor_creators));
250 }
251
252 } // namespace grpc
253