Repository navigation
Closed
Labels
Description
Related issues
[REQUIRED] Version info
node: v18.16.0
firebase-functions: 4.3.1
firebase-tools: 12.4.0
firebase-admin: 11.8.0
[REQUIRED] Test case
I have successfully deployed a cloud function:

but it gives PERMISSION_DENIED when triggered.
[REQUIRED] Steps to reproduce
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
admin.initializeApp();
export const setTimestampsOnCreate = functions.firestore
.document("listings/{listingId}")
.onCreate((snapshot) => {
const currentTime = admin.firestore.FieldValue.serverTimestamp();
return snapshot.ref.set(
{
createdAt: currentTime,
updatedAt: currentTime,
},
{
merge: true,
}
);
});
Then, firebase deploy.
[REQUIRED] Expected behavior
createdAt and updatedAt appear in the new listing document.
[REQUIRED] Actual behavior
Nothing appears, and this appears in the Logs Explorer of that function:
{
"textPayload": "Error: 7 PERMISSION_DENIED: Missing or insufficient permissions.\n at callErrorFromStatus (/workspace/node_modules/@grpc/grpc-js/build/src/call.js:31:19)\n at Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client.js:192:76)\n at Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:360:141)\n at Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:323:181)\n at /workspace/node_modules/@grpc/grpc-js/build/src/resolving-call.js:94:78\n at process.processTicksAndRejections (node:internal/process/task_queues:77:11)",
"insertId": "648c880e000cfb9650c27bb0",
"resource": {
"type": "cloud_function",
"labels": {
"region": "us-central1",
"function_name": "setTimestampsOnCreate",
"project_id": "dormati-b29a7"
}
},
"timestamp": "2023-06-16T16:04:30.850838Z",
"severity": "ERROR",
"labels": {
"execution_id": "o5u6vlzf6e9r",
"instance_id": "00c61b117c826b7c9fc18c8a07396f9e616a78e6c171e68f19f1883509553185306afbb9d971675d3d2c7342435e696a0c175110b72872058288"
},
"logName": "projects/dormati-b29a7/logs/cloudfunctions.googleapis.com%2Fcloud-functions",
"trace": "projects/dormati-b29a7/traces/3ad8e82b637efdf9568fc980600644e0",
"receiveTimestamp": "2023-06-16T16:04:31.075976411Z"
}
Were you able to successfully deploy your functions?
Yes
Reacted by Laurent Pellegrino, Waldo Lavaut, Jeff, Matthew Van der Bijl and Mattia Sanfilippo
Activity
laurentpellegrino commentedon Jun 24, 2023
I am experiencing a very similar issue with a v1 function that uses an onCreate Firebase Auth trigger and tries to access Firestore. As a weird thing, it looks like despite it being a v1 function and what the docs say, firebase deploy does not set [email protected] as permission but uses [email protected]. I don't know yet if that's the cause.
laurentpellegrino commentedon Jun 24, 2023
Using the default service account is not working but creating one, giving Cloud datastore user role, and associating this service account to the function fixes the issue.
What is weird is that I didn't touch permissions for the default service account so that's strange that some actions started to fail suddenly.
trevor-rex commentedon Jul 6, 2023
Something similar happened to me as well where I started losing permission access to my functions. I know that v1 functions use the App Engine default service account, and v2 functions use the Compute Engine service account, and it does seem like their wires got crossed somewhere lately and I started assigning function permissions to both just to be safe.
I did notice that a few weeks ago when I would few my v2 function on the Cloud Functions dashboard, it would show the same function endpoint as on the Cloud Run dashboard (e.g.
functionname-projectslug-uc.a.run.app), but now on the functions dashboard it shows the old v1 endpoint formatus-central1-project-name.cloudfunctions.net/functionName, and the Cloud Run dashboard shows the former endpoint separately. Maybe these are correlated and maybe they're not, but these two services are closely connected behind the scenes.Blunderchips commentedon Sep 25, 2023
We are having a similar issue. We have a utility trigger that injects a timestamp value when a document is created or updated for auditing (very similar to OP). The only thing the trigger does is add/edit a value with a ISO date string. The code works on gen 1 with the only change being changing to use gen 2.
The error in full:
We are using node 18 and keep all our dependencies up to date.
n-sviridenko commentedon Oct 27, 2023
Same issue here, but with firestore. Any news?
Blunderchips commentedon Oct 28, 2023
@taeold ?
syedashar1 commentedon Oct 28, 2023
Same issue here, but with firebase-admin
taeold commentedon Nov 6, 2023
Hi folks. This issue seems to me like an issue with the default service accounts associated with the function. Usually, these service accounts are granted Editor role which grants wide-array of permissions like ability to read/write to Firestore.
However, there are organizational/security policies that prevents automatically grants the default service account the Editor role:
https://cloud.google.com/resource-manager/docs/organization-policy/restricting-service-accounts#disable_service_account_default_grants
You can always manually grant the Editor role to your default service account via by visiting GCP IAM page:
{project-id}@appspot.gserviceaccount.com{project-number}[email protected]Can someone confirm if the organizational policy could be at fault here?
google-oss-bot commentedon Nov 15, 2023
Hey @kevinnammour. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 3 days I will close it automatically.
If you have more information that will help us get to the bottom of this, just add a comment!
34 remaining items
t-genc commentedon Mar 18, 2024
If anyone still facing this issue, the first step is to ensure that you're logged in with the correct Firebase account using the Firebase CLI.
fernandojoliveira commentedon Mar 21, 2024
Also worked for me!!
Although I am using Gen1 I had to give to Compute Engine the Editor role
Thanks
insivika commentedon Mar 31, 2024
This!!
HenrikBechmann commentedon Apr 14, 2024
@lpellegr I get insufficient permissions with a version 1 functions call
in which I try to set up a user, account (and two more) documents.
You say "[1]creating one [service account], [2]giving Cloud datastore user role, and [3]associating this service account to the function fixes the issue.", but I don't know how to do any of of those three things. Can you direct me to some documentation?
Thanks.
HenrikBechmann commentedon Apr 15, 2024
@lpellegr Never mind, I've decide to abandon this snake pit, and do the onCreate and onDelete procedures at the front end.
As a general comment, I'm a bit surprised that there is such a snake pit! It's Firebase!!
jackstruck commentedon Aug 10, 2024
I was able to get this to work when deploying via a github action by manually specifying the compute engine service account within the gcloud cli command per these instructions.
--service-account SERVICE_ACCOUNT_EMAILbrnhffmnn commentedon Aug 15, 2024
The permission recommendations suggest to remove the Editor role:
I applied the recommendation some time ago. Reverted it now. Functions work again.
narimannemo commentedon Aug 16, 2024
Amazing, mate! This worked for me! I had this issue with Cloud Run, and it seems the only fix is exactly what you mentioned—manually configuring the Compute Engine service account using the gcloud CLI command. After the frustration of trying the other solutions, I gave yours a shot, and voilà! Fixed! Thanks a ton!
grascioni commentedon Sep 10, 2024
Took a minute or two after the change was saved, but granting the Editor role to my Compute service account in the IAM console also worked for me.
Toolenaar commentedon Nov 11, 2024
I am having the same issues. All of a sudden my cloudrun service cant access my Firestore database anymore. I have tried to change the permissions of my service account in IAM but without any success.
dbertolini commentedon Nov 27, 2024
Still having this issue
nmayo-earthblox commentedon Dec 12, 2024
I had this problem after updating my versions of various libraries:
firebase-functionsfrom v4.5.0 to v6.1.1firebase-adminfrom 12.0.0 to v13.0.1@google-cloud/firestorefrom 4.15.1 to 7.11.0But it disappeared when I started importing and using a Firestore object from
firebase-adminrather than@google-cloud/firestore.k-garcia-veho commentedon Jan 4, 2025
Thanks to everyone on here who added all the information. I encountered a subset of similar problems when migrating my project from a personal google account to my organization's.
First, I was unable deploy my firebase functions to the new account, an error returned:
Gen1 operation for function myFunction:(leaving out path) Build failed: failed to Fetch: failed to download archive gs://gcf-sources-XXXXXXXXX-us-central1/rXXXXXX-XXXXX-XXXX-XXXX-XXX-XXXXXXXXXXX/version-1/function-source.zip: Access to bucket XXXXXX denied. You must grant Storage Object Viewer permission to [email protected]. If you are using VPC Service Controls, you must also grant it access to your service perimeter.The error message listed the service account that was being used to deploy the functions. When I checked IAM accounts, it was not listed. I had to add the account manually and grant it the Storage Object Viewer permission. I believe I had to grant it a couple other permissions but I cannot recall. These were useful in figuring out what to change:
Once the permissions were added, the error messaged changed to something along the lines of:
Unable to set the invoker for the IAM policy on the following functionsSo while the functions were deploying, the system wasn't able to invoke them as part of the process. I was able to fix this with this information:
I am pretty sure I also had to go to Cloud Run Functions in google cloud console, check all my functions and add a principal to them, AllUsers or AllAuthenticatedUsers so my app could invoke the functions.
I was able to deploy the functions but then I was unable to run it as per THIS ISSUE because permission was DENIED. I was able to grant permissions this way:
I believe what has happened is related to Google making changes on policy.
So if you created a project after May 3, 2024 the app service account did not get an IAM policy and has no permissions. The really messed up part, specially for me (not experienced), is that IAM list a number of accounts associated to the project but not the app service account. I was granting editor roles to all the accounts listed with no avail. IF you access service accounts through cloud console or the last link listed just above, you would see that the app service account is listed. It is normally [email protected]. IF you go and check IAM and that account is not listed, you need to add it manually and give it editor role. In my case I gave it that and Firestore Service Agent because my issue was related to firestore access.
After doing this, it didn't work immediately but 3 or 5 minutes later the functions started to execute appropriately. I think all this information is already posted in different forms, hope this clarifies it a bit.
taeold commentedon Mar 26, 2025
Hi all - thank you all very much for advices on how to resolve this issue.
It looks like #1425 (comment) has been most helpful so far, so I'd repeat it here:
===
Hi folks. This issue seems to me like an issue with the default service accounts associated with the function. Usually, these service accounts are granted Editor role which grants wide-array of permissions like ability to read/write to Firestore.
However, there are organizational/security policies that prevents automatically grants the default service account the Editor role:
https://cloud.google.com/resource-manager/docs/organization-policy/restricting-service-accounts#disable_service_account_default_grants
You can always manually grant the Editor role to your default service account via by visiting GCP IAM page:
{project-id}@appspot.gserviceaccount.com{project-number}[email protected]Can someone confirm if the organizational policy could be at fault here?
===
For context, this issue may have hit your project due to a change made in how default compute engine accounts are granted permission for project are created after May 3, 2024:
The common workarounds are:
Manually granting Editor role for service account associated with your function
Creating a custom service account, granting specific roles required for function to operate, and attaching the service account to the function using the
serviceAccountconfigurationI'll close the issue, but feel free to create a new issue or feel free to reach out to our free Firebase Support for your project-specific inquiries.