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 grpc::string & server,const grpc::string & cred_type,const grpc::string & override_hostname,bool use_prod_roots,const std::shared_ptr<CallCredentials> & creds,const ChannelArguments & args)88 std::shared_ptr<Channel> CreateTestChannel(
89     const grpc::string& server, const grpc::string& cred_type,
90     const grpc::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 grpc::string & server,const grpc::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 grpc::string& server, const grpc::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 grpc::string & server,const grpc::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 grpc::string& server, const grpc::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 grpc::string & server,const grpc::string & override_hostname,testing::transport_security security_type,bool use_prod_roots)116 std::shared_ptr<Channel> CreateTestChannel(
117     const grpc::string& server, const grpc::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 grpc::string & server,testing::transport_security security_type)124 std::shared_ptr<Channel> CreateTestChannel(
125     const grpc::string& server, testing::transport_security security_type) {
126   return CreateTestChannel(server, "foo.test.google.fr", security_type, false);
127 }
128 
CreateTestChannel(const grpc::string & server,const grpc::string & credential_type,const std::shared_ptr<CallCredentials> & creds)129 std::shared_ptr<Channel> CreateTestChannel(
130     const grpc::string& server, const grpc::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 grpc::string & server,const grpc::string & cred_type,const grpc::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 grpc::string& server, const grpc::string& cred_type,
146     const grpc::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 grpc::string& connect_to =
179         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,
185                                          channel_args);
186     } else {
187       return experimental::CreateCustomChannelWithInterceptors(
188           connect_to, channel_creds, channel_args,
189           std::move(interceptor_creators));
190     }
191   } else {
192     channel_creds = testing::GetCredentialsProvider()->GetChannelCredentials(
193         cred_type, &channel_args);
194     GPR_ASSERT(channel_creds != nullptr);
195 
196     if (interceptor_creators.empty()) {
197       return ::grpc::CreateCustomChannel(server, channel_creds, channel_args);
198     } else {
199       return experimental::CreateCustomChannelWithInterceptors(
200           server, channel_creds, channel_args, std::move(interceptor_creators));
201     }
202   }
203 }
204 
CreateTestChannel(const grpc::string & server,const grpc::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)205 std::shared_ptr<Channel> CreateTestChannel(
206     const grpc::string& server, const grpc::string& override_hostname,
207     testing::transport_security security_type, bool use_prod_roots,
208     const std::shared_ptr<CallCredentials>& creds, const ChannelArguments& args,
209     std::vector<
210         std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
211         interceptor_creators) {
212   grpc::string credential_type =
213       security_type == testing::ALTS
214           ? testing::kAltsCredentialsType
215           : (security_type == testing::TLS ? testing::kTlsCredentialsType
216                                            : testing::kInsecureCredentialsType);
217   return CreateTestChannel(server, credential_type, override_hostname,
218                            use_prod_roots, creds, args,
219                            std::move(interceptor_creators));
220 }
221 
CreateTestChannel(const grpc::string & server,const grpc::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)222 std::shared_ptr<Channel> CreateTestChannel(
223     const grpc::string& server, const grpc::string& override_hostname,
224     testing::transport_security security_type, bool use_prod_roots,
225     const std::shared_ptr<CallCredentials>& creds,
226     std::vector<
227         std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
228         interceptor_creators) {
229   return CreateTestChannel(server, override_hostname, security_type,
230                            use_prod_roots, creds, ChannelArguments(),
231                            std::move(interceptor_creators));
232 }
233 
CreateTestChannel(const grpc::string & server,const grpc::string & credential_type,const std::shared_ptr<CallCredentials> & creds,std::vector<std::unique_ptr<experimental::ClientInterceptorFactoryInterface>> interceptor_creators)234 std::shared_ptr<Channel> CreateTestChannel(
235     const grpc::string& server, const grpc::string& credential_type,
236     const std::shared_ptr<CallCredentials>& creds,
237     std::vector<
238         std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
239         interceptor_creators) {
240   ChannelArguments channel_args;
241   MaybeSetCustomChannelArgs(&channel_args);
242   std::shared_ptr<ChannelCredentials> channel_creds =
243       testing::GetCredentialsProvider()->GetChannelCredentials(credential_type,
244                                                                &channel_args);
245   GPR_ASSERT(channel_creds != nullptr);
246   if (creds.get()) {
247     channel_creds = grpc::CompositeChannelCredentials(channel_creds, creds);
248   }
249   return experimental::CreateCustomChannelWithInterceptors(
250       server, channel_creds, channel_args, std::move(interceptor_creators));
251 }
252 
253 }  // namespace grpc
254