如何通过云功能将GCP的安全中心资产导出到云存储?

问题描述 投票:0回答:1

我有一个云函数调用SCC的list_assets并将分页的输出转换为List(以获取所有结果)。但是,由于我在组织树中拥有很多资产,因此获取和云功能超时(最大超时540秒)花费了很多时间。

asset_iterator = security_client.list_assets(org_name)
asset_fetch_all=list(asset_iterator)

我尝试通过WebUI导出,并且工作正常(大约5分钟)。是否可以使用API​​将资产从SCC直接导出到Cloud Storage存储桶?

python google-cloud-platform google-cloud-storage assets securitycenter
1个回答
0
投票

我用Python开发了同样的东西,以导出到BQ。在BigQuery中进行搜索比在文件中进行搜索更容易。该代码与GCS存储非常相似。这是我在BQ的工作代码

import os

from google.cloud import asset_v1
from google.cloud.asset_v1.proto import asset_service_pb2
from google.cloud.asset_v1 import enums

def GCF_ASSET_TO_BQ(request):

    client = asset_v1.AssetServiceClient()
    parent = 'organizations/{}'.format(os.getenv('ORGANIZATION_ID'))
    output_config = asset_service_pb2.OutputConfig()
    output_config.bigquery_destination.dataset = 'projects/{}/datasets/{}'.format(os.getenv('PROJECT_ID'),os.getenv('DATASET'))
    content_type = enums.ContentType.RESOURCE
    output_config.bigquery_destination.table = 'asset_export'
    output_config.bigquery_destination.force = True

    response = client.export_assets(parent, output_config, content_type=content_type)

    # For waiting the finish
    # response.result()
    # Do stuff after export
    return "done", 200


if __name__ == "__main__":
    GCF_ASSET_TO_BQ('')

如您所见,Env Var中有一些值(OrganizationID,projectId和Dataset)。要导出到Cloud Storage,您必须像这样更改output_config的定义

output_config = asset_service_pb2.OutputConfig()
output_config.gcs_destination.uri = 'gs://path/to/file' 

您有example in other languages here

© www.soinside.com 2019 - 2024. All rights reserved.