Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc-cloned / deps / grpc / src / core / ext / transport / chttp2 / client / secure / secure_channel_create.cc
1 /*
2  *
3  * Copyright 2015 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 <grpc/support/port_platform.h>
20
21 #include <grpc/grpc.h>
22
23 #include <string.h>
24
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/string_util.h>
27
28 #include "src/core/ext/filters/client_channel/client_channel.h"
29 #include "src/core/ext/filters/client_channel/resolver_registry.h"
30 #include "src/core/ext/transport/chttp2/client/chttp2_connector.h"
31 #include "src/core/lib/channel/channel_args.h"
32 #include "src/core/lib/gprpp/memory.h"
33 #include "src/core/lib/iomgr/sockaddr_utils.h"
34 #include "src/core/lib/security/credentials/credentials.h"
35 #include "src/core/lib/security/security_connector/security_connector.h"
36 #include "src/core/lib/security/transport/target_authority_table.h"
37 #include "src/core/lib/slice/slice_hash_table.h"
38 #include "src/core/lib/slice/slice_internal.h"
39 #include "src/core/lib/surface/api_trace.h"
40 #include "src/core/lib/surface/channel.h"
41 #include "src/core/lib/uri/uri_parser.h"
42
43 namespace grpc_core {
44
45 class Chttp2SecureClientChannelFactory : public ClientChannelFactory {
46  public:
47   Subchannel* CreateSubchannel(const grpc_channel_args* args) override {
48     grpc_channel_args* new_args = GetSecureNamingChannelArgs(args);
49     if (new_args == nullptr) {
50       gpr_log(GPR_ERROR,
51               "Failed to create channel args during subchannel creation.");
52       return nullptr;
53     }
54     grpc_connector* connector = grpc_chttp2_connector_create();
55     Subchannel* s = Subchannel::Create(connector, new_args);
56     grpc_connector_unref(connector);
57     grpc_channel_args_destroy(new_args);
58     return s;
59   }
60
61  private:
62   static grpc_channel_args* GetSecureNamingChannelArgs(
63       const grpc_channel_args* args) {
64     grpc_channel_credentials* channel_credentials =
65         grpc_channel_credentials_find_in_args(args);
66     if (channel_credentials == nullptr) {
67       gpr_log(GPR_ERROR,
68               "Can't create subchannel: channel credentials missing for secure "
69               "channel.");
70       return nullptr;
71     }
72     // Make sure security connector does not already exist in args.
73     if (grpc_security_connector_find_in_args(args) != nullptr) {
74       gpr_log(GPR_ERROR,
75               "Can't create subchannel: security connector already present in "
76               "channel args.");
77       return nullptr;
78     }
79     // To which address are we connecting? By default, use the server URI.
80     const grpc_arg* server_uri_arg =
81         grpc_channel_args_find(args, GRPC_ARG_SERVER_URI);
82     const char* server_uri_str = grpc_channel_arg_get_string(server_uri_arg);
83     GPR_ASSERT(server_uri_str != nullptr);
84     grpc_uri* server_uri =
85         grpc_uri_parse(server_uri_str, true /* suppress errors */);
86     GPR_ASSERT(server_uri != nullptr);
87     const TargetAuthorityTable* target_authority_table =
88         FindTargetAuthorityTableInArgs(args);
89     UniquePtr<char> authority;
90     if (target_authority_table != nullptr) {
91       // Find the authority for the target.
92       const char* target_uri_str =
93           Subchannel::GetUriFromSubchannelAddressArg(args);
94       grpc_uri* target_uri =
95           grpc_uri_parse(target_uri_str, false /* suppress errors */);
96       GPR_ASSERT(target_uri != nullptr);
97       if (target_uri->path[0] != '\0') {  // "path" may be empty
98         const grpc_slice key = grpc_slice_from_static_string(
99             target_uri->path[0] == '/' ? target_uri->path + 1
100                                        : target_uri->path);
101         const UniquePtr<char>* value = target_authority_table->Get(key);
102         if (value != nullptr) authority.reset(gpr_strdup(value->get()));
103         grpc_slice_unref_internal(key);
104       }
105       grpc_uri_destroy(target_uri);
106     }
107     // If the authority hasn't already been set (either because no target
108     // authority table was present or because the target was not present
109     // in the table), fall back to using the original server URI.
110     if (authority == nullptr) {
111       authority = ResolverRegistry::GetDefaultAuthority(server_uri_str);
112     }
113     grpc_arg args_to_add[2];
114     size_t num_args_to_add = 0;
115     if (grpc_channel_args_find(args, GRPC_ARG_DEFAULT_AUTHORITY) == nullptr) {
116       // If the channel args don't already contain GRPC_ARG_DEFAULT_AUTHORITY,
117       // add the arg, setting it to the value just obtained.
118       args_to_add[num_args_to_add++] = grpc_channel_arg_string_create(
119           const_cast<char*>(GRPC_ARG_DEFAULT_AUTHORITY), authority.get());
120     }
121     grpc_channel_args* args_with_authority =
122         grpc_channel_args_copy_and_add(args, args_to_add, num_args_to_add);
123     grpc_uri_destroy(server_uri);
124     // Create the security connector using the credentials and target name.
125     grpc_channel_args* new_args_from_connector = nullptr;
126     RefCountedPtr<grpc_channel_security_connector>
127         subchannel_security_connector =
128             channel_credentials->create_security_connector(
129                 /*call_creds=*/nullptr, authority.get(), args_with_authority,
130                 &new_args_from_connector);
131     if (subchannel_security_connector == nullptr) {
132       gpr_log(GPR_ERROR,
133               "Failed to create secure subchannel for secure name '%s'",
134               authority.get());
135       grpc_channel_args_destroy(args_with_authority);
136       return nullptr;
137     }
138     grpc_arg new_security_connector_arg =
139         grpc_security_connector_to_arg(subchannel_security_connector.get());
140     grpc_channel_args* new_args = grpc_channel_args_copy_and_add(
141         new_args_from_connector != nullptr ? new_args_from_connector
142                                            : args_with_authority,
143         &new_security_connector_arg, 1);
144     subchannel_security_connector.reset(DEBUG_LOCATION, "lb_channel_create");
145     if (new_args_from_connector != nullptr) {
146       grpc_channel_args_destroy(new_args_from_connector);
147     }
148     grpc_channel_args_destroy(args_with_authority);
149     return new_args;
150   }
151 };
152
153 namespace {
154
155 grpc_channel* CreateChannel(const char* target, const grpc_channel_args* args) {
156   if (target == nullptr) {
157     gpr_log(GPR_ERROR, "cannot create channel with NULL target name");
158     return nullptr;
159   }
160   // Add channel arg containing the server URI.
161   UniquePtr<char> canonical_target =
162       ResolverRegistry::AddDefaultPrefixIfNeeded(target);
163   grpc_arg arg = grpc_channel_arg_string_create(
164       const_cast<char*>(GRPC_ARG_SERVER_URI), canonical_target.get());
165   const char* to_remove[] = {GRPC_ARG_SERVER_URI};
166   grpc_channel_args* new_args =
167       grpc_channel_args_copy_and_add_and_remove(args, to_remove, 1, &arg, 1);
168   grpc_channel* channel =
169       grpc_channel_create(target, new_args, GRPC_CLIENT_CHANNEL, nullptr);
170   grpc_channel_args_destroy(new_args);
171   return channel;
172 }
173
174 }  // namespace
175
176 }  // namespace grpc_core
177
178 namespace {
179
180 grpc_core::Chttp2SecureClientChannelFactory* g_factory;
181 gpr_once g_factory_once = GPR_ONCE_INIT;
182
183 void FactoryInit() {
184   g_factory = grpc_core::New<grpc_core::Chttp2SecureClientChannelFactory>();
185 }
186
187 }  // namespace
188
189 // Create a secure client channel:
190 //   Asynchronously: - resolve target
191 //                   - connect to it (trying alternatives as presented)
192 //                   - perform handshakes
193 grpc_channel* grpc_secure_channel_create(grpc_channel_credentials* creds,
194                                          const char* target,
195                                          const grpc_channel_args* args,
196                                          void* reserved) {
197   grpc_core::ExecCtx exec_ctx;
198   GRPC_API_TRACE(
199       "grpc_secure_channel_create(creds=%p, target=%s, args=%p, "
200       "reserved=%p)",
201       4, ((void*)creds, target, (void*)args, (void*)reserved));
202   GPR_ASSERT(reserved == nullptr);
203   grpc_channel* channel = nullptr;
204   if (creds != nullptr) {
205     // Add channel args containing the client channel factory and channel
206     // credentials.
207     gpr_once_init(&g_factory_once, FactoryInit);
208     grpc_arg args_to_add[] = {
209         grpc_core::ClientChannelFactory::CreateChannelArg(g_factory),
210         grpc_channel_credentials_to_arg(creds)};
211     grpc_channel_args* new_args = grpc_channel_args_copy_and_add(
212         args, args_to_add, GPR_ARRAY_SIZE(args_to_add));
213     new_args = creds->update_arguments(new_args);
214     // Create channel.
215     channel = grpc_core::CreateChannel(target, new_args);
216     // Clean up.
217     grpc_channel_args_destroy(new_args);
218   }
219   return channel != nullptr ? channel
220                             : grpc_lame_client_channel_create(
221                                   target, GRPC_STATUS_INTERNAL,
222                                   "Failed to create secure client channel");
223 }