Skip to content

Commit 87a7eb4

Browse files
Sita04gcf-owl-bot[bot]partheabusunkim96
authored andcommitted
docs(samples): added mute config samples (#276)
* docs(samples): init add mute config samples * docs(samples): added test for mute config samples * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * docs(samples): lint fix * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * docs(samples): lint fix * docs(samples): applied documentation review comments * docs(samples): applied documentation review comments * lint fix * docs(samples): fixed syntax typo * docs(samples): modified create finding method * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * remove unused import * docs(samples): fixed finding path and return mismatch * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix: fix finding path * docs(samples): fix finding yield param * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * minor test output fix Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <[email protected]> Co-authored-by: Bu Sun Kim <[email protected]>
1 parent 680a3c0 commit 87a7eb4

File tree

4 files changed

+368
-10
lines changed

4 files changed

+368
-10
lines changed

securitycenter/snippets/snippets_findings.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ def list_source(organization_id):
147147
return i
148148

149149

150-
def create_finding(source_name):
150+
def create_finding(source_name, finding_id):
151151
"""Creates a new finding."""
152152
# [START securitycenter_create_finding]
153153
from google.cloud import securitycenter
154-
from google.cloud.securitycenter_v1 import CreateFindingRequest, Finding
154+
from google.cloud.securitycenter_v1 import Finding
155155
import datetime
156156

157157
# Create a new client.
@@ -167,9 +167,6 @@ def create_finding(source_name):
167167
# e.g.:
168168
# source_name = "organizations/111122222444/sources/1234"
169169

170-
# Controlled by caller.
171-
finding_id = "samplefindingid"
172-
173170
# The resource this finding applies to. The CSCC UI can link
174171
# the findings for a resource to the corresponding Asset of a resource
175172
# if there are matches.
@@ -182,11 +179,10 @@ def create_finding(source_name):
182179
event_time=event_time,
183180
)
184181

185-
request = CreateFindingRequest(
186-
parent=source_name, finding_id=finding_id, finding=finding,
187-
)
188182
# Call The API.
189-
created_finding = client.create_finding(request=request)
183+
created_finding = client.create_finding(
184+
request={"parent": source_name, "finding_id": finding_id, "finding": finding}
185+
)
190186
print(created_finding)
191187
# [END securitycenter_create_finding]
192188
return created_finding

securitycenter/snippets/snippets_findings_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def test_list_source(organization_id):
7676

7777

7878
def test_create_finding(source_name):
79-
created_finding = snippets_findings.create_finding(source_name)
79+
created_finding = snippets_findings.create_finding(source_name, "samplefindingid")
8080
assert len(created_finding.name) > 0
8181

