资源是组织的 Google Cloud 资源,例如 Compute Engine 实例或 Cloud Storage 存储分区。本指南介绍了如何使用 Security Command Center 客户端库访问有关组织资源的元数据。
准备工作
在设置来源之前,您需要完成以下操作:
页面大小
所有 Security Command Center list API 都是分页的。每个响应都会返回一个结果页面和一个返回下一页的令牌。页面大小可配置。默认的 pageSize 为 10,可设置为最小值 1 和最大值 1000。
列出所有资源
以下示例演示了如何列出组织的所有资源:
gcloud
# ORGANIZATION_ID=12344321 gcloud scc assets list $ORGANIZATION_ID
如需查看更多示例,请运行以下命令:
gcloud scc assets list --help
Python
Java
Go
Node.js
每个资源的输出都是一个 JSON 对象,类似如下:
asset:
createTime: '2020-10-05T17:55:14.823Z'
iamPolicy:
policyBlob: '{"bindings":[{"role":"roles/owner","members":["serviceAccount:service-account@project-id.iam.gserviceaccount.com","user:user-email@gmail.com"]}]}'
name: organizations/organization-id/assets/asset-id
resourceProperties:
createTime: '2020-10-05T17:36:17.915Z'
lifecycleState: ACTIVE
name: project-id
parent: '{"id":"organization-id","type":"organization"}'
projectId: project-id
projectNumber: 'project-number'
securityCenterProperties:
resourceDisplayName: project-id
resourceName: //cloudresourcemanager.googleapis.com/projects/project-number
resourceOwners:
- serviceAccount:service-account@project-id.iam.gserviceaccount.com
- user:user-email@gmail.com
resourceParent: //cloudresourcemanager.googleapis.com/organizations/organization-id
resourceParentDisplayName: organization-name
resourceProject: //cloudresourcemanager.googleapis.com/projects/project-number
resourceProjectDisplayName: project-id
resourceType: google.cloud.resourcemanager.Project
securityMarks:
name: organizations/organization-id/assets/asset-id/securityMarks
updateTime: '2020-10-05T17:55:14.823Z'
过滤资源
一个组织可能包含许多资源。以上示例不使用任何过滤条件,因此会返回所有资源。Security Command Center 可让您使用资源过滤条件来获取特定资源的相关信息。过滤条件与 SQL 语句中的“where”子句类似,但列除外,它们会应用于 API 返回的对象。
上述示例中的示例输出显示了可用于资产过滤条件的一些字段、子字段及其属性。Security Command Center 支持完整的 JSON 数组和对象作为潜在属性类型。您可以按以下条件进行过滤:
- 数组元素
- 对象中部分字符串匹配的完整 JSON 对象
- JSON 对象子字段
子字段必须是数字、字符串或布尔值,过滤条件表达式必须使用以下比较运算符:
- 字符串
- 完全相等运算符
=
- 部分字符串与
:
匹配
- 完全相等运算符
- 数字
- 不等式
<
、>
、<=
、>=
- 相等
=
- 不等式
- 布尔值
- 相等
=
- 相等
以下示例使用过滤条件仅返回组织中的项目资源:
gcloud
# ORGANIZATION_ID=12344321 FILTER="security_center_properties.resource_type=\"google.cloud.resourcemanager.Project\"" gcloud scc assets list $ORGANIZATION_ID --filter="$FILTER"
如需查看更多示例,请运行以下命令:
gcloud scc assets list --help
Python
Java
Go
Node.js
列出某个时间点
前面的示例展示了如何列出组织当前的资源组。Security Command Center 还可让您查看组织资源的历史快照。以下示例返回特定时间点的所有资源的状态。Security Command Center 支持毫秒级时间分辨率。
gcloud
# ORGANIZATION_ID=12344321 # READ_TIME follows the format YYYY-MM-DDThh:mm:ss.ffffffZ READ_TIME=2019-02-28T07:00:06.861Z gcloud scc assets list $ORGANIZATION_ID --read-time=$READ_TIME
如需查看更多示例,请运行以下命令:
gcloud scc assets list --help
Python
Java
Go
Node.js
列出状态变化的资源
Security Command Center 可让您比较两个时间点的资源,以确定该资源在指定时间段内是添加、移除还是存在。以下示例将 READ_TIME
时存在的项目与 COMPARE_DURATION
指定的前一个时间点进行比较。COMPARE_DURATION
以秒为单位。
设置 COMPARE_DURATION
后,列出的资源结果的 stateChange
特性使用以下值之一更新:
ADDED
:该资源在compareDuration
的开始不存在,但在readTime
时存在。REMOVED
:资源在compareDuration
的开始存在,但在readTime
时不存在。ACTIVE
:资源在compareDuration
定义的时间段的开始和结束以及readTime
时都存在。
gcloud
# ORGANIZATION_ID=12344321 # READ_TIME follows the format YYYY-MM-DDThh:mm:ss.ffffffZ READ_TIME=2019-02-28T07:00:06.861Z FILTER="security_center_properties.resource_type=\"google.cloud.resourcemanager.Project\"" COMPARE_DURATION=86400s gcloud scc assets list $ORGANIZATION_ID --read-time=$READ_TIME \ --filter="$FILTER" \ --compare-duration=$COMPARE_DURATION
如需查看更多示例,请运行以下命令:
gcloud scc assets list --help
Python
Java
Go
Node.js
FilterExamples
以下是一些其他有用的资源过滤条件。您可以在过滤器条件中使用 AND
和 OR
来组合参数以及扩展或优化结果。
查找具有特定所有者的项目资源
"security_center_properties.resource_type = \"google.cloud.resourcemanager.Project\" AND security_center_properties.resource_owners : \"$USER\""
$USER
通常采用 user:someone@domain.com
格式。比较 user
时使用子字符串运算符 :
,不需要完全匹配。
包含开放 HTTP 端口的防火墙规则
"security_center_properties.resource_type = \"google.compute.Firewall\" AND resource_properties.name =\"default-allow-http\""
属于特定项目的资源
"security_center_properties.resource_parent = \"$PROJECT_1_NAME\" OR security_center_properties.resource_parent = \"$PROJECT_2_NAME\""
$PROJECT_1_NAME
和 $PROJECT_2_NAME
是 //cloudresourcemanager.googleapis.com/projects/$PROJECT_ID
形式的资源标识符,其中 $PROJECT_ID
是项目编号。完整示例如下所示://cloudresourcemanager.googleapis.com/projects/100090906
查找名称包含特定字符串的 Compute Engine 映像
以下过滤条件会返回包含子字符串“Debia”的 Compute Engine 映像:
"security_center_properties.resource_type = \"google.compute.Image\" AND resource_properties.name : \"Debia\""
属性包含键值对的资源
此过滤条件返回停用 bucketPolicyOnly
的 Cloud Storage 存储分区。resourceProperties.iamConfiguration
的值编码为字符串。您可以使用 \
字符来转义字符串中的特殊字符,包括键名称和值之间的运算符 :
。
"resourceProperties.iamConfiguration:"\"bucketPolicyOnly\"\:{\"enabled\"\:false""
查找在特定时间之前创建的项目资源
这些示例过滤器与 2019 年 7 月 18 日下午 8:26:21(格林尼治标准时间)创建的资源相匹配。借助 create_time
过滤器,您可以使用以下格式和类型表示时间:
以整数字面量表示的 Unix 时间(以毫秒为单位)
"create_time <= 1563481581000"
作为字符串字面量的 RFC 3339
"create_time <= \"2019-07-18T20:26:21+00:00\""
从结果中排除资源
要从结果中排除某个资源,请在参数前添加 -
字符。该运算类似于在 SQL 语句中使用 NOT 运算符。
以下过滤条件会返回除 Debia
以外的所有项目资源:
"security_center_properties.resource_type = \"google.cloud.resourcemanager.Project\" AND -resource_properties.projectId = \"Debia\""