Home | History | Annotate | Download | only in grpclb
      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 <grpc/support/alloc.h>
     26 #include <grpc/support/string_util.h>
     27 
     28 #include "src/core/ext/filters/client_channel/client_channel.h"
     29 #include "src/core/lib/channel/channel_args.h"
     30 #include "src/core/lib/gpr/string.h"
     31 #include "src/core/lib/iomgr/sockaddr_utils.h"
     32 #include "src/core/lib/security/credentials/credentials.h"
     33 #include "src/core/lib/security/transport/target_authority_table.h"
     34 #include "src/core/lib/slice/slice_internal.h"
     35 
     36 namespace grpc_core {
     37 namespace {
     38 
     39 int BalancerNameCmp(const grpc_core::UniquePtr<char>& a,
     40                     const grpc_core::UniquePtr<char>& b) {
     41   return strcmp(a.get(), b.get());
     42 }
     43 
     44 RefCountedPtr<TargetAuthorityTable> CreateTargetAuthorityTable(
     45     grpc_lb_addresses* addresses) {
     46   TargetAuthorityTable::Entry* target_authority_entries =
     47       static_cast<TargetAuthorityTable::Entry*>(gpr_zalloc(
     48           sizeof(*target_authority_entries) * addresses->num_addresses));
     49   for (size_t i = 0; i < addresses->num_addresses; ++i) {
     50     char* addr_str;
     51     GPR_ASSERT(grpc_sockaddr_to_string(
     52                    &addr_str, &addresses->addresses[i].address, true) > 0);
     53     target_authority_entries[i].key = grpc_slice_from_copied_string(addr_str);
     54     target_authority_entries[i].value.reset(
     55         gpr_strdup(addresses->addresses[i].balancer_name));
     56     gpr_free(addr_str);
     57   }
     58   RefCountedPtr<TargetAuthorityTable> target_authority_table =
     59       TargetAuthorityTable::Create(addresses->num_addresses,
     60                                    target_authority_entries, BalancerNameCmp);
     61   gpr_free(target_authority_entries);
     62   return target_authority_table;
     63 }
     64 
     65 }  // namespace
     66 }  // namespace grpc_core
     67 
     68 grpc_channel_args* grpc_lb_policy_grpclb_modify_lb_channel_args(
     69     grpc_channel_args* args) {
     70   const char* args_to_remove[1];
     71   size_t num_args_to_remove = 0;
     72   grpc_arg args_to_add[2];
     73   size_t num_args_to_add = 0;
     74   // Add arg for targets info table.
     75   const grpc_arg* arg = grpc_channel_args_find(args, GRPC_ARG_LB_ADDRESSES);
     76   GPR_ASSERT(arg != nullptr);
     77   GPR_ASSERT(arg->type == GRPC_ARG_POINTER);
     78   grpc_lb_addresses* addresses =
     79       static_cast<grpc_lb_addresses*>(arg->value.pointer.p);
     80   grpc_core::RefCountedPtr<grpc_core::TargetAuthorityTable>
     81       target_authority_table = grpc_core::CreateTargetAuthorityTable(addresses);
     82   args_to_add[num_args_to_add++] =
     83       grpc_core::CreateTargetAuthorityTableChannelArg(
     84           target_authority_table.get());
     85   // Substitute the channel credentials with a version without call
     86   // credentials: the load balancer is not necessarily trusted to handle
     87   // bearer token credentials.
     88   grpc_channel_credentials* channel_credentials =
     89       grpc_channel_credentials_find_in_args(args);
     90   grpc_channel_credentials* creds_sans_call_creds = nullptr;
     91   if (channel_credentials != nullptr) {
     92     creds_sans_call_creds =
     93         grpc_channel_credentials_duplicate_without_call_credentials(
     94             channel_credentials);
     95     GPR_ASSERT(creds_sans_call_creds != nullptr);
     96     args_to_remove[num_args_to_remove++] = GRPC_ARG_CHANNEL_CREDENTIALS;
     97     args_to_add[num_args_to_add++] =
     98         grpc_channel_credentials_to_arg(creds_sans_call_creds);
     99   }
    100   grpc_channel_args* result = grpc_channel_args_copy_and_add_and_remove(
    101       args, args_to_remove, num_args_to_remove, args_to_add, num_args_to_add);
    102   // Clean up.
    103   grpc_channel_args_destroy(args);
    104   if (creds_sans_call_creds != nullptr) {
    105     grpc_channel_credentials_unref(creds_sans_call_creds);
    106   }
    107   return result;
    108 }
    109