英文:
How to assign a role to the service account of a Google Cloud Function?
问题
我有一个用于获取Firestore文档的Google Cloud函数。
当我使用默认的计算引擎服务帐户时,该函数可以正常工作。但由于安全原因,我想采用最小特权原则,并创建一个仅具有我的函数所需特定角色的唯一服务帐户。
我已经创建了一个名为func-testing-http@myawesomeapp.iam.gserviceaccount.com的新服务帐户,并将其附加到函数上。但目前,该服务帐户没有任何权限。因此,它无法查看Firestore文档,函数会出现权限错误。
我正试图在Google Cloud控制台上为该服务帐户分配roles/datastore.viewer角色,因为我认为这将提供检索Firestore文档所需的权限。以下是我的步骤:
- IAM 和管理员
- 服务帐户
- 点击
func-testing-http@myawesomeapp.iam.gserviceaccount.com - 权限选项卡
- 授权访问按钮
但然后出现的界面令人困惑。
它要求我添加一个新的主体,我认为我不需要这样做。我理解,在这个上下文中,主体本身应该是服务帐户。此外,我在下拉框中找不到Firestore Viewer角色(roles/datastore.viewer)。只有很少一部分角色可供选择,我认为很多角色都不见了。
是否有人可以指导我通过Google Cloud控制台或gcloud CLI为我的新服务帐户分配Firestore Viewer角色的过程?
英文:
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 func-testing-http@myawesomeapp.iam.gserviceaccount.com 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:
- IAM and Admin
- Service Accounts
- Clicked
func-testing-http@myawesomeapp.iam.gserviceaccount.com - Permissions Tab
- Grant Access button
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?
答案1
得分: 0
这真是令人非常沮丧,但我最终解决了。
使用控制台:
问题在于创建服务帐户时有一个包含3个步骤的向导。第2步和第3步被标记为可选。如果跳过第2步,那么服务帐户将不会分配任何角色。
因此,在服务帐户创建向导的第2步中分配所需的角色就可以避免整个情况。
如果您已经创建了不带任何角色的服务帐户,则需要在“项目级别”执行以下操作:
- 点击“IAM 和管理员”。
- 点击“IAM”。您还看不到您的服务帐户,因为它没有任何项目角色。
- 点击“授予访问权限”按钮。
- 在“添加主体”框中输入您的服务帐户电子邮件。您也可以在这里搜索它,它将显示出来。
- 在“分配角色”下拉菜单中分配您的角色。
- 现在刷新IAM页面,您将在列表中看到您的服务帐户。以后您可以在此返回以进行编辑或添加其他角色。
使用gcloud:
您还可以使用以下gcloud命令创建服务帐户:
gcloud iam service-accounts create SA_NAME \
--description="DESCRIPTION" \
--display-name="DISPLAY_NAME"
然后使用以下gcloud命令添加角色:
gcloud projects add-iam-policy-binding PROJECT_ID \
--member="serviceAccount:SA_NAME@PROJECT_ID.iam.gserviceaccount.com" \
--role="ROLE_NAME"
注意:似乎您无法使用一个gcloud命令添加多个角色。因此,如果要添加多个角色,只需重复相同的命令。
英文:
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:
- Click IAM and Admin
- Click IAM. You will not see your service account here yet because it does not have any project roles.
- Click Grand Access button
- In the "Add principals" box input your service account email. You can also search for it here and it will show up.
- In the "Assign Roles" drop down assign your roles
- Now refresh the IAM page and you will see your service account in the list. You can come back here to edit or add additional roles if you want later.
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论