| 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 <atomic> |
| 24 |
#include <memory> |
| 25 |
|
| 26 |
#include "absl/status/status.h" |
| 27 |
#include "absl/strings/str_cat.h" |
| 28 |
#include "absl/strings/string_view.h" |
| 29 |
|
| 30 |
#include <grpc/slice.h> |
| 31 |
#include <grpc/support/alloc.h> |
| 32 |
#include <grpc/support/log.h> |
| 33 |
|
| 34 |
#include "src/core/lib/iomgr/error.h" |
| 35 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 36 |
#include "src/core/lib/promise/promise.h" |
| 37 |
#include "src/core/lib/slice/slice.h" |
| 38 |
#include "src/core/lib/slice/slice_internal.h" |
| 39 |
#include "src/core/lib/surface/api_trace.h" |
| 40 |
#include "src/core/lib/surface/validate_metadata.h" |
| 41 |
#include "src/core/lib/transport/metadata_batch.h" |
| 42 |
|
| 43 |
grpc_core::TraceFlag grpc_plugin_credentials_trace(false, "plugin_credentials"); |
| 44 |
|
| 45 |
grpc_plugin_credentials::~grpc_plugin_credentials() { |
| 46 |
if (plugin_.state != nullptr && plugin_.destroy != nullptr) { |
| 47 |
plugin_.destroy(plugin_.state); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
std::string grpc_plugin_credentials::debug_string() { |
| 52 |
char* debug_c_str = nullptr; |
| 53 |
if (plugin_.debug_string != nullptr) { |
| 54 |
debug_c_str = plugin_.debug_string(plugin_.state); |
| 55 |
} |
| 56 |
std::string debug_str( |
| 57 |
debug_c_str != nullptr |
| 58 |
? debug_c_str |
| 59 |
: "grpc_plugin_credentials did not provide a debug string"); |
| 60 |
gpr_free(debug_c_str); |
| 61 |
return debug_str; |
| 62 |
} |
| 63 |
|
| 64 |
grpc_core::UniqueTypeName grpc_plugin_credentials::type() const { |
| 65 |
static grpc_core::UniqueTypeName::Factory kFactory("Plugin"); |
| 66 |
return kFactory.Create(); |
| 67 |
} |
| 68 |
|
| 69 |
absl::StatusOr<grpc_core::ClientMetadataHandle> |
| 70 |
grpc_plugin_credentials::PendingRequest::ProcessPluginResult( |
| 71 |
const grpc_metadata* md, size_t num_md, grpc_status_code status, |
| 72 |
const char* error_details) { |
| 73 |
if (status != GRPC_STATUS_OK) { |
| 74 |
return absl::UnavailableError(absl::StrCat( |
| 75 |
"Getting metadata from plugin failed with error: ", error_details)); |
| 76 |
} else { |
| 77 |
bool seen_illegal_header = false; |
| 78 |
for (size_t i = 0; i < num_md; ++i) { |
| 79 |
if (!GRPC_LOG_IF_ERROR("validate_metadata_from_plugin", |
| 80 |
grpc_validate_header_key_is_legal(md[i].key))) { |
| 81 |
seen_illegal_header = true; |
| 82 |
break; |
| 83 |
} else if (!grpc_is_binary_header_internal(md[i].key) && |
| 84 |
!GRPC_LOG_IF_ERROR( |
| 85 |
"validate_metadata_from_plugin", |
| 86 |
grpc_validate_header_nonbin_value_is_legal(md[i].value))) { |
| 87 |
gpr_log(GPR_ERROR, "Plugin added invalid metadata value."); |
| 88 |
seen_illegal_header = true; |
| 89 |
break; |
| 90 |
} |
| 91 |
} |
| 92 |
if (seen_illegal_header) { |
| 93 |
return absl::UnavailableError("Illegal metadata"); |
| 94 |
} else { |
| 95 |
absl::Status error; |
| 96 |
for (size_t i = 0; i < num_md; ++i) { |
| 97 |
md_->Append( |
| 98 |
grpc_core::StringViewFromSlice(md[i].key), |
| 99 |
grpc_core::Slice(grpc_core::CSliceRef(md[i].value)), |
| 100 |
[&error](absl::string_view message, const grpc_core::Slice&) { |
| 101 |
error = absl::UnavailableError(message); |
| 102 |
}); |
| 103 |
} |
| 104 |
if (!error.ok()) return std::move(error); |
| 105 |
return grpc_core::ClientMetadataHandle(std::move(md_)); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
grpc_core::Poll<absl::StatusOr<grpc_core::ClientMetadataHandle>> |
| 111 |
grpc_plugin_credentials::PendingRequest::PollAsyncResult() { |
| 112 |
if (!ready_.load(std::memory_order_acquire)) { |
| 113 |
return grpc_core::Pending{}; |
| 114 |
} |
| 115 |
return ProcessPluginResult(metadata_.data(), metadata_.size(), status_, |
| 116 |
error_details_.c_str()); |
| 117 |
} |
| 118 |
|
| 119 |
void grpc_plugin_credentials::PendingRequest::RequestMetadataReady( |
| 120 |
void* request, const grpc_metadata* md, size_t num_md, |
| 121 |
grpc_status_code status, const char* error_details) { |
| 122 |
/* called from application code */ |
| 123 |
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
| 124 |
grpc_core::ExecCtx exec_ctx(GRPC_EXEC_CTX_FLAG_IS_FINISHED | |
| 125 |
GRPC_EXEC_CTX_FLAG_THREAD_RESOURCE_LOOP); |
| 126 |
grpc_core::RefCountedPtr<grpc_plugin_credentials::PendingRequest> r( |
| 127 |
static_cast<grpc_plugin_credentials::PendingRequest*>(request)); |
| 128 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_plugin_credentials_trace)) { |
| 129 |
gpr_log(GPR_INFO, |
| 130 |
"plugin_credentials[%p]: request %p: plugin returned " |
| 131 |
"asynchronously", |
| 132 |
r->creds(), r.get()); |
| 133 |
} |
| 134 |
for (size_t i = 0; i < num_md; ++i) { |
| 135 |
grpc_metadata p; |
| 136 |
p.key = grpc_core::CSliceRef(md[i].key); |
| 137 |
p.value = grpc_core::CSliceRef(md[i].value); |
| 138 |
r->metadata_.push_back(p); |
| 139 |
} |
| 140 |
r->error_details_ = error_details == nullptr ? "" : error_details; |
| 141 |
r->status_ = status; |
| 142 |
r->ready_.store(true, std::memory_order_release); |
| 143 |
r->waker_.Wakeup(); |
| 144 |
} |
| 145 |
|
| 146 |
grpc_core::ArenaPromise<absl::StatusOr<grpc_core::ClientMetadataHandle>> |
| 147 |
grpc_plugin_credentials::GetRequestMetadata( |
| 148 |
grpc_core::ClientMetadataHandle initial_metadata, |
| 149 |
const grpc_call_credentials::GetRequestMetadataArgs* args) { |
| 150 |
if (plugin_.get_metadata == nullptr) { |
| 151 |
return grpc_core::Immediate(std::move(initial_metadata)); |
| 152 |
} |
| 153 |
|
| 154 |
// Create pending_request object. |
| 155 |
auto request = grpc_core::MakeRefCounted<PendingRequest>( |
| 156 |
Ref(), std::move(initial_metadata), args); |
| 157 |
// Invoke the plugin. The callback holds a ref to us. |
| 158 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_plugin_credentials_trace)) { |
| 159 |
gpr_log(GPR_INFO, "plugin_credentials[%p]: request %p: invoking plugin", |
| 160 |
this, request.get()); |
| 161 |
} |
| 162 |
grpc_metadata creds_md[GRPC_METADATA_CREDENTIALS_PLUGIN_SYNC_MAX]; |
| 163 |
size_t num_creds_md = 0; |
| 164 |
grpc_status_code status = GRPC_STATUS_OK; |
| 165 |
const char* error_details = nullptr; |
| 166 |
// Add an extra ref to the request object for the async callback. |
| 167 |
// If the request completes synchronously, we'll drop this later. |
| 168 |
// If the request completes asynchronously, it will own a ref to the request |
| 169 |
// object (which we release from our ownership below). |
| 170 |
auto child_request = request->Ref(); |
| 171 |
if (!plugin_.get_metadata(plugin_.state, request->context(), |
| 172 |
PendingRequest::RequestMetadataReady, |
| 173 |
child_request.get(), creds_md, &num_creds_md, |
| 174 |
&status, &error_details)) { |
| 175 |
child_request.release(); |
| 176 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_plugin_credentials_trace)) { |
| 177 |
gpr_log(GPR_INFO, |
| 178 |
"plugin_credentials[%p]: request %p: plugin will return " |
| 179 |
"asynchronously", |
| 180 |
this, request.get()); |
| 181 |
} |
| 182 |
return [request] { return request->PollAsyncResult(); }; |
| 183 |
} |
| 184 |
// Synchronous return. |
| 185 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_plugin_credentials_trace)) { |
| 186 |
gpr_log(GPR_INFO, |
| 187 |
"plugin_credentials[%p]: request %p: plugin returned " |
| 188 |
"synchronously", |
| 189 |
this, request.get()); |
| 190 |
} |
| 191 |
auto result = request->ProcessPluginResult(creds_md, num_creds_md, status, |
| 192 |
error_details); |
| 193 |
// Clean up. |
| 194 |
for (size_t i = 0; i < num_creds_md; ++i) { |
| 195 |
grpc_core::CSliceUnref(creds_md[i].key); |
| 196 |
grpc_core::CSliceUnref(creds_md[i].value); |
| 197 |
} |
| 198 |
gpr_free(const_cast<char*>(error_details)); |
| 199 |
|
| 200 |
return grpc_core::Immediate(std::move(result)); |
| 201 |
} |
| 202 |
|
| 203 |
grpc_plugin_credentials::grpc_plugin_credentials( |
| 204 |
grpc_metadata_credentials_plugin plugin, |
| 205 |
grpc_security_level min_security_level) |
| 206 |
: grpc_call_credentials(min_security_level), plugin_(plugin) {} |
| 207 |
|
| 208 |
grpc_call_credentials* grpc_metadata_credentials_create_from_plugin( |
| 209 |
grpc_metadata_credentials_plugin plugin, |
| 210 |
grpc_security_level min_security_level, void* reserved) { |
| 211 |
GRPC_API_TRACE("grpc_metadata_credentials_create_from_plugin(reserved=%p)", 1, |
| 212 |
(reserved)); |
| 213 |
GPR_ASSERT(reserved == nullptr); |
| 214 |
return new grpc_plugin_credentials(plugin, min_security_level); |
| 215 |
} |