如何从Python';s服务器客户端库?
我正在尝试使用Firestore模拟器测试我的安全规则。我制定了如何从Python';s服务器客户端库?,python,firebase,google-cloud-firestore,firebase-authentication,firebase-security,Python,Firebase,Google Cloud Firestore,Firebase Authentication,Firebase Security,我正在尝试使用Firestore模拟器测试我的安全规则。我制定了firestore.rules安全规则,禁止所有读写操作: service cloud.firestore { match /databases/{database}/documents { allow read, write: if false; } } 我从终端启动了Firestore emulator: firebase emulators:start --only firestore 然后
firestore.rules
安全规则,禁止所有读写操作:
service cloud.firestore {
match /databases/{database}/documents {
allow read, write: if false;
}
}
我从终端启动了Firestore emulator:
firebase emulators:start --only firestore
然后,我初始化了Firestore客户端:
import firebase_admin
from firebase_admin import firestore
firebase_app = firebase_admin.initialize_app()
self.client = firestore.client(app=firebase_app)
我向Firestore emulator的数据库添加了一个示例文档:
self.client.collection("Users").document("user_1").set(...)
这一切都成功了,这是出乎意料的。我不允许所有写入,但我只执行了一次,Firestore emulator成功地确认规则在以下情况下生效:
[info] ✔ firestore: Rules updated.
我终于遇到了以下几个州:
服务器客户端库绕过所有云Firestore安全规则,而是通过Google应用程序默认凭据进行身份验证
是否真的没有办法修改self.client
或firebase\u app
,导致上述写入失败?我无法想象这种类型的身份验证只在客户端受支持(如在iOS/Android设备上)。本页继续介绍:
如果您正在使用服务器客户端库或REST或RPC API,请确保为Cloud Firestore设置身份和访问管理(IAM)
但根据我的实际安全规则,我试图限制对某些集合/文档的访问,而IAM似乎只包括全局权限,例如。只有使用“@firebase/Testing”模块的nodejs应用程序才支持使用仿真器测试安全规则,如下所述: 使用
@firebase/testing
模块与本地运行的仿真器进行交互
使用此模块,您可以初始化SDK以提供用户信息,这些信息将被传递到模拟器,用于测试规则的特定目的
如果您想改用python,则必须对该模块的功能进行反向工程,并将其写入您自己的测试代码中。这可能比学习足够多的JavaScript来执行文档中描述的测试更费劲。感谢您对文档的澄清。我将用JS编写测试,但要澄清的是:如果安全规则部署到生产环境中,并且我从Python客户端库连接到生产Firestore数据库,那么编写是否会失败?我链接的关于绕过安全规则的客户端库的引文使这有点混乱。还是我也必须用JS编写生产代码?服务器SDK总是绕过安全规则。规则仅适用于使用为这些平台提供的SDK的web和移动客户端。