Skip to content

Commit c8a9982

Browse files
Revert "Remove overrideChannelBuilder (#678)"
This reverts commit 5dc3bce.
1 parent 7533855 commit c8a9982

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/testutil/IntegrationTestUtil.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.Assert.assertNull;
2020

2121
import android.content.Context;
22+
import android.net.SSLCertificateSocketFactory;
2223
import android.os.StrictMode;
2324
import androidx.test.core.app.ApplicationProvider;
2425
import com.google.android.gms.tasks.Task;
@@ -40,11 +41,13 @@
4041
import com.google.firebase.firestore.core.DatabaseInfo;
4142
import com.google.firebase.firestore.local.Persistence;
4243
import com.google.firebase.firestore.model.DatabaseId;
44+
import com.google.firebase.firestore.remote.GrpcCallProvider;
4345
import com.google.firebase.firestore.testutil.provider.FirestoreProvider;
4446
import com.google.firebase.firestore.util.AsyncQueue;
4547
import com.google.firebase.firestore.util.Listener;
4648
import com.google.firebase.firestore.util.Logger;
4749
import com.google.firebase.firestore.util.Logger.Level;
50+
import io.grpc.okhttp.OkHttpChannelBuilder;
4851
import java.util.ArrayList;
4952
import java.util.HashMap;
5053
import java.util.List;
@@ -141,7 +144,25 @@ public static FirebaseFirestoreSettings newTestSettingsWithSnapshotTimestampsEna
141144

142145
if (CONNECT_TO_EMULATOR) {
143146
settings.setHost(String.format("%s:%d", EMULATOR_HOST, EMULATOR_PORT));
144-
settings.setSslEnabled(false);
147+
148+
// The `sslEnabled` flag in DatabaseInfo currently does not in fact disable all SSL checks.
149+
// Instead, we manually disable the SSL certificate check and the hostname verification for
150+
// connections to the emulator.
151+
// TODO(mrschmidt): Update the client to respect the `sslEnabled` flag and remove these
152+
// channel overrides.
153+
OkHttpChannelBuilder channelBuilder =
154+
new OkHttpChannelBuilder(EMULATOR_HOST, EMULATOR_PORT) {
155+
@Override
156+
protected String checkAuthority(String authority) {
157+
return authority;
158+
}
159+
};
160+
channelBuilder.hostnameVerifier((hostname, session) -> true);
161+
SSLCertificateSocketFactory insecureFactory =
162+
(SSLCertificateSocketFactory) SSLCertificateSocketFactory.getInsecure(0, null);
163+
channelBuilder.sslSocketFactory(insecureFactory);
164+
channelBuilder.usePlaintext();
165+
GrpcCallProvider.overrideChannelBuilder(() -> channelBuilder);
145166
} else {
146167
settings.setHost(provider.firestoreHost());
147168
}

firebase-firestore/src/main/java/com/google/firebase/firestore/remote/GrpcCallProvider.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.firebase.firestore.remote;
1616

1717
import android.content.Context;
18+
import androidx.annotation.VisibleForTesting;
1819
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
1920
import com.google.android.gms.common.GooglePlayServicesRepairableException;
2021
import com.google.android.gms.security.ProviderInstaller;
@@ -24,6 +25,7 @@
2425
import com.google.firebase.firestore.util.AsyncQueue;
2526
import com.google.firebase.firestore.util.Executors;
2627
import com.google.firebase.firestore.util.Logger;
28+
import com.google.firebase.firestore.util.Supplier;
2729
import com.google.firestore.v1.FirestoreGrpc;
2830
import io.grpc.CallCredentials;
2931
import io.grpc.CallOptions;
@@ -41,11 +43,26 @@ public class GrpcCallProvider {
4143

4244
private static final String LOG_TAG = "GrpcCallProvider";
4345

46+
private static Supplier<ManagedChannelBuilder<?>> overrideChannelBuilderSupplier;
47+
4448
private final Task<ManagedChannel> channelTask;
4549
private final AsyncQueue asyncQueue;
4650

4751
private CallOptions callOptions;
4852

53+
/**
54+
* Helper function to globally override the channel that RPCs use. Useful for testing when you
55+
* want to bypass SSL certificate checking.
56+
*
57+
* @param channelBuilderSupplier The supplier for a channel builder that is used to create gRPC
58+
* channels.
59+
*/
60+
@VisibleForTesting
61+
public static void overrideChannelBuilder(
62+
Supplier<ManagedChannelBuilder<?>> channelBuilderSupplier) {
63+
overrideChannelBuilderSupplier = channelBuilderSupplier;
64+
}
65+
4966
GrpcCallProvider(
5067
AsyncQueue asyncQueue,
5168
Context context,
@@ -88,12 +105,16 @@ private ManagedChannel initChannel(Context context, DatabaseInfo databaseInfo) {
88105
Logger.warn(LOG_TAG, "Failed to update ssl context: %s", e);
89106
}
90107

91-
ManagedChannelBuilder<?> channelBuilder =
92-
ManagedChannelBuilder.forTarget(databaseInfo.getHost());
93-
if (!databaseInfo.isSslEnabled()) {
94-
// Note that the boolean flag does *NOT* switch the wire format from Protobuf to Plaintext.
95-
// It merely turns off SSL encryption.
96-
channelBuilder.usePlaintext();
108+
ManagedChannelBuilder<?> channelBuilder;
109+
if (overrideChannelBuilderSupplier != null) {
110+
channelBuilder = overrideChannelBuilderSupplier.get();
111+
} else {
112+
channelBuilder = ManagedChannelBuilder.forTarget(databaseInfo.getHost());
113+
if (!databaseInfo.isSslEnabled()) {
114+
// Note that the boolean flag does *NOT* switch the wire format from Protobuf to Plaintext.
115+
// It merely turns off SSL encryption.
116+
channelBuilder.usePlaintext();
117+
}
97118
}
98119

99120
// Ensure gRPC recovers from a dead connection. (Not typically necessary, as the OS will

0 commit comments

Comments
 (0)