Github 操作中使用 applicationDefault 的 Firebase InitializeApp - 错误:凭据文件中的内容无效

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

我有一个nodejs应用程序,使用以下代码来初始化应用程序:

import { applicationDefault, initializeApp } from 'firebase-admin/app';
  const app = initializeApp({
    credential: applicationDefault(),
    projectId: FIREBASE_PROJECT_ID,
  });

我现在尝试在 github-actions 管道中运行测试,首先运行以下步骤以使用工作负载身份联合来向 google 进行身份验证并创建

GOOGLE_APPLICATION_CREDENTIALS
env var,其中包含为服务帐户生成的 json 文件(具有 firebase 管理员权限)。根据文档在非谷歌环境中设置。

相关Github Actions代码:

      - name: Checkout code
        uses: actions/checkout@v3

      - id: 'auth'
        name: 'Authenticate to Google Cloud'
        uses: 'google-github-actions/auth@v2'
        with:
          workload_identity_provider: 'PATH-TO-PROVIDER'
          service_account: '[email protected]'

       ...install dependencies etc...

       - name: Run tests
        run: |
          pnpm run lint
          pnpm run test

注意:从 google-github-actions docs,我已确保

create_credentials_file
export_environment_variables
设置为 true,并通过 SSH 连接到 github 运行程序来验证它们是否存在。

但是,当 firebase 应用程序尝试初始化时,我收到以下错误:

 Error: Invalid contents in the credentials file
  at credentialFromFile (node_modules/.pnpm/[email protected]/node_modules/firebase-admin/lib/app/credential-internal.js:496:11)
  at getApplicationDefault (node_modules/.pnpm/[email protected]/node_modules/firebase-admin/lib/app/credential-internal.js:401:16)
  at applicationDefault (node_modules/.pnpm/[email protected]/node_modules/firebase-admin/lib/app/credential-factory.js:55:80)
  at connectFirebase (src/services/firebase.mts:17:17)
  at Context.<anonymous> (test/test.mts:38:25)
  at process.processImmediate (node:internal/timers:471:21)

有什么想法可以解决这个问题吗?我宁愿避免向 github 操作秘密添加长期存在的 JSON 密钥(作为值添加到 GOOGLE_APPLICATION_CREDENTIALS 变量)。

firebase-authentication github-actions google-oauth
1个回答
0
投票

使用通过 Workload Identity 获取的凭据时,您可以获取 Security Token Service 的访问令牌,但不能将其用作调用 Google Cloud API 的访问令牌,因此您需要获取另一个访问令牌。

最后我可以通过此代码通过工作流程身份调用 firebase api。 https://github.com/lirlia/firebase-remote-config-actions/blob/4fe40103d330447bff63a20e66b8a81c59149c14/src/credential.ts#L34-L82

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