3 * Copyright 2017 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 "src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel.h"
25 #include <grpc/grpc_security.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/string_util.h>
29 #include "src/core/ext/filters/client_channel/client_channel.h"
30 #include "src/core/ext/filters/client_channel/server_address.h"
31 #include "src/core/lib/channel/channel_args.h"
32 #include "src/core/lib/gpr/string.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/transport/target_authority_table.h"
36 #include "src/core/lib/slice/slice_internal.h"
42 int BalancerNameCmp(const grpc_core::UniquePtr<char>& a,
43 const grpc_core::UniquePtr<char>& b) {
44 return strcmp(a.get(), b.get());
47 RefCountedPtr<TargetAuthorityTable> CreateTargetAuthorityTable(
48 const ServerAddressList& addresses) {
49 TargetAuthorityTable::Entry* target_authority_entries =
50 static_cast<TargetAuthorityTable::Entry*>(
51 gpr_zalloc(sizeof(*target_authority_entries) * addresses.size()));
52 for (size_t i = 0; i < addresses.size(); ++i) {
55 grpc_sockaddr_to_string(&addr_str, &addresses[i].address(), true) > 0);
56 target_authority_entries[i].key = grpc_slice_from_copied_string(addr_str);
58 char* balancer_name = grpc_channel_arg_get_string(grpc_channel_args_find(
59 addresses[i].args(), GRPC_ARG_ADDRESS_BALANCER_NAME));
60 target_authority_entries[i].value.reset(gpr_strdup(balancer_name));
62 RefCountedPtr<TargetAuthorityTable> target_authority_table =
63 TargetAuthorityTable::Create(addresses.size(), target_authority_entries,
65 gpr_free(target_authority_entries);
66 return target_authority_table;
71 grpc_channel_args* ModifyGrpclbBalancerChannelArgs(
72 const ServerAddressList& addresses, grpc_channel_args* args) {
73 InlinedVector<const char*, 1> args_to_remove;
74 InlinedVector<grpc_arg, 2> args_to_add;
75 // Add arg for targets info table.
76 RefCountedPtr<TargetAuthorityTable> target_authority_table =
77 CreateTargetAuthorityTable(addresses);
78 args_to_add.emplace_back(
79 CreateTargetAuthorityTableChannelArg(target_authority_table.get()));
80 // Substitute the channel credentials with a version without call
81 // credentials: the load balancer is not necessarily trusted to handle
82 // bearer token credentials.
83 grpc_channel_credentials* channel_credentials =
84 grpc_channel_credentials_find_in_args(args);
85 RefCountedPtr<grpc_channel_credentials> creds_sans_call_creds;
86 if (channel_credentials != nullptr) {
87 creds_sans_call_creds =
88 channel_credentials->duplicate_without_call_credentials();
89 GPR_ASSERT(creds_sans_call_creds != nullptr);
90 args_to_remove.emplace_back(GRPC_ARG_CHANNEL_CREDENTIALS);
91 args_to_add.emplace_back(
92 grpc_channel_credentials_to_arg(creds_sans_call_creds.get()));
94 grpc_channel_args* result = grpc_channel_args_copy_and_add_and_remove(
95 args, args_to_remove.data(), args_to_remove.size(), args_to_add.data(),
98 grpc_channel_args_destroy(args);
102 grpc_channel* CreateGrpclbBalancerChannel(const char* target_uri,
103 const grpc_channel_args& args) {
104 grpc_channel_credentials* creds =
105 grpc_channel_credentials_find_in_args(&args);
106 if (creds == nullptr) {
107 // Build with security but parent channel is insecure.
108 return grpc_insecure_channel_create(target_uri, &args, nullptr);
110 const char* arg_to_remove = GRPC_ARG_CHANNEL_CREDENTIALS;
111 grpc_channel_args* new_args =
112 grpc_channel_args_copy_and_remove(&args, &arg_to_remove, 1);
113 grpc_channel* channel =
114 grpc_secure_channel_create(creds, target_uri, new_args, nullptr);
115 grpc_channel_args_destroy(new_args);
119 } // namespace grpc_core