Skip to content

Commit 805b673

Browse files
committed
gRPC: add support for SSL disabled to GrpcConnection.
Unfortunately, there currently is no way to verify this change actually works.
1 parent e219c93 commit 805b673

File tree

3 files changed

+53
-82
lines changed

3 files changed

+53
-82
lines changed

Firestore/Example/Firestore.xcodeproj/project.pbxproj

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@
8181
5492E07A202154D600B64F25 /* FIRTypeTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5492E071202154D600B64F25 /* FIRTypeTests.mm */; };
8282
5492E07F202154EC00B64F25 /* FSTTransactionTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5492E07B202154EB00B64F25 /* FSTTransactionTests.mm */; };
8383
5492E080202154EC00B64F25 /* FSTSmokeTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5492E07C202154EB00B64F25 /* FSTSmokeTests.mm */; };
84-
5492E081202154EC00B64F25 /* FSTStreamTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5492E07D202154EB00B64F25 /* FSTStreamTests.mm */; };
8584
5492E082202154EC00B64F25 /* FSTDatastoreTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5492E07E202154EC00B64F25 /* FSTDatastoreTests.mm */; };
8685
5492E09D2021552D00B64F25 /* FSTLocalStoreTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5492E0832021552A00B64F25 /* FSTLocalStoreTests.mm */; };
8786
5492E09F2021552D00B64F25 /* FSTLevelDBMigrationsTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5492E0862021552A00B64F25 /* FSTLevelDBMigrationsTests.mm */; };
@@ -360,7 +359,6 @@
360359
5492E071202154D600B64F25 /* FIRTypeTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = FIRTypeTests.mm; sourceTree = "<group>"; };
361360
5492E07B202154EB00B64F25 /* FSTTransactionTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = FSTTransactionTests.mm; sourceTree = "<group>"; };
362361
5492E07C202154EB00B64F25 /* FSTSmokeTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = FSTSmokeTests.mm; sourceTree = "<group>"; };
363-
5492E07D202154EB00B64F25 /* FSTStreamTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = FSTStreamTests.mm; sourceTree = "<group>"; };
364362
5492E07E202154EC00B64F25 /* FSTDatastoreTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = FSTDatastoreTests.mm; sourceTree = "<group>"; };
365363
5492E0832021552A00B64F25 /* FSTLocalStoreTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = FSTLocalStoreTests.mm; sourceTree = "<group>"; };
366364
5492E0852021552A00B64F25 /* FSTRemoteDocumentCacheTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FSTRemoteDocumentCacheTests.h; sourceTree = "<group>"; };
@@ -1231,7 +1229,6 @@
12311229
DE03B3621F215E1600A30B9C /* CAcert.pem */,
12321230
5492E07E202154EC00B64F25 /* FSTDatastoreTests.mm */,
12331231
5492E07C202154EB00B64F25 /* FSTSmokeTests.mm */,
1234-
5492E07D202154EB00B64F25 /* FSTStreamTests.mm */,
12351232
5492E07B202154EB00B64F25 /* FSTTransactionTests.mm */,
12361233
);
12371234
path = Integration;
@@ -1518,11 +1515,8 @@
15181515
"${BUILT_PRODUCTS_DIR}/GTMSessionFetcher/GTMSessionFetcher.framework",
15191516
"${BUILT_PRODUCTS_DIR}/GoogleUtilities/GoogleUtilities.framework",
15201517
"${BUILT_PRODUCTS_DIR}/Protobuf-iOS8.0/Protobuf.framework",
1521-
"${BUILT_PRODUCTS_DIR}/gRPC/GRPCClient.framework",
15221518
"${BUILT_PRODUCTS_DIR}/gRPC-C++/grpcpp.framework",
15231519
"${BUILT_PRODUCTS_DIR}/gRPC-Core/grpc.framework",
1524-
"${BUILT_PRODUCTS_DIR}/gRPC-ProtoRPC/ProtoRPC.framework",
1525-
"${BUILT_PRODUCTS_DIR}/gRPC-RxLibrary/RxLibrary.framework",
15261520
"${BUILT_PRODUCTS_DIR}/leveldb-library/leveldb.framework",
15271521
"${BUILT_PRODUCTS_DIR}/nanopb/nanopb.framework",
15281522
);
@@ -1532,11 +1526,8 @@
15321526
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/GTMSessionFetcher.framework",
15331527
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/GoogleUtilities.framework",
15341528
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Protobuf.framework",
1535-
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/GRPCClient.framework",
15361529
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/grpcpp.framework",
15371530
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/grpc.framework",
1538-
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/ProtoRPC.framework",
1539-
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxLibrary.framework",
15401531
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/leveldb.framework",
15411532
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/nanopb.framework",
15421533
);
@@ -1765,11 +1756,8 @@
17651756
"${BUILT_PRODUCTS_DIR}/GTMSessionFetcher/GTMSessionFetcher.framework",
17661757
"${BUILT_PRODUCTS_DIR}/GoogleUtilities/GoogleUtilities.framework",
17671758
"${BUILT_PRODUCTS_DIR}/Protobuf-iOS8.0/Protobuf.framework",
1768-
"${BUILT_PRODUCTS_DIR}/gRPC/GRPCClient.framework",
17691759
"${BUILT_PRODUCTS_DIR}/gRPC-C++/grpcpp.framework",
17701760
"${BUILT_PRODUCTS_DIR}/gRPC-Core/grpc.framework",
1771-
"${BUILT_PRODUCTS_DIR}/gRPC-ProtoRPC/ProtoRPC.framework",
1772-
"${BUILT_PRODUCTS_DIR}/gRPC-RxLibrary/RxLibrary.framework",
17731761
"${BUILT_PRODUCTS_DIR}/leveldb-library/leveldb.framework",
17741762
"${BUILT_PRODUCTS_DIR}/nanopb/nanopb.framework",
17751763
);
@@ -1779,11 +1767,8 @@
17791767
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/GTMSessionFetcher.framework",
17801768
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/GoogleUtilities.framework",
17811769
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Protobuf.framework",
1782-
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/GRPCClient.framework",
17831770
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/grpcpp.framework",
17841771
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/grpc.framework",
1785-
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/ProtoRPC.framework",
1786-
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxLibrary.framework",
17871772
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/leveldb.framework",
17881773
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/nanopb.framework",
17891774
);
@@ -2002,7 +1987,6 @@
20021987
5492E0422021440500B64F25 /* FSTHelpers.mm in Sources */,
20031988
5491BC731FB44593008B3588 /* FSTIntegrationTestCase.mm in Sources */,
20041989
5492E080202154EC00B64F25 /* FSTSmokeTests.mm in Sources */,
2005-
5492E081202154EC00B64F25 /* FSTStreamTests.mm in Sources */,
20061990
5492E07F202154EC00B64F25 /* FSTTransactionTests.mm in Sources */,
20071991
5492E0442021457E00B64F25 /* XCTestCase+Await.mm in Sources */,
20081992
B67BF44A216EB43000CA9097 /* create_noop_connectivity_monitor.cc in Sources */,
@@ -2217,14 +2201,6 @@
22172201
"-iquote",
22182202
"\"${PODS_CONFIGURATION_BUILD_DIR}/Protobuf/Protobuf.framework/Headers\"",
22192203
"-iquote",
2220-
"\"${PODS_CONFIGURATION_BUILD_DIR}/gRPC-Core/grpc.framework/Headers\"",
2221-
"-iquote",
2222-
"\"${PODS_CONFIGURATION_BUILD_DIR}/gRPC-ProtoRPC/ProtoRPC.framework/Headers\"",
2223-
"-iquote",
2224-
"\"${PODS_CONFIGURATION_BUILD_DIR}/gRPC-RxLibrary/RxLibrary.framework/Headers\"",
2225-
"-iquote",
2226-
"\"${PODS_CONFIGURATION_BUILD_DIR}/gRPC/GRPCClient.framework/Headers\"",
2227-
"-iquote",
22282204
"\"${PODS_CONFIGURATION_BUILD_DIR}/leveldb-library/leveldb.framework/Headers\"",
22292205
"-iquote",
22302206
"\"${PODS_CONFIGURATION_BUILD_DIR}/nanopb/nanopb.framework/Headers\"",
@@ -2301,14 +2277,6 @@
23012277
"-iquote",
23022278
"\"${PODS_CONFIGURATION_BUILD_DIR}/Protobuf/Protobuf.framework/Headers\"",
23032279
"-iquote",
2304-
"\"${PODS_CONFIGURATION_BUILD_DIR}/gRPC-Core/grpc.framework/Headers\"",
2305-
"-iquote",
2306-
"\"${PODS_CONFIGURATION_BUILD_DIR}/gRPC-ProtoRPC/ProtoRPC.framework/Headers\"",
2307-
"-iquote",
2308-
"\"${PODS_CONFIGURATION_BUILD_DIR}/gRPC-RxLibrary/RxLibrary.framework/Headers\"",
2309-
"-iquote",
2310-
"\"${PODS_CONFIGURATION_BUILD_DIR}/gRPC/GRPCClient.framework/Headers\"",
2311-
"-iquote",
23122280
"\"${PODS_CONFIGURATION_BUILD_DIR}/leveldb-library/leveldb.framework/Headers\"",
23132281
"-iquote",
23142282
"\"${PODS_CONFIGURATION_BUILD_DIR}/nanopb/nanopb.framework/Headers\"",
@@ -2510,14 +2478,6 @@
25102478
"-iquote",
25112479
"\"${PODS_CONFIGURATION_BUILD_DIR}/Protobuf/Protobuf.framework/Headers\"",
25122480
"-iquote",
2513-
"\"${PODS_CONFIGURATION_BUILD_DIR}/gRPC-Core/grpc.framework/Headers\"",
2514-
"-iquote",
2515-
"\"${PODS_CONFIGURATION_BUILD_DIR}/gRPC-ProtoRPC/ProtoRPC.framework/Headers\"",
2516-
"-iquote",
2517-
"\"${PODS_CONFIGURATION_BUILD_DIR}/gRPC-RxLibrary/RxLibrary.framework/Headers\"",
2518-
"-iquote",
2519-
"\"${PODS_CONFIGURATION_BUILD_DIR}/gRPC/GRPCClient.framework/Headers\"",
2520-
"-iquote",
25212481
"\"${PODS_CONFIGURATION_BUILD_DIR}/leveldb-library/leveldb.framework/Headers\"",
25222482
"-iquote",
25232483
"\"${PODS_CONFIGURATION_BUILD_DIR}/nanopb/nanopb.framework/Headers\"",
@@ -2595,14 +2555,6 @@
25952555
"-iquote",
25962556
"\"${PODS_CONFIGURATION_BUILD_DIR}/Protobuf/Protobuf.framework/Headers\"",
25972557
"-iquote",
2598-
"\"${PODS_CONFIGURATION_BUILD_DIR}/gRPC-Core/grpc.framework/Headers\"",
2599-
"-iquote",
2600-
"\"${PODS_CONFIGURATION_BUILD_DIR}/gRPC-ProtoRPC/ProtoRPC.framework/Headers\"",
2601-
"-iquote",
2602-
"\"${PODS_CONFIGURATION_BUILD_DIR}/gRPC-RxLibrary/RxLibrary.framework/Headers\"",
2603-
"-iquote",
2604-
"\"${PODS_CONFIGURATION_BUILD_DIR}/gRPC/GRPCClient.framework/Headers\"",
2605-
"-iquote",
26062558
"\"${PODS_CONFIGURATION_BUILD_DIR}/leveldb-library/leveldb.framework/Headers\"",
26072559
"-iquote",
26082560
"\"${PODS_CONFIGURATION_BUILD_DIR}/nanopb/nanopb.framework/Headers\"",

