【问题标题】:Is it possible to restrict firebase firestore access to only service accounts and API keys using cloud firestore rules?是否可以使用云 firestore 规则将 firebase firestore 访问权限限制为仅服务帐户和 API 密钥?
【发布时间】:2022-11-23 21:23:03
【问题描述】:

我正在使用 firebase 的云 firestore 数据库,我想将对该数据库的访问限制为两个通过 API 密钥和一个服务帐户进行身份验证的移动应用程序。

我查看了 firebase documentation on implementing custom rules,但我没有看到任何 API 密钥或服务帐户示例,因此我不确定如何针对此用例使用规则。

从这个 stackoverflow question and answer 看来,使用 firebase admin API 的服务帐户似乎绕过了任何 firestore 安全规则,对吗?如果是这样,完全关闭对我只希望此服务帐户更新的任何 firestore 集合的写访问是否合理?

到目前为止,我已经尝试过类似的方法来允许从服务帐户进行访问,但它没有按预期工作。我不确定云 Firestore 规则是否支持限制对某些 API 密钥的访问。

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /sales_reps/{document=**} {
      allow read: if true;
      allow write: if request.auth.token.email == '<first-service-account-email>';
    }
    match /gps_tracking/{document=**} {
      allow read: if true;
      allow write: if request.auth.token.email == '<second-service-account-email>';
    }
    match /{document=\*\*} {
      allow read: if true;
      allow write: if false;
    }
  }
}

【问题讨论】:

    标签: firebase google-cloud-firestore firebase-security


    【解决方案1】:

    从这个 stackoverflow question and answer 看起来像服务 使用 firebase admin API 的帐户绕过任何 firestore 安全 规则,对吗?

    是的,这是正确的

    如果是这样,完全关闭对任何对象的写访问是否合理? 我只希望此服务帐户能够使用的 firestore 集合 更新?

    是的,因为使用客户端 SDK(或 REST API)的用户不会更新您的集合,所以这是正确的方法。


    所以具体来说不需要使用像这样的规则

    allow write: if request.auth.token.email == '<first-service-account-email>';
    

    做就是了

    allow write: if false;
    

    对于您只希望服务帐户具有写访问权限的所有集合。

    【讨论】:

      猜你喜欢
      • 2021-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      • 2022-11-16
      • 1970-01-01
      • 2017-05-28
      相关资源
      最近更新 更多