Skip to content

Conversation

@sarajmunjal
Copy link
Contributor

@sarajmunjal sarajmunjal commented Aug 12, 2019

This PR makes it possible for the Java admin SDK to talk to the RTDB emulator instead of the actual database instance.
This can happen in the following fashion:-

  1. If the user passes in a valid emulator URL (http scheme only, host can't contain firebaseio.com, has the ns query parameter)
  2. Else, if the environment variable FIREBASE_RTDB_EMULATOR_HOST is set to a value of the form host:port.

If either 1. or 2. is true, we decide to talk to the RTDB emulator. In such cases, we

  • provide a valid emulator URL (either by replacing the supplied URL or making a new one)
  • replace existing credentials with EmulatorCredentials, which use owner as the access token.

URL replacement happens in three places:-

  1. while constructing FirebaseOptions instances in FirebaseApp
  2. when a specific databaseUrl is passed in FirebaseDatabase.getInstance(String url)
  3. when a specific URL with path is passed in FirebaseDatabase.getReferenceFromUrl(String url)

Tests have been added with different combinations of passed-in URLs and environment variable values for classes FirebaseApp, FirebaseDatabase, and also for EmulatorHelper: the helper class that actually does URL replacement if necessary.

Notes for reviewers
I have made changes to existing behavior to Utilities.parseUrl(String url): these changes were necessary to recognize the ns query parameter, and also to suppost valid <net_loc> URL semantics where a URL like http://localhost:8080/ is deemed valid.
These changes are nearly identical to changes to the same file in this Android SDK PR.

RELEASE NOTE: Developers can now test Realtime Database API calls by directing the SDK traffic to the RTDB emulator. Set the FIREBASE_DATABASE_EMULATOR_HOST environment variable to specify the emulator endpoint in host:port format.

Copy link
Contributor

@hiranya911 hiranya911 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the test failures.

@yuchenshi yuchenshi changed the title Connect to RTDB emulator when correct vars are set [WIP] Connect to RTDB emulator when correct vars are set Aug 13, 2019
@sarajmunjal sarajmunjal changed the title [WIP] Connect to RTDB emulator when correct vars are set Connect to RTDB emulator when correct vars are set Aug 17, 2019
@sarajmunjal sarajmunjal changed the title Connect to RTDB emulator when correct vars are set Connect to RTDB emulator when valid emulator URL is passed OR env vars are set correctly Aug 17, 2019
Copy link
Contributor

@schmidt-sebastian schmidt-sebastian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! I think from a functional perspective this is fine, but I wonder if there is a way to make it more obvious when we are talking out the environment variable and when we are talking about a database URL.

Furthermore, all existing SDKs always blindly overwrite any URL if the emulator host environment variable is set. I wonder if we should do the same?

Copy link
Contributor

@hiranya911 hiranya911 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few suggestions, the main one being handling emulator credentials all within the FirebaseDatabase implementation. Basically allow empty credentials in FirebaseOptions when configured for emulator mode, and then init EmulatorCredentials in FirebaseDatabase.

Copy link
Member

@yuchenshi yuchenshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM to me. Please address the comments and get an approval from at least one other reviewer before merging.

Copy link
Contributor

@schmidt-sebastian schmidt-sebastian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left some more comments around URL parsing, but the rest of the code and the plumbing looks fantastic now! Thanks for your perseverance.

@schmidt-sebastian schmidt-sebastian removed their assignment Aug 22, 2019
Copy link
Contributor

@hiranya911 hiranya911 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for seeing this through. Just one comment regarding the core SDK. I'll defer to @schmidt-sebastian regarding the changes in the database package.

Copy link
Contributor

@hiranya911 hiranya911 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with one comment. Feel free to merge once that comment and any other open comments from @schmidt-sebastian are addressed.

Copy link
Contributor

@schmidt-sebastian schmidt-sebastian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! Thanks for all the updates!

@hiranya911 hiranya911 changed the title Connect to RTDB emulator when valid emulator URL is passed OR env vars are set correctly feat(rtdb): Connect to RTDB emulator when valid emulator URL is passed OR env vars are set correctly Aug 23, 2019
@sarajmunjal sarajmunjal merged commit 9ace7b1 into master Aug 23, 2019
@sarajmunjal sarajmunjal deleted the sarajmun-connect-rtdb-emulator branch August 23, 2019 21:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants