3 * Copyright 2016 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>
21 #include "src/core/lib/security/credentials/plugin/plugin_credentials.h"
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>
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"
36 grpc_core::TraceFlag grpc_plugin_credentials_trace(false, "plugin_credentials");
38 grpc_plugin_credentials::~grpc_plugin_credentials() {
40 if (plugin_.state != nullptr && plugin_.destroy != nullptr) {
41 plugin_.destroy(plugin_.state);
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;
50 pending_request->prev->next = pending_request->next;
52 if (pending_request->next != nullptr) {
53 pending_request->next->prev = pending_request->prev;
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);
65 if (!r->cancelled) pending_request_remove_locked(r);
67 // Ref to credentials not needed anymore.
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) {
77 gpr_asprintf(&msg, "Getting metadata from plugin failed with error: %s",
79 error = GRPC_ERROR_CREATE_FROM_COPIED_STRING(msg);
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;
88 } else if (!grpc_is_binary_header_internal(md[i].key) &&
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;
97 if (seen_illegal_header) {
98 error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Illegal metadata");
100 for (size_t i = 0; i < num_md; ++i) {
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);
111 static void plugin_md_request_metadata_ready(void* request,
112 const grpc_metadata* 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)) {
124 "plugin_credentials[%p]: request %p: plugin returned "
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.
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)) {
137 "plugin_credentials[%p]: request %p: plugin was previously "
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.
158 if (pending_requests_ != nullptr) {
159 pending_requests_->prev = request;
161 request->next = pending_requests_;
162 pending_requests_ = request;
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",
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)) {
179 "plugin_credentials[%p]: request %p: plugin will return "
183 return false; // Asynchronous return.
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)) {
194 "plugin_credentials[%p]: request %p was cancelled, error "
195 "will be returned asynchronously",
200 if (GRPC_TRACE_FLAG_ENABLED(grpc_plugin_credentials_trace)) {
202 "plugin_credentials[%p]: request %p: plugin returned "
206 *error = process_plugin_result(request, creds_md, num_creds_md, status,
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);
214 gpr_free((void*)error_details);
220 void grpc_plugin_credentials::cancel_get_request_metadata(
221 grpc_credentials_mdelem_array* md_array, grpc_error* error) {
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,
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);
238 GRPC_ERROR_UNREF(error);
241 grpc_plugin_credentials::grpc_plugin_credentials(
242 grpc_metadata_credentials_plugin plugin)
243 : grpc_call_credentials(plugin.type), plugin_(plugin) {
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,
251 GPR_ASSERT(reserved == nullptr);
252 return grpc_core::New<grpc_plugin_credentials>(plugin);