Google 安全命令中心 - 如何以 JSON 格式导出结果

Google Security Command Center - how to export findings in JSON format

使用这个例子:

from google.cloud import securitycenter

# Create a client.
client = securitycenter.SecurityCenterClient()

# organization_id is the numeric ID of the organization. e.g.:
# organization_id = "111122222444"
org_name = "organizations/{org_id}".format(org_id=organization_id)
# The "sources/-" suffix lists findings across all sources.  You
# also use a specific source_name instead.
all_sources = "{org_name}/sources/-".format(org_name=org_name)
finding_result_iterator = client.list_findings(all_sources)
for i, finding_result in enumerate(finding_result_iterator):
    print(
        "{}: name: {} resource: {}".format(
            i, finding_result.finding.name, finding_result.finding.resource_name
        )
    )

我想将所有发现导出为 JSON 数组,但是键入 (finding_result.finding) returns: class'google.cloud.securitycenter_v1.types.Finding'

使用 json.dumps(finding_result.finding) 导致错误,它不是 JSON 可序列化的。

使用 gcloud SDK,这可以通过指定“--format json”来实现

我想通了。

您必须添加以下导入 from google.cloud.securitycenter import ListFindingsResponse

然后在循环中添加以下行

ListFindingsResponse.ListFindingsResult.to_json(finding_result)

所以解决方案应该如下

from google.cloud import securitycenter
from google.cloud.securitycenter import ListFindingsResponse

# Create a client.
client = securitycenter.SecurityCenterClient()

# organization_id is the numeric ID of the organization. e.g.:
# organization_id = "111122222444"
org_name = "organizations/{org_id}".format(org_id=organization_id)
# The "sources/-" suffix lists findings across all sources.  You
# also use a specific source_name instead.
all_sources = "{org_name}/sources/-".format(org_name=org_name)
finding_result_iterator = client.list_findings(all_sources)
for i, finding_result in enumerate(finding_result_iterator):
    print(
        ListFindingsResponse.ListFindingsResult.to_json(finding_result)
    )
    print(
        "{}: name: {} resource: {}".format(
            i, finding_result.finding.name, finding_result.finding.resource_name
        )
    )