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