3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
21 #include <grpc/grpc.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/string_util.h>
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"
45 class Chttp2SecureClientChannelFactory : public ClientChannelFactory {
47 Subchannel* CreateSubchannel(const grpc_channel_args* args) override {
48 grpc_channel_args* new_args = GetSecureNamingChannelArgs(args);
49 if (new_args == nullptr) {
51 "Failed to create channel args during subchannel creation.");
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);
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) {
68 "Can't create subchannel: channel credentials missing for secure "
72 // Make sure security connector does not already exist in args.
73 if (grpc_security_connector_find_in_args(args) != nullptr) {
75 "Can't create subchannel: security connector already present in "
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
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);
105 grpc_uri_destroy(target_uri);
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);
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());
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) {
133 "Failed to create secure subchannel for secure name '%s'",
135 grpc_channel_args_destroy(args_with_authority);
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);
148 grpc_channel_args_destroy(args_with_authority);
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");
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);
176 } // namespace grpc_core
180 grpc_core::Chttp2SecureClientChannelFactory* g_factory;
181 gpr_once g_factory_once = GPR_ONCE_INIT;
184 g_factory = grpc_core::New<grpc_core::Chttp2SecureClientChannelFactory>();
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,
195 const grpc_channel_args* args,
197 grpc_core::ExecCtx exec_ctx;
199 "grpc_secure_channel_create(creds=%p, target=%s, args=%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
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);
215 channel = grpc_core::CreateChannel(target, new_args);
217 grpc_channel_args_destroy(new_args);
219 return channel != nullptr ? channel
220 : grpc_lame_client_channel_create(
221 target, GRPC_STATUS_INTERNAL,
222 "Failed to create secure client channel");