启用 Security Command Center API 通知功能。通知将信息发送到 Pub/Sub 主题,以在几分钟内提供发现结果最新动态和新的发现结果。由于 Security Command Center 在组织级层工作,因此 Security Command Center API 通知包含在 Security Command Center 信息中心内显示的所有发现结果信息。
您可以将 Security Command Center Pub/Sub 通知直接关联到 Cloud Functions 操作。如需查看可在响应、丰富和解决方面提供帮助的安全性函数,请参阅 Security Command Center 工具提醒示例应用。
准备工作
- 要启用 Security Command Center API 通知,您必须具有 Security Center Admin IAM 角色。
- 要访问 Security Command Center 信息中心,您必须具有 Security Center Admin Viewer IAM 角色。
- 要向通知服务帐号或
gcloud
工具帐号授予适当的角色,您必须具有 Organization Administrator IAM 角色。
详细了解 Security Command Center 角色。
设置 Security Command Center API 通知
要设置通知,请先启用 Security Command Center API。
启用 Security Command Center API
要启用 Security Command Center API,请执行以下操作:
- 转到 Cloud Console 中的 页面。
转到“API 库”页面 - 选择您要为其启用 Notifications API 的项目。
- 在搜索框中,输入
Security Command Center
,然后点击搜索结果中的 Security Command Center。 - 在随即显示的 API 页面上,点击启用。
您的项目现已启用 Security Command Center API。接下来,使用 gcloud
工具或客户端库订阅 Pub/Sub 主题并设置权限。
设置 Pub/Sub 主题
在此步骤中,您将创建并订阅要向其发送通知的 Pub/Sub 主题。如果您不需要以编程方式调用 API,建议您使用 gcloud
工具命令,因为更容易设置。
gcloud
要使用 gcloud
工具设置 Security Command Center API 通知功能,请执行以下操作:
- 设置 Pub/Sub 主题和订阅。
- 设置
gcloud
工具帐号权限。
第 1 步:设置 Pub/Sub
如需设置和订阅 Pub/Sub 主题,请执行以下操作:
- 转到 Google Cloud Console。
转到 Google Cloud Console - 选择启用了 Security Command Center API 的项目。
- 点击激活 Cloud Shell。
创建新主题或重复使用现有主题。要创建新主题,请执行以下操作:
gcloud pubsub topics create topic-id
设置环境变量:
设置主题 ID:
export TOPIC_ID=topic-id
创建对主题的订阅:
gcloud pubsub subscriptions create subscription-id --topic topic-id
如需详细了解如何设置 Pub/Sub,请参阅管理主题和订阅。接下来,您需要为帐号设置权限。
第 2 步:设置 gcloud
工具帐号权限
要创建 NotificationConfig
,您的 gcloud
工具帐号需要具备以下 Identity and Access Management (IAM) 角色:
roles/securitycenter.admin
或roles/securitycenter.notificationConfigEditor
- 要接收通知的 Pub/Sub 主题上的
roles/pubsub.admin
如需授予这些权限,请执行以下操作:
- 转到 Google Cloud Console。
转到 Google Cloud Console - 选择启用了 Security Command Center API 的项目。
点击激活 Cloud Shell。
设置您的组织名称:
export ORGANIZATION_ID=organization-id
为 Pub/Sub 主题所属的项目设置项目 ID:
export PUBSUB_PROJECT=project-id
设置您要使用的
gcloud
工具帐号:export GCLOUD_ACCOUNT=your-username@email.com
设置主题 ID 或使用第 1 步中设置的主题。
export TOPIC_ID=topic-id
向
gcloud
工具帐号授予具有pubsub.topics.setIamPolicy
权限的 Pub/Sub 角色:gcloud pubsub topics add-iam-policy-binding \ projects/$PUBSUB_PROJECT/topics/$TOPIC_ID \ --member="user:$GCLOUD_ACCOUNT" \ --role='roles/pubsub.admin'
向
gcloud
工具帐号授予具有所有securitycenter.notification
权限的组织角色,例如roles/securitycenter.notificationConfigEditor
或roles/securitycenter.admin
。gcloud organizations add-iam-policy-binding $ORGANIZATION_ID \ --member="user:$GCLOUD_ACCOUNT" \ --role='role-name'
接下来,创建 NotificationConfig。
客户端库
要使用客户端库设置 Security Command Center API 通知功能,请执行以下操作:
- 设置服务帐号。
- 下载 API 客户端库。
- 设置开发环境。
- 设置 Pub/Sub 主题和订阅。
第 1 步:设置服务帐号
Security Command Center API 通知功能使用具有适当权限的服务帐号来配置通知。此服务帐号仅用于初始设置配置,后续您可以重复使用该帐号创建更多通知配置。此服务帐号不同于您在设置 Security Command Center 时为您创建的服务帐号。
要创建服务帐号,请执行以下操作:
- 转到 Google Cloud Console。
转到 Google Cloud Console - 选择启用了 Security Command Center API 的项目。
- 点击激活 Cloud Shell。
设置环境变量:
设置您的组织名称:
export ORGANIZATION_ID=organization-id
为要启用 Notifications API 的项目设置项目 ID:
export PROJECT_ID=project-id
设置要用于新服务帐号的自定义 ID,如
scc-notifications
。服务帐号名称的长度必须介于 6 到 30 个字符之间、必须以字母开头,且必须全部为小写字母数字字符和连字符:export SERVICE_ACCOUNT=custom-id
设置应存储服务帐号密钥的路径,例如
export KEY_LOCATION=/home/$USER/mykeys/$SERVICE_ACCOUNT.json
:export KEY_LOCATION=full-key-location-path # This is used by client libraries to find the key export GOOGLE_APPLICATION_CREDENTIALS=$KEY_LOCATION
创建与您的项目 ID 关联的服务帐号:
gcloud iam service-accounts create $SERVICE_ACCOUNT --display-name \ "Service Account for [USER]" --project $PROJECT_ID
创建密钥以与服务帐号关联。该密钥用于创建
NotificationConfig
,并永久存储在您在前面的步骤中指定的KEY_LOCATION
中。gcloud iam service-accounts keys create $KEY_LOCATION --iam-account \ $SERVICE_ACCOUNT@$PROJECT_ID.iam.gserviceaccount.com
向服务帐号授予具有所有
securitycenter.notification
权限的组织角色(如roles/securitycenter.notificationConfigEditor
或roles/securitycenter.admin
)。gcloud organizations add-iam-policy-binding $ORGANIZATION_ID \ --member="serviceAccount:$SERVICE_ACCOUNT@$PROJECT_ID.iam.gserviceaccount.com" \ --role='role-name'
服务帐号现已设置为与通知搭配使用,而服务帐号密钥存储在您指定的 KEY_LOCATION 中。如需详细了解服务帐号,请参阅创建和管理服务帐号密钥。
第 2 步:设置开发环境
如需使用 Security Command Center API 通知功能,您可以使用 gcloud
工具或下载客户端库,并设置适用于您选择的语言的开发环境。
Python
可选:在安装 Python 库之前,建议您使用 Virtualenv 来创建独立的 Python 环境。
virtualenv your-env source your-env/bin/activate
安装 pip 以管理 Python 库安装。
运行以下命令以安装 Python 库:
pip install google-cloud-securitycenter
Java
要将 Security Command Center Java 库作为依赖项添加到项目中,请从 Maven 代码库中选择一个工件。通知包含在库版本 0.119.0
及更高版本中。
如果您使用的是 Intellij,请将 GOOGLE_APPLICATION_CREDENTIALS
环境变量设置为您在前面的步骤中下载的通知服务帐号密钥的绝对路径:
- 在 Intellij 中,点击运行 > 修改配置。
在应用 > Run_Configuration_For_Sample > 环境变量下设置以下变量:
GOOGLE_APPLICATION_CREDENTIALS=absolute-path-to-service-account-key
Go
要安装通知 API Go 依赖项,请运行以下命令:
如需下载 Go 库,请运行以下命令:
go get -u cloud.google.com/go/securitycenter/apiv1
Node.js
在项目文件夹中,使用 npm
安装任何所需的 Notifications PI 依赖项:
npm install --save @google-cloud/security-center/
第 3 步:设置 Pub/Sub
如需向 Pub/Sub 发送通知,您需要订阅 Pub/Sub 主题,并向通知服务帐号授予具有 pubsub.topics.setIamPolicy
权限的 IAM 角色:
创建新主题或重复使用现有主题。要创建新主题,请执行以下操作:
gcloud pubsub topics create topic-id
设置环境变量:
设置主题 ID:
export TOPIC_ID=topic-id
为启用了通知 API 的项目设置项目 ID:
export CONSUMER_PROJECT=project-id
设置您在先前步骤中创建的服务帐号的电子邮件:
export SERVICE_ACCOUNT_EMAIL=service-account-name@$CONSUMER_PROJECT.iam.gserviceaccount.com
创建对主题的订阅:
gcloud pubsub subscriptions create subscription-id --topic topic-id
向通知服务帐号授予具有
pubsub.topics.setIamPolicy
权限的角色:gcloud pubsub topics add-iam-policy-binding \ projects/$CONSUMER_PROJECT/topics/$TOPIC_ID \ --member="serviceAccount:$SERVICE_ACCOUNT_EMAIL" \ --role='roles/pubsub.admin'
如需详细了解如何设置 Pub/Sub,请参阅管理主题和订阅。在下一步中,通过创建 NotificationConfig
来完成设置。
创建 NotificationConfig
在创建 NotificationConfig
之前,请考虑以下事项:
- 每个组织只能有一定数量的
NotificationConfig
文件。如需了解详情,请参阅配额和限制。 - 您必须拥有组织管理员 IAM 角色,以便向通知服务帐号或
gcloud
工具帐号授予适当的角色。
NotificationConfig
包含一个 filter
字段,用于限制对有用事件的通知。此字段接受 Security Command Center API findings.list
方法中提供的所有过滤条件。
要使用 gcloud
命令行工具向帐号授予安全中心通知配置编辑者 IAM 角色,请执行以下操作:
设置环境变量:
设置您的组织名称:
export ORGANIZATION_ID=organization-id
设置您在设置发现结果通知时创建的服务帐号的电子邮件,或设置用于运行
gcloud
工具命令的帐号:设置用于运行
gcloud
工具命令的帐号的电子邮件:export EMAIL=your-username@email.com
或设置服务帐号的电子邮件:
export EMAIL=service-account-name@$CONSUMER_PROJECT.iam.gserviceaccount.com
向该服务帐号或用于
gcloud
工具命令的帐号授予必要的角色:向运行
gcloud
工具命令的帐号授予角色:gcloud organizations add-iam-policy-binding \ $ORGANIZATION_ID \ --member="user:$EMAIL" \ --role='roles/securitycenter.notificationConfigEditor'
或向服务帐号授予角色:
gcloud organizations add-iam-policy-binding \ $ORGANIZATION_ID \ --member="serviceAccount:$EMAIL" \ --role='roles/securitycenter.notificationConfigEditor'
向服务帐号或 gcloud
工具帐号授予权限之后,使用您选择的语言或平台创建 NotificationConfig
:
gcloud
# The numeric ID of the organization
ORGANIZATION_ID=organization-id
# The topic to which the notifications are published
PUBSUB_TOPIC="projects/project-id/topics/topic-id"
# The description for the NotificationConfig
DESCRIPTION="Notifies for active findings"
# Filters for active findings
FILTER="state=\"ACTIVE\""
gcloud scc notifications create notification-name \
--organization "$ORGANIZATION_ID" \
--description "$DESCRIPTION" \
--pubsub-topic $PUBSUB_TOPIC \
--filter "$FILTER"
Python
Java
Go
Node.js
PHP
Ruby
C#
通知现已发布到您指定的 Pub/Sub 主题。要发布通知,Security Command Center 会使用 service-org-organization-id@gcp-sa-scc-notification.iam.gserviceaccount.com
形式的组织级层服务帐号,角色为 securitycenter.notificationServiceAgent
。需要组织级层的服务帐号角色才能接收通知。
在前面的步骤中,您创建了 service-account-id@project-id.iam.gserviceaccount.com
形式的项目级层通知服务帐号,或者针对 gcloud
工具使用了用户帐号。您可以选择撤消您授予此帐号的权限。如需了解详情,请参阅 IAM 文档中的授予、更改和撤消访问权限。
后续步骤
- 了解如何管理 Notifications API。
- 了解如何过滤通知。