I have a Google Cloud Function designed to fetch a Firestore document.
The function works when I use the default Compute Engine service account. However, due to security reasons, I want to adopt the principle of least privilege and create a unique service account with only the specific roles required for my function.
I've created a new service account named [email protected] and attached it to the function. But currently, this service account does not have any permissions. So it can't view the Firestore document, and the function fails with a permission error.
I am attempting to assign the roles/datastore.viewer role to the service account using Google Cloud Console, as I believe this will provide the necessary permissions to retrieve the Firestore document. These were my steps:
[email protected]But then the interface that appears is confusing.
It wants me to add a new principal, which I don't think I need to do. It's my understanding that the principal in this context would be the service account itself. Also, I can't find the Firestore Viewer role (roles/datastore.viewer) in the dropdown box. There's only a small amount of roles to choose from, I think many are missing.
Could someone guide me through the process of assigning the Firestore Viewer role to my new service account, either via Google Cloud Console or gcloud CLI?
This was incredibly frustrating, but I figured it out in the end.
Using the console:
The problem was that when creating a service account there is a wizard with 3 steps. Step 2 and 3 are marked as optional. And if you skip over step 2 then the service account is created with no roles.
So this entire situtation can be avoided by just assigning the required roles during step 2 of the wizard of the service account creation.
If you have already created the service account without any roles, then it needs to be done at the "project level". By following:
Using gcloud:
You can also use the below gcloud command to create a service account:
gcloud iam service-accounts create SA_NAME \
--description="DESCRIPTION" \
--display-name="DISPLAY_NAME"
And then use the following gcloud command to add a role:
gcloud projects add-iam-policy-binding PROJECT_ID \
--member="serviceAccount:SA_NAME@PROJECT_ID.iam.gserviceaccount.com" \
--role="ROLE_NAME"
Note: It seems you cannot add multiple roles with one gcloud command. So just repeat the same command if you want to add multiple roles.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With