3 * Copyright 2018 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/xds/xds_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"
40 grpc_channel_args* ModifyXdsBalancerChannelArgs(grpc_channel_args* args) {
41 InlinedVector<const char*, 1> args_to_remove;
42 InlinedVector<grpc_arg, 2> args_to_add;
43 // Substitute the channel credentials with a version without call
44 // credentials: the load balancer is not necessarily trusted to handle
45 // bearer token credentials.
46 grpc_channel_credentials* channel_credentials =
47 grpc_channel_credentials_find_in_args(args);
48 RefCountedPtr<grpc_channel_credentials> creds_sans_call_creds;
49 if (channel_credentials != nullptr) {
50 creds_sans_call_creds =
51 channel_credentials->duplicate_without_call_credentials();
52 GPR_ASSERT(creds_sans_call_creds != nullptr);
53 args_to_remove.emplace_back(GRPC_ARG_CHANNEL_CREDENTIALS);
54 args_to_add.emplace_back(
55 grpc_channel_credentials_to_arg(creds_sans_call_creds.get()));
57 grpc_channel_args* result = grpc_channel_args_copy_and_add_and_remove(
58 args, args_to_remove.data(), args_to_remove.size(), args_to_add.data(),
61 grpc_channel_args_destroy(args);
65 grpc_channel* CreateXdsBalancerChannel(const char* target_uri,
66 const grpc_channel_args& args) {
67 grpc_channel_credentials* creds =
68 grpc_channel_credentials_find_in_args(&args);
69 if (creds == nullptr) {
70 // Build with security but parent channel is insecure.
71 return grpc_insecure_channel_create(target_uri, &args, nullptr);
73 const char* arg_to_remove = GRPC_ARG_CHANNEL_CREDENTIALS;
74 grpc_channel_args* new_args =
75 grpc_channel_args_copy_and_remove(&args, &arg_to_remove, 1);
76 grpc_channel* channel =
77 grpc_secure_channel_create(creds, target_uri, new_args, nullptr);
78 grpc_channel_args_destroy(new_args);
82 } // namespace grpc_core