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

Use Admin AuthTokenProvider when targeting Emulator #3228

Merged
merged 11 commits into from
Jul 14, 2020

Conversation

schmidt-sebastian
Copy link
Contributor

Fixes #3144

@schmidt-sebastian schmidt-sebastian changed the title Use empty auth token provider when talking to Emulator Use empty AuthTokenProvider when targeting Emulator Jun 17, 2020
@google-oss-bot
Copy link
Contributor

google-oss-bot commented Jun 17, 2020

Binary Size Report

Affected SDKs

  • @firebase/database

    Type Base (b07f822) Head (aa5ee6c) Diff
    browser 268 kB 269 kB +848 B (+0.3%)
    esm2017 235 kB 236 kB +523 B (+0.2%)
    main 269 kB 270 kB +848 B (+0.3%)
    module 267 kB 268 kB +843 B (+0.3%)
  • firebase

    Type Base (b07f822) Head (aa5ee6c) Diff
    firebase-database.js 187 kB 187 kB +18 B (+0.0%)
    firebase.js 819 kB 819 kB +18 B (+0.0%)

Test Logs

@yuchenshi
Copy link
Member

I'll be brief here. I believe this will break two use cases:

  1. Admin SDK authenticating as Admin (where Authorization: Bearer owner must be specified, instead of empty.)
  2. Developers using production Firebase Auth to login a production user and then write to local RTDB emulator using the production JWT.

Feel free to correct me if I'm wrong, but I may be overly cautious since we had a similar change before that broke admin and firebase-testing. If you can correct me by testing admin, production auth, and @firebase/testing, that will be even better. Bonus points if those tests can be automated.

@schmidt-sebastian
Copy link
Contributor Author

@yuchenshi Thanks for your comment. I am kind of stuck here now, but will try to come up with a way to address your concerns. I hope we can do more than just silence the logs.

@filipesilva
Copy link

@schmidt-sebastian did you perhaps come across a workaround that could be used meanwhile? It looks like this fix ended up being a bit more complicated than it looked like. I'm happy enough working around it on my side for the time being, if that's an option.

@schmidt-sebastian
Copy link
Contributor Author

I haven't been able to come up with a way to solve this - as far as I can tell, we might just have to disable the log output. Does anything actually break on your end, or are you mostly (and rightfully) concerned about the log output?

@filipesilva
Copy link

filipesilva commented Jun 26, 2020

Anything that involves using recent emulator version with RTDB and without having GOOGLE_APPLICATION_CREDENTIALS set will fail. It's not logging, it's that RTDB doesn't work at all.

In the original repro in #3144, the cloud fn times out with RTDB, but not with Firestore, because of this problem. On our setup we're stuck on firebase-tools@7.12.1 and can't update because of it. For some reason older emulator versions are ok.

@samtstern samtstern requested a review from yuchenshi July 7, 2020 16:48
@samtstern
Copy link
Contributor

@schmidt-sebastian do we have any way to detect if we're in an Admin or Client context? If so the behavior matrix looks like this:

  • In client context...
    • Host is emulator: use Firebase Auth
    • Host is prod: use Firebase Auth
  • In admin context...
    • Host is emulator: use EmptyTokenProvider that always returns exactly owner
    • Host is prod: use standard cloud auth

@changeset-bot
Copy link

changeset-bot bot commented Jul 7, 2020

🦋 Changeset is good to go

Latest commit: 004f58d

We got this.

This PR includes changesets to release 8 packages
Name Type
firebase Patch
@firebase/database Patch
@firebase/testing Patch
firebase-browserify-test Patch
firebase-package-typings-test Patch
firebase-messaging-selenium-test Patch
firebase-typescript-test Patch
firebase-webpack-test Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@schmidt-sebastian schmidt-sebastian force-pushed the mrschmidt/tokenprovider branch 2 times, most recently from 191af58 to d67005b Compare July 8, 2020 03:58
@schmidt-sebastian schmidt-sebastian changed the title Use empty AuthTokenProvider when targeting Emulator Use Admin AuthTokenProvider when targeting Emulator Jul 8, 2020
Copy link
Contributor Author

@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.

Sam - this might be ready for another review. It at least solves the reported problem. I hope it causes not too many others.

Copy link
Contributor

@samtstern samtstern 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 99% correct now but I have one more question.

@@ -116,6 +123,11 @@ export class RepoManager {
dbUrl = `http://${dbEmulatorHost}?ns=${repoInfo.namespace}`;
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this branch if (dbEmulatorHost) { } trigger only if the emulator connection is made via env var? What about if the user does this:

firebase.initializeApp({
  databaseURL: 'http://localhost:9000?ns=namespace'
})

You might consider using the EmulatorAdminTokenProvider any time Constants.NODE_ADMIN is true and the database is http:// (not https://). That's what we do in the Firebase CLI, it catches more cases and it also has the nice side benefit of not allowing someone's admin credentials to ever go to an insecure host.

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 this time and please address Sam's latest comment.

However, I'm still concerned about test coverage. Are the cases (and the awesome matrix Sam has put together) manually tested, unit tested, or integration tested?

dbUrl = `http://${dbEmulatorHost}?ns=${repoInfo.namespace}`;
parsedUrl = parseRepoInfo(dbUrl);
repoInfo = parsedUrl.repoInfo;
} else {
isEmulator =
parsedUrl.repoInfo.host === 'localhost' && !parsedUrl.repoInfo.secure;
Copy link
Contributor

Choose a reason for hiding this comment

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

We do see people running their emulator on non-localhost hosts. Like 0.0.0.0. I think just anything non-secure (http://) is the right condition.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. I will verify this manually on Monday, which should allow us to release this next week.

Copy link
Contributor

@samtstern samtstern left a comment

Choose a reason for hiding this comment

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

One final comment about non-localhost emulators.

@filipesilva
Copy link

@schmidt-sebastian if there's a snapshot build somewhere that I can pull in via npm I can test on my setup too.

@schmidt-sebastian
Copy link
Contributor Author

I manually verified using Sam's testing matrix.

@filipesilva We will publish a prerelease version on NPM, likely tomorrow.

@schmidt-sebastian schmidt-sebastian assigned jsdt and unassigned samtstern Jul 13, 2020
@hsubox76 hsubox76 self-requested a review July 14, 2020 20:53
@hsubox76 hsubox76 merged commit c2b737b into master Jul 14, 2020
@samtstern
Copy link
Contributor

So it looks like firebase-admin-node 9.0.0 went out yesterday ... does that mean this bugfix missed the last 8.x release?

@google-oss-bot google-oss-bot mentioned this pull request Jul 15, 2020
@schmidt-sebastian
Copy link
Contributor Author

@samtstern firebase-admin-node 8.x should pick this up on re-install since we don't pin versions.

@google-oss-bot google-oss-bot mentioned this pull request Jul 16, 2020
@schmidt-sebastian schmidt-sebastian deleted the mrschmidt/tokenprovider branch July 17, 2020 01:40
@firebase firebase locked and limited conversation to collaborators Aug 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Database SDK tries to access credential even when FIREBASE_DATABASE_EMULATOR_HOST is set
7 participants