我可以使用 gcloud 命令调整服务帐户的权限并在 firebase 函数中启用对存储桶的写访问权限吗?
Can I use the gcloud command to adjust permissions for a service account and enable write access to a storage bucket inside firebase functions?
我有一个 firebase 函数,我想允许对云存储的写访问。我相信我需要设置一个具有这些权限的服务帐户,然后在我的函数中以编程方式 g运行t 它们,但我很困惑如何执行此操作。
firebase 函数在触发器上将文件写入存储桶。 firebase 存储的存储设置设置为默认值,这意味着它们需要对客户端进行身份验证:
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
在本文档 (https://cloud.google.com/functions/docs/concepts/iam) 中,在 "Runtime service account" 下,我看到了这个:
At runtime, Cloud Functions uses the service account
PROJECT_ID@appspot.gserviceaccount.com, which has the Editor role on
the project. You can change the roles of this service account to limit
or extend the permissions for your running functions.
当它说 "runtime," 时,我假设这意味着 firebase 函数在该服务帐户的上下文中运行,并且授予它 运行 权限。因此,我假设我需要确保该服务帐户的权限具有写入权限,正如我从 link (https://console.cloud.google.com/iam-admin/roles?authuser=0&consoleUI=FIREBASE&project=blahblah-2312312).
中看到的那样
我看到名为 storage.objects.create
的权限,并且假设我需要将其添加到服务帐户。
为了调查服务帐户的当前设置,我 运行 这些命令:
$ gcloud iam service-accounts describe blahblah-2312312@appspot.gserviceaccount.com
displayName: App Engine default service account
email: blahblah-2312312@appspot.gserviceaccount.com
etag: BwVwvSpcGy0=
name: projects/blahblah-2312312/serviceAccounts/blahblah-2312312@appspot.gserviceaccount.com
oauth2ClientId: '98989898989898'
projectId: blahblah-2312312
uniqueId: '12312312312312'
$ gcloud iam service-accounts get-iam-policy blahblah-2312312@appspot.gserviceaccount.com
etag: ACAB
我不确定是否有办法从中获取更多详细信息,也不确定 etag ACAB 表示什么。
查看此文档后 (https://cloud.google.com/iam/docs/granting-roles-to-service-accounts) 我认为我需要 g运行t 权限。但是,我不完全确定如何从 JSON 示例开始,它应该是什么结构,然后关联策略,或者这是否是正确的路径。
{
"bindings": [
{
"role": "roles/iam.serviceAccountUser",
"members": [
"user:alice@gmail.com"
]
},
{
"role": "roles/owner",
"members": [
"user:bob@gmail.com"
]
}
],
"etag": "BwUqLaVeua8=",
}
例如,我的问题是:
- 我需要自己制作电子标签吗?
- 我在成员数组中使用什么电子邮件地址?
我看到这个命令被列为示例
gcloud iam service-accounts add-iam-policy-binding \
my-sa-123@my-project-123.iam.gserviceaccount.com \
--member='user:jane@gmail.com' --role='roles/editor'
我不明白的是为什么我必须指定两个准电子邮件地址。一个是服务帐户,一个是与服务帐户关联的用户。这是否意味着用户 jane@gmail.com
可以在服务帐户的凭据下操作?我可以只让服务帐户拥有我在云功能中使用的权限吗?
是否有更简单的方法可以仅使用命令行来执行此操作,而无需手动编辑JSON?
然后,一旦我正确建立了我的凭据,我是否需要使用 JSON 服务帐户文件,如许多示例所示:
var admin = require('firebase-admin');
var serviceAccount = require('path/to/serviceAccountKey.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://<DATABASE_NAME>.firebaseio.com'
});
或者,我可以只调用 admin.initializeApp()
并且因为“...在运行时,Cloud Functions 使用服务帐户 PROJECT_ID@appspot.gserviceaccount.com...”函数会自动获取那些权限?
问题是(如此处记录:)我错误地将存储桶的第一个参数指定为存储桶中的 子目录 而不是 只是一个桶。这意味着存储认为我正在尝试访问一个不存在的存储桶,并且我收到了权限错误。
我有一个 firebase 函数,我想允许对云存储的写访问。我相信我需要设置一个具有这些权限的服务帐户,然后在我的函数中以编程方式 g运行t 它们,但我很困惑如何执行此操作。
firebase 函数在触发器上将文件写入存储桶。 firebase 存储的存储设置设置为默认值,这意味着它们需要对客户端进行身份验证:
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
在本文档 (https://cloud.google.com/functions/docs/concepts/iam) 中,在 "Runtime service account" 下,我看到了这个:
At runtime, Cloud Functions uses the service account PROJECT_ID@appspot.gserviceaccount.com, which has the Editor role on the project. You can change the roles of this service account to limit or extend the permissions for your running functions.
当它说 "runtime," 时,我假设这意味着 firebase 函数在该服务帐户的上下文中运行,并且授予它 运行 权限。因此,我假设我需要确保该服务帐户的权限具有写入权限,正如我从 link (https://console.cloud.google.com/iam-admin/roles?authuser=0&consoleUI=FIREBASE&project=blahblah-2312312).
中看到的那样我看到名为 storage.objects.create
的权限,并且假设我需要将其添加到服务帐户。
为了调查服务帐户的当前设置,我 运行 这些命令:
$ gcloud iam service-accounts describe blahblah-2312312@appspot.gserviceaccount.com
displayName: App Engine default service account
email: blahblah-2312312@appspot.gserviceaccount.com
etag: BwVwvSpcGy0=
name: projects/blahblah-2312312/serviceAccounts/blahblah-2312312@appspot.gserviceaccount.com
oauth2ClientId: '98989898989898'
projectId: blahblah-2312312
uniqueId: '12312312312312'
$ gcloud iam service-accounts get-iam-policy blahblah-2312312@appspot.gserviceaccount.com
etag: ACAB
我不确定是否有办法从中获取更多详细信息,也不确定 etag ACAB 表示什么。
查看此文档后 (https://cloud.google.com/iam/docs/granting-roles-to-service-accounts) 我认为我需要 g运行t 权限。但是,我不完全确定如何从 JSON 示例开始,它应该是什么结构,然后关联策略,或者这是否是正确的路径。
{
"bindings": [
{
"role": "roles/iam.serviceAccountUser",
"members": [
"user:alice@gmail.com"
]
},
{
"role": "roles/owner",
"members": [
"user:bob@gmail.com"
]
}
],
"etag": "BwUqLaVeua8=",
}
例如,我的问题是:
- 我需要自己制作电子标签吗?
- 我在成员数组中使用什么电子邮件地址?
我看到这个命令被列为示例
gcloud iam service-accounts add-iam-policy-binding \
my-sa-123@my-project-123.iam.gserviceaccount.com \
--member='user:jane@gmail.com' --role='roles/editor'
我不明白的是为什么我必须指定两个准电子邮件地址。一个是服务帐户,一个是与服务帐户关联的用户。这是否意味着用户 jane@gmail.com
可以在服务帐户的凭据下操作?我可以只让服务帐户拥有我在云功能中使用的权限吗?
是否有更简单的方法可以仅使用命令行来执行此操作,而无需手动编辑JSON?
然后,一旦我正确建立了我的凭据,我是否需要使用 JSON 服务帐户文件,如许多示例所示:
var admin = require('firebase-admin');
var serviceAccount = require('path/to/serviceAccountKey.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://<DATABASE_NAME>.firebaseio.com'
});
或者,我可以只调用 admin.initializeApp()
并且因为“...在运行时,Cloud Functions 使用服务帐户 PROJECT_ID@appspot.gserviceaccount.com...”函数会自动获取那些权限?
问题是(如此处记录: