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 <grpc/support/log.h>
22 #include <grpcpp/create_channel.h>
23 #include <grpcpp/security/credentials.h>
24
25 #include "test/cpp/util/test_credentials_provider.h"
26
27 namespace grpc {
28
29 namespace {
30
31 const char kProdTlsCredentialsType[] = "prod_ssl";
32
33 class SslCredentialProvider : public testing::CredentialTypeProvider {
34 public:
GetChannelCredentials(grpc::ChannelArguments * args)35 std::shared_ptr<ChannelCredentials> GetChannelCredentials(
36 grpc::ChannelArguments* args) override {
37 return SslCredentials(SslCredentialsOptions());
38 }
GetServerCredentials()39 std::shared_ptr<ServerCredentials> GetServerCredentials() override {
40 return nullptr;
41 }
42 };
43
44 gpr_once g_once_init_add_prod_ssl_provider = GPR_ONCE_INIT;
45 // Register ssl with non-test roots type to the credentials provider.
AddProdSslType()46 void AddProdSslType() {
47 testing::GetCredentialsProvider()->AddSecureType(
48 kProdTlsCredentialsType, std::unique_ptr<testing::CredentialTypeProvider>(
49 new SslCredentialProvider));
50 }
51
52 } // namespace
53
54 // When cred_type is 'ssl', if server is empty, override_hostname is used to
55 // create channel. Otherwise, connect to server and override hostname if
56 // override_hostname is provided.
57 // When cred_type is not 'ssl', override_hostname is ignored.
58 // Set use_prod_root to true to use the SSL root for connecting to google.
59 // In this case, path to the roots pem file must be set via environment variable
60 // GRPC_DEFAULT_SSL_ROOTS_FILE_PATH.
61 // Otherwise, root for test SSL cert will be used.
62 // creds will be used to create a channel when cred_type is 'ssl'.
63 // Use examples:
64 // CreateTestChannel(
65 // "1.1.1.1:12345", "ssl", "override.hostname.com", false, creds);
66 // CreateTestChannel("test.google.com:443", "ssl", "", true, creds);
67 // same as above
68 // 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)69 std::shared_ptr<Channel> CreateTestChannel(
70 const grpc::string& server, const grpc::string& cred_type,
71 const grpc::string& override_hostname, bool use_prod_roots,
72 const std::shared_ptr<CallCredentials>& creds,
73 const ChannelArguments& args) {
74 ChannelArguments channel_args(args);
75 std::shared_ptr<ChannelCredentials> channel_creds;
76 if (cred_type.empty()) {
77 return CreateCustomChannel(server, InsecureChannelCredentials(), args);
78 } else if (cred_type == testing::kTlsCredentialsType) { // cred_type == "ssl"
79 if (use_prod_roots) {
80 gpr_once_init(&g_once_init_add_prod_ssl_provider, &AddProdSslType);
81 channel_creds = testing::GetCredentialsProvider()->GetChannelCredentials(
82 kProdTlsCredentialsType, &channel_args);
83 if (!server.empty() && !override_hostname.empty()) {
84 channel_args.SetSslTargetNameOverride(override_hostname);
85 }
86 } else {
87 // override_hostname is discarded as the provider handles it.
88 channel_creds = testing::GetCredentialsProvider()->GetChannelCredentials(
89 testing::kTlsCredentialsType, &channel_args);
90 }
91 GPR_ASSERT(channel_creds != nullptr);
92
93 const grpc::string& connect_to =
94 server.empty() ? override_hostname : server;
95 if (creds.get()) {
96 channel_creds = CompositeChannelCredentials(channel_creds, creds);
97 }
98 return CreateCustomChannel(connect_to, channel_creds, channel_args);
99 } else {
100 channel_creds = testing::GetCredentialsProvider()->GetChannelCredentials(
101 cred_type, &channel_args);
102 GPR_ASSERT(channel_creds != nullptr);
103
104 return CreateCustomChannel(server, channel_creds, args);
105 }
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,const ChannelArguments & args)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 const ChannelArguments& args) {
113 grpc::string type =
114 security_type == testing::ALTS
115 ? testing::kAltsCredentialsType
116 : (security_type == testing::TLS ? testing::kTlsCredentialsType
117 : testing::kInsecureCredentialsType);
118 return CreateTestChannel(server, type, override_hostname, use_prod_roots,
119 creds, args);
120 }
121
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)122 std::shared_ptr<Channel> CreateTestChannel(
123 const grpc::string& server, const grpc::string& override_hostname,
124 testing::transport_security security_type, bool use_prod_roots,
125 const std::shared_ptr<CallCredentials>& creds) {
126 return CreateTestChannel(server, override_hostname, security_type,
127 use_prod_roots, creds, ChannelArguments());
128 }
129
CreateTestChannel(const grpc::string & server,const grpc::string & override_hostname,testing::transport_security security_type,bool use_prod_roots)130 std::shared_ptr<Channel> CreateTestChannel(
131 const grpc::string& server, const grpc::string& override_hostname,
132 testing::transport_security security_type, bool use_prod_roots) {
133 return CreateTestChannel(server, override_hostname, security_type,
134 use_prod_roots, std::shared_ptr<CallCredentials>());
135 }
136
137 // Shortcut for end2end and interop tests.
CreateTestChannel(const grpc::string & server,testing::transport_security security_type)138 std::shared_ptr<Channel> CreateTestChannel(
139 const grpc::string& server, testing::transport_security security_type) {
140 return CreateTestChannel(server, "foo.test.google.fr", security_type, false);
141 }
142
CreateTestChannel(const grpc::string & server,const grpc::string & credential_type,const std::shared_ptr<CallCredentials> & creds)143 std::shared_ptr<Channel> CreateTestChannel(
144 const grpc::string& server, const grpc::string& credential_type,
145 const std::shared_ptr<CallCredentials>& creds) {
146 ChannelArguments channel_args;
147 std::shared_ptr<ChannelCredentials> channel_creds =
148 testing::GetCredentialsProvider()->GetChannelCredentials(credential_type,
149 &channel_args);
150 GPR_ASSERT(channel_creds != nullptr);
151 if (creds.get()) {
152 channel_creds = CompositeChannelCredentials(channel_creds, creds);
153 }
154 return CreateCustomChannel(server, channel_creds, channel_args);
155 }
156
157 } // namespace grpc
158