In older versions of Firebase, we could add a rules section to our firebase.json file, and upload new security rules on every deploy.
How do we use the firebase-tools v3 command-line tools to deploy database security rules?
This page says that it's possible: "Rules for Firebase Storage"
This page hints that the command line tools can do it, but firebase --help and firebase deploy --help don't seem to hint at how to do it? (Apologies if I missed it...)
(related: where is the canonical doc for everything that can go into firebase.json? I found it on the old Firebase site, but can't find it via search on the new docs.)
Thanks!
You can use firebase deploy --only database if you only want to update database rules. It will overwrite your existing rules.
You can check out Firebase CLI Reference for more info
You can use firebase deploy or firebase deploy --only database from the command line, BUT most important:
Please note hereunder firebase.json format: The "rules" entry is under "database" entry.
It was taken from Firebase Sample code.
{
"database": {
"rules": "database-rules.json"
},
"hosting": {
"public": "./",
"ignore": [
"firebase.json",
"database-rules.json",
]
}
}
To deploy a new set of security rules with firebase cli
firebase deploy --only firestore:rules
To deploy a new set of security rules, add a rules top-level key to your firebase.json.
Example firebase.json file:
{
"rules": "firebase_rules.json",
"hosting": {
"public": "doc",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}
The firebase_rules.json is a JSON file that contains the security rules. Here's an example:
{
"rules": {
".read": false,
".write": false
}
}
When you run firebase deploy, it will send the contents of firebase_rules.json to the server, replacing/updating any rules configurations.
Related
I've set up multiple different indexes on my Firestore development database. Now, I would like to export them into the firestore.indexes.json so that the process of setting up prod environment would be easier. Is there a way to export those indexes using Firebase CLI? The same applies to security rules, although I know that I can copy paste them.
It's possible!
Run from CLI firebase firestore:indexes inside your firebase project folder.
Providing you have indexes already setup and logged into Firebase via the CLI too, you'll get a formatted JSON output for you to copy.
Example:
{
"indexes": [
{
"collectionId": "teslaData",
"fields": [
{
"fieldPath": "Model",
"mode": "ASCENDING"
},
{
"fieldPath": "Price",
"mode": "ASCENDING"
}
]
}
]
}
Exported indexes can be re imported using firebase deploy --only firestore:indexes. Check following doc extract.
https://firebase.google.com/docs/firestore/query-data/indexing
You can also deploy indexes with the Firebase CLI. To get started, run
firebase init firestore in your project directory. During setup, the
Firebase CLI generates a JSON file with the default indexes in the
correct format. Edit the file to add more indexes and deploy it with
the firebase deploy command. If you only want to deploy indexes, add
the --only firestore:indexes flag. If you make edits to the indexes
using the Firebase console, make sure you also update your local
indexes file.
I'm using Firebase CLI 4.2.1 if that helps.
Edit: It's still working as of 9.6.0.
In your Firebase project folder execute this in the terminal:
firebase firestore:indexes > firestore.indexes.json
And it will save a file called firestore.indexes.json with your indexes.
You can then upload that file onto other Firebase projects.
I don't think there is currently an API for getting the Firestore security rules from a project. You can deploy rules through the CLI, which can also be embedded in custom Node scripts, and invoked from CI processes. But as far as I know there is no API to read the rules from a project.
It sounds like a good reason to file a feature request.
This is how my project files are laid out
myProjectFolder
.firebaserc
firebase.json
firestore.indexes.json
functions
Run the commands firebase use myApp-dev then firebase firestore:indexes > firestore.indexes.json to export your current dev project's indexes to a file
myApp-dev and myApp-prod is the "Project ID". To find it in Firebase, click the cog wheel next to "Project Overview" --> Project settings --> General tab (you should see it below)
In file firebase.json make sure it is pointing to the exported firestore.indexes.json for its indexes:
{
"functions": [
{
"source": "functions",
"codebase": "default",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log"
],
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
]
}
],
"firestore": {
"indexes": "firestore.indexes.json"
}
}
Run the commands firebase use myApp-prod and firebase deploy --only firestore:indexes
If the accepted answer isn't working for you (I got a permissions error) for firestore indexes you can go to your firebase console > Cloud firestore > Indexes then open up the network tab in inspector, clear all the requests and refresh the page. Once the page is loaded you can find the JSON formatted response of the indexes (I found mine by searching the word 'indexes' in the search bar of the network tab) in the XHR filter of network requests. It should look something like 'indexes?key=...' you can copy this JSON response.
If you've already initialized firebase in your project with firebase init, you can simply paste it into your project's firestore.indexes.json file.
Then change each name property to a collectionGroup property. eg: 'name': 'projects/[your project name]...' to 'collectionGroup': '[name of collection for this index]'
Run firebase deploy --only firestore:indexes to update any changes made in your text editor back to the firestore indexes tab
for firestore security rules, in a less complicated but similar manner, you can copy and paste the rules shown in the firebase console into the firestore.rules file of your project.
sample firestore.indexes.json file
{
"indexes": [
{
"collectionGroup": "faq",
"queryScope": "COLLECTION",
"fields": [
{
"fieldPath": "searchKeywords",
"arrayConfig": "CONTAINS"
},
{
"fieldPath": "answered",
"order": "ASCENDING"
},
{
"fieldPath": "relevanceScore",
"order": "ASCENDING"
},
{
"fieldPath": "__name__",
"order": "ASCENDING"
}
]
}
]
}
The Cloud Firestore Index Definition Reference page shows how.
You can export indexes with the CLI using firebase
firestore:indexes.
I've set up multiple different indexes on my Firestore development database. Now, I would like to export them into the firestore.indexes.json so that the process of setting up prod environment would be easier. Is there a way to export those indexes using Firebase CLI? The same applies to security rules, although I know that I can copy paste them.
It's possible!
Run from CLI firebase firestore:indexes inside your firebase project folder.
Providing you have indexes already setup and logged into Firebase via the CLI too, you'll get a formatted JSON output for you to copy.
Example:
{
"indexes": [
{
"collectionId": "teslaData",
"fields": [
{
"fieldPath": "Model",
"mode": "ASCENDING"
},
{
"fieldPath": "Price",
"mode": "ASCENDING"
}
]
}
]
}
Exported indexes can be re imported using firebase deploy --only firestore:indexes. Check following doc extract.
https://firebase.google.com/docs/firestore/query-data/indexing
You can also deploy indexes with the Firebase CLI. To get started, run
firebase init firestore in your project directory. During setup, the
Firebase CLI generates a JSON file with the default indexes in the
correct format. Edit the file to add more indexes and deploy it with
the firebase deploy command. If you only want to deploy indexes, add
the --only firestore:indexes flag. If you make edits to the indexes
using the Firebase console, make sure you also update your local
indexes file.
I'm using Firebase CLI 4.2.1 if that helps.
Edit: It's still working as of 9.6.0.
In your Firebase project folder execute this in the terminal:
firebase firestore:indexes > firestore.indexes.json
And it will save a file called firestore.indexes.json with your indexes.
You can then upload that file onto other Firebase projects.
I don't think there is currently an API for getting the Firestore security rules from a project. You can deploy rules through the CLI, which can also be embedded in custom Node scripts, and invoked from CI processes. But as far as I know there is no API to read the rules from a project.
It sounds like a good reason to file a feature request.
This is how my project files are laid out
myProjectFolder
.firebaserc
firebase.json
firestore.indexes.json
functions
Run the commands firebase use myApp-dev then firebase firestore:indexes > firestore.indexes.json to export your current dev project's indexes to a file
myApp-dev and myApp-prod is the "Project ID". To find it in Firebase, click the cog wheel next to "Project Overview" --> Project settings --> General tab (you should see it below)
In file firebase.json make sure it is pointing to the exported firestore.indexes.json for its indexes:
{
"functions": [
{
"source": "functions",
"codebase": "default",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log"
],
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
]
}
],
"firestore": {
"indexes": "firestore.indexes.json"
}
}
Run the commands firebase use myApp-prod and firebase deploy --only firestore:indexes
If the accepted answer isn't working for you (I got a permissions error) for firestore indexes you can go to your firebase console > Cloud firestore > Indexes then open up the network tab in inspector, clear all the requests and refresh the page. Once the page is loaded you can find the JSON formatted response of the indexes (I found mine by searching the word 'indexes' in the search bar of the network tab) in the XHR filter of network requests. It should look something like 'indexes?key=...' you can copy this JSON response.
If you've already initialized firebase in your project with firebase init, you can simply paste it into your project's firestore.indexes.json file.
Then change each name property to a collectionGroup property. eg: 'name': 'projects/[your project name]...' to 'collectionGroup': '[name of collection for this index]'
Run firebase deploy --only firestore:indexes to update any changes made in your text editor back to the firestore indexes tab
for firestore security rules, in a less complicated but similar manner, you can copy and paste the rules shown in the firebase console into the firestore.rules file of your project.
sample firestore.indexes.json file
{
"indexes": [
{
"collectionGroup": "faq",
"queryScope": "COLLECTION",
"fields": [
{
"fieldPath": "searchKeywords",
"arrayConfig": "CONTAINS"
},
{
"fieldPath": "answered",
"order": "ASCENDING"
},
{
"fieldPath": "relevanceScore",
"order": "ASCENDING"
},
{
"fieldPath": "__name__",
"order": "ASCENDING"
}
]
}
]
}
The Cloud Firestore Index Definition Reference page shows how.
You can export indexes with the CLI using firebase
firestore:indexes.
I've set up multiple different indexes on my Firestore development database. Now, I would like to export them into the firestore.indexes.json so that the process of setting up prod environment would be easier. Is there a way to export those indexes using Firebase CLI? The same applies to security rules, although I know that I can copy paste them.
It's possible!
Run from CLI firebase firestore:indexes inside your firebase project folder.
Providing you have indexes already setup and logged into Firebase via the CLI too, you'll get a formatted JSON output for you to copy.
Example:
{
"indexes": [
{
"collectionId": "teslaData",
"fields": [
{
"fieldPath": "Model",
"mode": "ASCENDING"
},
{
"fieldPath": "Price",
"mode": "ASCENDING"
}
]
}
]
}
Exported indexes can be re imported using firebase deploy --only firestore:indexes. Check following doc extract.
https://firebase.google.com/docs/firestore/query-data/indexing
You can also deploy indexes with the Firebase CLI. To get started, run
firebase init firestore in your project directory. During setup, the
Firebase CLI generates a JSON file with the default indexes in the
correct format. Edit the file to add more indexes and deploy it with
the firebase deploy command. If you only want to deploy indexes, add
the --only firestore:indexes flag. If you make edits to the indexes
using the Firebase console, make sure you also update your local
indexes file.
I'm using Firebase CLI 4.2.1 if that helps.
Edit: It's still working as of 9.6.0.
In your Firebase project folder execute this in the terminal:
firebase firestore:indexes > firestore.indexes.json
And it will save a file called firestore.indexes.json with your indexes.
You can then upload that file onto other Firebase projects.
I don't think there is currently an API for getting the Firestore security rules from a project. You can deploy rules through the CLI, which can also be embedded in custom Node scripts, and invoked from CI processes. But as far as I know there is no API to read the rules from a project.
It sounds like a good reason to file a feature request.
This is how my project files are laid out
myProjectFolder
.firebaserc
firebase.json
firestore.indexes.json
functions
Run the commands firebase use myApp-dev then firebase firestore:indexes > firestore.indexes.json to export your current dev project's indexes to a file
myApp-dev and myApp-prod is the "Project ID". To find it in Firebase, click the cog wheel next to "Project Overview" --> Project settings --> General tab (you should see it below)
In file firebase.json make sure it is pointing to the exported firestore.indexes.json for its indexes:
{
"functions": [
{
"source": "functions",
"codebase": "default",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log"
],
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
]
}
],
"firestore": {
"indexes": "firestore.indexes.json"
}
}
Run the commands firebase use myApp-prod and firebase deploy --only firestore:indexes
If the accepted answer isn't working for you (I got a permissions error) for firestore indexes you can go to your firebase console > Cloud firestore > Indexes then open up the network tab in inspector, clear all the requests and refresh the page. Once the page is loaded you can find the JSON formatted response of the indexes (I found mine by searching the word 'indexes' in the search bar of the network tab) in the XHR filter of network requests. It should look something like 'indexes?key=...' you can copy this JSON response.
If you've already initialized firebase in your project with firebase init, you can simply paste it into your project's firestore.indexes.json file.
Then change each name property to a collectionGroup property. eg: 'name': 'projects/[your project name]...' to 'collectionGroup': '[name of collection for this index]'
Run firebase deploy --only firestore:indexes to update any changes made in your text editor back to the firestore indexes tab
for firestore security rules, in a less complicated but similar manner, you can copy and paste the rules shown in the firebase console into the firestore.rules file of your project.
sample firestore.indexes.json file
{
"indexes": [
{
"collectionGroup": "faq",
"queryScope": "COLLECTION",
"fields": [
{
"fieldPath": "searchKeywords",
"arrayConfig": "CONTAINS"
},
{
"fieldPath": "answered",
"order": "ASCENDING"
},
{
"fieldPath": "relevanceScore",
"order": "ASCENDING"
},
{
"fieldPath": "__name__",
"order": "ASCENDING"
}
]
}
]
}
The Cloud Firestore Index Definition Reference page shows how.
You can export indexes with the CLI using firebase
firestore:indexes.
I'm following this tutorial Getting Started with Firebase Hosting on the Web - Firecasts
I'm trying to host a simple index.html file in firebase but when I type firebase deploy its gives me the following error:
No targets found. Valid targets are: database,storage,functions,hosting. hosting error image
I found my firebase.json file is empty it contains only { } .
run firebase init again.
When you are asked for - Which Firebase CLI features do you want to setup for this folder?
Press Space to select features, it will mark that feature with asterisk.
Then Enter to confirm your choices.
Make sure you select the options by pressing spacebar, then press enter.
run firebase init and if your firebase.json is empty with only {} then add
{
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}
replace "public": "app" with "public": "public"
then run firebase deploy
I am trying to use the cleanUrls option to drop .html in routes. Works nicely locally with firebase serve. But once deployed to prod, .html is required.
My test site: https://maptennis.firebaseapp.com/
Click on Login will drive you to /login and gives a 404. Append .html to the URL and it will work.
My firebase.json config file:
{
"database": {
"rules": "database.rules.json"
},
"hosting": {
"public": "public"
},
"cleanUrls": true,
"trailingSlash": false
}
Any insight here would be great.
I was facing similar issue. My firebase.json file looked like this:
{
"hosting": {
"public": "public"
},
"cleanUrls": true
}
I noticed I was using an older version of Firebase CLI. When I updated to the latest version and tried to deploy with same config file, following error occurred:
hosting: We found a hosting key inside firebase.json as well as
hosting configuration keys that are not nested inside the hosting key.
Please run firebase tools:migrate to fix this issue. Please note that
this will overwrite any configuration keys nested inside the hosting
key with configuration keys at the root level of firebase.json.
Error: Hosting key and legacy hosting keys are both present in
firebase.json.
I changed my json config file to this:
{
"hosting": {
"public": "public",
"cleanUrls": true
}
}
Deploy was successful and cleanUrls worked this time :)