|
| 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