This repository has been archived by the owner on Sep 5, 2023. It is now read-only.
/
snippets_list_assets.py
212 lines (169 loc) · 6.6 KB
/
snippets_list_assets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python
#
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" Examples of listing assets in Cloud Security Command Center."""
def list_all_assets(organization_id):
"""Demonstrate listing and printing all assets."""
i = 0
# [START demo_list_all_assets]
from google.cloud import securitycenter
client = securitycenter.SecurityCenterClient()
# organization_id is the numeric ID of the organization.
# organization_id = "1234567777"
org_name = "organizations/{org_id}".format(org_id=organization_id)
# Call the API and print results.
asset_iterator = client.list_assets(request={"parent": org_name})
for i, asset_result in enumerate(asset_iterator):
print(i, asset_result)
# [END demo_list_all_assets]
return i
def list_assets_with_filters(organization_id):
"""Demonstrate listing assets with a filter."""
i = 0
# [START demo_list_assets_with_filter]
from google.cloud import securitycenter
client = securitycenter.SecurityCenterClient()
# organization_id is the numeric ID of the organization.
# organization_id = "1234567777"
org_name = "organizations/{org_id}".format(org_id=organization_id)
project_filter = (
"security_center_properties.resource_type="
+ '"google.cloud.resourcemanager.Project"'
)
# Call the API and print results.
asset_iterator = client.list_assets(
request={"parent": org_name, "filter": project_filter}
)
for i, asset_result in enumerate(asset_iterator):
print(i, asset_result)
# [END demo_list_assets_with_filter]
return i
def list_assets_with_filters_and_read_time(organization_id):
"""Demonstrate listing assets with a filter."""
i = 0
# [START demo_list_assets_with_filter_and_time]
from datetime import datetime, timedelta
from google.cloud import securitycenter
client = securitycenter.SecurityCenterClient()
# organization_id is the numeric ID of the organization.
# organization_id = "1234567777"
org_name = "organizations/{org_id}".format(org_id=organization_id)
project_filter = (
"security_center_properties.resource_type="
+ '"google.cloud.resourcemanager.Project"'
)
# Lists assets as of yesterday.
read_time = datetime.utcnow() - timedelta(days=1)
# Call the API and print results.
asset_iterator = client.list_assets(
request={
"parent": org_name,
"filter": project_filter,
"read_time": read_time,
}
)
for i, asset_result in enumerate(asset_iterator):
print(i, asset_result)
# [END demo_list_assets_with_filter_and_time]
return i
def list_point_in_time_changes(organization_id):
"""Demonstrate listing assets along with their state changes."""
i = 0
# [START demo_list_assets_changes]
from datetime import timedelta
from google.cloud import securitycenter
client = securitycenter.SecurityCenterClient()
# organization_id is the numeric ID of the organization.
# organization_id = "1234567777"
org_name = "organizations/{org_id}".format(org_id=organization_id)
project_filter = (
"security_center_properties.resource_type="
+ '"google.cloud.resourcemanager.Project"'
)
# List assets and their state change the last 30 days
compare_delta = timedelta(days=30)
# Call the API and print results.
asset_iterator = client.list_assets(
request={
"parent": org_name,
"filter": project_filter,
"compare_duration": compare_delta,
}
)
for i, asset in enumerate(asset_iterator):
print(i, asset)
# [END demo_list_assets_changes]
return i
def group_assets(organization_id):
"""Demonstrates grouping all assets by type. """
i = 0
# [START group_all_assets]
from google.cloud import securitycenter
client = securitycenter.SecurityCenterClient()
# organization_id is the numeric ID of the organization.
# organization_id = "1234567777"
org_name = "organizations/{org_id}".format(org_id=organization_id)
group_by_type = "security_center_properties.resource_type"
result_iterator = client.group_assets(
request={"parent": org_name, "group_by": group_by_type}
)
for i, result in enumerate(result_iterator):
print((i + 1), result)
# [END group_all_assets]
return i
def group_filtered_assets(organization_id):
"""Demonstrates grouping assets by type with a filter. """
i = 0
# [START group_all_assets_with_filter]
from google.cloud import securitycenter
client = securitycenter.SecurityCenterClient()
# organization_id is the numeric ID of the organization.
# organization_id = "1234567777"
org_name = "organizations/{org_id}".format(org_id=organization_id)
group_by_type = "security_center_properties.resource_type"
only_projects = (
"security_center_properties.resource_type="
+ '"google.cloud.resourcemanager.Project"'
)
result_iterator = client.group_assets(
request={"parent": org_name, "group_by": group_by_type, "filter": only_projects}
)
for i, result in enumerate(result_iterator):
print((i + 1), result)
# [END group_all_assets_with_filter]
# only one asset type is a project
return i
def group_assets_by_changes(organization_id):
"""Demonstrates grouping assets by their changes over a period of time."""
i = 0
# [START group_all_assets_by_change]
from datetime import timedelta
from google.cloud import securitycenter
client = securitycenter.SecurityCenterClient()
duration = timedelta(days=5)
# organization_id is the numeric ID of the organization.
# organization_id = "1234567777"
org_name = "organizations/{org_id}".format(org_id=organization_id)
result_iterator = client.group_assets(
request={
"parent": org_name,
"group_by": "state_change",
"compare_duration": duration,
}
)
for i, result in enumerate(result_iterator):
print((i + 1), result)
# [END group_all_assets_by_change]
return i