|
17 | 17 | #include "Firestore/core/src/firebase/firestore/remote/grpc_connection.h"
|
18 | 18 |
|
19 | 19 | #include <algorithm>
|
| 20 | +#include <fstream> |
| 21 | +#include <sstream> |
20 | 22 | #include <string>
|
21 | 23 | #include <utility>
|
22 | 24 |
|
|
52 | 54 |
|
53 | 55 | } // namespace
|
54 | 56 |
|
| 57 | +GrpcConnection::TestCredentials* GrpcConnection::test_credentials_ = nullptr; |
| 58 | + |
55 | 59 | GrpcConnection::GrpcConnection(const DatabaseInfo& database_info,
|
56 | 60 | util::AsyncQueue* worker_queue,
|
57 | 61 | grpc::CompletionQueue* grpc_queue,
|
|
116 | 120 | }
|
117 | 121 |
|
118 | 122 | std::shared_ptr<grpc::Channel> GrpcConnection::CreateChannel() const {
|
119 |
| - return grpc::CreateChannel( |
120 |
| - database_info_->host(), |
121 |
| - grpc::SslCredentials(grpc::SslCredentialsOptions())); |
| 123 | + if (!test_credentials_) { |
| 124 | + return grpc::CreateChannel( |
| 125 | + database_info_->host(), |
| 126 | + grpc::SslCredentials(grpc::SslCredentialsOptions())); |
| 127 | + } |
| 128 | + |
| 129 | + if (test_credentials_->use_insecure_channel) { |
| 130 | + return grpc::CreateChannel(database_info_->host(), |
| 131 | + grpc::InsecureChannelCredentials()); |
| 132 | + } |
| 133 | + |
| 134 | + std::ifstream cert_file{test_credentials_->certificate_path}; |
| 135 | + HARD_ASSERT(cert_file.good(), |
| 136 | + StringFormat("Unable to open root certificates at file path %s", |
| 137 | + test_credentials_->certificate_path) |
| 138 | + .c_str()); |
| 139 | + std::stringstream cert_buffer; |
| 140 | + cert_buffer << cert_file.rdbuf(); |
| 141 | + grpc::SslCredentialsOptions options; |
| 142 | + options.pem_root_certs = cert_buffer.str(); |
| 143 | + |
| 144 | + grpc::ChannelArguments args; |
| 145 | + args.SetSslTargetNameOverride(test_credentials_->target_name); |
| 146 | + return grpc::CreateCustomChannel(database_info_->host(), |
| 147 | + grpc::SslCredentials(options), args); |
122 | 148 | }
|
123 | 149 |
|
124 | 150 | std::unique_ptr<GrpcStream> GrpcConnection::CreateStream(
|
|
183 | 209 | active_calls_.erase(found);
|
184 | 210 | }
|
185 | 211 |
|
| 212 | +/*static*/ void GrpcConnection::UseTestCertificate( |
| 213 | + absl::string_view certificate_path, absl::string_view target_name) { |
| 214 | + HARD_ASSERT(!certificate_path.empty(), "Empty path to test certificate"); |
| 215 | + HARD_ASSERT(!target_name.empty(), "Empty SSL target name"); |
| 216 | + |
| 217 | + if (!test_credentials_) { |
| 218 | + // Deliberately never deleted. |
| 219 | + test_credentials_ = new TestCredentials{}; |
| 220 | + } |
| 221 | + |
| 222 | + test_credentials_->certificate_path = |
| 223 | + std::string{certificate_path.data(), certificate_path.size()}; |
| 224 | + test_credentials_->target_name = |
| 225 | + std::string{target_name.data(), target_name.size()}; |
| 226 | + // TODO(varconst): hostname if necessary. |
| 227 | +} |
| 228 | + |
| 229 | +/*static*/ void GrpcConnection::UseInsecureChannel() { |
| 230 | + if (!test_credentials_) { |
| 231 | + // Deliberately never deleted. |
| 232 | + test_credentials_ = new TestCredentials{}; |
| 233 | + } |
| 234 | + |
| 235 | + test_credentials_->use_insecure_channel = true; |
| 236 | + // TODO(varconst): hostname if necessary. |
| 237 | +} |
| 238 | + |
186 | 239 | } // namespace remote
|
187 | 240 | } // namespace firestore
|
188 | 241 | } // namespace firebase
|
0 commit comments