| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 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_INTERNAL_CPP_CLIENT_SECURE_CREDENTIALS_H |
| 20 |
#define GRPC_INTERNAL_CPP_CLIENT_SECURE_CREDENTIALS_H |
| 21 |
|
| 22 |
#include <stddef.h> |
| 23 |
|
| 24 |
#include <memory> |
| 25 |
#include <string> |
| 26 |
#include <vector> |
| 27 |
|
| 28 |
#include "absl/strings/str_cat.h" |
| 29 |
|
| 30 |
#include <grpc/grpc.h> |
| 31 |
#include <grpc/grpc_security.h> |
| 32 |
#include <grpc/impl/codegen/grpc_types.h> |
| 33 |
#include <grpc/status.h> |
| 34 |
#include <grpcpp/channel.h> |
| 35 |
#include <grpcpp/impl/grpc_library.h> |
| 36 |
#include <grpcpp/security/credentials.h> |
| 37 |
#include <grpcpp/support/channel_arguments.h> |
| 38 |
#include <grpcpp/support/client_interceptor.h> |
| 39 |
// TODO(yashykt): We shouldn't be including "src/core" headers. |
| 40 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 41 |
#include "src/core/lib/security/credentials/credentials.h" |
| 42 |
#include "src/cpp/server/thread_pool_interface.h" |
| 43 |
|
| 44 |
namespace grpc { |
| 45 |
|
| 46 |
class Channel; |
| 47 |
|
| 48 |
class SecureChannelCredentials final : public ChannelCredentials { |
| 49 |
public: |
| 50 |
explicit SecureChannelCredentials(grpc_channel_credentials* c_creds); |
| 51 |
~SecureChannelCredentials() override { |
| 52 |
grpc_core::ExecCtx exec_ctx; |
| 53 |
if (c_creds_ != nullptr) c_creds_->Unref(); |
| 54 |
} |
| 55 |
grpc_channel_credentials* GetRawCreds() { return c_creds_; } |
| 56 |
|
| 57 |
std::shared_ptr<Channel> CreateChannelImpl( |
| 58 |
const std::string& target, const ChannelArguments& args) override; |
| 59 |
|
| 60 |
SecureChannelCredentials* AsSecureCredentials() override { return this; } |
| 61 |
|
| 62 |
private: |
| 63 |
std::shared_ptr<Channel> CreateChannelWithInterceptors( |
| 64 |
const std::string& target, const ChannelArguments& args, |
| 65 |
std::vector<std::unique_ptr< |
| 66 |
grpc::experimental::ClientInterceptorFactoryInterface>> |
| 67 |
interceptor_creators) override; |
| 68 |
grpc_channel_credentials* const c_creds_; |
| 69 |
}; |
| 70 |
|
| 71 |
class SecureCallCredentials final : public CallCredentials { |
| 72 |
public: |
| 73 |
explicit SecureCallCredentials(grpc_call_credentials* c_creds); |
| 74 |
~SecureCallCredentials() override { |
| 75 |
grpc_core::ExecCtx exec_ctx; |
| 76 |
if (c_creds_ != nullptr) c_creds_->Unref(); |
| 77 |
} |
| 78 |
grpc_call_credentials* GetRawCreds() { return c_creds_; } |
| 79 |
|
| 80 |
bool ApplyToCall(grpc_call* call) override; |
| 81 |
SecureCallCredentials* AsSecureCredentials() override { return this; } |
| 82 |
std::string DebugString() override { |
| 83 |
return absl::StrCat("SecureCallCredentials{", |
| 84 |
std::string(c_creds_->debug_string()), "}"); |
| 85 |
} |
| 86 |
|
| 87 |
private: |
| 88 |
grpc_call_credentials* const c_creds_; |
| 89 |
}; |
| 90 |
|
| 91 |
namespace internal { |
| 92 |
|
| 93 |
std::shared_ptr<ChannelCredentials> WrapChannelCredentials( |
| 94 |
grpc_channel_credentials* creds); |
| 95 |
|
| 96 |
} // namespace internal |
| 97 |
|
| 98 |
namespace experimental { |
| 99 |
|
| 100 |
// Transforms C++ STS Credentials options to core options. The pointers of the |
| 101 |
// resulting core options point to the memory held by the C++ options so C++ |
| 102 |
// options need to be kept alive until after the core credentials creation. |
| 103 |
grpc_sts_credentials_options StsCredentialsCppToCoreOptions( |
| 104 |
const StsCredentialsOptions& options); |
| 105 |
|
| 106 |
} // namespace experimental |
| 107 |
|
| 108 |
class MetadataCredentialsPluginWrapper final : private GrpcLibraryCodegen { |
| 109 |
public: |
| 110 |
static void Destroy(void* wrapper); |
| 111 |
static int GetMetadata( |
| 112 |
void* wrapper, grpc_auth_metadata_context context, |
| 113 |
grpc_credentials_plugin_metadata_cb cb, void* user_data, |
| 114 |
grpc_metadata creds_md[GRPC_METADATA_CREDENTIALS_PLUGIN_SYNC_MAX], |
| 115 |
size_t* num_creds_md, grpc_status_code* status, |
| 116 |
const char** error_details); |
| 117 |
static char* DebugString(void* wrapper); |
| 118 |
|
| 119 |
explicit MetadataCredentialsPluginWrapper( |
| 120 |
std::unique_ptr<MetadataCredentialsPlugin> plugin); |
| 121 |
|
| 122 |
private: |
| 123 |
void InvokePlugin( |
| 124 |
grpc_auth_metadata_context context, |
| 125 |
grpc_credentials_plugin_metadata_cb cb, void* user_data, |
| 126 |
grpc_metadata creds_md[GRPC_METADATA_CREDENTIALS_PLUGIN_SYNC_MAX], |
| 127 |
size_t* num_creds_md, grpc_status_code* status_code, |
| 128 |
const char** error_details); |
| 129 |
std::unique_ptr<ThreadPoolInterface> thread_pool_; |
| 130 |
std::unique_ptr<MetadataCredentialsPlugin> plugin_; |
| 131 |
}; |
| 132 |
|
| 133 |
} // namespace grpc |
| 134 |
|
| 135 |
#endif // GRPC_INTERNAL_CPP_CLIENT_SECURE_CREDENTIALS_H |