Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / src / core / ext / filters / client_channel / lb_policy / xds / xds_channel_secure.cc
1 /*
2  *
3  * Copyright 2018 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 "src/core/ext/filters/client_channel/lb_policy/xds/xds_channel.h"
22
23 #include <string.h>
24
25 #include <grpc/grpc_security.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/string_util.h>
28
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"
37
38 namespace grpc_core {
39
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()));
56   }
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(),
59       args_to_add.size());
60   // Clean up.
61   grpc_channel_args_destroy(args);
62   return result;
63 }
64
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);
72   }
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);
79   return channel;
80 }
81
82 }  // namespace grpc_core