| 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 |
#ifndef GRPC_CORE_LIB_SECURITY_CREDENTIALS_PLUGIN_PLUGIN_CREDENTIALS_H |
| 20 |
#define GRPC_CORE_LIB_SECURITY_CREDENTIALS_PLUGIN_PLUGIN_CREDENTIALS_H |
| 21 |
|
| 22 |
#include <grpc/support/port_platform.h> |
| 23 |
|
| 24 |
#include <stddef.h> |
| 25 |
|
| 26 |
#include <atomic> |
| 27 |
#include <string> |
| 28 |
#include <utility> |
| 29 |
|
| 30 |
#include "absl/container/inlined_vector.h" |
| 31 |
#include "absl/status/statusor.h" |
| 32 |
|
| 33 |
#include <grpc/grpc_security.h> |
| 34 |
#include <grpc/grpc_security_constants.h> |
| 35 |
#include <grpc/impl/codegen/grpc_types.h> |
| 36 |
#include <grpc/status.h> |
| 37 |
|
| 38 |
#include "src/core/lib/debug/trace.h" |
| 39 |
#include "src/core/lib/gpr/useful.h" |
| 40 |
#include "src/core/lib/gprpp/ref_counted.h" |
| 41 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 42 |
#include "src/core/lib/gprpp/unique_type_name.h" |
| 43 |
#include "src/core/lib/promise/activity.h" |
| 44 |
#include "src/core/lib/promise/arena_promise.h" |
| 45 |
#include "src/core/lib/promise/poll.h" |
| 46 |
#include "src/core/lib/security/credentials/call_creds_util.h" |
| 47 |
#include "src/core/lib/security/credentials/credentials.h" |
| 48 |
#include "src/core/lib/slice/slice.h" |
| 49 |
#include "src/core/lib/transport/transport.h" |
| 50 |
|
| 51 |
extern grpc_core::TraceFlag grpc_plugin_credentials_trace; |
| 52 |
|
| 53 |
// This type is forward declared as a C struct and we cannot define it as a |
| 54 |
// class. Otherwise, compiler will complain about type mismatch due to |
| 55 |
// -Wmismatched-tags. |
| 56 |
struct grpc_plugin_credentials final : public grpc_call_credentials { |
| 57 |
public: |
| 58 |
explicit grpc_plugin_credentials(grpc_metadata_credentials_plugin plugin, |
| 59 |
grpc_security_level min_security_level); |
| 60 |
~grpc_plugin_credentials() override; |
| 61 |
|
| 62 |
grpc_core::ArenaPromise<absl::StatusOr<grpc_core::ClientMetadataHandle>> |
| 63 |
GetRequestMetadata(grpc_core::ClientMetadataHandle initial_metadata, |
| 64 |
const GetRequestMetadataArgs* args) override; |
| 65 |
|
| 66 |
std::string debug_string() override; |
| 67 |
|
| 68 |
grpc_core::UniqueTypeName type() const override; |
| 69 |
|
| 70 |
private: |
| 71 |
class PendingRequest : public grpc_core::RefCounted<PendingRequest> { |
| 72 |
public: |
| 73 |
PendingRequest(grpc_core::RefCountedPtr<grpc_plugin_credentials> creds, |
| 74 |
grpc_core::ClientMetadataHandle initial_metadata, |
| 75 |
const grpc_call_credentials::GetRequestMetadataArgs* args) |
| 76 |
: call_creds_(std::move(creds)), |
| 77 |
context_( |
| 78 |
grpc_core::MakePluginAuthMetadataContext(initial_metadata, args)), |
| 79 |
md_(std::move(initial_metadata)) {} |
| 80 |
|
| 81 |
~PendingRequest() override { |
| 82 |
grpc_auth_metadata_context_reset(&context_); |
| 83 |
for (size_t i = 0; i < metadata_.size(); i++) { |
| 84 |
grpc_core::CSliceUnref(metadata_[i].key); |
| 85 |
grpc_core::CSliceUnref(metadata_[i].value); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
absl::StatusOr<grpc_core::ClientMetadataHandle> ProcessPluginResult( |
| 90 |
const grpc_metadata* md, size_t num_md, grpc_status_code status, |
| 91 |
const char* error_details); |
| 92 |
|
| 93 |
grpc_core::Poll<absl::StatusOr<grpc_core::ClientMetadataHandle>> |
| 94 |
PollAsyncResult(); |
| 95 |
|
| 96 |
static void RequestMetadataReady(void* request, const grpc_metadata* md, |
| 97 |
size_t num_md, grpc_status_code status, |
| 98 |
const char* error_details); |
| 99 |
|
| 100 |
grpc_auth_metadata_context context() const { return context_; } |
| 101 |
grpc_plugin_credentials* creds() const { return call_creds_.get(); } |
| 102 |
|
| 103 |
private: |
| 104 |
std::atomic<bool> ready_{false}; |
| 105 |
grpc_core::Waker waker_{ |
| 106 |
grpc_core::Activity::current()->MakeNonOwningWaker()}; |
| 107 |
grpc_core::RefCountedPtr<grpc_plugin_credentials> call_creds_; |
| 108 |
grpc_auth_metadata_context context_; |
| 109 |
grpc_core::ClientMetadataHandle md_; |
| 110 |
// final status |
| 111 |
absl::InlinedVector<grpc_metadata, 2> metadata_; |
| 112 |
std::string error_details_; |
| 113 |
grpc_status_code status_; |
| 114 |
}; |
| 115 |
|
| 116 |
int cmp_impl(const grpc_call_credentials* other) const override { |
| 117 |
// TODO(yashykt): Check if we can do something better here |
| 118 |
return grpc_core::QsortCompare( |
| 119 |
static_cast<const grpc_call_credentials*>(this), other); |
| 120 |
} |
| 121 |
|
| 122 |
grpc_metadata_credentials_plugin plugin_; |
| 123 |
}; |
| 124 |
|
| 125 |
#endif /* GRPC_CORE_LIB_SECURITY_CREDENTIALS_PLUGIN_PLUGIN_CREDENTIALS_H */ |