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 <stdlib.h> 22 #include <string.h> 23 24 #include <grpc/support/alloc.h> 25 #include <grpc/support/log.h> 26 #include <grpc/support/string_util.h> 27 28 #include "src/core/lib/security/credentials/alts/grpc_alts_credentials_options.h" 29 #include "src/core/tsi/alts/handshaker/transport_security_common_api.h" 30 31 static grpc_alts_credentials_options* alts_client_options_copy( 32 const grpc_alts_credentials_options* options); 33 34 static void alts_client_options_destroy(grpc_alts_credentials_options* options); 35 target_service_account_create(const char * service_account)36static target_service_account* target_service_account_create( 37 const char* service_account) { 38 if (service_account == nullptr) { 39 return nullptr; 40 } 41 auto* sa = static_cast<target_service_account*>( 42 gpr_zalloc(sizeof(target_service_account))); 43 sa->data = gpr_strdup(service_account); 44 return sa; 45 } 46 grpc_alts_credentials_client_options_add_target_service_account(grpc_alts_credentials_options * options,const char * service_account)47void grpc_alts_credentials_client_options_add_target_service_account( 48 grpc_alts_credentials_options* options, const char* service_account) { 49 if (options == nullptr || service_account == nullptr) { 50 gpr_log( 51 GPR_ERROR, 52 "Invalid nullptr arguments to " 53 "grpc_alts_credentials_client_options_add_target_service_account()"); 54 return; 55 } 56 auto client_options = 57 reinterpret_cast<grpc_alts_credentials_client_options*>(options); 58 target_service_account* node = target_service_account_create(service_account); 59 node->next = client_options->target_account_list_head; 60 client_options->target_account_list_head = node; 61 } 62 target_service_account_destroy(target_service_account * service_account)63static void target_service_account_destroy( 64 target_service_account* service_account) { 65 if (service_account == nullptr) { 66 return; 67 } 68 gpr_free(service_account->data); 69 gpr_free(service_account); 70 } 71 72 static const grpc_alts_credentials_options_vtable vtable = { 73 alts_client_options_copy, alts_client_options_destroy}; 74 grpc_alts_credentials_client_options_create()75grpc_alts_credentials_options* grpc_alts_credentials_client_options_create() { 76 auto client_options = static_cast<grpc_alts_credentials_client_options*>( 77 gpr_zalloc(sizeof(grpc_alts_credentials_client_options))); 78 client_options->base.vtable = &vtable; 79 return &client_options->base; 80 } 81 alts_client_options_copy(const grpc_alts_credentials_options * options)82static grpc_alts_credentials_options* alts_client_options_copy( 83 const grpc_alts_credentials_options* options) { 84 if (options == nullptr) { 85 return nullptr; 86 } 87 grpc_alts_credentials_options* new_options = 88 grpc_alts_credentials_client_options_create(); 89 auto new_client_options = 90 reinterpret_cast<grpc_alts_credentials_client_options*>(new_options); 91 /* Copy target service accounts. */ 92 target_service_account* prev = nullptr; 93 auto node = 94 (reinterpret_cast<const grpc_alts_credentials_client_options*>(options)) 95 ->target_account_list_head; 96 while (node != nullptr) { 97 target_service_account* new_node = 98 target_service_account_create(node->data); 99 if (prev == nullptr) { 100 new_client_options->target_account_list_head = new_node; 101 } else { 102 prev->next = new_node; 103 } 104 prev = new_node; 105 node = node->next; 106 } 107 /* Copy rpc protocol versions. */ 108 grpc_gcp_rpc_protocol_versions_copy(&options->rpc_versions, 109 &new_options->rpc_versions); 110 return new_options; 111 } 112 alts_client_options_destroy(grpc_alts_credentials_options * options)113static void alts_client_options_destroy( 114 grpc_alts_credentials_options* options) { 115 if (options == nullptr) { 116 return; 117 } 118 auto* client_options = 119 reinterpret_cast<grpc_alts_credentials_client_options*>(options); 120 target_service_account* node = client_options->target_account_list_head; 121 while (node != nullptr) { 122 target_service_account* next_node = node->next; 123 target_service_account_destroy(node); 124 node = next_node; 125 } 126 } 127