Firestore/core/src/firebase/firestore/remote/grpc_connection.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <memory>
2121
#include <string>
22+
#include <unordered_map>
2223
#include <vector>
2324

2425
#include "Firestore/core/src/firebase/firestore/auth/token.h"
@@ -80,25 +81,29 @@ class GrpcConnection {
8081
void Unregister(GrpcCall* call);
8182

8283
/**
83-
* For tests only: use a custom root certificate file and the given SSL
84-
* target name for all connections. Call before creating any streams or calls.
84+
* Don't use SSL, send all traffic unencrypted. Call before creating any
85+
* streams or calls.
8586
*/
86-
static void UseTestCertificate(absl::string_view certificate_path,
87-
absl::string_view target_name);
87+
static void UseInsecureChannel(const std::string& host);
8888

8989
/**
90-
* For tests only: don't use SSL, send all traffic unencrypted. Call before
91-
* creating any streams or calls. Overrides a test certificate.
90+
* For tests only: use a custom root certificate file and the given SSL
91+
* target name for all connections. Call before creating any streams or calls.
9292
*/
93-
static void UseInsecureChannel();
93+
static void UseTestCertificate(const std::string& host,
94+
const std::string& certificate_path,
95+
const std::string& target_name);
9496

9597
private:
96-
struct TestCredentials {
98+
static bool HasSpecialConfig(const std::string& host);
99+
100+
struct HostConfig {
97101
std::string certificate_path;
98102
std::string target_name;
99103
bool use_insecure_channel = false;
100104
};
101-
static TestCredentials* test_credentials_;
105+
using ConfigByHost = std::unordered_map<std::string, HostConfig>;
106+
static ConfigByHost* config_by_host_;
102107

103108
std::unique_ptr<grpc::ClientContext> CreateContext(
104109
const auth::Token& credential) const;

Firestore/core/src/firebase/firestore/remote/grpc_connection.mm

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
} // namespace
5656

57-
GrpcConnection::TestCredentials* GrpcConnection::test_credentials_ = nullptr;
57+
GrpcConnection::ConfigByHost* GrpcConnection::config_by_host_ = nullptr;
5858

5959
GrpcConnection::GrpcConnection(const DatabaseInfo& database_info,
6060
util::AsyncQueue* worker_queue,
@@ -120,31 +120,35 @@
120120
}
121121

122122
std::shared_ptr<grpc::Channel> GrpcConnection::CreateChannel() const {
123-
if (!test_credentials_) {
123+
const std::string& host = database_info_->host();
124+
125+
if (!HasSpecialConfig(host)) {
124126
return grpc::CreateChannel(
125-
database_info_->host(),
126-
grpc::SslCredentials(grpc::SslCredentialsOptions()));
127+
host, grpc::SslCredentials(grpc::SslCredentialsOptions()));
127128
}
128129

129-
if (test_credentials_->use_insecure_channel) {
130-
return grpc::CreateChannel(database_info_->host(),
131-
grpc::InsecureChannelCredentials());
130+
const HostConfig& host_config = (*config_by_host_)[host];
131+
132+
// For the case when `Settings.sslEnabled == false`.
133+
if (host_config.use_insecure_channel) {
134+
return grpc::CreateChannel(host, grpc::InsecureChannelCredentials());
132135
}
133136

134-
std::ifstream cert_file{test_credentials_->certificate_path};
137+
// For tests only
138+
139+
std::ifstream cert_file{host_config.certificate_path};
135140
HARD_ASSERT(cert_file.good(),
136141
StringFormat("Unable to open root certificates at file path %s",
137-
test_credentials_->certificate_path)
142+
host_config.certificate_path)
138143
.c_str());
139144
std::stringstream cert_buffer;
140145
cert_buffer << cert_file.rdbuf();
141146
grpc::SslCredentialsOptions options;
142147
options.pem_root_certs = cert_buffer.str();
143148

144149
grpc::ChannelArguments args;
145-
args.SetSslTargetNameOverride(test_credentials_->target_name);
146-
return grpc::CreateCustomChannel(database_info_->host(),
147-
grpc::SslCredentials(options), args);
150+
args.SetSslTargetNameOverride(host_config.target_name);
151+
return grpc::CreateCustomChannel(host, grpc::SslCredentials(options), args);
148152
}
149153

150154
std::unique_ptr<GrpcStream> GrpcConnection::CreateStream(
@@ -210,30 +214,40 @@
210214
}
211215

212216
/*static*/ void GrpcConnection::UseTestCertificate(
213-
absl::string_view certificate_path, absl::string_view target_name) {
217+
const std::string& host,
218+
const std::string& certificate_path,
219+
const std::string& target_name) {
220+
HARD_ASSERT(!host.empty(), "Empty host name");
214221
HARD_ASSERT(!certificate_path.empty(), "Empty path to test certificate");
215222
HARD_ASSERT(!target_name.empty(), "Empty SSL target name");
216223

217-
if (!test_credentials_) {
224+
if (!config_by_host_) {
218225
// Deliberately never deleted.
219-
test_credentials_ = new TestCredentials{};
226+
config_by_host_ = new ConfigByHost{};
220227
}
221228

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.
229+
HostConfig& host_config = (*config_by_host_)[host];
230+
host_config.certificate_path = certificate_path;
231+
host_config.target_name = target_name;
227232
}
228233

229-
/*static*/ void GrpcConnection::UseInsecureChannel() {
230-
if (!test_credentials_) {
234+
/*static*/ void GrpcConnection::UseInsecureChannel(const std::string& host) {
235+
HARD_ASSERT(!host.empty(), "Empty host name");
236+
237+
if (!config_by_host_) {
231238
// Deliberately never deleted.
232-
test_credentials_ = new TestCredentials{};
239+
config_by_host_ = new ConfigByHost{};
233240
}
234241

235-
test_credentials_->use_insecure_channel = true;
236-
// TODO(varconst): hostname if necessary.
242+
HostConfig& test_config = (*config_by_host_)[host];
243+
test_config.use_insecure_channel = true;
244+
}
245+
246+
/*static*/ bool GrpcConnection::HasSpecialConfig(const std::string& host) {
247+
if (!config_by_host_) {
248+
return false;
249+
}
250+
return config_by_host_->find(host) != config_by_host_->end();
237251
}
238252

239253
} // namespace remote

0 commit comments

Comments
 (0)