8282

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright 2022 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+
18+
# [START securitycenter_create_mute_config]
19+
20+
21+
def create_mute_rule(parent_path: str, mute_config_id: str) -> None:
22+
"""
23+
Creates a mute configuration under a given scope that will mute
24+
all new findings that match a given filter.
25+
Existing findings will NOT BE muted.
26+
Args:
27+
parent_path: use any one of the following options:
28+
- organizations/{organization_id}
29+
- folders/{folder_id}
30+
- projects/{project_id}
31+
mute_config_id: Set a unique id; max of 63 chars.
32+
"""
33+
34+
from google.cloud import securitycenter
35+
36+
client = securitycenter.SecurityCenterClient()
37+
38+
mute_config = securitycenter.MuteConfig()
39+
mute_config.description = "Mute low-medium IAM grants excluding 'compute' "
40+
# Set mute rule(s).
41+
# To construct mute rules and for supported properties, see:
42+
# https://cloud.google.com/security-command-center/docs/how-to-mute-findings#create_mute_rules
43+
mute_config.filter = (
44+
'severity="LOW" OR severity="MEDIUM" AND '
45+
'category="Persistence: IAM Anomalous Grant" AND '
46+
'-resource.type:"compute"'
47+
)
48+
49+
request = securitycenter.CreateMuteConfigRequest()
50+
request.parent = parent_path
51+
request.mute_config_id = mute_config_id
52+
request.mute_config = mute_config
53+
54+
mute_config = client.create_mute_config(request=request)
55+
print(f"Mute rule created successfully: {mute_config.name}")
56+
57+
58+
# [END securitycenter_create_mute_config]
59+
60+
61+
# [START securitycenter_delete_mute_config]
62+
def delete_mute_rule(mute_config_name: str) -> None:
63+
"""
64+
Deletes a mute configuration given its resource name.
65+
Note: Previously muted findings are not affected when a mute config is deleted.
66+
Args:
67+
mute_config_name: Specify the name of the mute config to delete.
68+
Use any one of the following formats:
69+
- organizations/{organization}/muteConfigs/{config_id}
70+
- folders/{folder}/muteConfigs/{config_id} or
71+
- projects/{project}/muteConfigs/{config_id}
72+
"""
73+
from google.cloud import securitycenter
74+
75+
client = securitycenter.SecurityCenterClient()
76+
77+
request = securitycenter.DeleteMuteConfigRequest()
78+
request.name = mute_config_name
79+
80+
client.delete_mute_config(request)
81+
print(f"Mute rule deleted successfully: {mute_config_name}")
82+
83+
84+
# [END securitycenter_delete_mute_config]
85+
86+
87+
# [START securitycenter_get_mute_config]
88+
def get_mute_rule(mute_config_name: str) -> None:
89+
"""
90+
Retrieves a mute configuration given its resource name.
91+
Args:
92+
mute_config_name: Name of the mute config to retrieve.
93+
Use any one of the following formats:
94+
- organizations/{organization}/muteConfigs/{config_id}
95+
- folders/{folder}/muteConfigs/{config_id}
96+
- projects/{project}/muteConfigs/{config_id}
97+
"""
98+
from google.cloud import securitycenter
99+
100+
client = securitycenter.SecurityCenterClient()
101+
102+
request = securitycenter.GetMuteConfigRequest()
103+
request.name = mute_config_name
104+
105+
mute_config = client.get_mute_config(request)
106+
print(f"Retrieved the mute rule: {mute_config.name}")
107+
108+
109+
# [END securitycenter_get_mute_config]
110+
111+
112+
# [START securitycenter_list_mute_configs]
113+
def list_mute_rules(parent: str) -> None:
114+
"""
115+
Listing mute configs at organization level will return all the configs
116+
at the org, folder and project levels.
117+
Similarly, listing configs at folder level will list all the configs
118+
at the folder and project levels.
119+
Args:
120+
parent: Use any one of the following resource paths to list mute configurations:
121+
- organizations/{organization_id}
122+
- folders/{folder_id}
123+
- projects/{project_id}
124+
"""
125+
from google.cloud import securitycenter
126+
127+
client = securitycenter.SecurityCenterClient()
128+
129+
request = securitycenter.ListMuteConfigsRequest()
130+
request.parent = parent
131+
132+
# List all Mute Configs present in the resource.
133+
for mute_config in client.list_mute_configs(request):
134+
print(mute_config.name)
135+
136+
137+
# [END securitycenter_list_mute_configs]
138+
139+
140+
# [START securitycenter_update_mute_config]
141+
def update_mute_rule(mute_config_name: str) -> None:
142+
"""
143+
Updates an existing mute configuration.
144+
The following can be updated in a mute config: description, and filter/ mute rule.
145+
Args:
146+
mute_config_name: Specify the name of the mute config to delete.
147+
Use any one of the following formats:
148+
- organizations/{organization}/muteConfigs/{config_id}
149+
- folders/{folder}/muteConfigs/{config_id}
150+
- projects/{project}/muteConfigs/{config_id}
151+
"""
152+
from google.cloud import securitycenter
153+
from google.protobuf import field_mask_pb2
154+
155+
client = securitycenter.SecurityCenterClient()
156+
157+
update_mute_config = securitycenter.MuteConfig()
158+
update_mute_config.name = mute_config_name
159+
update_mute_config.description = "Updated mute config description"
160+
161+
field_mask = field_mask_pb2.FieldMask(paths=["description"])
162+
163+
request = securitycenter.UpdateMuteConfigRequest()
164+
request.mute_config = update_mute_config
165+
# Set the update mask to specify which properties of the Mute Config should be updated.
166+
# If empty, all mutable fields will be updated.
167+
# Make sure that the mask fields match the properties changed in 'update_mute_config'.
168+
# For more info on constructing update mask path, see the proto or:
169+
# https://cloud.google.com/security-command-center/docs/reference/rest/v1/folders.muteConfigs/patch?hl=en#query-parameters
170+
request.update_mask = field_mask
171+
172+
mute_config = client.update_mute_config(request)
173+
print(f"Updated mute rule : {mute_config}")
174+
175+
176+
# [END securitycenter_update_mute_config]
177+
178+
179+
# [START securitycenter_set_mute_unmute]
180+
def set_mute_unmute_finding(finding_path: str) -> None:
181+
"""
182+
Mute/unmute an individual finding.
183+
If a finding is already muted, muting it again has no effect.
184+
Similarly, unmuting a finding that isn't muted has no effect.
185+
Various mute states are: MUTE_UNSPECIFIED/MUTE/UNMUTE.
186+
Args:
187+
finding_path: The relative resource name of the finding. See:
188+
https://cloud.google.com/apis/design/resource_names#relative_resource_name
189+
Use any one of the following formats:
190+
- organizations/{organization_id}/sources/{source_id}/finding/{finding_id},
191+
- folders/{folder_id}/sources/{source_id}/finding/{finding_id},
192+
- projects/{project_id}/sources/{source_id}/finding/{finding_id}.
193+
"""
194+
from google.cloud import securitycenter
195+
196+
client = securitycenter.SecurityCenterClient()
197+
198+
request = securitycenter.SetMuteRequest()
199+
request.name = finding_path
200+
request.mute = securitycenter.Finding.Mute.MUTED
201+
202+
finding = client.set_mute(request)
203+
print(f"Mute value for the finding: {finding.mute.name}")
204+
205+
206+
# [END securitycenter_set_mute_unmute]
207+
208+
209+
# [START securitycenter_bulk_mute]
210+
def bulk_mute_findings(parent_path: str, mute_rule: str) -> None:
211+
"""
212+
Kicks off a long-running operation (LRO) to bulk mute findings for a parent based on a filter.
213+
The parent can be either an organization, folder, or project. The findings
214+
matched by the filter will be muted after the LRO is done.
215+
Args:
216+
parent_path: use any one of the following options:
217+
- organizations/{organization}
218+
- folders/{folder}
219+
- projects/{project}
220+
mute_rule: Expression that identifies findings that should be updated.
221+
"""
222+
from google.cloud import securitycenter
223+
224+
client = securitycenter.SecurityCenterClient()
225+
226+
request = securitycenter.BulkMuteFindingsRequest()
227+
request.parent = parent_path
228+
# To create mute rules, see:
229+
# https://cloud.google.com/security-command-center/docs/how-to-mute-findings#create_mute_rules
230+
request.filter = mute_rule
231+
232+
response = client.bulk_mute_findings(request)
233+
print(f"Bulk mute findings completed successfully! : {response}")
234+
235+
236+
# [END securitycenter_bulk_mute]

0 commit comments

Comments
 (0)