Imported Upstream version 1.36.0
[platform/upstream/grpc.git] / src / core / ext / filters / client_channel / lb_policy / grpclb / grpclb_channel_secure.cc
1 /*
2  *
3  * Copyright 2017 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/grpclb/grpclb_channel.h"
22
23 #include <string.h>
24
25 #include "absl/container/inlined_vector.h"
26
27 #include <grpc/grpc_security.h>
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/string_util.h>
30
31 #include "src/core/ext/filters/client_channel/client_channel.h"
32 #include "src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_balancer_addresses.h"
33 #include "src/core/ext/filters/client_channel/server_address.h"
34 #include "src/core/lib/channel/channel_args.h"
35 #include "src/core/lib/gpr/string.h"
36 #include "src/core/lib/iomgr/sockaddr_utils.h"
37 #include "src/core/lib/security/credentials/credentials.h"
38 #include "src/core/lib/slice/slice_internal.h"
39
40 namespace grpc_core {
41
42 grpc_channel_args* ModifyGrpclbBalancerChannelArgs(grpc_channel_args* args) {
43   absl::InlinedVector<const char*, 1> args_to_remove;
44   absl::InlinedVector<grpc_arg, 1> args_to_add;
45   // Substitute the channel credentials with a version without call
46   // credentials: the load balancer is not necessarily trusted to handle
47   // bearer token credentials.
48   grpc_channel_credentials* channel_credentials =
49       grpc_channel_credentials_find_in_args(args);
50   RefCountedPtr<grpc_channel_credentials> creds_sans_call_creds;
51   if (channel_credentials != nullptr) {
52     creds_sans_call_creds =
53         channel_credentials->duplicate_without_call_credentials();
54     GPR_ASSERT(creds_sans_call_creds != nullptr);
55     args_to_remove.emplace_back(GRPC_ARG_CHANNEL_CREDENTIALS);
56     args_to_add.emplace_back(
57         grpc_channel_credentials_to_arg(creds_sans_call_creds.get()));
58   }
59   grpc_channel_args* result = grpc_channel_args_copy_and_add_and_remove(
60       args, args_to_remove.data(), args_to_remove.size(), args_to_add.data(),
61       args_to_add.size());
62   // Clean up.
63   grpc_channel_args_destroy(args);
64   return result;
65 }
66
67 grpc_channel* CreateGrpclbBalancerChannel(const char* target_uri,
68                                           const grpc_channel_args& args) {
69   grpc_channel_credentials* creds =
70       grpc_channel_credentials_find_in_args(&args);
71   if (creds == nullptr) {
72     // Build with security but parent channel is insecure.
73     return grpc_insecure_channel_create(target_uri, &args, nullptr);
74   }
75   const char* arg_to_remove = GRPC_ARG_CHANNEL_CREDENTIALS;
76   grpc_channel_args* new_args =
77       grpc_channel_args_copy_and_remove(&args, &arg_to_remove, 1);
78   grpc_channel* channel =
79       grpc_secure_channel_create(creds, target_uri, new_args, nullptr);
80   grpc_channel_args_destroy(new_args);
81   return channel;
82 }
83
84 }  // namespace grpc_core