Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / src / core / lib / security / credentials / plugin / plugin_credentials.cc
1 /*
2  *
3  * Copyright 2016 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/lib/security/credentials/plugin/plugin_credentials.h"
22
23 #include <string.h>
24
25 #include <grpc/grpc.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/string_util.h>
29 #include <grpc/support/sync.h>
30
31 #include "src/core/lib/slice/slice_internal.h"
32 #include "src/core/lib/slice/slice_string_helpers.h"
33 #include "src/core/lib/surface/api_trace.h"
34 #include "src/core/lib/surface/validate_metadata.h"
35
36 grpc_core::TraceFlag grpc_plugin_credentials_trace(false, "plugin_credentials");
37
38 grpc_plugin_credentials::~grpc_plugin_credentials() {
39   gpr_mu_destroy(&mu_);
40   if (plugin_.state != nullptr && plugin_.destroy != nullptr) {
41     plugin_.destroy(plugin_.state);
42   }
43 }
44
45 void grpc_plugin_credentials::pending_request_remove_locked(
46     pending_request* pending_request) {
47   if (pending_request->prev == nullptr) {
48     pending_requests_ = pending_request->next;
49   } else {
50     pending_request->prev->next = pending_request->next;
51   }
52   if (pending_request->next != nullptr) {
53     pending_request->next->prev = pending_request->prev;
54   }
55 }
56
57 // Checks if the request has been cancelled.
58 // If not, removes it from the pending list, so that it cannot be
59 // cancelled out from under us.
60 // When this returns, r->cancelled indicates whether the request was
61 // cancelled before completion.
62 void grpc_plugin_credentials::pending_request_complete(pending_request* r) {
63   GPR_DEBUG_ASSERT(r->creds == this);
64   gpr_mu_lock(&mu_);
65   if (!r->cancelled) pending_request_remove_locked(r);
66   gpr_mu_unlock(&mu_);
67   // Ref to credentials not needed anymore.
68   Unref();
69 }
70
71 static grpc_error* process_plugin_result(
72     grpc_plugin_credentials::pending_request* r, const grpc_metadata* md,
73     size_t num_md, grpc_status_code status, const char* error_details) {
74   grpc_error* error = GRPC_ERROR_NONE;
75   if (status != GRPC_STATUS_OK) {
76     char* msg;
77     gpr_asprintf(&msg, "Getting metadata from plugin failed with error: %s",
78                  error_details);
79     error = GRPC_ERROR_CREATE_FROM_COPIED_STRING(msg);
80     gpr_free(msg);
81   } else {
82     bool seen_illegal_header = false;
83     for (size_t i = 0; i < num_md; ++i) {
84       if (!GRPC_LOG_IF_ERROR("validate_metadata_from_plugin",
85                              grpc_validate_header_key_is_legal(md[i].key))) {
86         seen_illegal_header = true;
87         break;
88       } else if (!grpc_is_binary_header_internal(md[i].key) &&
89                  !GRPC_LOG_IF_ERROR(
90                      "validate_metadata_from_plugin",
91                      grpc_validate_header_nonbin_value_is_legal(md[i].value))) {
92         gpr_log(GPR_ERROR, "Plugin added invalid metadata value.");
93         seen_illegal_header = true;
94         break;
95       }
96     }
97     if (seen_illegal_header) {
98       error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Illegal metadata");
99     } else {
100       for (size_t i = 0; i < num_md; ++i) {
101         grpc_mdelem mdelem =
102             grpc_mdelem_create(md[i].key, md[i].value, nullptr);
103         grpc_credentials_mdelem_array_add(r->md_array, mdelem);
104         GRPC_MDELEM_UNREF(mdelem);
105       }
106     }
107   }
108   return error;
109 }
110
111 static void plugin_md_request_metadata_ready(void* request,
112                                              const grpc_metadata* md,
113                                              size_t num_md,
114                                              grpc_status_code status,
115                                              const char* error_details) {
116   /* called from application code */
117   grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
118   grpc_core::ExecCtx exec_ctx(GRPC_EXEC_CTX_FLAG_IS_FINISHED |
119                               GRPC_EXEC_CTX_FLAG_THREAD_RESOURCE_LOOP);
120   grpc_plugin_credentials::pending_request* r =
121       static_cast<grpc_plugin_credentials::pending_request*>(request);
122   if (GRPC_TRACE_FLAG_ENABLED(grpc_plugin_credentials_trace)) {
123     gpr_log(GPR_INFO,
124             "plugin_credentials[%p]: request %p: plugin returned "
125             "asynchronously",
126             r->creds, r);
127   }
128   // Remove request from pending list if not previously cancelled.
129   r->creds->pending_request_complete(r);
130   // If it has not been cancelled, process it.
131   if (!r->cancelled) {
132     grpc_error* error =
133         process_plugin_result(r, md, num_md, status, error_details);
134     GRPC_CLOSURE_SCHED(r->on_request_metadata, error);
135   } else if (GRPC_TRACE_FLAG_ENABLED(grpc_plugin_credentials_trace)) {
136     gpr_log(GPR_INFO,
137             "plugin_credentials[%p]: request %p: plugin was previously "
138             "cancelled",
139             r->creds, r);
140   }
141   gpr_free(r);
142 }
143
144 bool grpc_plugin_credentials::get_request_metadata(
145     grpc_polling_entity* pollent, grpc_auth_metadata_context context,
146     grpc_credentials_mdelem_array* md_array, grpc_closure* on_request_metadata,
147     grpc_error** error) {
148   bool retval = true;  // Synchronous return.
149   if (plugin_.get_metadata != nullptr) {
150     // Create pending_request object.
151     pending_request* request =
152         static_cast<pending_request*>(gpr_zalloc(sizeof(*request)));
153     request->creds = this;
154     request->md_array = md_array;
155     request->on_request_metadata = on_request_metadata;
156     // Add it to the pending list.
157     gpr_mu_lock(&mu_);
158     if (pending_requests_ != nullptr) {
159       pending_requests_->prev = request;
160     }
161     request->next = pending_requests_;
162     pending_requests_ = request;
163     gpr_mu_unlock(&mu_);
164     // Invoke the plugin.  The callback holds a ref to us.
165     if (GRPC_TRACE_FLAG_ENABLED(grpc_plugin_credentials_trace)) {
166       gpr_log(GPR_INFO, "plugin_credentials[%p]: request %p: invoking plugin",
167               this, request);
168     }
169     Ref().release();
170     grpc_metadata creds_md[GRPC_METADATA_CREDENTIALS_PLUGIN_SYNC_MAX];
171     size_t num_creds_md = 0;
172     grpc_status_code status = GRPC_STATUS_OK;
173     const char* error_details = nullptr;
174     if (!plugin_.get_metadata(
175             plugin_.state, context, plugin_md_request_metadata_ready, request,
176             creds_md, &num_creds_md, &status, &error_details)) {
177       if (GRPC_TRACE_FLAG_ENABLED(grpc_plugin_credentials_trace)) {
178         gpr_log(GPR_INFO,
179                 "plugin_credentials[%p]: request %p: plugin will return "
180                 "asynchronously",
181                 this, request);
182       }
183       return false;  // Asynchronous return.
184     }
185     // Returned synchronously.
186     // Remove request from pending list if not previously cancelled.
187     request->creds->pending_request_complete(request);
188     // If the request was cancelled, the error will have been returned
189     // asynchronously by plugin_cancel_get_request_metadata(), so return
190     // false.  Otherwise, process the result.
191     if (request->cancelled) {
192       if (GRPC_TRACE_FLAG_ENABLED(grpc_plugin_credentials_trace)) {
193         gpr_log(GPR_INFO,
194                 "plugin_credentials[%p]: request %p was cancelled, error "
195                 "will be returned asynchronously",
196                 this, request);
197       }
198       retval = false;
199     } else {
200       if (GRPC_TRACE_FLAG_ENABLED(grpc_plugin_credentials_trace)) {
201         gpr_log(GPR_INFO,
202                 "plugin_credentials[%p]: request %p: plugin returned "
203                 "synchronously",
204                 this, request);
205       }
206       *error = process_plugin_result(request, creds_md, num_creds_md, status,
207                                      error_details);
208     }
209     // Clean up.
210     for (size_t i = 0; i < num_creds_md; ++i) {
211       grpc_slice_unref_internal(creds_md[i].key);
212       grpc_slice_unref_internal(creds_md[i].value);
213     }
214     gpr_free((void*)error_details);
215     gpr_free(request);
216   }
217   return retval;
218 }
219
220 void grpc_plugin_credentials::cancel_get_request_metadata(
221     grpc_credentials_mdelem_array* md_array, grpc_error* error) {
222   gpr_mu_lock(&mu_);
223   for (pending_request* pending_request = pending_requests_;
224        pending_request != nullptr; pending_request = pending_request->next) {
225     if (pending_request->md_array == md_array) {
226       if (GRPC_TRACE_FLAG_ENABLED(grpc_plugin_credentials_trace)) {
227         gpr_log(GPR_INFO, "plugin_credentials[%p]: cancelling request %p", this,
228                 pending_request);
229       }
230       pending_request->cancelled = true;
231       GRPC_CLOSURE_SCHED(pending_request->on_request_metadata,
232                          GRPC_ERROR_REF(error));
233       pending_request_remove_locked(pending_request);
234       break;
235     }
236   }
237   gpr_mu_unlock(&mu_);
238   GRPC_ERROR_UNREF(error);
239 }
240
241 grpc_plugin_credentials::grpc_plugin_credentials(
242     grpc_metadata_credentials_plugin plugin)
243     : grpc_call_credentials(plugin.type), plugin_(plugin) {
244   gpr_mu_init(&mu_);
245 }
246
247 grpc_call_credentials* grpc_metadata_credentials_create_from_plugin(
248     grpc_metadata_credentials_plugin plugin, void* reserved) {
249   GRPC_API_TRACE("grpc_metadata_credentials_create_from_plugin(reserved=%p)", 1,
250                  (reserved));
251   GPR_ASSERT(reserved == nullptr);
252   return grpc_core::New<grpc_plugin_credentials>(plugin);
253 }