通过服务帐户限制 Firestore 访问

问题描述 投票:0回答:1

我有一个节点应用程序,它使用 firebase sdk 读取/写入 firestore 数据库。

  "dependencies": {
    "firebase": "^9.12.1",
    "firebase-admin": "^10.0.2",
  },
import admin = require('firebase-admin');
import { initializeApp } from 'firebase-admin/app';

const app = initializeApp({
  credential: admin.credential.cert({
    "type": "service_account",
    "project_id": ...,
    "private_key_id": ...,
    "private_key": ...,
    "client_email": "[email protected]",
    "client_id": ...,
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_x509_cert_url": ...
  } as any
  )
});
let db = admin.firestore();

如果我的安全规则完全开放,这个应用程序能够很好地读取/写入 firestore:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
     allow read: if true
     allow write: if true
    }
  }
}

但是,我只想将读/写访问权限限制为应用初始化期间指定的服务帐户(“[电子邮件受保护]”)。

我是否需要在安全规则中添加任何内容来限制仅访问此服务帐户?或者这是由 firestore 隐式完成的?

firebase google-cloud-platform google-cloud-firestore google-iam
1个回答
0
投票

我是否需要在安全规则中添加任何内容来限制仅访问此服务帐户?

从使用服务帐户初始化的后端或管理 SDK(即除 Web 和移动 SDK 之外的任何内容)对 Firestore 的所有访问都会完全绕过安全规则。 规则仅适用于使用为这些平台提供的 SDK 进行的 Web 和移动访问。

或者这是由 firestore 隐式完成的?

每个 Google Cloud 产品都具有 IAM 角色和执行各种操作的权限。 您应该查看文档以找出其中哪些可能适用于您的用例。

© www.soinside.com 2019 - 2024. All rights reserved.