Firebase云函数权限被拒绝

10
在Firebase云函数中,我在实时数据库的读写操作上遇到了admin-firebase的访问被拒绝的问题。此处https://github.com/firebase/firebase-functions/issues/16并未解决我的问题,App engine默认服务帐号权限设置为编辑器。
我还配置了Node.js管理员sdk和服务账户密钥,这里一切正常运行。
我已经在数据库中设置了默认安全规则。
以下是示例函数。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
admin.database.enableLogging(true);

//event fires properly
exports.sendNotif = functions.database.ref('/messages/{roomId}/{msgKey}')
    .onWrite(event => {
        const message = event.data.val().text;
    //this executes as expected
        console.log(message);

//here I am getting access denied and consequently function timeout after 60 seconds.
        return admin.database().ref('/userFCMTokens').once('value')
            .then(snap => console.log(snap.val()));
    });

以下是来自 Firebase 实时数据库的日志片段:

Realtime connection established.  
2017-06-08T15:19:03.168Z I sendNotif: p:0: connection ready  
2017-06-08T15:19:03.168Z I sendNotif: p:0: {"r":28,"a":"gauth","b":{"cred”:”********************”}}  
2017-06-08T15:19:03.169Z I sendNotif: p:0: Listen on /userFCMTokens for default  
2017-06-08T15:19:03.169Z I sendNotif: p:0: {"r":29,"a":"q","b":{"p":"/userFCMTokens","h":""}}  
2017-06-08T15:19:03.538Z I sendNotif: p:0: from server: {"r":28,"b":{"s":"permission_denied","d":"Access denied."}}  
2017-06-08T15:19:03.538Z I sendNotif: Auth token revoked: permission_denied/Access denied. 
2017-06-08T15:19:03.538Z I sendNotif: c:0:13: Closing realtime connection.  
2017-06-08T15:19:03.538Z I sendNotif: c:0:13: Shutting down all connections  
2017-06-08T15:19:03.538Z I sendNotif: c:0:13:0 WebSocket is being closed 

以下简述无需登录实时数据库:

2017-06-08T15:26:23.164035495Z D sendNotif: Function execution started
2017-06-08T15:26:23.164076543Z D sendNotif: Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions
2017-06-08T15:26:23.539Z I sendNotif: message console logged
2017-06-08T15:27:23.165321703Z D sendNotif: Function execution took 60002 ms, finished with status: 'timeout'

为什么我不能在云函数中使用管理员帐户进行读写操作?


请问为什么您使用Firebase Admin SDK而不是使用event.data.adminRef.root,然后构建到“/userFCMTokens”的路径呢? - Jen Person
我正在参考这个例子,https://github.com/firebase/functions-samples/blob/master/fcm-notifications/functions/index.js。我会尝试按照你的建议去做。 - Filip P.
我知道了!好的,这并没有解决为什么出现permission_denied错误的问题,但是我建议尝试使用event.data.adminRef.root而不是admin.database.ref()。 - Jen Person
您能否在最新的 SDK 上使用新项目实现相同的行为?这似乎是针对特定项目的错误,类似于我在需要从 alpha/beta 升级的旧项目中看到的问题。 - Kato
在新项目中一切正常。出现此行为的项目是大约4个月前创建的。但是最近创建了云函数,使用了npm依赖项“firebase-admin”:“〜4.2.1”,“firebase-functions”:“^0.5.7”。 - Filip P.
在将引用从admin.database()更改为event.data.adminRef.root.child后,我仍然无法访问。 - Filip P.
3个回答

4

是的,我检查了这些权限,它们被设置为编辑器。实际上,这在我的问题的第二句话中已经说明了。 - Filip P.
抱歉,我错过了那个 John。你可以尝试的另一件事是:在 https://console.cloud.google.com/apis/dashboard 上禁用并重新启用云函数 API。 - laurenzlong
1
我也尝试过这样做,使用 Firebase CLI 提交空函数文件删除了所有函数,然后多次禁用和启用 API。最终,我放弃了在此项目中使用 Firebase 函数的想法。 - Filip P.
如果未来有人看到这篇文章并遇到同样的问题...我试图使用europe-west1部署HTTP函数,但是这不受支持。当删除区域配置(默认为us-central)后,此问题得以解决,而无需提示登录。 - jskidd3
1
有一些类似的问题被报告了,似乎“移除”函数并重新部署可以更新必要的权限。很奇怪。 - Дмитро Булах

3
我需要让我的应用程序中的任何人都能够使用可调用的 https 函数,因此我必须将“所有用户”分配给 GCP 中特定函数的“Cloud Functions Invoker”权限。

enter image description here


2

2022年中期左右的Firebase/GCP现在在文档中提供了清晰的信息:

Firebase Develop Admin         Full read/write access to:
roles/firebase.developAdmin    Google Analytics
                               Firebase App Check
                               ...
                               Cloud Functions for Firebase
                                  (deploying functions requires special configuration)
                               Firebase ML

特殊配置可以使得在给一个普通的编辑者添加角色时,可以添加到roles/cloudfunctions.adminroles/iam.serviceAccountUser。也有其他的选择,比如委托给一个拥有者,但是添加这些角色最有可能是最小化且更加安全的。详细信息请参考特殊配置

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接