3 * Copyright 2018 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 #include <grpc/support/string_util.h>
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"
31 static grpc_alts_credentials_options* alts_client_options_copy(
32 const grpc_alts_credentials_options* options);
34 static void alts_client_options_destroy(grpc_alts_credentials_options* options);
36 static target_service_account* target_service_account_create(
37 const char* service_account) {
38 if (service_account == nullptr) {
41 auto* sa = static_cast<target_service_account*>(
42 gpr_zalloc(sizeof(target_service_account)));
43 sa->data = gpr_strdup(service_account);
47 void 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) {
52 "Invalid nullptr arguments to "
53 "grpc_alts_credentials_client_options_add_target_service_account()");
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;
63 static void target_service_account_destroy(
64 target_service_account* service_account) {
65 if (service_account == nullptr) {
68 gpr_free(service_account->data);
69 gpr_free(service_account);
72 static const grpc_alts_credentials_options_vtable vtable = {
73 alts_client_options_copy, alts_client_options_destroy};
75 grpc_alts_credentials_options* grpc_alts_credentials_client_options_create(
77 auto client_options = static_cast<grpc_alts_credentials_client_options*>(
78 gpr_zalloc(sizeof(grpc_alts_credentials_client_options)));
79 client_options->base.vtable = &vtable;
80 return &client_options->base;
83 static grpc_alts_credentials_options* alts_client_options_copy(
84 const grpc_alts_credentials_options* options) {
85 if (options == nullptr) {
88 grpc_alts_credentials_options* new_options =
89 grpc_alts_credentials_client_options_create();
90 auto new_client_options =
91 reinterpret_cast<grpc_alts_credentials_client_options*>(new_options);
92 /* Copy target service accounts. */
93 target_service_account* prev = nullptr;
95 (reinterpret_cast<const grpc_alts_credentials_client_options*>(options))
96 ->target_account_list_head;
97 while (node != nullptr) {
98 target_service_account* new_node =
99 target_service_account_create(node->data);
100 if (prev == nullptr) {
101 new_client_options->target_account_list_head = new_node;
103 prev->next = new_node;
108 /* Copy rpc protocol versions. */
109 grpc_gcp_rpc_protocol_versions_copy(&options->rpc_versions,
110 &new_options->rpc_versions);
114 static void alts_client_options_destroy(
115 grpc_alts_credentials_options* options) {
116 if (options == nullptr) {
119 auto* client_options =
120 reinterpret_cast<grpc_alts_credentials_client_options*>(options);
121 target_service_account* node = client_options->target_account_list_head;
122 while (node != nullptr) {
123 target_service_account* next_node = node->next;
124 target_service_account_destroy(node);