Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add process override sdk #781

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/com/google/firebase/FirebaseApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ static FirebaseApp initializeApp(FirebaseOptions options, String name,
!instances.containsKey(normalizedName),
"FirebaseApp name " + normalizedName + " already exists!");

overrideProcessEnvironment(options);
FirebaseApp firebaseApp = new FirebaseApp(normalizedName, options, tokenRefresherFactory);
instances.put(normalizedName, firebaseApp);
return firebaseApp;
Expand Down Expand Up @@ -583,4 +584,8 @@ private static FirebaseOptions getOptionsFromEnvironment() throws IOException {
builder.setCredentials(APPLICATION_DEFAULT_CREDENTIALS);
return builder.build();
}

private static void overrideProcessEnvironment(FirebaseOptions options) {
options.getProcessEnvironmentOverride().forEach(FirebaseProcessEnvironment::setenv);
}
}
29 changes: 27 additions & 2 deletions src/main/java/com/google/firebase/FirebaseOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.google.firebase.internal.FirebaseThreadManagers;
import com.google.firebase.internal.NonNull;
import com.google.firebase.internal.Nullable;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -85,13 +84,15 @@ public GoogleCredentials get() {
private final JsonFactory jsonFactory;
private final ThreadManager threadManager;
private final FirestoreOptions firestoreOptions;
private final Map<String, String> processEnvironmentOverride;

private FirebaseOptions(@NonNull final FirebaseOptions.Builder builder) {
private FirebaseOptions(@NonNull final Builder builder) {
this.databaseUrl = builder.databaseUrl;
this.credentialsSupplier = checkNotNull(
builder.credentialsSupplier, "FirebaseOptions must be initialized with setCredentials().");
this.databaseAuthVariableOverride = builder.databaseAuthVariableOverride;
this.projectId = builder.projectId;
this.processEnvironmentOverride = builder.processEnvironmentOverride;
if (!Strings.isNullOrEmpty(builder.storageBucket)) {
checkArgument(!builder.storageBucket.startsWith("gs://"),
"StorageBucket must not include 'gs://' prefix.");
Expand Down Expand Up @@ -207,6 +208,16 @@ public int getReadTimeout() {
return readTimeout;
}

/**
* Return the process environment override values used for setting the env in
* {@link com.google.firebase.internal.FirebaseProcessEnvironment}
*
* @return {@link Map} of environment variables used for overriding
*/
public Map<String, String> getProcessEnvironmentOverride() {
return processEnvironmentOverride;
}

@NonNull
ThreadManager getThreadManager() {
return threadManager;
Expand Down Expand Up @@ -260,6 +271,7 @@ public static final class Builder {
private ThreadManager threadManager;
private int connectTimeout;
private int readTimeout;
private Map<String, String> processEnvironmentOverride;

/**
* Constructs an empty builder.
Expand Down Expand Up @@ -495,6 +507,19 @@ public Builder setReadTimeout(int readTimeout) {
return this;
}

/**
* Sets the environment variable to override in
* {@link com.google.firebase.internal.FirebaseProcessEnvironment}
*
* @param processEnvironmentOverride {@link Map} of key, value for environment variables
* @return This <code>Builder</code> instance is returned so subsequent calls can be
* chained.
*/
public Builder setProcessEnvironmentOverride(Map<String, String> processEnvironmentOverride) {
this.processEnvironmentOverride = processEnvironmentOverride;
return this;
}

/**
* Builds the {@link FirebaseOptions} instance from the previously set options.
*
Expand Down
8 changes: 7 additions & 1 deletion src/test/java/com/google/firebase/FirebaseOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@
import com.google.firebase.testing.ServiceAccount;
import com.google.firebase.testing.TestUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;

import org.junit.Test;

/**
Expand Down Expand Up @@ -76,6 +77,8 @@ public void createOptionsWithAllValuesSet() throws IOException {
GsonFactory jsonFactory = new GsonFactory();
NetHttpTransport httpTransport = new NetHttpTransport();
FirestoreOptions firestoreOptions = FirestoreOptions.newBuilder().build();
Map<String, String> processEnvOverride = new HashMap<>();
processEnvOverride.put("FIREBASE_AUTH_EMULATOR_HOST", "localhost:9092");
FirebaseOptions firebaseOptions =
FirebaseOptions.builder()
.setDatabaseUrl(FIREBASE_DB_URL)
Expand All @@ -86,6 +89,7 @@ public void createOptionsWithAllValuesSet() throws IOException {
.setHttpTransport(httpTransport)
.setThreadManager(MOCK_THREAD_MANAGER)
.setConnectTimeout(30000)
.setProcessEnvironmentOverride(processEnvOverride)
.setReadTimeout(60000)
.setFirestoreOptions(firestoreOptions)
.build();
Expand All @@ -98,6 +102,8 @@ public void createOptionsWithAllValuesSet() throws IOException {
assertEquals(30000, firebaseOptions.getConnectTimeout());
assertEquals(60000, firebaseOptions.getReadTimeout());
assertSame(firestoreOptions, firebaseOptions.getFirestoreOptions());
assertEquals("localhost:9092",
firebaseOptions.getProcessEnvironmentOverride().get("FIREBASE_AUTH_EMULATOR_HOST"));

GoogleCredentials credentials = firebaseOptions.getCredentials();
assertNotNull(credentials);
Expand Down