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

Firestore emulator doesn't recognise rules flag #1303

Closed
Yuripetusko opened this issue May 16, 2019 · 18 comments
Closed

Firestore emulator doesn't recognise rules flag #1303

Yuripetusko opened this issue May 16, 2019 · 18 comments

Comments

@Yuripetusko
Copy link

[REQUIRED] Environment info

firebase-tools: 6.9.2

Platform: macOS

[REQUIRED] Test case

According to docs

The emulator supports an optional --rules CLI flag. It expects the name of a local file containing your Cloud Firestore Security Rules and applies those rules to all projects. If you don't provide the local file path or use the loadFirestoreRules method as described below, the emulator treats all projects as having open rules.

[REQUIRED] Steps to reproduce

firestore (master) $ firebase emulators:start --only firestore --rules firestore.stable.rules

[REQUIRED] Expected behavior

--rules flag to be recognised as per Documentation

[REQUIRED] Actual behavior

error: unknown option --rules'`

@samtstern
Copy link
Contributor

samtstern commented May 16, 2019 via email

@abeisgoat
Copy link
Contributor

This is definitely a documentation error, I believe we're having some conversations about docs today so I'll make sure to bring this up as a point of confusion!

@Yuripetusko
Copy link
Author

Yuripetusko commented May 16, 2019

Yes I tried 3 different ways:

1 --rules flag as above
2 await firebase.loadFirestoreRules(projectId: 'test', rules: fs.readFileSync('firestore.rules', 'utf8')) - doesn't work
3

"emulators": {
    "firestore": {
      "port": "5002",
      "rules": "firestore/firestore.stable.rules"
    }
  }

in firebase.json which also doesn't work

It always just use default one from this

"firestore": {
    "rules": "firestore/firestore.rules",
    "indexes": "firestore/firestore.indexes.json"
  },

@Yuripetusko
Copy link
Author

Yuripetusko commented May 16, 2019

So I guess it now turned into a feature request

@samtstern
Copy link
Contributor

@Yuripetusko yeah this is a FR not a bug since right now we only look for what's in firestore.rules not emulators.firestore.rules.

Could you explain your use case a bit more? What is the meaning of .stable.rules for your project?

@Yuripetusko
Copy link
Author

@samtstern I guess it's not a big deal, just for few days we had multiple generated rules as we were testing some security rules and didn't want to deploy them straight away but wanted to use them in our jest tests

@samtstern
Copy link
Contributor

@Yuripetusko FYI we provide a way to load rules files from inside your tests using the @firebase/testing library:
https://github.com/firebase/quickstart-nodejs/blob/master/firestore-emulator/javascript-quickstart/test/test.js#L37

That's probably your best bet if you want to use a different rule set only in tests.

@Yuripetusko
Copy link
Author

@samtstern I actually already do this, but it doesn't work, looks like emulator still overwrites rules that it finds in firebase.json

@ryanpbrewster
Copy link
Contributor

That's surprising, could you provide a snippet showing how you're using the loadFirestoreRules function from @firebase/testing?

@Yuripetusko
Copy link
Author

Yuripetusko commented May 17, 2019

this is how I do it

const firebase = require('@firebase/testing');
const fs = require('fs');

const projectId = 'firestore-migration-test';
const coverageUrl = `http://${process.env.FIREBASE_FIRESTORE_EMULATOR_ADDRESS}/emulator/v1/projects/${projectId}:ruleCoverage.html`;
const rules = fs.readFileSync('firestore.stable.rules', 'utf8');

...


const setup = async () => {
  const app = await firebase.initializeTestApp({ projectId, auth: { uid: 'D4iD36vNgNPAbpg7oKYscJxazyI3' } });

  const db = app.firestore();

  // Apply rules
  await firebase.loadFirestoreRules({ projectId, rules });

  return db;
};

And I am sure that file is loaded as if I change path it complains that fs cannot find a file

And I am sure that setup is called as auth user is set exactly as in setup function

@Yuripetusko
Copy link
Author

The only thing I do differently is I don't use firebase/testing asserts, and use jest instead, but setup is using firebase/test..

@ryanpbrewster
Copy link
Contributor

That code looks correct, and when I try it it works.

Can you provide a short repro of the issue you're running into (i.e., your rules, a test script, and the command you use to execute them)?

@Yuripetusko
Copy link
Author

Yuripetusko commented May 20, 2019

@ryanpbrewster Indeed while I was creating a reproducible repository this morning it worked using loadFirestoreRules, must be some other issue why it didn't work for me before which I will investigate. Thanks for going through it with me 🤷‍♂

@samtstern
Copy link
Contributor

@Yuripetusko thanks for following up!

@therefromhere
Copy link

@abeisgoat Could this be reopened since the documentation is still incorrect?

https://firebase.google.com/docs/firestore/security/test-rules-emulator#before_you_run_the_emulator

@samtstern
Copy link
Contributor

@therefromhere good call, I will have to get those docs fixed.

@samtstern samtstern reopened this Jun 13, 2019
@matheo
Copy link
Contributor

matheo commented Jun 15, 2019

@samtstern It would be good to add a configuration schema for the firebase.json so we know which options are supported and the IDE can autocomplete too, one like this one, but complete:
https://github.com/SirWindfield/vscode-firebase-configuration-schema-plugin/blob/master/schemas/firebase.schema.json

@samtstern
Copy link
Contributor

@therefromhere I have submitted a docs change, it will go live probably tomorrow.
@matheo that's a great idea, let me file a new issue for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants