我最后尝试使用 terraform 设置一个谷歌项目,其后端应该使用 firebase auth 实例进行验证。为此,我需要一个通常在 Firebase Web 控制台中生成的凭据文件
Project Settings
> Service Accounts
> Firebase Admin SDK
> Generate new private key
。
因为我想自动检索该文件,所以我正在寻找如何使用 terraform 自己生成该文件,但我没有找到任何相关文档。文件结构如下
{
"auth_provider_x509_cert_url":
"auth_uri":
"client_email":
"client_id":
"client_x509_cert_url":
"private_key":
"private_key_id":
"project_id":
"token_uri":
"type":
"universe_domain":
}
在项目中,我将使用
env
设置一个 GOOGLE_APPLICATION_CREDENTIALS="/firebase-service-account.json"
变量来告诉在哪里可以找到我的凭证文件。
在我的 terraform 文件中我做了类似的事情
resource "google_service_account" "sa_backend_firebase_adminsdk" {
account_id = "sa-backend-firebase-adminsdk"
display_name = "Backend Firebase SDK SA"
project = var.GCLOUD_PROJECT_ID
}
resource "google_service_account_key" "sa_backend_firebase_adminsdk_key" {
service_account_id = google_service_account.sa_backend_firebase_adminsdk.name
private_key_type = "TYPE_GOOGLE_CREDENTIALS_FILE"
}
locals {
private_key_id = split("/", google_service_account_key.sa_backend_firebase_adminsdk_key.id)[5]
service_key_account_json = jsonencode({
type = "service_account",
project_id = "${var.GCLOUD_PROJECT_ID}",
private_key_id = "${local.private_key_id}",
private_key = "-----BEGIN PRIVATE KEY-----\n${local.private_key}\n-----END PRIVATE KEY-----\n",
client_email = "${google_service_account.sa_backend_firebase_adminsdk.email}",
client_id = "${google_service_account.sa_backend_firebase_adminsdk.unique_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 = "https://www.googleapis.com/robot/v1/metadata/x509/${google_service_account.sa_backend_firebase_adminsdk.email}",
universe_domain = "googleapis.com"
})
}
resource "local_sensitive_file" "sa_backend_firebase_adminsdk_private_key_file" {
content = local.service_key_account_json
filename = "${path.module}/${var.GCLOUD_PROJECT_NAME}-private-key.json"
}
最后我还有两个细粒度的附加问题,因为它们可能会导致该过程无法正常工作:
client_x509_cert_url
https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-`39pwf%40m` 而我生成的文件包含 https:// /www.googleapis.com/robot/v1/metadata/x509/sa-backend-firebase-adminsdk`@`您不需要自己发明这个,terraform已经有资源来创建密钥文件:https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/google_service_account_key#private_key-1
你可能需要这样的东西:
resource "google_service_account" "sa" {
account_id = var.sa_name
}
resource "google_service_account_key" "sa_key" {
service_account_id = google_service_account.sa.email
}
output "trigger_screening_key" {
value = base64decode(google_service_account_key.sa_key.private_key)
sensitive = true
}
请注意顶部有关私钥以明文形式存储在状态文件中的警告。