为项目自动设置 BQ 审计日志接收器

BQ audit logs sink set automatically for a project

每次新建项目,在项目中使用BQ,都需要手动设置BQ logs sink,包括filter:

example of BQ logs sink

filter of logs

问题 - 有没有办法让它自动化,一旦为新项目创建了 BQ 资源,它就会自动创建一个接收器? 它应该有:

接收器名称:BQLogsDataset_[ProjectNameOrID]

目的地:bigquery.googleapis.com/projects/billing/datasets/BQLogsDataset

作家身份:会创造一个合适的

将应用过滤器:resource.type="bigquery_resource" logName="projects/[ProjectNameOrID]/logs/cloudaudit.googleapis.com%2Fdata_access"

可能有人已经实现了类似的或有任何其他解决方案如何在每次创建具有 BQ 资源的新项目时不忘记启用 BQ 日志记录?

谢谢!

我的建议是使用可以通过 API 定义的组织级接收器。可以在 link

中找到对 API 的引用

这是有关设置的文档中的相关描述:

A parent resource—a project, folder, billing account, or organization where in which the sink is created. The sink can only export logs belonging to the parent resource. The parents have the following formats:

"projects/[PROJECT_ID]" "organizations/[ORGANIZATION_ID]" "folders/[FOLDER_ID]" "billingAccounts/[BILLING_ACCOUNT_ID]"

An include children parameter. This is only relevant to sinks within organizations. See Aggregated exports.

注意:此设置目前只能通过 API 完成,不能通过 google 控制台 UI

您可以在组织级别创建 Bigquery 接收器并启用 --include-children 标志,这将配置组织中的所有项目。

示例 gcloud 命令:

gcloud logging sinks create my-org-bq-sink bigquery.googleapis.com/projects/my-gcp-project/datasets/my_bq_audit_dataset --include-children --organization=MY_ORG_ID

有关此 gcloud 命令的更多详细信息here

如果您使用的是 Terraform,那么您可以使用 google_logging_organization_sink

希望这对您有所帮助。

解决方案 - 使用下面的命令为项目(以及下面的子项)设置接收器。 BQ 日志也将被相应地过滤:

gcloud logging sinks create <My-sink-name> bigquery.googleapis.com/projects/<My-billing-project-ID>/datasets/<My-dataset-name> --include-children --log-filter='resource.type="bigquery_resource" AND logName:"cloudaudit.googleapis.com/data_access"' --organization=<My-organization-ID>