Skip to content

Firestore emulator doesn't recognise rules flag #1303

Closed

Description

@Yuripetusko

[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'`

Activity

  1. samtstern commented on May 16, 2019

    @samtstern
    Contributor
  2. abeisgoat commented on May 16, 2019

    @abeisgoat
    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!

  3. Yuripetusko commented on May 16, 2019

    @Yuripetusko
    Author

    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"
      },
    
  4. Yuripetusko commented on May 16, 2019

    @Yuripetusko
    Author

    So I guess it now turned into a feature request

  5. samtstern commented on May 16, 2019

    @samtstern
    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?

  6. Yuripetusko commented on May 16, 2019

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

  7. samtstern commented on May 16, 2019

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

  8. Yuripetusko commented on May 16, 2019

    @Yuripetusko
    Author

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

  9. ryanpbrewster commented on May 16, 2019

    @ryanpbrewster
    Contributor

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

  10. Yuripetusko commented on May 17, 2019

    @Yuripetusko
    Author

    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

  11. Yuripetusko commented on May 17, 2019

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

  12. ryanpbrewster commented on May 17, 2019

    @ryanpbrewster
    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)?

  13. Yuripetusko commented on May 20, 2019

    @Yuripetusko
    Author

    @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 🤷‍♂

  14. samtstern commented on May 20, 2019

    @samtstern
    Contributor

    @Yuripetusko thanks for following up!

  15. samtstern commented on Jun 13, 2019

    @samtstern
    Contributor

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

  16. matheo commented on Jun 15, 2019

    @matheo
    Contributor

    @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

  17. samtstern commented on Jun 17, 2019

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions