| 1 |
// |
| 2 |
// Copyright 2020 gRPC authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
// |
| 16 |
|
| 17 |
#include "test/core/util/tls_utils.h" |
| 18 |
|
| 19 |
#include <stdio.h> |
| 20 |
|
| 21 |
#include <grpc/slice.h> |
| 22 |
#include <grpc/support/alloc.h> |
| 23 |
#include <grpc/support/log.h> |
| 24 |
#include <grpc/support/string_util.h> |
| 25 |
#include <grpc/support/time.h> |
| 26 |
|
| 27 |
#include "src/core/lib/gpr/tmpfile.h" |
| 28 |
#include "src/core/lib/iomgr/error.h" |
| 29 |
#include "src/core/lib/iomgr/load_file.h" |
| 30 |
#include "src/core/lib/slice/slice_internal.h" |
| 31 |
#include "test/core/util/test_config.h" |
| 32 |
|
| 33 |
namespace grpc_core { |
| 34 |
|
| 35 |
namespace testing { |
| 36 |
|
| 37 |
TmpFile::TmpFile(absl::string_view data) { |
| 38 |
name_ = CreateTmpFileAndWriteData(data); |
| 39 |
GPR_ASSERT(!name_.empty()); |
| 40 |
} |
| 41 |
|
| 42 |
TmpFile::~TmpFile() { GPR_ASSERT(remove(name_.c_str()) == 0); } |
| 43 |
|
| 44 |
void TmpFile::RewriteFile(absl::string_view data) { |
| 45 |
// Create a new file containing new data. |
| 46 |
std::string new_name = CreateTmpFileAndWriteData(data); |
| 47 |
GPR_ASSERT(!new_name.empty()); |
| 48 |
#ifdef GPR_WINDOWS |
| 49 |
// Remove the old file. |
| 50 |
// On Windows rename requires that the new name not exist, whereas |
| 51 |
// on posix systems rename does an atomic replacement of the new |
| 52 |
// name. |
| 53 |
GPR_ASSERT(remove(name_.c_str()) == 0); |
| 54 |
#endif |
| 55 |
// Rename the new file to the original name. |
| 56 |
GPR_ASSERT(rename(new_name.c_str(), name_.c_str()) == 0); |
| 57 |
} |
| 58 |
|
| 59 |
std::string TmpFile::CreateTmpFileAndWriteData(absl::string_view data) { |
| 60 |
char* name = nullptr; |
| 61 |
FILE* file_descriptor = gpr_tmpfile("test", &name); |
| 62 |
GPR_ASSERT(fwrite(data.data(), 1, data.size(), file_descriptor) == |
| 63 |
data.size()); |
| 64 |
GPR_ASSERT(fclose(file_descriptor) == 0); |
| 65 |
GPR_ASSERT(file_descriptor != nullptr); |
| 66 |
GPR_ASSERT(name != nullptr); |
| 67 |
std::string name_to_return = name; |
| 68 |
gpr_free(name); |
| 69 |
return name_to_return; |
| 70 |
} |
| 71 |
|
| 72 |
PemKeyCertPairList MakeCertKeyPairs(absl::string_view private_key, |
| 73 |
absl::string_view certs) { |
| 74 |
if (private_key.empty() && certs.empty()) { |
| 75 |
return {}; |
| 76 |
} |
| 77 |
return PemKeyCertPairList{PemKeyCertPair(private_key, certs)}; |
| 78 |
} |
| 79 |
|
| 80 |
std::string GetFileContents(const char* path) { |
| 81 |
grpc_slice slice = grpc_empty_slice(); |
| 82 |
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file", grpc_load_file(path, 0, &slice))); |
| 83 |
std::string data = std::string(StringViewFromSlice(slice)); |
| 84 |
grpc_slice_unref(slice); |
| 85 |
return data; |
| 86 |
} |
| 87 |
|
| 88 |
int SyncExternalVerifier::Verify(void* user_data, |
| 89 |
grpc_tls_custom_verification_check_request*, |
| 90 |
grpc_tls_on_custom_verification_check_done_cb, |
| 91 |
void*, grpc_status_code* sync_status, |
| 92 |
char** sync_error_details) { |
| 93 |
auto* self = static_cast<SyncExternalVerifier*>(user_data); |
| 94 |
if (self->success_) { |
| 95 |
*sync_status = GRPC_STATUS_OK; |
| 96 |
return true; // Synchronous call |
| 97 |
} |
| 98 |
*sync_status = GRPC_STATUS_UNAUTHENTICATED; |
| 99 |
*sync_error_details = gpr_strdup("SyncExternalVerifier failed"); |
| 100 |
return true; // Synchronous call |
| 101 |
} |
| 102 |
|
| 103 |
void SyncExternalVerifier::Destruct(void* user_data) { |
| 104 |
auto* self = static_cast<SyncExternalVerifier*>(user_data); |
| 105 |
delete self; |
| 106 |
} |
| 107 |
|
| 108 |
AsyncExternalVerifier::~AsyncExternalVerifier() { |
| 109 |
// Tell the thread to shut down. |
| 110 |
{ |
| 111 |
MutexLock lock(&mu_); |
| 112 |
queue_.push_back(Request{nullptr, nullptr, nullptr, true}); |
| 113 |
} |
| 114 |
// Wait for thread to exit. |
| 115 |
thread_.Join(); |
| 116 |
grpc_shutdown(); |
| 117 |
} |
| 118 |
|
| 119 |
int AsyncExternalVerifier::Verify( |
| 120 |
void* user_data, grpc_tls_custom_verification_check_request* request, |
| 121 |
grpc_tls_on_custom_verification_check_done_cb callback, void* callback_arg, |
| 122 |
grpc_status_code*, char**) { |
| 123 |
auto* self = static_cast<AsyncExternalVerifier*>(user_data); |
| 124 |
// Add request to queue to be picked up by worker thread. |
| 125 |
MutexLock lock(&self->mu_); |
| 126 |
self->queue_.push_back(Request{request, callback, callback_arg, false}); |
| 127 |
return false; // Asynchronous call |
| 128 |
} |
| 129 |
|
| 130 |
namespace { |
| 131 |
|
| 132 |
void DestroyExternalVerifier(void* arg) { |
| 133 |
auto* verifier = static_cast<AsyncExternalVerifier*>(arg); |
| 134 |
delete verifier; |
| 135 |
} |
| 136 |
|
| 137 |
} // namespace |
| 138 |
|
| 139 |
void AsyncExternalVerifier::Destruct(void* user_data) { |
| 140 |
auto* self = static_cast<AsyncExternalVerifier*>(user_data); |
| 141 |
// Spawn a detached thread to destroy the verifier, to make sure that we don't |
| 142 |
// try to join the worker thread from within the worker thread. |
| 143 |
Thread destroy_thread("DestroyExternalVerifier", DestroyExternalVerifier, |
| 144 |
self, nullptr, Thread::Options().set_joinable(false)); |
| 145 |
destroy_thread.Start(); |
| 146 |
} |
| 147 |
|
| 148 |
void AsyncExternalVerifier::WorkerThread(void* arg) { |
| 149 |
auto* self = static_cast<AsyncExternalVerifier*>(arg); |
| 150 |
while (true) { |
| 151 |
// Check queue for work. |
| 152 |
bool got_request = false; |
| 153 |
Request request; |
| 154 |
{ |
| 155 |
MutexLock lock(&self->mu_); |
| 156 |
if (!self->queue_.empty()) { |
| 157 |
got_request = true; |
| 158 |
request = self->queue_.front(); |
| 159 |
self->queue_.pop_front(); |
| 160 |
} |
| 161 |
} |
| 162 |
// If nothing found in the queue, sleep for a bit and try again. |
| 163 |
if (!got_request) { |
| 164 |
gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100)); |
| 165 |
continue; |
| 166 |
} |
| 167 |
// If we're being told to shut down, return. |
| 168 |
if (request.shutdown) { |
| 169 |
return; |
| 170 |
} |
| 171 |
// Process the request. |
| 172 |
if (self->success_) { |
| 173 |
request.callback(request.request, request.callback_arg, GRPC_STATUS_OK, |
| 174 |
""); |
| 175 |
} else { |
| 176 |
request.callback(request.request, request.callback_arg, |
| 177 |
GRPC_STATUS_UNAUTHENTICATED, |
| 178 |
"AsyncExternalVerifier failed"); |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
} // namespace testing |
| 184 |
|
| 185 |
} // namespace grpc_core |