|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright 2019 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +""" Examples of listing assets in Cloud Security Command Center.""" |
| 18 | +import os |
| 19 | +from datetime import datetime, timedelta |
| 20 | + |
| 21 | + |
| 22 | +# The numeric identifier for the organization. |
| 23 | +ORGANIZATION_ID = os.environ["GCLOUD_ORGANIZATION"] |
| 24 | + |
| 25 | + |
| 26 | +def test_list_all_assets(): |
| 27 | + """Demonstrate listing and printing all assets.""" |
| 28 | + from google.cloud import securitycenter_v1beta1 as securitycenter |
| 29 | + |
| 30 | + # [START demo_list_all_assets] |
| 31 | + client = securitycenter.SecurityCenterClient() |
| 32 | + # ORGANIZATION_ID is the numeric ID of the organization (e.g. 123213123121) |
| 33 | + org_name = "organizations/{org_id}".format(org_id=ORGANIZATION_ID) |
| 34 | + |
| 35 | + # Call the API and print results. |
| 36 | + asset_iterator = client.list_assets(org_name) |
| 37 | + for i, asset_result in enumerate(asset_iterator): |
| 38 | + print(i, asset_result) |
| 39 | + # [END demo_list_all_assets] |
| 40 | + assert i > 0 |
| 41 | + |
| 42 | + |
| 43 | +def test_list_assets_with_filters(): |
| 44 | + """Demonstrate listing assets with a filter.""" |
| 45 | + from google.cloud import securitycenter_v1beta1 as securitycenter |
| 46 | + |
| 47 | + # [START demo_list_assets_with_filter] |
| 48 | + client = securitycenter.SecurityCenterClient() |
| 49 | + |
| 50 | + # ORGANIZATION_ID is the numeric ID of the organization (e.g. 123213123121) |
| 51 | + org_name = "organizations/{org_id}".format(org_id=ORGANIZATION_ID) |
| 52 | + |
| 53 | + project_filter = ( |
| 54 | + "security_center_properties.resource_type=" |
| 55 | + + '"google.cloud.resourcemanager.Project"' |
| 56 | + ) |
| 57 | + # Call the API and print results. |
| 58 | + asset_iterator = client.list_assets(org_name, filter_=project_filter) |
| 59 | + for i, asset_result in enumerate(asset_iterator): |
| 60 | + print(i, asset_result) |
| 61 | + # [END demo_list_assets_with_filter] |
| 62 | + assert i > 0 |
| 63 | + |
| 64 | + |
| 65 | +def test_list_assets_with_filters_and_read_time(): |
| 66 | + """Demonstrate listing assets with a filter.""" |
| 67 | + from datetime import datetime, timedelta |
| 68 | + from google.cloud import securitycenter_v1beta1 as securitycenter |
| 69 | + from google.protobuf.timestamp_pb2 import Timestamp |
| 70 | + |
| 71 | + # [START demo_list_assets_with_filter_and_time] |
| 72 | + client = securitycenter.SecurityCenterClient() |
| 73 | + |
| 74 | + # ORGANIZATION_ID is the numeric ID of the organization (e.g. 123213123121) |
| 75 | + org_name = "organizations/{org_id}".format(org_id=ORGANIZATION_ID) |
| 76 | + |
| 77 | + project_filter = ( |
| 78 | + "security_center_properties.resource_type=" |
| 79 | + + '"google.cloud.resourcemanager.Project"' |
| 80 | + ) |
| 81 | + |
| 82 | + # Lists assets as of yesterday. |
| 83 | + read_time = datetime.utcnow() - timedelta(days=1) |
| 84 | + timestamp_proto = Timestamp() |
| 85 | + timestamp_proto.FromDatetime(read_time) |
| 86 | + |
| 87 | + # Call the API and print results. |
| 88 | + asset_iterator = client.list_assets( |
| 89 | + org_name, filter_=project_filter, read_time=timestamp_proto |
| 90 | + ) |
| 91 | + for i, asset_result in enumerate(asset_iterator): |
| 92 | + print(i, asset_result) |
| 93 | + # [END demo_list_assets_with_filter_and_time] |
| 94 | + assert i > 0 |
| 95 | + |
| 96 | + |
| 97 | +def test_list_point_in_time_changes(): |
| 98 | + """Demonstrate listing assets along with their state changes.""" |
| 99 | + from google.cloud import securitycenter_v1beta1 as securitycenter |
| 100 | + from google.protobuf.duration_pb2 import Duration |
| 101 | + from datetime import timedelta |
| 102 | + |
| 103 | + # [START demo_list_assets_changes] |
| 104 | + client = securitycenter.SecurityCenterClient() |
| 105 | + |
| 106 | + # ORGANIZATION_ID is the numeric ID of the organization (e.g. 123213123121) |
| 107 | + org_name = "organizations/{org_id}".format(org_id=ORGANIZATION_ID) |
| 108 | + project_filter = ( |
| 109 | + "security_center_properties.resource_type=" |
| 110 | + + '"google.cloud.resourcemanager.Project"' |
| 111 | + ) |
| 112 | + |
| 113 | + # List assets and their state change the last 30 days |
| 114 | + compare_delta = timedelta(days=30) |
| 115 | + # Convert the timedelta to a Duration |
| 116 | + duration_proto = Duration() |
| 117 | + duration_proto.FromTimedelta(compare_delta) |
| 118 | + # Call the API and print results. |
| 119 | + asset_iterator = client.list_assets( |
| 120 | + org_name, filter_=project_filter, compare_duration=duration_proto |
| 121 | + ) |
| 122 | + for i, asset in enumerate(asset_iterator): |
| 123 | + print(i, asset) |
| 124 | + |
| 125 | + # [END demo_list_assets_changes] |
| 126 | + assert i > 0 |
0 commit comments